Cows
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total Submissions: 13421 |
|
Accepted: 4442 |
Description
Farmer John ' s cows has discovered that the clover growing along the ridge of the hill (which we can think of as a One-dim Ensional number line) in he field is particularly good.
Farmer John had N cows (we number the cows from 1 to N). Each of Farmer John's N cows have a range of clover that she particularly likes (these ranges might overlap). The ranges is defined by a closed interval [s,e].
But some cows is strong and some is weak. Given Cows:cowi and COWJ, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei-si > EJ-SJ, we say that cowi are stronger than COWJ.
For each cow, how many cows is stronger than her? Farmer John needs your help!
Input
The input contains multiple test cases.
For each test case, the first line is a integer n (1 <= n <=), which is the number of cows. Then come N lines, the i-th of which contains the Integers:s and E (0 <= S < e <=) specifying the start end L Ocation respectively of a range preferred by some cow. Locations is 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 is stronger than COWI.
Sample Input
31 20 33 40
Sample Output
1 0 0
Hint
Huge input and output,scanf and printf is recommended.
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath > #include <algorithm> #include <cstdlib>using namespace std; #define N 100010int c[n],val[n];struct node{ int x,y,id;} E[n];bool CMP (node A,node b) {if (A.Y!=B.Y) return a.y>b.y; return a.x<b.x;} int lowbit (int x) {return x& (-X);} void update (int pos,int m) {while (pos<n) {c[pos]+=m; Pos+=lowbit (POS); }}int sum (int x) {int sum=0; while (x>0) {sum+=c[x]; X=x-lowbit (x); } return sum;} int main () {int n; while (scanf ("%d", &n)!=eof) {if (n==0) break; Memset (C,0,sizeof (c)); for (int i=1;i<=n;i++) {scanf ("%d%d", &e[i].x,&e[i].y); e[i].x++,e[i].y++; E[i].id=i; } sort (e+1,e+1+n,cmp); Val[e[1].id]=suM (e[1].x); Update (e[1].x,1); for (int i=2;i<=n;i++) {if (E[I].X==E[I-1].X&&E[I].Y==E[I-1].Y) Val[e[i].id]=val[e[i-1].id]; else Val[e[i].id]=sum (e[i].x); Update (e[i].x,1); } printf ("%d", val[1]); for (int i=2;i<=n;i++) printf ("%d", val[i]); printf ("\ n"); } return 0;}
(Tree-like array) POJ 2481