Guest User

Untitled

a guest
May 17th, 2016
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.56 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class DQuery
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         InputReader in = new InputReader(System.in);
  9.         OutputWriter out = new OutputWriter(System.out);
  10.  
  11.         Solver solver = new Solver(in, out);
  12.         solver.solve(1);
  13.  
  14.         out.flush();
  15.  
  16.         in.close();
  17.         out.close();
  18.     }
  19.  
  20.     static class Solver
  21.     {
  22.         int n, q, sqrtN, size;
  23.         int[] arr, count;
  24.         Query[] queries;
  25.         InputReader in;
  26.         OutputWriter out;
  27.  
  28.         public Solver(InputReader in, OutputWriter out)
  29.         {
  30.             this.in = in;
  31.             this.out = out;
  32.         }
  33.  
  34.         void solve(int testNumber)
  35.         {
  36.             n = in.nextInt();
  37.             arr = new int[n + 1];
  38.             count = new int[(int) (1e6 + 5)];
  39.  
  40.             for (int i = 1; i <= n; i++)
  41.             {
  42.                 arr[i] = in.nextInt();
  43.  
  44.                 count[arr[i]]++;
  45.  
  46.                 if (count[arr[i]] == 1)
  47.                     size++;
  48.             }
  49.  
  50.             q = in.nextInt();
  51.             sqrtN = (int) Math.sqrt(n);
  52.             queries = new Query[q];
  53.  
  54.             for (int i = 0; i < q; i++)
  55.             {
  56.                 int left, right;
  57.  
  58.                 left = in.nextInt();
  59.                 right = in.nextInt();
  60.                 queries[i] = new Query(i, left, right);
  61.             }
  62.  
  63.             Arrays.sort(queries, new Comparator<Query>()
  64.             {
  65.                 @Override public int compare(Query o1, Query o2)
  66.                 {
  67.                     if (o1.blockNumber == o2.blockNumber)
  68.                         return Integer.compare(o1.right, o2.right);
  69.  
  70.                     return Integer.compare(o1.blockNumber, o2.blockNumber);
  71.                 }
  72.             });
  73.  
  74.             int left, right;
  75.             int[] answer;
  76.  
  77.             left = 1;
  78.             right = n;
  79.             answer = new int[q];
  80.  
  81.             for (int i = 0; i < q; i++)
  82.             {
  83.                 while (left < queries[i].left)
  84.                 {
  85.                     delete(arr[left]);
  86.                     left++;
  87.                 }
  88.  
  89.                 while (left > queries[i].left)
  90.                 {
  91.                     left--;
  92.                     add(arr[left]);
  93.                 }
  94.  
  95.                 while (right < queries[i].right)
  96.                 {
  97.                     right++;
  98.                     add(arr[right]);
  99.                 }
  100.  
  101.                 while (right > queries[i].right)
  102.                 {
  103.                     delete(arr[right]);
  104.                     right--;
  105.                 }
  106.  
  107.                 answer[queries[i].queryNumber] = size;
  108.             }
  109.  
  110.             for (int i = 0; i < q; i++)
  111.                 out.println(answer[i]);
  112.  
  113.         }
  114.  
  115.         void add(int num)
  116.         {
  117.             count[num]++;
  118.  
  119.             if (count[num] == 1)
  120.                 size++;
  121.         }
  122.  
  123.         void delete(int num)
  124.         {
  125.             count[num]--;
  126.  
  127.             if (count[num] == 0)
  128.                 size--;
  129.         }
  130.  
  131.         class Query
  132.         {
  133.             int queryNumber, left, right, blockNumber;
  134.  
  135.             public Query(int queryNumber, int left, int right)
  136.             {
  137.                 this.queryNumber = queryNumber;
  138.                 this.left = left;
  139.                 this.right = right;
  140.                 blockNumber = left / sqrtN;
  141.             }
  142.  
  143.         }
  144.  
  145.     }
  146.  
  147.     static class InputReader
  148.     {
  149.         private InputStream stream;
  150.         private byte[] buf = new byte[1024];
  151.         private int curChar;
  152.         private int numChars;
  153.  
  154.         public InputReader(InputStream stream)
  155.         {
  156.             this.stream = stream;
  157.         }
  158.  
  159.         public int read()
  160.         {
  161.             if (numChars == -1)
  162.                 throw new InputMismatchException();
  163.  
  164.             if (curChar >= numChars)
  165.             {
  166.                 curChar = 0;
  167.                 try
  168.                 {
  169.                     numChars = stream.read(buf);
  170.                 }
  171.                 catch (IOException e)
  172.                 {
  173.                     throw new InputMismatchException();
  174.                 }
  175.                 if (numChars <= 0)
  176.                     return -1;
  177.             }
  178.  
  179.             return buf[curChar++];
  180.         }
  181.  
  182.         public int nextInt()
  183.         {
  184.             int c = read();
  185.  
  186.             while (isSpaceChar(c))
  187.                 c = read();
  188.  
  189.             int sgn = 1;
  190.  
  191.             if (c == '-')
  192.             {
  193.                 sgn = -1;
  194.                 c = read();
  195.             }
  196.  
  197.             int res = 0;
  198.  
  199.             do
  200.             {
  201.                 if (c < '0' || c > '9')
  202.                     throw new InputMismatchException();
  203.  
  204.                 res *= 10;
  205.                 res += c & 15;
  206.  
  207.                 c = read();
  208.             } while (!isSpaceChar(c));
  209.  
  210.             return res * sgn;
  211.         }
  212.  
  213.         public int[] nextIntArray(int arraySize)
  214.         {
  215.             int array[] = new int[arraySize];
  216.  
  217.             for (int i = 0; i < arraySize; i++)
  218.                 array[i] = nextInt();
  219.  
  220.             return array;
  221.         }
  222.  
  223.         public long nextLong()
  224.         {
  225.             int c = read();
  226.  
  227.             while (isSpaceChar(c))
  228.                 c = read();
  229.  
  230.             int sign = 1;
  231.  
  232.             if (c == '-')
  233.             {
  234.                 sign = -1;
  235.  
  236.                 c = read();
  237.             }
  238.  
  239.             long result = 0;
  240.  
  241.             do
  242.             {
  243.                 if (c < '0' || c > '9')
  244.                     throw new InputMismatchException();
  245.  
  246.                 result *= 10;
  247.                 result += c & 15;
  248.  
  249.                 c = read();
  250.             } while (!isSpaceChar(c));
  251.  
  252.             return result * sign;
  253.         }
  254.  
  255.         public long[] nextLongArray(int arraySize)
  256.         {
  257.             long array[] = new long[arraySize];
  258.  
  259.             for (int i = 0; i < arraySize; i++)
  260.                 array[i] = nextLong();
  261.  
  262.             return array;
  263.         }
  264.  
  265.         public float nextFloat() // problematic
  266.         {
  267.             float result, div;
  268.             byte c;
  269.  
  270.             result = 0;
  271.             div = 1;
  272.             c = (byte) read();
  273.  
  274.             while (c <= ' ')
  275.                 c = (byte) read();
  276.  
  277.             boolean isNegative = (c == '-');
  278.  
  279.             if (isNegative)
  280.                 c = (byte) read();
  281.  
  282.             do
  283.             {
  284.                 result = result * 10 + c - '0';
  285.             } while ((c = (byte) read()) >= '0' && c <= '9');
  286.  
  287.             if (c == '.')
  288.                 while ((c = (byte) read()) >= '0' && c <= '9')
  289.                     result += (c - '0') / (div *= 10);
  290.  
  291.             if (isNegative)
  292.                 return -result;
  293.  
  294.             return result;
  295.         }
  296.  
  297.         public double nextDouble() // not completely accurate
  298.         {
  299.             double ret = 0, div = 1;
  300.             byte c = (byte) read();
  301.  
  302.             while (c <= ' ')
  303.                 c = (byte) read();
  304.  
  305.             boolean neg = (c == '-');
  306.  
  307.             if (neg)
  308.                 c = (byte) read();
  309.  
  310.             do
  311.             {
  312.                 ret = ret * 10 + c - '0';
  313.             } while ((c = (byte) read()) >= '0' && c <= '9');
  314.  
  315.             if (c == '.')
  316.                 while ((c = (byte) read()) >= '0' && c <= '9')
  317.                     ret += (c - '0') / (div *= 10);
  318.  
  319.             if (neg)
  320.                 return -ret;
  321.  
  322.             return ret;
  323.         }
  324.  
  325.         public String next()
  326.         {
  327.             int c = read();
  328.  
  329.             while (isSpaceChar(c))
  330.                 c = read();
  331.  
  332.             StringBuilder res = new StringBuilder();
  333.  
  334.             do
  335.             {
  336.                 res.appendCodePoint(c);
  337.  
  338.                 c = read();
  339.             } while (!isSpaceChar(c));
  340.  
  341.             return res.toString();
  342.         }
  343.  
  344.         public boolean isSpaceChar(int c)
  345.         {
  346.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  347.         }
  348.  
  349.         public String nextLine()
  350.         {
  351.             int c = read();
  352.  
  353.             StringBuilder result = new StringBuilder();
  354.  
  355.             do
  356.             {
  357.                 result.appendCodePoint(c);
  358.  
  359.                 c = read();
  360.             } while (!isNewLine(c));
  361.  
  362.             return result.toString();
  363.         }
  364.  
  365.         public boolean isNewLine(int c)
  366.         {
  367.             return c == '\n';
  368.         }
  369.  
  370.         public void close()
  371.         {
  372.             try
  373.             {
  374.                 stream.close();
  375.             }
  376.             catch (IOException e)
  377.             {
  378.                 e.printStackTrace();
  379.             }
  380.         }
  381.  
  382.     }
  383.  
  384.     static class OutputWriter
  385.     {
  386.         private PrintWriter writer;
  387.  
  388.         public OutputWriter(OutputStream stream)
  389.         {
  390.             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
  391.                     stream)));
  392.         }
  393.  
  394.         public OutputWriter(Writer writer)
  395.         {
  396.             this.writer = new PrintWriter(writer);
  397.         }
  398.  
  399.         public void println(int x)
  400.         {
  401.             writer.println(x);
  402.         }
  403.  
  404.         public void print(int x)
  405.         {
  406.             writer.print(x);
  407.         }
  408.  
  409.         public void println(int array[], int size)
  410.         {
  411.             for (int i = 0; i < size; i++)
  412.                 println(array[i]);
  413.         }
  414.  
  415.         public void print(int array[], int size)
  416.         {
  417.             for (int i = 0; i < size; i++)
  418.                 print(array[i] + " ");
  419.         }
  420.  
  421.         public void println(long x)
  422.         {
  423.             writer.println(x);
  424.         }
  425.  
  426.         public void print(long x)
  427.         {
  428.             writer.print(x);
  429.         }
  430.  
  431.         public void println(long array[], int size)
  432.         {
  433.             for (int i = 0; i < size; i++)
  434.                 println(array[i]);
  435.         }
  436.  
  437.         public void print(long array[], int size)
  438.         {
  439.             for (int i = 0; i < size; i++)
  440.                 print(array[i]);
  441.         }
  442.  
  443.         public void println(float num)
  444.         {
  445.             writer.println(num);
  446.         }
  447.  
  448.         public void print(float num)
  449.         {
  450.             writer.print(num);
  451.         }
  452.  
  453.         public void println(double num)
  454.         {
  455.             writer.println(num);
  456.         }
  457.  
  458.         public void print(double num)
  459.         {
  460.             writer.print(num);
  461.         }
  462.  
  463.         public void println(String s)
  464.         {
  465.             writer.println(s);
  466.         }
  467.  
  468.         public void print(String s)
  469.         {
  470.             writer.print(s);
  471.         }
  472.  
  473.         public void println()
  474.         {
  475.             writer.println();
  476.         }
  477.  
  478.         public void printSpace()
  479.         {
  480.             writer.print(" ");
  481.         }
  482.  
  483.         public void flush()
  484.         {
  485.             writer.flush();
  486.         }
  487.  
  488.         public void close()
  489.         {
  490.             writer.close();
  491.         }
  492.  
  493.     }
  494.  
  495.     static class CMath
  496.     {
  497.         static long power(long number, long power)
  498.         {
  499.             if (number == 1 || number == 0 || power == 0)
  500.                 return 1;
  501.  
  502.             if (power == 1)
  503.                 return number;
  504.  
  505.             if (power % 2 == 0)
  506.                 return power(number * number, power / 2);
  507.             else
  508.                 return power(number * number, power / 2) * number;
  509.         }
  510.  
  511.         static long modPower(long number, long power, long mod)
  512.         {
  513.             if (number == 1 || number == 0 || power == 0)
  514.                 return 1;
  515.  
  516.             number = mod(number, mod);
  517.  
  518.             if (power == 1)
  519.                 return number;
  520.  
  521.             long square = mod(number * number, mod);
  522.  
  523.             if (power % 2 == 0)
  524.                 return modPower(square, power / 2, mod);
  525.             else
  526.                 return mod(modPower(square, power / 2, mod) * number, mod);
  527.         }
  528.  
  529.         static long moduloInverse(long number, long mod)
  530.         {
  531.             return modPower(number, mod - 2, mod);
  532.         }
  533.  
  534.         static long mod(long number, long mod)
  535.         {
  536.             return number - (number / mod) * mod;
  537.         }
  538.  
  539.     }
  540.  
  541. }
Add Comment
Please, Sign In to add comment