Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define filer() freopen("in.txt","r",stdin)
- #define filew() freopen("out.txt","w",stdout)
- #include<iostream>
- #include<stdio.h>
- #include<string.h>
- #include<math.h>
- #include<algorithm>
- #include<queue>
- #include<stack>
- #include<string>
- #include<vector>
- #include <map>
- #define INF 1<<30
- #define PI pair<int,int>
- #define f first
- #define s second
- #define SET(a, x) memset((a), (x), sizeof(a))
- #define pb push_back
- #define i64 long long
- #define EPS (1e-9)
- using namespace std;
- typedef vector<int> VI;
- typedef vector<PI> vii;
- //i64 INF=(i64)((i64)1<<(i64)59);
- int N;
- struct node
- {
- int x,y;
- }A[15009];
- struct point
- {
- int ind;
- int val;
- }B[30009];
- bool cmp(point a,point b)
- {
- return a.val<b.val;
- }
- int vst[30009],tree[(1<<18)+10],MaxVal,bit;
- void update(int idx ,int val){
- while (idx <= MaxVal){
- tree[idx] += val;
- idx += (idx & -idx);
- }
- }
- int read(int idx){
- int sum = 0;
- while (idx > 0){
- sum += tree[idx];
- idx -= (idx & -idx);
- }
- return sum;
- }
- int findG(int cumFre){
- int idx = 0;
- int bitMask=bit;
- while ((bitMask != 0) && (idx < MaxVal)){
- int tIdx = idx + bitMask;
- if (cumFre >= tree[tIdx]){
- // if current cumulative frequency is equal to cumFre,
- // we are still looking for higher index (if exists)
- idx = tIdx;
- cumFre -= tree[tIdx];
- }
- bitMask >>= 1;
- }
- if (cumFre != 0)
- return -1;
- else
- return idx;
- }
- int readSingle(int idx){
- int sum = tree[idx]; // sum will be decreased
- if (idx > 0){ // special case
- int z = idx - (idx & -idx); // make z first
- idx--; // idx is no important any more, so instead y, you can use idx
- while (idx != z){ // at some iteration idx (y) will become z
- sum -= tree[idx];
- // substruct tree frequency which is between y and "the same path"
- idx -= (idx & -idx);
- }
- }
- return sum;
- }
- int main()
- {
- //filer();
- //filew();
- int T,cas=0;
- int i,j,x,y;
- scanf("%d",&T);
- MaxVal=1<<16;
- bit=1<<15;
- while(T--)
- {
- SET(tree,0);
- scanf("%d",&N);
- //if(N==0)continue;
- j=0;
- /*MaxVal=N;
- for(i=30;i>=0;i--)
- {
- if(MaxVal & (1<<i))
- {
- bit=1<<(i);
- break;
- }
- }*/
- for(i=0;i<N;i++)
- {
- vst[i+1]=INF;
- scanf("%d%d",&A[i].x,&A[i].y);
- B[j].ind=i;
- B[j++].val=A[i].x;
- }
- sort(B,B+j,cmp);
- int k=1;
- A[B[0].ind].x=k;
- for(i=1;i<j;i++)
- {
- if(B[i].val!=B[i-1].val)k++;
- A[B[i].ind].x=k;
- }
- // for(i=0;i<N;i++)cout<<A[i].x<<endl;
- int z;
- printf("Case #%d:\n",++cas);
- //cout<<"Case #"<<++cas<<":"<<endl;
- for(i=0;i<N;i++)
- {
- j=A[i].x;
- if(vst[j]==A[i].y)update(j,1);
- else if(vst[j]!=INF && A[i].y<vst[j])
- {
- vst[j]=A[i].y;
- x=readSingle(j);
- update(j,-x);
- update(j,1);
- x=read(j);
- y=findG(x);
- //while(y!=-1 && y<MaxVal)
- while( y<k)
- {
- //cout<<read(y)<<endl;
- y++;
- if(vst[y]<A[i].y)break;
- z=readSingle(y);
- update(y,-z);
- vst[y]=INF;
- y=findG(x);
- }
- }
- else if(vst[j]==INF)
- {
- bool t=0;
- x=read(j);
- if(!x)
- {
- t=1;
- vst[j]=A[i].y;
- update(j,1);
- }
- else
- {
- y=findG(x-1);
- y++;
- if(vst[y]>A[i].y)
- {
- t=1;
- vst[j]=A[i].y;
- update(j,1);
- }
- }
- if(t)
- {
- y=findG(x+1);
- while( y<k)
- {
- //cout<<read(y)<<endl;
- y++;
- //cout<<read(y)<<endl;
- if(vst[y]<A[i].y)break;
- z=readSingle(y);
- update(y,-z);
- vst[y]=INF;
- y=findG(x+1);
- }
- }
- }
- //cout<<"Step:"<<i+1<<endl;
- //for(k=0;k<N;k++)cout<<read(k+1);
- //cout<<endl;
- //cout<<read(N)<<endl;
- printf("%d\n",read(N+1));
- }
- if(T)//cout<<endl;
- printf("\n");
- }
- return 0;
- }
- /*
- Test Case:
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement