Advertisement
farsid

Untitled

Mar 8th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. #define filer() freopen("in.txt","r",stdin)
  2. #define filew() freopen("out.txt","w",stdout)
  3. #include<iostream>
  4. #include<stdio.h>
  5. #include<string.h>
  6. #include<math.h>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<stack>
  10. #include<string>
  11. #include<vector>
  12. #include <map>
  13. #define INF 1<<30
  14. #define PI pair<int,int>
  15. #define f first
  16. #define s second
  17. #define SET(a, x) memset((a), (x), sizeof(a))
  18. #define pb push_back
  19. #define i64 long long
  20. #define EPS (1e-9)
  21. using namespace std;
  22. typedef vector<int> VI;
  23. typedef vector<PI> vii;
  24. //i64 INF=(i64)((i64)1<<(i64)59);
  25.  
  26. int N;
  27.  
  28. struct node
  29. {
  30. int x,y;
  31. }A[15009];
  32.  
  33. struct point
  34. {
  35. int ind;
  36. int val;
  37. }B[30009];
  38.  
  39. bool cmp(point a,point b)
  40. {
  41. return a.val<b.val;
  42. }
  43.  
  44. int vst[30009],tree[(1<<18)+10],MaxVal,bit;
  45.  
  46. void update(int idx ,int val){
  47. while (idx <= MaxVal){
  48. tree[idx] += val;
  49. idx += (idx & -idx);
  50. }
  51. }
  52.  
  53. int read(int idx){
  54. int sum = 0;
  55. while (idx > 0){
  56. sum += tree[idx];
  57. idx -= (idx & -idx);
  58. }
  59. return sum;
  60. }
  61.  
  62. int findG(int cumFre){
  63. int idx = 0;
  64. int bitMask=bit;
  65. while ((bitMask != 0) && (idx < MaxVal)){
  66. int tIdx = idx + bitMask;
  67. if (cumFre >= tree[tIdx]){
  68. // if current cumulative frequency is equal to cumFre,
  69. // we are still looking for higher index (if exists)
  70. idx = tIdx;
  71. cumFre -= tree[tIdx];
  72. }
  73. bitMask >>= 1;
  74. }
  75. if (cumFre != 0)
  76. return -1;
  77. else
  78. return idx;
  79. }
  80.  
  81. int readSingle(int idx){
  82. int sum = tree[idx]; // sum will be decreased
  83. if (idx > 0){ // special case
  84. int z = idx - (idx & -idx); // make z first
  85. idx--; // idx is no important any more, so instead y, you can use idx
  86. while (idx != z){ // at some iteration idx (y) will become z
  87. sum -= tree[idx];
  88. // substruct tree frequency which is between y and "the same path"
  89. idx -= (idx & -idx);
  90. }
  91. }
  92. return sum;
  93. }
  94.  
  95. int main()
  96. {
  97. //filer();
  98.  
  99. //filew();
  100. int T,cas=0;
  101. int i,j,x,y;
  102. scanf("%d",&T);
  103. MaxVal=1<<16;
  104. bit=1<<15;
  105. while(T--)
  106. {
  107.  
  108. SET(tree,0);
  109.  
  110. scanf("%d",&N);
  111. //if(N==0)continue;
  112. j=0;
  113. /*MaxVal=N;
  114. for(i=30;i>=0;i--)
  115. {
  116. if(MaxVal & (1<<i))
  117. {
  118. bit=1<<(i);
  119. break;
  120. }
  121. }*/
  122. for(i=0;i<N;i++)
  123. {
  124. vst[i+1]=INF;
  125. scanf("%d%d",&A[i].x,&A[i].y);
  126.  
  127. B[j].ind=i;
  128. B[j++].val=A[i].x;
  129.  
  130. }
  131.  
  132. sort(B,B+j,cmp);
  133. int k=1;
  134.  
  135. A[B[0].ind].x=k;
  136. for(i=1;i<j;i++)
  137. {
  138. if(B[i].val!=B[i-1].val)k++;
  139. A[B[i].ind].x=k;
  140. }
  141. // for(i=0;i<N;i++)cout<<A[i].x<<endl;
  142. int z;
  143. printf("Case #%d:\n",++cas);
  144. //cout<<"Case #"<<++cas<<":"<<endl;
  145. for(i=0;i<N;i++)
  146. {
  147. j=A[i].x;
  148.  
  149. if(vst[j]==A[i].y)update(j,1);
  150.  
  151. else if(vst[j]!=INF && A[i].y<vst[j])
  152. {
  153. vst[j]=A[i].y;
  154. x=readSingle(j);
  155. update(j,-x);
  156. update(j,1);
  157. x=read(j);
  158. y=findG(x);
  159. //while(y!=-1 && y<MaxVal)
  160. while( y<k)
  161. {
  162. //cout<<read(y)<<endl;
  163. y++;
  164.  
  165. if(vst[y]<A[i].y)break;
  166. z=readSingle(y);
  167. update(y,-z);
  168. vst[y]=INF;
  169. y=findG(x);
  170. }
  171. }
  172.  
  173. else if(vst[j]==INF)
  174. {
  175. bool t=0;
  176. x=read(j);
  177. if(!x)
  178. {
  179. t=1;
  180. vst[j]=A[i].y;
  181. update(j,1);
  182. }
  183. else
  184. {
  185. y=findG(x-1);
  186. y++;
  187. if(vst[y]>A[i].y)
  188. {
  189. t=1;
  190. vst[j]=A[i].y;
  191. update(j,1);
  192. }
  193.  
  194. }
  195. if(t)
  196. {
  197. y=findG(x+1);
  198. while( y<k)
  199. {
  200. //cout<<read(y)<<endl;
  201. y++;
  202. //cout<<read(y)<<endl;
  203. if(vst[y]<A[i].y)break;
  204. z=readSingle(y);
  205. update(y,-z);
  206. vst[y]=INF;
  207. y=findG(x+1);
  208. }
  209. }
  210.  
  211.  
  212. }
  213. //cout<<"Step:"<<i+1<<endl;
  214. //for(k=0;k<N;k++)cout<<read(k+1);
  215. //cout<<endl;
  216. //cout<<read(N)<<endl;
  217. printf("%d\n",read(N+1));
  218. }
  219. if(T)//cout<<endl;
  220. printf("\n");
  221. }
  222. return 0;
  223. }
  224. /*
  225. Test Case:
  226.  
  227. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement