[toc]
Codeforces Round 603
ABCD
都是很水的题。 cout << 000输出0,而cout<<"000"才输出000,因为这个WA6次要被笑(打)死。
E
判定括号序列合法性:每一个前缀和不小于0,同时最后一个前缀和等于0.线段树更新即可.新技能Get.
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
int minz[maxn*4],tg[maxn*4],maxz[maxn*4],tg2[maxn*4];
char s[maxn];
inline int ls(int x){
return x<<1;
}
inline int rs(int x){
return x<<1|1;
}
void pushdown(int rt){
if(tg[rt]==0) return;
minz[ls(rt)]+=tg[rt];
tg[ls(rt)]+=tg[rt];
minz[rs(rt)]+=tg[rt];
tg[rs(rt)]+=tg[rt];
tg[rt]=0;
}
void pushdown2(int rt){
if(tg2[rt]==0) return;
maxz[ls(rt)]+=tg2[rt];
tg2[ls(rt)]+=tg2[rt];
maxz[rs(rt)]+=tg2[rt];
tg2[rs(rt)]+=tg2[rt];
tg2[rt]=0;
}
int upd(int il,int ir,int d,int rt,int l,int r){
if(il>r||ir<l) return minz[rt];
if(l>=il&&r<=ir){
minz[rt]+=d;
tg[rt]+=d;
return minz[rt];
}
pushdown(rt);
int mid=(l+r)>>1;
return minz[rt]=min(upd(il,ir,d,ls(rt),l,mid),upd(il,ir,d,rs(rt),mid+1,r));
}
int ask(int il,int ir,int rt,int l,int r){
if(il>r||ir<l) return 0x3f3f3f3f;
if(l>=il&&r<=ir) return minz[rt];
pushdown(rt);
int mid=(l+r)>>1;
return min(ask(il,ir,ls(rt),l,mid),ask(il,ir,rs(rt),mid+1,r));
}
int upd2(int il,int ir,int d,int rt,int l,int r){
if(il>r||ir<l) return maxz[rt];
if(l>=il&&r<=ir){
maxz[rt]+=d;
tg2[rt]+=d;
return maxz[rt];
}
pushdown2(rt);
int mid=(l+r)>>1;
return maxz[rt]=max(upd2(il,ir,d,ls(rt),l,mid),upd2(il,ir,d,rs(rt),mid+1,r));
}
int ask2(int il,int ir,int rt,int l,int r){
if(il>r||ir<l) return 0;
if(l>=il&&r<=ir) return maxz[rt];
pushdown2(rt);
int mid=(l+r)>>1;
return max(ask2(il,ir,ls(rt),l,mid),ask2(il,ir,rs(rt),mid+1,r));
}
int st[maxn];
int main(){
int n;
scanf("%d",&n);
scanf("%s",s+1);
int cur=1;
for(int i=1;i<=n;i++){
if(s[i]==‘L‘) cur=max(1,cur-1);
else if(s[i]==‘R‘) {
cur++;
}
else if(s[i]==‘(‘) {
if(st[cur]==1) ;
else if(st[cur]==0){
st[cur]=1;
upd(cur,n,1,1,1,n);
upd2(cur,n,1,1,1,n);
}
else {
st[cur]=1;
upd(cur,n,2,1,1,n);
upd2(cur,n,2,1,1,n);
}
}
else if(s[i]==‘)‘){
if(st[cur]==-1);
else if(st[cur]==0){
st[cur]=-1;
upd(cur,n,-1,1,1,n);
upd2(cur,n,-1,1,1,n);
}
else {
st[cur]=-1;
upd(cur,n,-2,1,1,n);
upd2(cur,n,-2,1,1,n);
}
}
else{
if(st[cur]==0) ;
else if(st[cur]==1){
st[cur]=0;
upd(cur,n,-1,1,1,n);
upd2(cur,n,-1,1,1,n);
}
else {
st[cur]=0;
upd(cur,n,1,1,1,n);
upd2(cur,n,1,1,1,n);
}
}
if(ask(1,n,1,1,n)>=0&&ask(n,n,1,1,n)==0){
printf("%d ", ask2(1,n,1,1,n));
}
else printf("-1 ");
}
return 0;
}
原文地址:https://www.cnblogs.com/vege-chicken-rainstar/p/11980524.html
时间: 2024-10-15 23:47:54