Guest User

TEMPLEQ

a guest
May 5th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.47 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. import static java.lang.Integer.MAX_VALUE;
  8. import static java.lang.Integer.max;
  9.  
  10. class TEMPLEQ {
  11.  
  12.     private static int tree[] = new int[400005];
  13.     private static int lazy[] = new int[400005];
  14.     static Pair a[] = new Pair[100005];
  15.     private static final int UNDEFINED = MAX_VALUE;
  16.     private static Map<Integer, Integer> realIndices = new HashMap<>();
  17.  
  18.     public static void main(String[] args) {
  19.         for (int i = 0; i < 100005; i++) {
  20.             a[i] = new Pair();
  21.         }
  22.         FastReader fr = FastReader.SYSTEM_READER;
  23.         int n, q;
  24.         n = fr.nextInt();
  25.         q = fr.nextInt();
  26.         for (int i = 0; i < n; i++) {
  27.             a[i].init(i, fr.nextInt());
  28.         }
  29.         Arrays.sort(a, 0, n, Comparator.comparingInt(o -> o.value));
  30.         buildTree(a,1, 0, n-1);
  31.         for (int i = 0; i < n; i++) {
  32.             realIndices.put(a[i].idx, i);
  33.         }
  34.         for (int i = 0; i < q; i++) {
  35.             int type = fr.nextInt();
  36.             int value = fr.nextInt();
  37.             answerQuery(type, value, n);
  38.         }
  39.     }
  40.     private static void buildTree(Pair[] a, int id, int start, int end) {
  41.         if (start == end) {
  42.             tree[id] = a[start].value;
  43.             return;
  44.         }
  45.         int mid = mid(start, end);
  46.         int left = id << 1;
  47.         int right = (id << 1) + 1;
  48.         buildTree(a, left, start, mid);
  49.         buildTree(a, right, mid + 1, end);
  50.         tree[id] = max(tree[left], tree[right]);
  51.     }
  52.     private static int mid(int start, int end) { return (start + end) / 2; }
  53.     private static void updateNode(int id, int l, int r, int x) {
  54.         lazy[id] += x;
  55.         tree[id] += (r - l + 1) * x;
  56.     }
  57.     private static void shift(int id, int start, int end) {
  58.         int mid = mid(start, end);
  59.         updateNode(id << 1, start, mid, lazy[id]);
  60.         updateNode((id << 1) + 1, mid+1, end, lazy[id]);
  61.         lazy[id] = 0;
  62.     }
  63.     private static void rangeUpdate(int id, int start, int end, int l, int r, int x) {
  64.         if (start > r || end < l) {
  65.             return;
  66.         }
  67.         if (start >= l && end <= r) {
  68.             updateNode(id, start, end, x);
  69.             return;
  70.         }
  71.         shift(id, start, end);
  72.         int mid = mid(start, end);
  73.         int left = id << 1;
  74.         int right = (id << 1) + 1;
  75.         rangeUpdate(left, start, mid, l, r, x);
  76.         rangeUpdate(right, mid + 1, end, l, r, x);
  77.         tree[id] = max(tree[left], tree[right]);
  78.     }
  79.  
  80.     //this function returns smallest index that has value >=x
  81.     private static int smallestIndex(int id, int start, int end, int x) {
  82.         if (start == end) {
  83.             if (tree[id] >= x) {
  84.                 return start;
  85.             }
  86.             return UNDEFINED;
  87.         }
  88.         shift(id, start, end);
  89.         if (tree[id]<x) {
  90.             return UNDEFINED;
  91.         }
  92.         int mid = mid(start, end);
  93.         int temp = smallestIndex(id << 1, start, mid, x);
  94.         if (temp != UNDEFINED) {
  95.             return temp;
  96.         }
  97.         temp = smallestIndex((id << 1) + 1, mid + 1, end, x);
  98.         return temp;
  99.     }
  100.     private static void answerQuery(int type, int value, int n) {
  101.         if (type == 1) {
  102.             int idx = realIndices.get(value - 1);
  103.             rangeUpdate(1, 0, n - 1, idx, idx, 1);
  104.         } else if (type == 2) {
  105.             int idx = smallestIndex(1, 0, n - 1, value);
  106.             if (idx != UNDEFINED) {
  107.                 System.out.println(n - idx);
  108.                 return;
  109.             }
  110.             System.out.println(0);
  111.         } else {
  112.             int idx = smallestIndex(1, 0, n - 1, value);
  113.             if (idx != UNDEFINED) {
  114.                 rangeUpdate(1, 0, n - 1, idx, n - 1, -1);
  115.             }
  116.         }
  117.     }
  118.  
  119.  
  120.     static class Pair{
  121.         int idx;
  122.         int value;
  123.         void init(int idx, int value) {
  124.             this.idx = idx;
  125.             this.value = value;
  126.         }
  127.     }
  128.     static final class FastReader {
  129.         public static final FastReader SYSTEM_READER = new FastReader(System.in);
  130.         private final InputStream in;
  131.         private final byte[] buffer = new byte[1 << 16];
  132.         private int pos, count;
  133.  
  134.         public FastReader(InputStream in) {
  135.             this.in = in;
  136.             pos = count = 0;
  137.         }
  138.  
  139.         public int nextInt() {
  140.             int c;
  141.             while ((c = read()) < '0');
  142.             int result = c - '0';
  143.             while ((c = read() - '0') >= 0)
  144.                 result = 10 * result + c;
  145.             return result;
  146.         }
  147.         public long nextLong() {
  148.             int c;
  149.             while ((c = read()) < '0');
  150.             long result = c - '0';
  151.             while ((c = read() - '0') >= 0)
  152.                 result = 10 * result + c;
  153.             return result;
  154.         }
  155.  
  156.         public String nextString() {
  157.             StringBuilder s = new StringBuilder();
  158.             int c;
  159.             while ((c = read()) >= 33)
  160.                 s.append((char) c);
  161.             return s.toString();
  162.         }
  163.  
  164.         private void fillBuffer() {
  165.             try {
  166.                 count = in.read(buffer, pos = 0, buffer.length);
  167.             } catch (Exception e) {
  168.             }
  169.         }
  170.  
  171.         public int read() {
  172.             if (pos == count)
  173.                 fillBuffer();
  174.             return buffer[pos++];
  175.         }
  176.     }
  177.  
  178. }
Add Comment
Please, Sign In to add comment