QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#297970 | #7973. 括号 | zhouhuanyi | 0 | 2ms | 3812kb | C++14 | 4.4kb | 2024-01-05 15:07:50 | 2024-01-05 15:07:51 |
answer
#include<iostream>
#include<cstdio>
#include<vector>
#define N 200000
using namespace std;
const int inf=(int)(1e9);
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
int n,q,a[N+1],b[N+1];
long long res;
char c[N+1],cs[N+1];
struct reads
{
int num,data;
};
reads operator + (reads a,reads b)
{
return a.data<=b.data?a:b;
}
struct seg
{
struct node
{
int l,r,sminn,lazy;
reads minn,minn2;
};
node tree[(N<<2)+1];
void push_up(int k)
{
tree[k].sminn=min(tree[k<<1].sminn,tree[k<<1|1].sminn),tree[k].minn=tree[k<<1].minn+tree[k<<1|1].minn,tree[k].minn2=tree[k<<1].minn2+tree[k<<1|1].minn2;
return;
}
void spread(int k)
{
if (tree[k].lazy)
{
tree[k<<1].sminn+=tree[k].lazy,tree[k<<1].lazy+=tree[k].lazy;
tree[k<<1|1].sminn+=tree[k].lazy,tree[k<<1|1].lazy+=tree[k].lazy;
tree[k].lazy=0;
}
return;
}
void build(int k,int l,int r)
{
tree[k].l=l,tree[k].r=r;
if (l==r)
{
if (cs[l]=='(') tree[k].minn=(reads){l,0},tree[k].minn2=(reads){l,inf};
else tree[k].minn=(reads){l,inf},tree[k].minn2=(reads){l,0};
return;
}
int mid=(l+r)>>1;
build(k<<1,l,mid),build(k<<1|1,mid+1,r),push_up(k);
return;
}
void add(int k,int l,int r,int d)
{
if (tree[k].l==l&&tree[k].r==r)
{
tree[k].sminn+=d,tree[k].lazy+=d;
return;
}
spread(k);
int mid=(tree[k].l+tree[k].r)>>1;
if (r<=mid) add(k<<1,l,r,d);
else if (l>=mid+1) add(k<<1|1,l,r,d);
else add(k<<1,l,mid,d),add(k<<1|1,mid+1,r,d);
push_up(k);
return;
}
void change(int k,int x,int d,int d2)
{
if (tree[k].l==tree[k].r)
{
tree[k].minn.data=d,tree[k].minn2.data=d2;
return;
}
spread(k);
int mid=(tree[k].l+tree[k].r)>>1;
if (x<=mid) change(k<<1,x,d,d2);
else change(k<<1|1,x,d,d2);
push_up(k);
return;
}
int find(int k,int x)
{
if (tree[k].l==tree[k].r) return tree[k].sminn>0?x:x-1;
spread(k);
int mid=(tree[k].l+tree[k].r)>>1;
if (x<=mid)
{
if (tree[k].l==x)
{
if (tree[k<<1].sminn>0) return find(k<<1|1,mid+1);
else return find(k<<1,x);
}
else
{
int ps=find(k<<1,x);
if (ps==mid) return find(k<<1|1,mid+1);
else return ps;
}
}
else return find(k<<1|1,x);
}
int find2(int k,int x)
{
if (tree[k].l==tree[k].r) return tree[k].sminn>0?x:x+1;
spread(k);
int mid=(tree[k].l+tree[k].r)>>1;
if (x<=mid) return find2(k<<1,x);
else
{
if (tree[k].r==x)
{
if (tree[k<<1|1].sminn>0) return find2(k<<1,mid);
else return find2(k<<1|1,x);
}
else
{
int ps=find2(k<<1|1,x);
if (ps==mid+1) return find2(k<<1,mid);
else return ps;
}
}
}
reads query(int k,int l,int r)
{
if (l>r) return (reads){0,inf};
if (tree[k].l==l&&tree[k].r==r) return tree[k].minn;
spread(k);
int mid=(tree[k].l+tree[k].r)>>1;
if (r<=mid) return query(k<<1,l,r);
else if (l>=mid+1) return query(k<<1|1,l,r);
else return query(k<<1,l,mid)+query(k<<1|1,mid+1,r);
}
reads query2(int k,int l,int r)
{
if (l>r) return (reads){0,inf};
if (tree[k].l==l&&tree[k].r==r) return tree[k].minn2;
spread(k);
int mid=(tree[k].l+tree[k].r)>>1;
if (r<=mid) return query2(k<<1,l,r);
else if (l>=mid+1) return query2(k<<1|1,l,r);
else return query2(k<<1,l,mid)+query2(k<<1|1,mid+1,r);
}
};
seg T;
void change(int x,int d)
{
reads ds;
int rt,ps,rst;
if (c[x]!=cs[x]) res+=d-a[x];
a[x]=d;
if (c[x]==cs[x]) rst=a[x];
else rst=-a[x];
if (cs[x]=='(') ps=T.find(1,x),ds=T.query2(1,1,ps),rt=ds.num,rst+=ds.data;
else ps=T.find2(1,x),ds=T.query(1,ps,n<<1),rt=ds.num,rst+=ds.data;
if (rst<0)
{
res+=rst;
if (cs[x]=='(') cs[x]=')',T.change(1,x,inf,(c[x]==')')?a[x]:-a[x]),cs[rt]='(',T.change(1,rt,(c[rt]=='(')?a[rt]:-a[rt],inf);
else cs[x]='(',T.change(1,x,(c[x]=='(')?a[x]:-a[x],inf),cs[rt]=')',T.change(1,rt,inf,(c[rt]==')')?a[rt]:-a[rt]);
}
else
{
if (cs[x]=='(') T.change(1,x,(c[x]=='(')?a[x]:-a[x],inf);
else T.change(1,x,inf,(c[x]==')')?a[x]:-a[x]);
}
return;
}
int main()
{
int x,d;
n=read(),q=read(),T.build(1,1,n<<1);
for (int i=1;i<=(n<<1);++i) b[i]=read();
for (int i=1;i<=(n<<1);++i) cin>>c[i],cs[i]=(i&1)?'(':')';
for (int i=1;i<=(n<<1);++i) change(i,b[i]);
printf("%lld\n",res);
for (int i=1;i<=q;++i) x=read(),d=read(),change(x,d),printf("%lld\n",res);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3612kb
input:
100 100 655884441 790777510 663667368 332762945 67681448 458058488 445481314 200508190 812326927 374891900 320371513 765529851 490260632 588113266 286392696 888016940 214376080 894477437 944447014 386015667 956960774 692332579 606560669 561835357 887377361 130572961 550186106 193341110 4130416 66982...
output:
1883520337 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 1938040724 193...
result:
wrong answer 46th lines differ - expected: '1405045389', found: '1488494894'
Subtask #2:
score: 0
Wrong Answer
Test #5:
score: 0
Wrong Answer
time: 2ms
memory: 3792kb
input:
1000 1000 851064227 277152131 421722407 126468670 510326499 619107836 287335428 653386549 173788833 304176934 21753544 293653999 493165671 887566717 813114839 976556173 459946448 939807420 605205411 920860669 545229689 895277168 777349694 126341157 564711820 892644312 314220085 125767094 816813109 9...
output:
2793453944 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2784207960 2810215614 2810215614 2810215614 2810215614 281...
result:
wrong answer 24th lines differ - expected: '2800727160', found: '2810215614'
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Wrong Answer
Test #14:
score: 0
Wrong Answer
time: 2ms
memory: 3812kb
input:
1000 1000 49658 21707 94558 56676 18487 74906 55206 78654 54538 14591 105694 138 3148 106151 90191 67461 90337 86524 39272 78899 111590 3181 67245 47146 1958 34378 6544 74125 93643 44483 2159 16309 41619 24332 1519 85340 25811 55827 51528 89913 71355 103446 97370 44299 107887 105014 44419 62592 1965...
output:
13395019 13351991 13351991 13351991 13351991 13351991 13351991 13351991 13351991 13381782 13345375 13349993 13338925 13322739 13322739 13322739 13322739 13322739 13307553 13291583 13291583 13291583 13292309 13288314 13309630 13294405 13325327 13325327 13368731 13368731 13368731 13368731 13399800 133...
result:
wrong answer 12th lines differ - expected: '13349826', found: '13349993'
Subtask #5:
score: 0
Skipped
Dependency #3:
0%
Subtask #6:
score: 0
Skipped
Dependency #5:
0%