【CF】223 Div.1 C Sereja and Brackets

水线段树。

  1 /* 380C */
  2 #include <iostream>
  3 #include <string>
  4 #include <map>
  5 #include <queue>
  6 #include <set>
  7 #include <stack>
  8 #include <vector>
  9 #include <deque>
 10 #include <algorithm>
 11 #include <cstdio>
 12 #include <cmath>
 13 #include <ctime>
 14 #include <cstring>
 15 #include <climits>
 16 #include <cctype>
 17 #include <cassert>
 18 #include <functional>
 19 #include <iterator>
 20 #include <iomanip>
 21 using namespace std;
 22 //#pragma comment(linker,"/STACK:102400000,1024000")
 23 
 24 #define sti                set<int>
 25 #define stpii            set<pair<int, int> >
 26 #define mpii            map<int,int>
 27 #define vi                vector<int>
 28 #define pii                pair<int,int>
 29 #define vpii            vector<pair<int,int> >
 30 #define rep(i, a, n)     for (int i=a;i<n;++i)
 31 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 32 #define clr                clear
 33 #define pb                 push_back
 34 #define mp                 make_pair
 35 #define fir                first
 36 #define sec                second
 37 #define all(x)             (x).begin(),(x).end()
 38 #define SZ(x)             ((int)(x).size())
 39 #define lson            l, mid, rt<<1
 40 #define rson            mid+1, r, rt<<1|1
 41 
 42 const int maxn = 1e6+5;
 43 char s[maxn];
 44 int ln[maxn<<2];
 45 int rn[maxn<<2];
 46 int L, R;
 47 
 48 inline void PushUp(int rt) {
 49     int lb = rt<<1;
 50     int rb = (rt<<1)|1;
 51     int tmp = min(ln[lb], rn[rb]);
 52     
 53     ln[rt] = ln[lb] + ln[rb] - tmp;
 54     rn[rt] = rn[lb] + rn[rb] - tmp;
 55 }
 56 
 57 void build(int l, int r, int rt) {
 58     if (l == r) {
 59         if (s[l] == '(')
 60             ln[rt] = 1;
 61         else
 62             rn[rt] = 1;
 63         return ;
 64     }
 65     
 66     int mid = (l+r)>>1;
 67     build(lson);
 68     build(rson);
 69     PushUp(rt);
 70 }
 71 
 72 pii query(int l, int r, int rt) {
 73     if (L<=l && R>=r) {
 74         return mp(ln[rt], rn[rt]);
 75     }
 76     int mid = (l+r)>>1;
 77     if (mid < L) {
 78         return query(rson);
 79     } else if (mid >= R) {
 80         return query(lson);
 81     } else {
 82         pii p1 = query(lson);
 83         pii p2 = query(rson);
 84         pii ret;
 85         int tmp = min(p1.fir, p2.sec);
 86         ret.fir = p1.fir + p2.fir - tmp;
 87         ret.sec = p1.sec + p2.sec - tmp;
 88         return ret;
 89     }
 90 }
 91 
 92 int main() {
 93     ios::sync_with_stdio(false);
 94     #ifndef ONLINE_JUDGE
 95         freopen("data.in", "r", stdin);
 96         freopen("data.out", "w", stdout);
 97     #endif
 98     
 99     scanf("%s", s+1);
100     
101     int n = strlen(s+1), m;
102     pii p;
103     int ans;
104     
105     build(1, n, 1);
106     scanf("%d", &m);
107     while (m--) {
108         scanf("%d %d", &L, &R);
109         p = query(1, n, 1);
110         ans = R-L+1-p.fir-p.sec;
111         printf("%d\n", ans);
112     }
113     
114     #ifndef ONLINE_JUDGE
115         printf("time = %d.\n", (int)clock());
116     #endif
117     
118     return 0;
119 }

 

posted on 2015-07-09 15:11  Bombe  阅读(277)  评论(0编辑  收藏  举报

导航