Facebook
From Diminutive Mousedeer, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 144
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define MAX (int)1e6+2
  4. struct Node{
  5.         int op;
  6.         int open;
  7.         int close;
  8. };
  9. Node st[MAX*4];
  10. string s;
  11. Node operator + (Node l, Node r){
  12.         Node res;
  13.         int tmp = min(l.open,r.close);
  14.         res.op = l.op + r.op + tmp;
  15.         res.open = l.open + r.open - tmp;
  16.         res.close = l.close + r.close - tmp;
  17.         return res;
  18. }
  19. void build(int id, int l,int r){
  20.         if(l==r){
  21.                 if(s[l]=='(') st[id]={0,1,0};
  22.                 else st[id]={0,0,1};
  23.                 return;
  24.         }
  25.         int m=(l+r)/2;
  26.         build(id*2,l,m);
  27.         build(id*2+1,m+1,r);
  28.         st[id]=st[id*2]+st[id*2+1] ;
  29. }
  30. Node query(int id,int l,int r,int u,int v){
  31.         if(v<l || r>u){
  32.                 Node x={0,0,0};
  33.                 return x;
  34.         }      
  35.         if(u<=l && r<=v) return st[id];
  36.         int mid = (l+r)/2;
  37.         Node x = query(id*2,l,mid,u,v)+query(id*2+1,mid+1,r,u,v);
  38.         return x;
  39. }
  40. int main(){
  41.         ios::sync_with_stdio(0);
  42.         cin.tie(0);
  43.         string s1;
  44.         cin>>s1;
  45.         s="0"+s1;
  46.         int t;
  47.         cin>>t;
  48.         int n = s1.size();
  49.         build(1,1,n);
  50.         for(int i=1;i<=29;i++){
  51.                 cout<<i<<' '<<st[i].op<<'\t';
  52.         }
  53.        
  54.         while(t--){
  55.                 int l,r;
  56.                 cin>>l>>r;
  57.                 Node x = query(1,1,n,l,r);
  58.                 cout<<x.op<<endl;
  59.         }
  60. }