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!
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.
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 看成二维坐标系
其实这个就是求一个点它的左上角有多少个点
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <cmath> 5 #include <algorithm> 6 #include <set> 7 #include <iostream> 8 #include <map> 9 #include <stack> 10 #include <string> 11 #include <vector> 12 #define pi acos(-1.0) 13 #define eps 1e-6 14 #define fi first 15 #define se second 16 #define lson l,m,rt<<1 17 #define rson m+1,r,rt<<1|1 18 #define bug printf("******\n") 19 #define mem(a,b) memset(a,b,sizeof(a)) 20 #define fuck(x) cout<<"["<<x<<"]"<<endl 21 #define f(a) a*a 22 #define sf(n) scanf("%d", &n) 23 #define sff(a,b) scanf("%d %d", &a, &b) 24 #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) 25 #define pf printf 26 #define FRE(i,a,b) for(i = a; i <= b; i++) 27 #define FREE(i,a,b) for(i = a; i >= b; i--) 28 #define FRL(i,a,b) for(i = a; i < b; i++) 29 #define FRLL(i,a,b) for(i = a; i > b; i--) 30 #define FIN freopen("DATA.txt","r",stdin) 31 #define lowbit(x) x&-x 32 #pragma comment (linker,"/STACK:102400000,102400000") 33 34 using namespace std; 35 typedef long long LL ; 36 const int maxn = 1e6 + 10; 37 int n, c[maxn], ans[maxn]; 38 struct node { 39 int x, y, id; 40 } qu[maxn]; 41 int cmp(node a, node b) { 42 if (a.y == b.y) return a.x < b.x; 43 return a.y > b.y; 44 } 45 void updata(int x) { 46 while(x <= 100005) { 47 c[x]+=1; 48 x += lowbit(x); 49 } 50 } 51 int sum(int x) { 52 int ret = 0; 53 while(x > 0) { 54 ret += c[x]; 55 x -= lowbit(x); 56 } 57 return ret; 58 } 59 int main() { 60 while(scanf("%d", &n), n) { 61 mem(c, 0); 62 for (int i = 1 ; i <= n ; i++) { 63 scanf("%d%d", &qu[i].x, &qu[i].y); 64 qu[i].x++, qu[i].y++; 65 qu[i].id = i; 66 } 67 sort(qu + 1, qu + 1 + n, cmp); 68 ans[qu[1].id] = sum(qu[1].x); 69 updata(qu[1].x); 70 for (int i = 2 ; i <= n ; i++) { 71 if (qu[i].x == qu[i - 1].x && qu[i].y == qu[i - 1].y) ans[qu[i].id] = ans[qu[i - 1].id]; 72 else ans[qu[i].id] = sum(qu[i].x); 73 updata(qu[i].x); 74 } 75 for (int i = 1 ; i <= n ; i++) 76 printf("%d%c", ans[i], i == n ? '\n' : ' '); 77 } 78 return 0; 79 }