poj2481 树状数组

简介:

排序后类似stars, 用树状数组。

注意两个数组都要memset和去重



#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct data{
    int s, e, i;
    friend bool operator <(data a, data b){
        if (a.e != b.e) return a.e > b.e;
        else return a.s<b.s;
    }
} a[100010];
int t[100010];
int lowbit(int x){
    return x & -x;
}
void build(int x){
    while (x <= 100001){
        t[x] ++;
        x += lowbit(x);
    }
}
int sum(int x){
    int res = 0;
    while (x){
        res += t[x];
        x -= lowbit(x);
    }
    return res;
}
int ans[100010];
int main(){
    int n;
    while (scanf("%d", &n) && n){
        memset(ans, 0, sizeof(ans));
        memset(t, 0, sizeof(t));
        for (int i = 0; i < n; i++) {scanf("%d%d", &a[i].s, &a[i].e);
        a[i].s++; a[i].e++; a[i].i = i;}
        sort(a, a + n);
        build(a[0].s);
        for (int i = 1; i < n; i++){
            if (a[i].s == a[i-1].s && a[i].e == a[i-1].e) {
                ans[a[i].i] = ans[a[i-1].i];
                build(a[i].s);
                continue;
            }
            ans[a[i].i] = sum(a[i].s);
            //cout<<a[i].i<<"    "<<a[i].s<<' '<<a[i].e<<' '<<ans[a[i].i]<<endl;
            build (a[i].s);
        }
        for (int i = 0; i < n-1; i++) printf("%d ", ans[i]); printf("%d", ans[n-1]);
        printf("\n");
    }
    return 0;
}


相关文章
|
7月前
poj 1990 MooFest 树状数组
题意就是有N头牛,每头牛都有一个坐标和声调值(x, v),两头牛之间通讯要花费的能量是他们的距离乘以最大的一个音调值,现在要任意两头牛之间都相互通讯一次,求总共需要花费多少能量?
22 0
|
人机交互
POJ-2524,Ubiquitous Religions(并查集模板题)
POJ-2524,Ubiquitous Religions(并查集模板题)
|
人工智能 BI 存储
|
机器学习/深度学习
【OJ】贪心法 Saruman's Army POJ 3069 /acmclub 12132
题目链接:点击打开链接 /* 6 10 贪心法Saruman's Army POJ 3069 1 7 15 20 30 50 ans=3 */ #include #include using namespace std; int x[1010]; ...
865 0
|
机器学习/深度学习
【OJ】贪心法 Saruman&#39;s Army POJ 3069 /acmclub 12132
题目链接:点击打开链接 /* 6 10 贪心法Saruman's Army POJ 3069 1 7 15 20 30 50 ans=3 */ #include #include using namespace std; int x[1010]; int main(){ // freopen("贪心法 Saruman's Army poj3069.
821 0
线段树-poj-2823
Sliding Window Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k
1058 0