两种姿势
之前我写的是第一种
树状数组进行离线查询
按照长度从小到大排序,每次将小于查询的匹配插入树状数组
// whn6325689 // Mr.Phoebe // http://blog.csdn.net/u013007900 #include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <climits> #include <complex> #include <fstream> #include <cassert> #include <cstdio> #include <bitset> #include <vector> #include <deque> #include <queue> #include <stack> #include <ctime> #include <set> #include <map> #include <cmath> #include <functional> #include <numeric> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef complex<ld> point; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef vector<int> vi; #define CLR(x,y) memset(x,y,sizeof(x)) #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x) #define lowbit(x) (x&(-x)) #define MID(x,y) (x+((y-x)>>1)) #define eps 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LLINF 1LL<<62 template<class T> inline bool read(T &n) { T x = 0, tmp = 1; char c = getchar(); while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar(); if(c == EOF) return false; if(c == '-') c = getchar(), tmp = -1; while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar(); n = x*tmp; return true; } template <class T> inline void write(T n) { if(n < 0) { putchar('-'); n = -n; } int len = 0,data[20]; while(n) { data[len++] = n%10; n /= 10; } if(!len) data[len++] = 0; while(len--) putchar(data[len]+48); } //----------------------------------- const int MAXN=1000010; char str[MAXN]; int q,c1[MAXN],c2[MAXN]; int len; void update(int c[],int i,int v) { for(;i<len+10;i+=lowbit(i)) c[i]+=v; } int getsum(int c[],int i) { int res=0; for(;i;i-=lowbit(i)) res+=c[i]; return res; } struct Node { int l,r,len; int ans,id; }b[MAXN],a[MAXN]; int st[MAXN],top=0; int tot=0; bool cmp1(Node a,Node b) { return a.len<b.len; } bool cmp2(Node a,Node b) { return a.id<b.id; } int main() { scanf("%s",str); len=strlen(str); for(int i=0;i<len;i++) { if(str[i]=='(') { st[top++]=i+1; } else if(top) { a[tot].l=st[--top]; a[tot].r=i+1; a[tot].len=a[tot].r-a[tot].l+1; tot++; } } scanf("%d",&q); for(int i=0;i<q;i++) { scanf("%d %d",&b[i].l,&b[i].r); b[i].len=b[i].r-b[i].l+1; b[i].id=i; } sort(a,a+tot,cmp1); sort(b,b+q,cmp1); for(int i=0,j=0;i<q;i++) { while(j<tot && a[j].len<=b[i].len) { update(c1,a[j].l,1),update(c2,a[j].r,1); j++; } b[i].ans=getsum(c2,b[i].r)-getsum(c1,b[i].l-1); } sort(b,b+q,cmp2); for(int i=0;i<q;i++) printf("%d\n",b[i].ans*2); return 0; }
用线段树在线维护
其实并不需要动态维护,只需要查询就可以了
线段树的数据域非别维护:
改线段内未匹配的左括号,未匹配的右括号,匹配的数目
pushup维护见重载符号+
// whn6325689 // Mr.Phoebe // http://blog.csdn.net/u013007900 #include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <climits> #include <complex> #include <fstream> #include <cassert> #include <cstdio> #include <bitset> #include <vector> #include <deque> #include <queue> #include <stack> #include <ctime> #include <set> #include <map> #include <cmath> #include <functional> #include <numeric> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef complex<ld> point; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef vector<int> vi; #define CLR(x,y) memset(x,y,sizeof(x)) #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x) #define lowbit(x) (x&(-x)) #define MID(x,y) (x+((y-x)>>1)) #define eps 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LLINF 1LL<<62 template<class T> inline bool read(T &n) { T x = 0, tmp = 1; char c = getchar(); while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar(); if(c == EOF) return false; if(c == '-') c = getchar(), tmp = -1; while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar(); n = x*tmp; return true; } template <class T> inline void write(T n) { if(n < 0) { putchar('-'); n = -n; } int len = 0,data[20]; while(n) { data[len++] = n%10; n /= 10; } if(!len) data[len++] = 0; while(len--) putchar(data[len]+48); } //----------------------------------- const int MAXN=1000010; int n, m; char str[MAXN]; struct Tree { int l, r, lv, rv, sum; Tree operator + (const Tree &a) { Tree h; h.sum = sum + a.sum; h.lv = lv + a.lv; // 没有匹配的左括号 h.rv = rv + a.rv; // 没有匹配的右括号 int num1 = min(lv, a.rv); // 左边和右边最少的括号数目即为匹配数 h.sum += num1 * 2; // 匹配数*2,则为匹配的括号个数 h.lv -= num1; // 未匹配就要减去 h.rv -= num1; return h; } } st[MAXN<<2]; Tree build(int l, int r, int id) { if (l == r) { if (str[l] == '(') { st[id].lv = 1; st[id].rv = 0; st[id].sum = 0; } else { st[id].lv = 0; st[id].rv = 1; st[id].sum = 0; } st[id].l = l; st[id].r = r; } else { int mid = (l + r) / 2; st[id] = build(l, mid, id * 2) + build(mid + 1, r, id * 2 + 1); st[id].l = l; st[id].r = r; } return st[id]; } Tree Query(int l, int r, int id) { if (l == st[id].l && r == st[id].r) return st[id]; int mid = (st[id].l + st[id].r) / 2; if (l <= mid && r > mid) return Query(l, mid, 2 * id) + Query(mid + 1, r, 2 * id + 1); else if (l <= mid && r <= mid) return Query(l, r, 2 * id); else return Query(l, r, 2 * id + 1); } int main() { scanf("%s%d", str + 1, &m); n = strlen(str + 1); build(1, n, 1); int l, r; while (m--) { scanf("%d%d", &l, &r); printf("%d\n", Query(l, r, 1).sum); } return 0; }