fork download
  1. import java.io.DataInputStream;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.Arrays;
  6. import java.util.Comparator;
  7.  
  8.  
  9. class RATING {
  10. static int tree[];
  11. public static void main(String[] args) throws IOException {
  12. // TODO Auto-generated method stub
  13. Reader r=new Reader();
  14. StringBuilder sb=new StringBuilder();
  15. int n=r.nextInt();
  16. Pair p[]=new Pair[n+1];
  17. Pair p2[]=new Pair[n+1];
  18. tree=new int[100001];
  19. p[0]=new Pair(0, 0, 0);
  20. p2[0]=new Pair(0, 0, 0);
  21. for(int i=1;i<=n;i++){
  22. p[i]=new Pair(i,r.nextInt(),r.nextInt());
  23. p2[i]=new Pair(i,p[i].rating2,p[i].rating1);
  24. update(p[i].rating2,1);
  25. }
  26. Arrays.sort(p);
  27. Arrays.sort(p2,new Comparator<Pair>() {
  28. @Override
  29. public int compare(Pair o1, Pair o2) {
  30. // TODO Auto-generated method stub
  31. if(o1.rating1==o2.rating1)return o1.rating2-o2.rating2;
  32. return o1.rating1-o2.rating1;
  33. }
  34. });
  35. //System.out.println(Arrays.toString(p));
  36. //System.out.println(Arrays.toString(p2));
  37. int answers[]=new int[n+1];
  38. for(int i=p.length-1;i>0;i--){
  39. int sum=read(p[i].rating2-1);
  40. int sub=0;
  41. int count=0;
  42. //if(count==0)
  43. sub=binarySearch(p2,p[i].rating2,p[i].index);
  44. if(sum==0){
  45. for(int j=i-1;j>0;j--){
  46. if(p[i].rating1>p[j].rating1)break;
  47. count++;
  48. }
  49. }
  50. // System.out.println(i+" "+sub);
  51. //else
  52. answers[p[i].index]=sum+sub-count;
  53. //sb.append(sum+"\n");
  54. update(p[i].rating2,-1);
  55. }
  56. for(int i=1;i<=n;i++)sb.append(answers[i]+"\n");
  57. pr.write(sb.toString());
  58. pr.flush();
  59.  
  60. }
  61. public static int binarySearch(Pair p2[],int val,int index){
  62. int low=1,high=p2.length-1,mid=(low+((high-low+1)>>1));
  63. while(low<high){
  64. mid=(low+((high-low+1)>>1));
  65. if(p2[mid].rating1<val)low=mid+1;
  66. else if(p2[mid].rating1==val){
  67. low=mid;
  68. }
  69. else high=mid-1;
  70. }
  71. if(p2[low].index==index){
  72. int count=0;
  73. for(int j=low-1;j>0 && p2[j].rating1==val;j--)
  74. count++;
  75. return count;
  76. }
  77. else{
  78. int j=low-1,count=0;
  79. for(;j>0 && p2[j].index!=index;j--);
  80. for(int k=j-1;k>0 && p2[k].rating1==val;k--)
  81. count++;
  82. return count;
  83.  
  84. }
  85. // return 0;
  86. }
  87. public static void update(int idx,int value){
  88. while(idx<tree.length){
  89. tree[idx]+=value;
  90. idx += idx & -idx;
  91. }
  92. }
  93. public static int read(int idx){
  94. int sum=0;
  95. while(idx>0){
  96. sum+=tree[idx];
  97. idx -=idx & -idx;
  98. }
  99. return sum;
  100. }
  101. static class Pair implements Comparable<Pair>{
  102. int index,rating1,rating2;
  103.  
  104. @Override
  105. public String toString() {
  106. return "Pair [index=" + index + ", rating1=" + rating1 + ", rating2=" + rating2 + "]\n";
  107. }
  108.  
  109. public int getIndex() {
  110. return index;
  111. }
  112.  
  113. public void setIndex(int index) {
  114. this.index = index;
  115. }
  116.  
  117. public int getRating1() {
  118. return rating1;
  119. }
  120.  
  121. public void setRating1(int rating1) {
  122. this.rating1 = rating1;
  123. }
  124.  
  125. public int getRating2() {
  126. return rating2;
  127. }
  128.  
  129. public void setRating2(int rating2) {
  130. this.rating2 = rating2;
  131. }
  132.  
  133. public Pair() {
  134. super();
  135. // TODO Auto-generated constructor stub
  136. }
  137.  
  138. public Pair(int index, int rating1, int rating2) {
  139. super();
  140. this.index = index;
  141. this.rating1 = rating1;
  142. this.rating2 = rating2;
  143. }
  144.  
  145. @Override
  146. public int compareTo(Pair o) {
  147. // TODO Auto-generated method stub
  148. if(this.rating1==o.rating1)return this.rating2-o.rating2;
  149. return this.rating1-o.rating1;
  150. }
  151. }
  152. static class Reader {
  153. final private int BUFFER_SIZE = 1 << 16;
  154. private DataInputStream din;
  155. private byte[] buffer;
  156. private int bufferPointer, bytesRead;
  157.  
  158. public Reader() {
  159. din = new DataInputStream(System.in);
  160. buffer = new byte[BUFFER_SIZE];
  161. bufferPointer = bytesRead = 0;
  162. }
  163.  
  164. public Reader(String file_name) throws IOException {
  165. din = new DataInputStream(new FileInputStream(file_name));
  166. buffer = new byte[BUFFER_SIZE];
  167. bufferPointer = bytesRead = 0;
  168. }
  169.  
  170. public String readLine() throws IOException {
  171. byte[] buf = new byte[64];
  172. int cnt = 0, c;
  173. while ((c = read()) != -1) {
  174. if (c == '\n')
  175. break;
  176. buf[cnt++] = (byte) c;
  177. }
  178. return new String(buf, 0, cnt);
  179. }
  180.  
  181. public int nextInt() throws IOException {
  182. int ret = 0;
  183. byte c = read();
  184. while (c <= ' ')
  185. c = read();
  186. boolean neg = (c == '-');
  187. if (neg)
  188. c = read();
  189. do {
  190. ret = ret * 10 + c - '0';
  191. } while ((c = read()) >= '0' && c <= '9');
  192. if (neg)
  193. return -ret;
  194. return ret;
  195. }
  196.  
  197. public long nextLong() throws IOException {
  198. long ret = 0;
  199. byte c = read();
  200. while (c <= ' ')
  201. c = read();
  202. boolean neg = (c == '-');
  203. if (neg)
  204. c = read();
  205. do {
  206. ret = ret * 10 + c - '0';
  207. } while ((c = read()) >= '0' && c <= '9');
  208. if (neg)
  209. return -ret;
  210. return ret;
  211. }
  212.  
  213. public double nextDouble() throws IOException {
  214. double ret = 0, div = 1;
  215. byte c = read();
  216. while (c <= ' ')
  217. c = read();
  218. boolean neg = (c == '-');
  219. if (neg)
  220. c = read();
  221. do {
  222. ret = ret * 10 + c - '0';
  223. } while ((c = read()) >= '0' && c <= '9');
  224. if (c == '.')
  225. while ((c = read()) >= '0' && c <= '9')
  226. ret += (c - '0') / (div *= 10);
  227. if (neg)
  228. return -ret;
  229. return ret;
  230. }
  231.  
  232. private void fillBuffer() throws IOException {
  233. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  234. if (bytesRead == -1)
  235. buffer[0] = -1;
  236. }
  237.  
  238. private byte read() throws IOException {
  239. if (bufferPointer == bytesRead)
  240. fillBuffer();
  241. return buffer[bufferPointer++];
  242. }
  243.  
  244. public void close() throws IOException {
  245. if (din == null)
  246. return;
  247. din.close();
  248. }
  249. }
  250.  
  251. }
  252. /*
  253. [Pair [index=0, rating1=0, rating2=0]
  254. , Pair [index=2, rating1=862, rating2=700]
  255. , Pair [index=8, rating1=1014, rating2=1473]
  256. , Pair [index=6, rating1=1033, rating2=950]
  257. , Pair [index=3, rating1=1075, rating2=1089]
  258. , Pair [index=4, rating1=1568, rating2=1557]
  259. , Pair [index=7, rating1=1656, rating2=1649]
  260. , Pair [index=1, rating1=1798, rating2=1832]
  261. , Pair [index=5, rating1=2575, rating2=1984]
  262. ]
  263. */
  264. /*
  265. 8
  266. 862 700
  267. 1014 1473
  268. 1033 600
  269. 1075 1475
  270. 1568 650
  271. 1656 1474
  272. 1798 1473
  273. 2575 1475
  274. */
  275. /*
  276. 9
  277. 862 100
  278. 1014 100
  279. 1033 100
  280. 1075 200
  281. 1568 300
  282. 1656 200
  283. 1798 300
  284. 2575 300
  285. 2600 200
  286. */
Success #stdin #stdout 0.07s 380160KB
stdin
9
100 862
100 1014
100 1033
1075 200
300 1568
200 1656
200 2600
300 2575
300 1798
stdout
0
1
2
0
3
3
4
6
5