Guest User

spoj dquery tle

a guest
Feb 6th, 2016
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. //http://www.spoj.com/problems/DQUERY/
  2.  
  3. import java.io.*;
  4.  
  5. import java.util.*;
  6.  
  7. public class Main {
  8. //public static Node node[];
  9. public static int a[],c[],q[][];
  10. public static void main(String[] args)throws IOException {
  11. // TODO Auto-generated method stub
  12.  
  13. // InputStreamReader isr=new InputStreamReader(System.in);
  14. OutputWriter out=new OutputWriter(System.out);
  15. //BufferedReader br=new BufferedReader(isr);
  16.  
  17. Reader r=new Reader();
  18. int n=r.nextInt();
  19. a=new int[n];
  20. for(int i=0;i<n;i++)
  21. a[i]=r.nextInt();
  22.  
  23. int m=r.nextInt();
  24.  
  25. c=new int[4*n];
  26. q=new int[m][4];
  27.  
  28. for(int i=0;i<m;i++)
  29. {
  30. q[i][0]=r.nextInt()-1;
  31. q[i][1]=r.nextInt()-1;
  32. q[i][3]=i;
  33.  
  34.  
  35. }
  36.  
  37. Arrays.sort(q,new Comparator<int[]>(){
  38.  
  39. public int compare(int a1[],int a2[])
  40. {
  41. return a1[1]-a2[1];
  42.  
  43.  
  44. }
  45.  
  46.  
  47. });
  48.  
  49.  
  50.  
  51. //StringTokenizer st=new StringTokenizer(br.readLine());
  52.  
  53.  
  54.  
  55.  
  56. build(1,0,n-1); // 1 based indexing for seg tree array
  57. StringBuilder sb=new StringBuilder("");
  58.  
  59. int lastpos[]=new int[1000001];
  60. //int ans[]=new int[m];
  61. //System.out.println(Arrays.toString(c));
  62. for(int i=0,qno=0;i<n;i++)
  63. {//System.out.println(i);
  64. if(lastpos[a[i]]-1 != -1 ) //last pos is 1-based as we chk if (lastpos == 0) for uninitialized
  65. {
  66. modify(1,0,n-1,lastpos[a[i]]-1);
  67. }
  68.  
  69. lastpos[a[i]]=i+1;
  70. //System.out.println(Arrays.toString(c));
  71. while(qno < m && q[qno][1]==i)
  72. {
  73. q[qno][2]=find(1,0,n-1,q[qno][0],i);
  74.  
  75. qno++;
  76.  
  77. }
  78.  
  79.  
  80. }
  81. Arrays.sort(q,new Comparator<int[]>(){
  82.  
  83. public int compare(int a1[],int a2[])
  84. {
  85. return a1[3]-a2[3];
  86.  
  87.  
  88. }
  89.  
  90.  
  91. });
  92. for(int i=0;i<m;i++)
  93. {sb.append(q[i][2]+"\n");
  94. }
  95. out.printLine((sb.toString()));
  96. out.close();
  97. }
  98.  
  99. public static void modify(int index,int l,int r,int pos)
  100. { //System.out.println(l+" "+r+" "+index);
  101. c[index]--;
  102. if(l==r)
  103. {
  104.  
  105.  
  106. return;
  107. }
  108.  
  109.  
  110. int mid=(l+r)/2;
  111.  
  112.  
  113. if(pos <=mid)
  114. modify(index<<1,l,mid,pos);
  115. else
  116. modify((index<<1)+1,mid+1,r,pos);
  117.  
  118. }
  119. public static void build(int index,int l,int r)
  120. {
  121. if(l==r)
  122. {
  123. c[index]=1;
  124. return;
  125. }
  126.  
  127. int mid=(l+r)/2;
  128. build(index<<1,l,mid);
  129. build((index<<1)+1,mid+1,r);
  130.  
  131. c[index]=(r-l+1);
  132.  
  133.  
  134.  
  135.  
  136. }
  137.  
  138.  
  139.  
  140. public static int find(int index,int l,int r,int x,int y)
  141. { //System.out.println(l+" "+r+" "+x+" "+y+" "+index);
  142. if(x<=l && y >=r)
  143. return c[index];
  144.  
  145. int mid=(l+r)/2;
  146.  
  147. if(y<=mid)
  148. return find(index<<1,l,mid,x,y);
  149. if(x>mid)
  150. return find((index<<1)+1,mid+1,r,x,y);
  151.  
  152.  
  153. return find(index<<1,l,mid,x,mid) + find((index<<1)+1,mid+1,r,mid+1,y); //note that since range is divided in half
  154. //range end pts. x and y are also changed
  155.  
  156. }
  157.  
  158.  
  159.  
  160. }
  161.  
  162. //class Node{
  163.  
  164.  
  165.  
  166. //}
  167. class Reader {
  168. final private int BUFFER_SIZE = 1 << 16;private DataInputStream din;private byte[] buffer;private int bufferPointer, bytesRead;
  169. public Reader(){din=new DataInputStream(System.in);buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;
  170. }public Reader(String file_name) throws IOException{din=new DataInputStream(new FileInputStream(file_name));buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0;
  171. }public String readLine() throws IOException{byte[] buf=new byte[64];int cnt=0,c;while((c=read())!=-1){if(c=='\n')break;buf[cnt++]=(byte)c;}return new String(buf,0,cnt);
  172. }public int nextInt() throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;
  173. }public long nextLong() throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret;
  174. }public double nextDouble() throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret;
  175. }private void fillBuffer() throws IOException{bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1;
  176. }private byte read() throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++];
  177. }public void close() throws IOException{if(din==null) return;din.close();}
  178. }
  179.  
  180. class OutputWriter {
  181. private final PrintWriter writer;
  182.  
  183. public OutputWriter(OutputStream outputStream) {
  184. writer = new PrintWriter(outputStream);
  185. }
  186.  
  187. public OutputWriter(Writer writer) {
  188. this.writer = new PrintWriter(writer);
  189. }
  190.  
  191. public void print(Object... objects) {
  192. for (int i = 0; i < objects.length; i++) {
  193. if (i != 0)
  194. writer.print(' ');
  195. writer.print(objects[i]);
  196. }
  197. }
  198.  
  199. public void printLine(Object... objects) {
  200. print(objects);
  201. writer.println();
  202. }
  203.  
  204. public void close() {
  205. writer.close();
  206. }
  207. }
Add Comment
Please, Sign In to add comment