题意:给一串括号,有2个操作,1。翻转某个括号。2。查询某段区间内未匹配的括号(或者说跟他匹配的在区间外面),第k个未匹配的括号是第几个。
做法:我们可以把一段区间的括号匹配情况总结为)(,即左边的右括号和右边的左括号,其余的自然都已经匹配了,这样就可以利用线段树去合并。对于操作1单点更新即可。操作2就麻烦很多了。我YY了一个做法,把这个区间有关的点(也就是线段树内区间查询的点)给拿出来,重新建一颗小的线段树(因为点数不超过2*logn个),然后从顶往下查询第k个的是哪个区间第几个(慢慢分类讨论吧。。)。到叶节点就可以知道是哪个区间的第几个是答案,然后再到原树上找,同样的方法,到叶节点了,叶节点的编号即是答案,我在拿出这些区间的时候记录了在原树的标号,所以后面2个操作都可以写成非递归的,可以减少点常数。
AC代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll long long
#define ull unsigned long long
#define eps 1e-8
#define NMAX 1000000000
#define MOD 1000000
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
#define mp make_pair
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
template<class T> inline T Max(T a, T b){ return a > b ? a : b; }
template<class T> inline T Min(T a, T b){ return a < b ? a : b; }
const int maxn = 200000+10;
struct SegTree
{
int x,y;
int flag;
}T[maxn<<2];
char s[maxn];
void pushup(int rt)
{
T[rt].x = T[rt<<1].x;
T[rt].y = T[rt<<1|1].y;
if(T[rt<<1].y > T[rt<<1|1].x) T[rt].y += T[rt<<1].y-T[rt<<1|1].x;
else T[rt].x += T[rt<<1|1].x-T[rt<<1].y;
}
void build(int l, int r, int rt)
{
T[rt].x = T[rt].y = T[rt].flag = 0;
if(l == r)
{
T[rt].flag = l;
if(s[l] == ')') T[rt].x = 1;
else T[rt].y = 1;
return;
}
int mid = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int L, int l, int r, int rt)
{
if(l == r)
{
T[rt].x ^= 1;
T[rt].y ^= 1;
return;
}
int mid = (l+r)>>1;
if(L <= mid) update(L,lson);
else update(L,rson);
pushup(rt);
}
struct node
{
int x,y,rt;
node(){}
node(int _x, int _y, int _rt):x(_x),y(_y),rt(_rt){}
}no[45];
int nct;
void get(int L, int R, int l, int r, int rt)
{
if(L <= l && R >= r)
{
no[++nct] = node(T[rt].x,T[rt].y,rt);
return;
}
int mid = (l+r)>>1;
if(L <= mid) get(L,R,lson);
if(R > mid) get(L,R,rson);
}
node tree[40<<2];
void pushupsmall(int rt)
{
tree[rt].x = tree[rt<<1].x; tree[rt].y = tree[rt<<1|1].y;
if(tree[rt<<1].y > tree[rt<<1|1].x) tree[rt].y += tree[rt<<1].y-tree[rt<<1|1].x;
else tree[rt].x += tree[rt<<1|1].x-tree[rt<<1].y;
}
void buildsmall(int l, int r, int rt)
{
tree[rt].rt = 0;
if(l == r)
{
tree[rt] = no[l];
return;
}
int mid = (l+r)>>1;
buildsmall(lson);
buildsmall(rson);
pushupsmall(rt);
}
pair<int,int> getans(int ge)
{
int pos = 1;
while(tree[pos].rt == 0)
{
if(ge <= tree[pos].x)
{
if(ge <= tree[pos<<1].x) pos = pos<<1;
else
{
ge = ge-tree[pos<<1].x+tree[pos<<1].y;
pos = pos<<1|1;
}
}
else
{
ge -= tree[pos].x;
if(tree[pos<<1].y > tree[pos<<1|1].x)
{
if(ge <= tree[pos<<1].y-tree[pos<<1|1].x)
{
ge += tree[pos<<1].x;
pos = pos<<1;
}
else
{
ge -= tree[pos<<1].y-tree[pos<<1|1].x;
ge += tree[pos<<1|1].x;
pos = pos<<1|1;
}
}
else
{
ge += tree[pos<<1|1].x;
pos = pos<<1|1;
}
}
}
return mp(ge,tree[pos].rt);
}
int solve(int ge, int pos)
{
while(T[pos].flag == 0)
{
if(ge <= T[pos].x)
{
if(ge <= T[pos<<1].x) pos = pos<<1;
else
{
ge = ge-T[pos<<1].x+T[pos<<1].y;
pos = pos<<1|1;
}
}
else
{
ge -= T[pos].x;
if(T[pos<<1].y > T[pos<<1|1].x)
{
if(ge <= T[pos<<1].y-T[pos<<1|1].x)
{
ge += T[pos<<1].x;
pos = pos<<1;
}
else
{
ge -= T[pos<<1].y-T[pos<<1|1].x;
ge += T[pos<<1|1].x;
pos = pos<<1|1;
}
}
else
{
ge += T[pos<<1|1].x;
pos = pos<<1|1;
}
}
}
return T[pos].flag;
}
int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
// freopen("o.txt","w",stdout);
#endif
int t;
scanf("%d",&t);
while(t--)
{
int n,q;
scanf("%d%d",&n,&q);
scanf("%s",s+1);
build(1,n,1);
// cout<<T[1].x<<" "<<T[1].y<<endl;
while(q--)
{
int ha,x,l,r,k;
scanf("%d",&ha);
if(ha == 1)
{
scanf("%d",&x);
update(x,1,n,1);
}
else
{
scanf("%d%d%d",&l,&r,&k);
nct = 0;
get(l,r,1,n,1);
buildsmall(1,nct,1);
if(tree[1].x+tree[1].y < k)
{
printf("-1\n");
continue;
}
pair<int,int> tmp = getans(k);
printf("%d\n",solve(tmp.first,tmp.second));
}
}
}
return 0;
}