Guest User

Sereja And Brackets

a guest
May 1st, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. import static java.lang.Integer.min;
  6. import static java.lang.Integer.parseInt;
  7.  
  8. public final class SerejaAndBrackets {
  9.  
  10.     static int tree[] = new int[4000002];
  11.     static int o[] = new int[4000002];
  12.     static int c[] = new int[4000002];
  13.     static char a[] = new char[1000000];
  14.  
  15.     public static void main(String[] args) throws IOException {
  16.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  17.         String s, temp[];
  18.         int m, l, r, n;
  19.         s = br.readLine();
  20.         n = s.length();
  21.         s.getChars(0, n, a, 0);
  22.         buildTree(1, 0, n);
  23.         m = parseInt(br.readLine());
  24.         for (int i = 0; i < m; i++) {
  25.             temp = br.readLine().split("\\s");
  26.             l = parseInt(temp[0]);
  27.             r = parseInt(temp[1]);
  28.             System.out.println(query(1, 0, n, l - 1, r)[0]*2);
  29.         }
  30.  
  31.     }
  32.  
  33.     //return value description -> t[maximumBracket subsequence length, no. of open brackets in range, no. of closed brackets in range]
  34.     //start end represents the range covered by the node id
  35.     //l and r is the range of the query asked
  36.     private static int[] query(int id, int start, int end, int l, int r) {
  37.         if (start >= r || end <= l) {
  38.             return new int[]{0, 0, 0};
  39.         }
  40.         if (start >= l && end <= r) {
  41.             return new int[]{tree[id], o[id], c[id]};
  42.         }
  43.         int mid = (start + end) / 2;
  44.         int leftChild = id << 1, rightChild = (id << 1) + 1;
  45.         int left[] = query(leftChild, start, mid, l, r);
  46.         int right[] = query(rightChild, mid, end, l, r);
  47.         //min of no. of open brackets in left child and closed brackets in right child
  48.         int tmp = min(left[1], right[2]);
  49.         int o = left[1] + right[1] - tmp;
  50.         int c = left[2] + right[2] - tmp;
  51.         int t = left[0] + right[0] + tmp;
  52.         return new int[]{t, o, c};
  53.     }
  54.     private static void buildTree(int id, int l, int r) {
  55.         if (r - l < 2) {
  56.             if (a[l] == '(') {
  57.                 o[id] = 1;
  58.             } else {
  59.                 c[id] = 1;
  60.             }
  61.             return;
  62.         }
  63.         int mid = (l + r) / 2;
  64.         int leftChild = id << 1, rightChild = (id << 1) + 1;
  65.         buildTree(leftChild, l, mid);
  66.         buildTree(rightChild, mid, r);
  67.         int tmp = min(o[id << 1], c[(id << 1) + 1]);
  68.         o[id] = o[leftChild] + o[rightChild] - tmp;
  69.         c[id] = c[leftChild] + c[rightChild] - tmp;
  70.         tree[id] = tree[leftChild] + tree[rightChild] + tmp;
  71.     }
  72. }
Add Comment
Please, Sign In to add comment