QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#110166 | #2560. Streetlights | abcdeffa | TL | 3ms | 8756kb | C++20 | 5.8kb | 2023-05-31 21:54:40 | 2023-05-31 21:54:42 |
Judging History
answer
#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
inline int read()
{
int X=0,w=0;char ch=0;
while(!isdigit(ch))w|=ch=='-',ch=getchar();
while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
namespace IO{
const int sz=1<<23;
char b[sz+5],*t=b,p[15];
inline void flush(){fwrite(b,1,t-b,stdout),t=b;}
inline void pc(char x){*t++=x;if(t-b==sz)flush();}
template<class T>void write(T x,char c='\n'){
if(x==0)pc('0');int t=0;
for(;x;x/=10)p[++t]=x%10+'0';
for(;t;--t)pc(p[t]);pc(c);
}
struct F{~F(){flush();}}f;
}
using IO::write;
#define k1 (k<<1)
#define k2 (k1+1)
#define N 100005
struct cx{
int x,y;
}bj[N];
struct cx1{
int x,y,z;
};
int he[N],la[N],ne[N],tot=0,a[N],n,tr[N<<2],ad[N<<2],ct[N<<2],mn[N<<2],mx[N<<2],mp1[350005];
int sz[N],sn[N],dfn[N],po[N],tp[N],num[N],fa[N],wg[N],id[N],sp[N],mxx[2005],idd[N],b[N],cut=0;
bool bz1[2005][2005],bz2[1005][2005];
map<int,int> mp;
bool bz[N];
vector<int> d1[2005],d2[2005];
void cal()
{
int ct=0;
vector<int> d;
fo(i,1,n)
{
while(!d.empty()&&a[i]>a[d.back()])d.pop_back();
if(!d.empty()&&a[i]==a[d.back()])
{
ct++;
d.pop_back();
}
d.push_back(i);
}
printf("%d\n",ct);
}
void lb(int x,int y)
{
ne[++tot]=he[x];
la[he[x]=tot]=y;
fa[y]=x;
}
void dg(int x)
{
sz[x]=1;sn[x]=0;
for(int ii=he[x];ii;ii=ne[ii])
{
int xx=la[ii];
dg(xx);
sz[x]+=sz[xx];
if(sz[xx]>sz[sn[x]])sn[x]=xx;
}
}
void dfs(int x,int tpp)
{
po[dfn[x]=++tot]=x;
tp[x]=tpp;
if(sn[x])dfs(sn[x],tpp);
for(int ii=he[x];ii;ii=ne[ii])
{
int xx=la[ii];
if(xx!=sn[x])dfs(xx,xx);
}
}
void build(int l,int r,int k)
{
tr[k]=ad[k]=0;
ct[k]=r-l+1;
if(l==r)
{
mx[k]=mn[k]=num[po[l]];
return;
}
int mid=l+r>>1;
build(l,mid,k1);
build(mid+1,r,k2);
mn[k]=min(mn[k1],mn[k2]);
mx[k]=max(mx[k1],mx[k2]);
}
void pushdown(int k)
{
if(ad[k])
{
ad[k1]+=ad[k];
tr[k1]+=ad[k];
ad[k2]+=ad[k];
tr[k2]+=ad[k];
ad[k]=0;
return;
}
}
void modify(int l,int r,int k,int x,int y,int v1,int v2)
{
if(mn[k]>v1)return;
if(l>=x&&r<=y&&mx[k]<=v1)
{
ad[k]+=v2;
tr[k]+=v2;
return;
}
int mid=l+r>>1;
pushdown(k);
if(mid>=x)modify(l,mid,k1,x,y,v1,v2);
if(mid<y)modify(mid+1,r,k2,x,y,v1,v2);
tr[k]=min(tr[k1],tr[k2]);
ct[k]=(tr[k1]==tr[k])*ct[k1]+(tr[k2]==tr[k])*ct[k2];
}
void chg(int x,int y,int v)
{
while(x)
{
int tpp=tp[x];
modify(1,tot,1,dfn[tpp],dfn[x],y,v);
if(num[tpp]<y)x=fa[tpp];
else break;
}
}
void solve(vector<cx> qs)
{
tot=0;
memset(wg,0,sizeof wg);
memset(he,0,sizeof he);
memset(bz,0,sizeof bz);
vector<int> pos;
for(auto xx:qs)if(!bz[xx.x])pos.push_back(xx.x),bz[xx.x]=1;
sort(pos.begin(),pos.end());
vector<int> d;
fo(i,1,n)
if(!bz[i]){
while(!d.empty()&&a[i]>a[d.back()])d.pop_back();
if(!d.empty()&&a[i]==a[d.back()])
{
wg[d.back()]=i;
d.pop_back();
}
d.push_back(i);
}
d.clear();
wg[0]=n+1;
int t=1;
id[0]=1;
num[1]=1000000000;
d.push_back(0);
int ctt=0;
a[qs[0].x]=qs[0].y;
fo(i,1,n)
{
while(wg[d.back()]<=i)d.pop_back();
if(!bz[i])
{
if(wg[i])
{
id[i]=++t;
num[t]=a[i];
lb(id[d.back()],id[i]);
d.push_back(i);
}
}else sp[i]=id[d.back()],ctt+=(a[i]>=num[sp[i]]);
}
dg(1);
tot=0;
dfs(1,1);
build(1,tot,1);
for(auto xx:pos)chg(sp[xx],a[xx],1);
vector<cx> a1;
int ls=1,cnt=0;
pos.push_back(n+1);
int tot1=0;
vector<int> pp;
for(auto xx:pos)if(!mp1[b[xx]])mp1[b[xx]]=++tot1,pp.push_back(b[xx]);
for(auto xx:pos)idd[xx]=mp1[b[xx]];
for(auto xx:qs)if(!mp1[mp[xx.y]])mp1[mp[xx.y]]=++tot1,pp.push_back(mp[xx.y]);
for(auto yy:pos)
{
if(ls<yy)
{
cnt++;mxx[cnt]=0;
d1[cnt].clear();d2[cnt].clear();
fo(t,ls,yy-1)mxx[cnt]=max(mxx[cnt],a[t]);
bj[cnt]={ls,yy-1};
vector<int> d;
fo(t,ls,yy-1)
{
while(!d.empty()&&a[t]>=a[d.back()])d.pop_back();
d.push_back(t);
}
for(auto xx:d)if(mp1[b[xx]])d1[cnt].push_back(mp1[b[xx]]);
for(auto xx:d1[cnt])bz1[cnt][xx]=1;
d.clear();
fd(t,yy-1,ls)
{
while(!d.empty()&&a[t]>=a[d.back()])d.pop_back();
d.push_back(t);
}
for(auto xx:d)if(mp1[b[xx]])d2[cnt].push_back(mp1[b[xx]]);
for(auto xx:d2[cnt])bz2[cnt][xx]=1;
}
ls=yy+1;
}
pos.pop_back();
for(auto xx:qs)
{
chg(sp[xx.x],a[xx.x],-1);
chg(sp[xx.x],a[xx.x]=xx.y,1);
b[xx.x]=mp[xx.y];
idd[xx.x]=mp1[b[xx.x]];
int ans=0;
if(!tr[1])ans=ct[1]-1;
int wz=0;
vector<cx1> d;
pos.push_back(n+1);
for(auto xx:pos)
{
if(bj[wz+1].y<xx&&wz<cnt)
{
wz++;
while(!d.empty()&&mxx[wz]>=d.back().z)
{
if(!d.back().y)ans+=bz2[wz][d.back().x];
d.pop_back();
}
d.push_back({wz,1,mxx[wz]});
}
if(xx==n+1)break;
while(!d.empty()&&a[xx]>d.back().z)d.pop_back();
if(!d.empty()&&d.back().z>=a[xx])
{
if(!d.back().y)ans+=(d.back().z==a[xx]);
else ans+=bz1[d.back().x][idd[xx]];
if(d.back().z==a[xx])d.pop_back();
}
d.push_back({idd[xx],0,a[xx]});
}
pos.pop_back();
write(ans);
}
for(auto xx:pp)mp1[xx]=0;
fo(i,1,cnt)
{
for(auto xx:d1[i])bz1[i][xx]=0;
for(auto xx:d2[i])bz2[i][xx]=0;
d1[i].clear();d2[i].clear();
}
cnt=0;
}
bool vis[N];
int main()
{
n=read();
int m=read();
fo(i,1,n)
{
a[i]=read();
if(!mp[a[i]])mp[a[i]]=++cut;
b[i]=mp[a[i]];
}
cal();
vector<cx> qs;
int szz=0;
while(m--)
{
int x=read(),y=read();
if(!mp[y])mp[y]=++cut;
if(!vis[x])
{
vis[x]=1;
szz++;
}
qs.push_back({x,y});
if(qs.size()==350)solve(qs),qs.clear();
}
if(!qs.empty())solve(qs);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 6592kb
input:
6 2 4 2 2 2 4 6 4 6 6 4
output:
3 2 2
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 8756kb
input:
50 100 310081863 722273055 654741011 310081863 654741011 722273055 654741011 722273055 654741011 654741011 654741011 310081863 310081863 722273055 654741011 654741011 654741011 722273055 310081863 654741011 310081863 310081863 310081863 722273055 310081863 654741011 654741011 310081863 722273055 722...
output:
28 28 28 29 30 31 31 31 31 31 31 31 31 32 33 34 34 33 33 33 33 32 32 31 31 31 32 32 31 31 31 31 30 30 30 31 31 31 31 31 31 30 30 29 30 31 32 32 32 32 32 31 32 33 33 33 33 32 32 31 32 33 31 31 32 31 32 31 31 31 30 31 30 29 29 28 28 29 28 28 27 27 27 27 27 27 26 27 28 27 28 29 28 28 28 28 29 29 28 29 28
result:
ok 101 lines
Test #3:
score: 0
Accepted
time: 3ms
memory: 6744kb
input:
50 100 93308794 275481889 130830018 675774101 130830018 93308794 275481889 999873895 275481889 104418887 130830018 275481889 675774101 999873895 130830018 841188804 360486542 104418887 140762403 275481889 275481889 770511267 104418887 140762403 93308794 675774101 104418887 770511267 130830018 933087...
output:
12 12 11 11 11 11 11 11 10 10 10 10 10 10 10 11 11 11 11 12 12 12 12 12 11 10 11 11 11 11 11 12 13 12 12 13 13 14 12 11 11 10 10 10 10 10 9 9 9 9 9 9 8 9 10 10 9 10 9 9 9 10 10 11 12 12 13 13 13 13 14 14 13 13 13 12 12 12 12 13 12 12 12 12 12 12 13 13 13 13 15 15 15 17 18 18 17 17 16 16 15
result:
ok 101 lines
Test #4:
score: 0
Accepted
time: 0ms
memory: 8696kb
input:
50 100 195248019 905127308 129122336 764519854 338556860 795943323 554412442 338556860 217191782 140699690 654772489 386182517 217191782 37485244 795943323 924638428 795943323 820028162 855279832 795943323 129122336 554412442 195248019 764519854 810525122 554412442 201706134 661330059 129122336 2090...
output:
5 5 6 5 5 5 4 4 3 3 3 3 3 2 4 3 3 3 4 5 5 5 5 5 5 5 5 4 4 4 5 6 6 5 5 4 4 4 3 3 3 3 3 3 4 4 4 4 3 3 4 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 5 5 5 5 6 6 6 5 5 5 5 5 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 7
result:
ok 101 lines
Test #5:
score: 0
Accepted
time: 0ms
memory: 6672kb
input:
50 100 772094573 19576803 263817454 873867094 557813690 952336439 500513802 392057352 305209480 199018938 206776586 514630037 466387810 403552086 50423285 658534934 19576803 404488754 179660945 591777562 262850065 817419372 680762089 591777562 424021147 403552086 718896141 456431927 680762089 595426...
output:
1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 2 2 2 3 3 3 3 3 3 2 2 2 2 2 2 2 1 1 1 0 0 0 0 0 0 0 0 0 1 2 2 3 3 3 3 3 3 2 2 2 3 3 3 3 3 3 3
result:
ok 101 lines
Test #6:
score: 0
Accepted
time: 3ms
memory: 6704kb
input:
50 100 5096114 61078240 254964021 318250156 571031769 256037951 208426954 833646260 732869624 746606948 226729785 151221431 611264696 351005299 205027954 706057630 453231547 874058912 462474957 366832522 823051853 289489922 109072951 103985450 269915659 377686154 809672410 12123621 732787174 9017273...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 101 lines
Test #7:
score: 0
Accepted
time: 1ms
memory: 4648kb
input:
50 100 976187983 976187983 879080743 976187983 827737130 827737130 827737130 827737130 815905933 811453113 789018592 789018592 681089922 675640665 659464656 635119734 635119734 633485638 633485638 567930339 552957008 484438465 484438465 484438465 387753272 377659696 376161946 976187983 367642977 376...
output:
16 15 14 14 15 16 17 17 18 18 31 30 29 28 27 26 25 24 23 22 21 20 20 19 20 19 18 18 18 18 18 17 17 16 16 16 16 15 15 15 15 14 13 12 12 12 12 11 11 10 9 9 9 8 8 7 7 6 7 6 7 7 7 8 8 7 8 10 10 10 10 10 11 10 10 12 14 15 15 16 16 15 16 18 19 20 26 26 27 28 29 28 28 27 26 25 24 23 23 22 21
result:
ok 101 lines
Test #8:
score: 0
Accepted
time: 2ms
memory: 4604kb
input:
50 100 843864537 245114944 227661173 137675097 918583745 80278395 44678681 37169219 37007425 27167524 4382795 4043558 3655016 3624538 2994987 1979195 1407769 819862 771067 665903 137891 137891 665903 771067 819862 1407769 1979195 2994987 3624538 3655016 4043558 4382795 27167524 37007425 37142735 305...
output:
17 17 16 15 7 7 8 5 5 5 5 5 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0 0 0 1 1 2 3 3 4 4 4 4 5 6 6 7 8 9 10 11 12 13 14 14 14 14 14 15 16 16 16 16 17 18 19 19 20 21 20 19 18 15 14 14 14 13 7 6 6 6 6 6 6 6 6 6 6 5 5 4 4 4 3 3 3 3 3 3 2 2 2 2
result:
ok 101 lines
Test #9:
score: 0
Accepted
time: 0ms
memory: 4652kb
input:
50 100 920202355 768392166 755066475 630812635 617367313 601334965 450742259 367726734 265094786 151773018 77676966 53524889 53524889 77676966 151773018 265094786 205222950 154745305 57476426 57476426 154745305 294856628 367726734 450742259 601334965 617367313 630812635 481253037 481253037 755066475...
output:
19 20 20 19 18 17 16 15 14 13 12 11 10 10 10 10 9 8 7 7 7 8 7 7 7 6 6 5 5 5 5 4 4 3 3 4 3 3 3 3 3 3 3 3 2 2 3 3 3 3 3 4 4 5 5 5 6 6 6 6 8 10 10 11 12 13 13 14 14 15 15 15 20 21 22 21 19 17 17 17 15 14 13 13 13 13 13 12 12 11 10 8 8 8 8 7 7 7 7 7 7
result:
ok 101 lines
Test #10:
score: -100
Time Limit Exceeded
input:
100000 250000 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1...