Cows POJ - 2481 树状数组

时间:2023-03-10 07:27:08
Cows POJ - 2481  树状数组
Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.

Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E].

But some cows are strong and some are weak. Given two cows: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow iis stronger than cow j.

For each cow, how many cows are stronger than her? Farmer John needs your help!

Input

The input contains multiple test cases. 
For each test case, the first line is an integer N (1 <= N <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge.

The end of the input contains a single 0.

Output

For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cow i

Sample Input

3
1 2
0 3
3 4
0

Sample Output

1 0 0

Hint

Huge input and output,scanf and printf is recommended.
这题和star非常的像
把线段的  l ,r  看成二维坐标系
其实这个就是求一个点它的左上角有多少个点
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000") using namespace std;
typedef long long LL ;
const int maxn = 1e6 + ;
int n, c[maxn], ans[maxn];
struct node {
int x, y, id;
} qu[maxn];
int cmp(node a, node b) {
if (a.y == b.y) return a.x < b.x;
return a.y > b.y;
}
void updata(int x) {
while(x <= ) {
c[x]+=;
x += lowbit(x);
}
}
int sum(int x) {
int ret = ;
while(x > ) {
ret += c[x];
x -= lowbit(x);
}
return ret;
}
int main() {
while(scanf("%d", &n), n) {
mem(c, );
for (int i = ; i <= n ; i++) {
scanf("%d%d", &qu[i].x, &qu[i].y);
qu[i].x++, qu[i].y++;
qu[i].id = i;
}
sort(qu + , qu + + n, cmp);
ans[qu[].id] = sum(qu[].x);
updata(qu[].x);
for (int i = ; i <= n ; i++) {
if (qu[i].x == qu[i - ].x && qu[i].y == qu[i - ].y) ans[qu[i].id] = ans[qu[i - ].id];
else ans[qu[i].id] = sum(qu[i].x);
updata(qu[i].x);
}
for (int i = ; i <= n ; i++)
printf("%d%c", ans[i], i == n ? '\n' : ' ');
}
return ;
}