P6864题解
北文
·
·
题解
题面传送门
今天模拟赛出了此题,感觉和大家做法不太一样(不是矩阵玩家),写个题解说一下。
题目分析:
首先考虑没有撤销怎么做。
每次新加入一对括号只要考虑这对括号与原串的影响。
记当前串为 S,当前答案为 now。
对于当前字符串把其分成多组互不包含的括号的形式
形如这样:
(.....)(...)(...)
设有 p 段。
操作 1 :在 S 后加一对括号,那么新加的这个串贡献为 p+1,因为它可以和前面的每一个括号匹配,再加上它本身。然后令 p\leftarrow p+1。
操作 2 :把 S 用一个括号包起来,那么贡献为 1,再令 p=1。
我们发现对于任意两个操作 2 之间的操作 1 ,他们的贡献总和是 \frac{p\times (p+1)}{2} (其实是等差数列求和),而一个 2 操作相当于一个分隔符。
假设操作序列为:1 1 1 2 1 1 2。
$$now=\frac{4\times (4+1)}{2}+\frac{3\times (3+1)}{2}+\frac{1\times (1+1)}{2}=17$$
这样我们只要知道每段的 $p$ 就能求出 $now$ 了。
考虑撤销操作。
如果撤销了 1 操作,相当于令某一个 $p\leftarrow p-1$。
如果撤销了 2 操作,相当于合并两个 $p1, p2$,令新 $p\leftarrow p1+p2-1$。
撤销(撤销 1 操作),相当于令某一个 $p\leftarrow p+1$。
撤销(撤销 2 操作),相等于使一个 $p$ 分裂成 $p1,p2$,我们只需要记下之前合并时的分界点即可。
剩下合并的事情,蒟蒻用了线段树维护,真是太难看了
## Sol:
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
using ll=long long;
struct Seg{
int t[N<<2], tag[N<<2];
void build(int u, int l, int r) {
if(l==r) return t[u]=l, void();
int mid=l+r>>1;
build(u<<1, l, mid);
build(u<<1|1, mid+1, r);
}
void pushdown(int u) {
if(tag[u]) {
t[u<<1]=tag[u];
t[u<<1|1]=tag[u];
tag[u<<1]=tag[u];
tag[u<<1|1]=tag[u];
tag[u]=0;
return ;
}
}
void update(int u, int l, int r, int L, int R, int v) {
if(L<=l&&r<=R) {
t[u]=v;
tag[u]=v;
return ;
}
int mid=l+r>>1;
pushdown(u);
if(L<=mid) update(u<<1, l, mid, L, R, v);
if(R>mid) update(u<<1|1, mid+1, r, L, R, v);
}
int query(int u, int l, int r, int x) {
if(l==r) return t[u];
int mid=l+r>>1;
pushdown(u);
if(x<=mid) return query(u<<1, l, mid, x);
else return query(u<<1|1, mid+1, r, x);
}
}L, R;
int n, top[N], tx[N], val[N];
int t[N];
void update(int x, int v) {
for(; x<=n; x+=x&-x)
t[x]+=v;
}
int query(int x) {
int ret=0;
for(; x; x-=x&-x)
ret+=t[x];
return ret;
}
ll calc(int x) {
return 1ll*(x+1)*x/2;
}
int pos[N], hmid[N];
int main() {
// freopen("dydy.in", "r", stdin);
// freopen("dydy.out","w",stdout);
scanf("%d", &n);
L.build(1, 1, n); R.build(1, 1, n);
int now=1; ll ans=1; update(1, 1);
for(int i=1, op, x; i<=n; i++) {
scanf("%d", &op); if(op==3) scanf("%d", &x);
if(op==1) {
int l=L.query(1, 1, n, now), r=R.query(1, 1, n, now);
ans-=calc(query(r)-query(l-1));
update(now, 1);
ans+=calc(query(r)-query(l-1));
top[i]=1; tx[i]=i; val[i]=-1;
} else if(op==2) {
ans++; now++; update(now, 1);
top[i]=2; tx[i]=i; val[i]=-1;
} else if(op==3) {
if(top[x]==1) {
int l=L.query(1, 1, n, pos[tx[x]]), r=R.query(1, 1, n, pos[tx[x]]);
ans-=calc(query(r)-query(l-1));
update(pos[tx[x]], val[x]);
ans+=calc(query(r)-query(l-1));
top[i]=1; tx[i]=tx[x]; val[i]=-val[x];
} else if(top[x]==2) {
if(val[x]==-1) {
int l1=L.query(1, 1, n, pos[tx[x]]-1), r1=R.query(1, 1, n, pos[tx[x]]-1);
int l2=L.query(1, 1, n, pos[tx[x]]), r2=R.query(1, 1, n, pos[tx[x]]);
ans-=calc(query(r1)-query(l1-1));
ans-=calc(query(r2)-query(l2-1));
update(pos[tx[x]], -1);
R.update(1, 1, n, l1, r1, r2);
L.update(1, 1, n, l2, r2, l1);
ans+=calc(query(r2)-query(l1-1));
top[i]=2; tx[i]=tx[x]; val[i]=1; hmid[tx[x]]=r1;
} else {
int l1=L.query(1, 1, n, pos[tx[x]]-1), r1=R.query(1, 1, n, pos[tx[x]]-1);
ans-=calc(query(r1)-query(l1-1));
R.update(1, 1, n, l1, hmid[tx[x]], hmid[tx[x]]);
L.update(1, 1, n, hmid[tx[x]]+1, r1, hmid[tx[x]]+1);
update(pos[tx[x]], 1);
ans+=calc(query(hmid[tx[x]])-query(l1-1));
ans+=calc(query(r1)-query(hmid[tx[x]]));
top[i]=2; tx[i]=tx[x]; val[i]=-1;
}
}
}
pos[i]=now; printf("%lld\n", ans);
}
return 0;
}
```