Advertisement
Guest User

Untitled

a guest
Jun 19th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.49 KB | None | 0 0
  1. import java.io.OutputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.io.PrintWriter;
  6. import java.util.PriorityQueue;
  7. import java.io.BufferedWriter;
  8. import java.util.InputMismatchException;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.AbstractCollection;
  12. import java.io.Writer;
  13. import java.io.OutputStreamWriter;
  14. import java.util.Collections;
  15. import java.io.InputStream;
  16.  
  17. /**
  18.  * Built using CHelper plug-in
  19.  * Actual solution is at the top
  20.  *
  21.  * @author ilyakor
  22.  */
  23. public class Main {
  24.     public static void main(String[] args) {
  25.         InputStream inputStream = System.in;
  26.         OutputStream outputStream = System.out;
  27.         InputReader in = new InputReader(inputStream);
  28.         OutputWriter out = new OutputWriter(outputStream);
  29.         TaskE solver = new TaskE();
  30.         solver.solve(1, in, out);
  31.         out.close();
  32.     }
  33.  
  34.     static class TaskE {
  35.         public void solve(int testNumber, InputReader in, OutputWriter out) {
  36.             int n = in.nextInt();
  37.             int m = in.nextInt();
  38.             ArrayList<ii> entries = new ArrayList<>();
  39.             for (int i = 0; i < n; ++i) {
  40.                 entries.add(new ii(in.nextInt(), i));
  41.             }
  42.  
  43.             SegmentTreeFastIntervalAddSum tree = new SegmentTreeFastIntervalAddSum(1 << 18);
  44.             for (ii entry : entries) if (entry.first == 1) tree.set(entry.second + 1, 1);
  45.  
  46.             Collections.shuffle(entries);
  47.             Collections.sort(entries);
  48.             PriorityQueue<TaskE.Query> qu = new PriorityQueue<>();
  49.             long[] ans = new long[m];
  50.             for (int i = 0; i < m; ++i) {
  51.                 TaskE.Query q = new TaskE.Query(in.nextInt() - 1, in.nextInt() - 1);
  52.                 q.ind = i;
  53.                 q.sum = tree.querySum(q.l + 1, q.r + 1);
  54.                 if (q.sum == 0) {
  55.                     ans[i] = 1;
  56.                 } else {
  57.                     qu.add(q);
  58.                 }
  59.             }
  60.  
  61.  
  62.             for (ii entry : entries) {
  63.                 int x = entry.first, ind = entry.second;
  64.                 if (x <= 1) continue;
  65.                 while (!qu.isEmpty() && qu.peek().sum < x - 1) {
  66.                     TaskE.Query q = qu.poll();
  67.                     q.sum = tree.querySum(q.l + 1, q.r + 1);
  68.                     if (q.sum >= x - 1) {
  69.                         qu.add(q);
  70.                     } else {
  71.                         ans[q.ind] = q.sum + 1;
  72.                     }
  73.                 }
  74.                 tree.set(ind + 1, x);
  75.             }
  76.             while (!qu.isEmpty()) {
  77.                 TaskE.Query q = qu.poll();
  78.                 q.sum = tree.querySum(q.l + 1, q.r + 1);
  79.                 ans[q.ind] = q.sum + 1;
  80.             }
  81.             for (int i = 0; i < m; ++i)
  82.                 out.printLine(ans[i]);
  83.         }
  84.  
  85.         static class Query implements Comparable<TaskE.Query> {
  86.             int l;
  87.             int r;
  88.             int ind;
  89.             long sum;
  90.  
  91.             public Query(int l, int r) {
  92.                 if (l > r) {
  93.                     int t = l;
  94.                     l = r;
  95.                     r = t;
  96.                 }
  97.                 this.l = l;
  98.                 this.r = r;
  99.                 sum = 0;
  100.             }
  101.  
  102.  
  103.             public int compareTo(TaskE.Query o) {
  104.                 return Long.compare(sum, o.sum);
  105.             }
  106.  
  107.         }
  108.  
  109.     }
  110.  
  111.     static class ii implements Comparable<ii> {
  112.         public int first;
  113.         public int second;
  114.  
  115.         public ii() {
  116.         }
  117.  
  118.         public ii(int first, int second) {
  119.             this.first = first;
  120.             this.second = second;
  121.         }
  122.  
  123.         public boolean equals(Object o) {
  124.             if (this == o) return true;
  125.             if (o == null || getClass() != o.getClass()) return false;
  126.  
  127.             ii ii = (ii) o;
  128.  
  129.             if (first != ii.first) return false;
  130.             if (second != ii.second) return false;
  131.  
  132.             return true;
  133.         }
  134.  
  135.         public int hashCode() {
  136.             int result = first;
  137.             result = 31 * result + second;
  138.             return result;
  139.         }
  140.  
  141.         public int compareTo(ii o) {
  142.             if (first != o.first) {
  143.                 return first < o.first ? -1 : 1;
  144.             }
  145.             if (second != o.second) {
  146.                 return second < o.second ? -1 : 1;
  147.             }
  148.             return 0;
  149.         }
  150.  
  151.  
  152.         public String toString() {
  153.             return "{" +
  154.                     "first=" + first +
  155.                     ", second=" + second +
  156.                     '}';
  157.         }
  158.  
  159.     }
  160.  
  161.     static class OutputWriter {
  162.         private final PrintWriter writer;
  163.  
  164.         public OutputWriter(OutputStream outputStream) {
  165.             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  166.         }
  167.  
  168.         public OutputWriter(Writer writer) {
  169.             this.writer = new PrintWriter(writer);
  170.         }
  171.  
  172.         public void print(Object... objects) {
  173.             for (int i = 0; i < objects.length; i++) {
  174.                 if (i != 0) {
  175.                     writer.print(' ');
  176.                 }
  177.                 writer.print(objects[i]);
  178.             }
  179.         }
  180.  
  181.         public void printLine(Object... objects) {
  182.             print(objects);
  183.             writer.println();
  184.         }
  185.  
  186.         public void close() {
  187.             writer.close();
  188.         }
  189.  
  190.     }
  191.  
  192.     static class SegmentTreeFastIntervalAddSum {
  193.         final int n;
  194.         final long[] t;
  195.         final long[] m;
  196.         final boolean[] h;
  197.         final long[] q;
  198.  
  199.         public SegmentTreeFastIntervalAddSum(int n) {
  200.             this.n = n;
  201.             t = new long[2 * n];
  202.             m = new long[2 * n];
  203.             h = new boolean[2 * n];
  204.             q = new long[2 * n];
  205.             for (int i = n; i < 2 * n; i++)
  206.                 q[i] = 1;
  207.             for (int i = 2 * n - 1; i > 1; i -= 2)
  208.                 q[i >> 1] = q[i] + q[i ^ 1];
  209.         }
  210.  
  211.         void modifierHelper(int i, long p) {
  212.             t[i] += p * q[i];
  213.             m[i] += p;
  214.             h[i] = true;
  215.         }
  216.  
  217.         void pop(int i) {
  218.             if (h[i >> 1]) {
  219.                 t[i >> 1] = t[i] + t[i ^ 1] + m[i >> 1] * q[i >> 1];
  220.             } else {
  221.                 t[i >> 1] = t[i] + t[i ^ 1];
  222.             }
  223.         }
  224.  
  225.         void popUp(int i) {
  226.             for (; i > 1; i >>= 1)
  227.                 pop(i);
  228.         }
  229.  
  230.         void push(int i) {
  231.             if (h[i >> 1]) {
  232.                 modifierHelper(i, m[i >> 1]);
  233.                 modifierHelper(i ^ 1, m[i >> 1]);
  234.                 h[i >> 1] = false;
  235.                 m[i >> 1] = 0;
  236.             }
  237.         }
  238.  
  239.         void pushDown(int i) {
  240.             int k;
  241.             for (k = 0; (i >> k) > 0; k++)
  242.                 ;
  243.             for (k -= 2; k >= 0; k--)
  244.                 push(i >> k);
  245.         }
  246.  
  247.         public void modifyAdd(int a, int b, long v) {
  248.             a += n;
  249.             b += n;
  250.             pushDown(a);
  251.             pushDown(b);
  252.             int ta = a;
  253.             int tb = b;
  254.  
  255.             for (; a <= b; a = (a + 1) >> 1, b = (b - 1) >> 1) {
  256.                 if ((a & 1) != 0)
  257.                     modifierHelper(a, v);
  258.                 if ((b & 1) == 0)
  259.                     modifierHelper(b, v);
  260.             }
  261.  
  262.             popUp(ta);
  263.             popUp(tb);
  264.         }
  265.  
  266.         public long querySum(int a, int b) {
  267.             a += n;
  268.             b += n;
  269.             pushDown(a);
  270.             pushDown(b);
  271.  
  272.             long res = 0;
  273.             for (; a <= b; a = (a + 1) >> 1, b = (b - 1) >> 1) {
  274.                 if ((a & 1) != 0)
  275.                     res += t[a];
  276.                 if ((b & 1) == 0)
  277.                     res += t[b];
  278.             }
  279.             return res;
  280.         }
  281.  
  282.         public void set(int i, long v) {
  283.             modifyAdd(i, i, -querySum(i, i) + v);
  284.         }
  285.  
  286.     }
  287.  
  288.     static class InputReader {
  289.         private InputStream stream;
  290.         private byte[] buffer = new byte[10000];
  291.         private int cur;
  292.         private int count;
  293.  
  294.         public InputReader(InputStream stream) {
  295.             this.stream = stream;
  296.         }
  297.  
  298.         public static boolean isSpace(int c) {
  299.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  300.         }
  301.  
  302.         public int read() {
  303.             if (count == -1) {
  304.                 throw new InputMismatchException();
  305.             }
  306.             try {
  307.                 if (cur >= count) {
  308.                     cur = 0;
  309.                     count = stream.read(buffer);
  310.                     if (count <= 0)
  311.                         return -1;
  312.                 }
  313.             } catch (IOException e) {
  314.                 throw new InputMismatchException();
  315.             }
  316.             return buffer[cur++];
  317.         }
  318.  
  319.         public int readSkipSpace() {
  320.             int c;
  321.             do {
  322.                 c = read();
  323.             } while (isSpace(c));
  324.             return c;
  325.         }
  326.  
  327.         public int nextInt() {
  328.             int sgn = 1;
  329.             int c = readSkipSpace();
  330.             if (c == '-') {
  331.                 sgn = -1;
  332.                 c = read();
  333.             }
  334.             int res = 0;
  335.             do {
  336.                 if (c < '0' || c > '9') {
  337.                     throw new InputMismatchException();
  338.                 }
  339.                 res = res * 10 + c - '0';
  340.                 c = read();
  341.             } while (!isSpace(c));
  342.             res *= sgn;
  343.             return res;
  344.         }
  345.  
  346.     }
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement