QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#110165 | #2560. Streetlights | ruizhangj | TL | 5ms | 10012kb | C++14 | 6.5kb | 2023-05-31 21:54:21 | 2023-05-31 21:54:22 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
namespace IO{
const int siz=1<<25;
char a[siz],b[siz],*ia=a+siz,*ea=a+siz,*ib=b,*eb=b+siz;
int p[105],l=0;
inline char gc(){if (ia==ea) fread(a,1,siz,stdin),ia=a;return *ia++;}
inline void flush(){fwrite(b,1,ib-b,stdout);ib=b;}
inline void pc(char c){if (ib==eb) flush();*ib++=c;}
inline int read(){
int x=0;char t=gc();
for (;t<'0' || t>'9';t=gc());
for (;'0'<=t && t<='9';t=gc()) x=x*10+t-'0';
return x;
}
inline void write(int x){
if (!x) pc('0');
for (;x;x/=10) p[++l]=x%10;
for (;l;--l) pc(p[l]+'0');pc('\n');
}
struct f{~f(){flush();}}F;
}
using IO::read;
using IO::write;
const int N=1e5+5,M=2.5e5+5,B=500;
struct oper{int x,h;}op[M];
int a[N],num[N+M];
int n,m;
void init(){
n=read(),m=read();
for (int i=1;i<=n;++i) num[i]=a[i]=read();
for (int i=1;i<=m;++i) op[i].x=read(),num[n+i]=op[i].h=read();
sort(num+1,num+1+n+m);num[0]=unique(num+1,num+1+n+m)-num-1;
for (int i=1;i<=n;++i) a[i]=lower_bound(num+1,num+1+num[0],a[i])-num;
for (int i=1;i<=m;++i) op[i].h=lower_bound(num+1,num+1+num[0],op[i].h)-num;
a[0]=a[n+1]=num[0]+1;
}
int bz[N],pre[N];
int st[N],tp;
queue<int> q;
vector<int> son[N];
int siz[N],s[N],dfn[N],rfn[N],top[N],fa[N],tot;
void dfs1(int x){
siz[x]=1,s[x]=0;
for (int y:son[x]){
fa[y]=x;
dfs1(y);
siz[x]+=siz[y];
if (siz[y]>siz[s[x]]) s[x]=y;
}
}
void dfs2(int x){
if (!top[x]) top[x]=x;
rfn[dfn[x]=++tot]=x;
if (s[x]){
top[s[x]]=top[x];
dfs2(s[x]);
}
for (int y:son[x])
if (y!=s[x])
dfs2(y);
}
int h[4*N],d[4*N],val[4*N],sum[4*N],tag[4*N];
void build(int x,int l,int r){
tag[x]=sum[x]=0;
if (l==r){
if (!bz[rfn[l]]) d[x]=h[x]=a[rfn[l]],val[x]=1;
else h[x]=d[x]=0,val[x]=0;
return;
}
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
h[x]=max(h[x<<1],h[x<<1|1]);
d[x]=min(d[x<<1],d[x<<1|1]);
val[x]=val[x<<1]+val[x<<1|1];
}
void pushdown(int x,int l,int r,int mid){
sum[x<<1]+=tag[x]*(mid-l+1),tag[x<<1]+=tag[x];
sum[x<<1|1]+=tag[x]*(r-mid),tag[x<<1|1]+=tag[x];
tag[x]=0;
}
void pushup(int x){sum[x]=sum[x<<1]+sum[x<<1|1];}
void update(int x,int l,int r,int L,int R,int k,bool fg){
if (L<=l && r<=R){
if (h[x]<=k){
if (fg) sum[x]+=r-l+1,++tag[x];
else sum[x]-=r-l+1,--tag[x];
return;
}
if (d[x]>k) return;
int mid=(l+r)>>1;
pushdown(x,l,r,mid);
update(x<<1,l,mid,L,R,k,fg);
update(x<<1|1,mid+1,r,L,R,k,fg);
pushup(x);
}
else{
int mid=(l+r)>>1;
pushdown(x,l,r,mid);
if (L<=mid) update(x<<1,l,mid,L,R,k,fg);
if (R>mid) update(x<<1|1,mid+1,r,L,R,k,fg);
pushup(x);
}
}
int query(int x,int l,int r){
if (!sum[x]) return val[x];
if (l==r) return 0;
int mid=(l+r)>>1;
pushdown(x,l,r,mid);
int res=query(x<<1,l,mid)+query(x<<1|1,mid+1,r);
pushup(x);
return res;
}
void build(){
st[tp=1]=0;
for (int i=0;i<=n+1;++i) pre[i]=fa[i]=0,son[i].clear();
for (int i=1;i<=n+1;++i){
if (bz[i]) continue;
while (a[i]>a[st[tp]]) --tp;
if (a[st[tp]]==a[i])
pre[i]=st[tp],--tp;
st[++tp]=i;
}
q.emplace(n+1);
while (!q.empty()){
int x=q.front();q.pop();
for (int i=x-1;i>pre[x];){
if (pre[i]){
son[x].emplace_back(i);
q.emplace(i);
i=pre[i];
}
else{
if (bz[i]) son[x].emplace_back(i);
--i;
}
}
}
for (int i=0;i<=n+1;++i) top[i]=0;
dfs1(n+1);
tot=0;dfs2(n+1);
build(1,1,tot);
}
void modify(int x,int hg,bool fg){
x=fa[x];
while (x && a[x]<=hg){
update(1,1,tot,dfn[top[x]],dfn[x],hg,fg);
x=fa[top[x]];
}
}
int firstanswer(){
int res=0;
st[tp=1]=0;
for (int i=1;i<=n+1;++i){
while (a[i]>a[st[tp]]) --tp;
if (a[st[tp]]==a[i]) ++res,--tp;
st[++tp]=i;
}
return res-1;
}
struct bp{
int tp,id,val;
bp(int x=0,int y=0,int z=0){tp=x,id=y,val=z;}
}c[N],sa[N];
int cnt,w;
int is_pre[B+5][N+M],is_suf[B+5][N+M],Tms=0;
int prepare(int id,int l,int r){
int res=0;
for (int i=l,mx=0;i<=r;++i)
if (a[i]>mx) is_pre[id][res=mx=a[i]]=Tms;
for (int i=r,mx=0;i>=l;--i)
if (a[i]>mx) is_suf[id][mx=a[i]]=Tms;
return res;
}
int calc_to_front(){
int tp=1,res=0;
sa[1]=(bp){1,0,num[0]+1};
for (int i=1;i<=cnt;++i){
bp t=c[i];
if (t.tp==1){
t.val=a[t.val];
while (t.val>sa[tp].val) --tp;
if (sa[tp].tp==1) res+=(sa[tp].val==t.val);
else res+=2*(is_suf[sa[tp].id][t.val]==Tms);
sa[++tp]=t;
}
else{
while (t.val>sa[tp].val) --tp;
sa[++tp]=t;
}
}
return res;
}
int calc_to_back(){
int tp=1,res=0;
sa[1]=(bp){1,0,num[0]+1};
for (int i=cnt;i>=1;--i){
bp t=c[i];
if (t.tp==1){
t.val=a[t.val];
while (t.val>sa[tp].val) --tp;
if (sa[tp].tp==1) res+=(sa[tp].val==t.val);
else res+=2*(is_pre[sa[tp].id][t.val]==Tms);
sa[++tp]=t;
}
else{
while (t.val>sa[tp].val) --tp;
sa[++tp]=t;
}
}
return res;
}
void work(){
write(firstanswer());
for (int k=1;k<=m;k+=B){
++Tms;
for (int i=1;i<=n;++i) bz[i]=0;
for (int i=k;i<k+B && i<=m;++i) bz[op[i].x]=1;
build();
for (int i=1;i<=n;++i) if (bz[i]) modify(i,a[i],0);
cnt=w=0;
int lst=0;
for (int i=1;i<=n;++i)
if (bz[i]){
if (lst!=i-1){
++w;
c[++cnt]=bp(2,w,prepare(w,lst+1,i-1));
}
lst=i;
c[++cnt]=(bp){1,0,i};
}
if (lst!=n){
++w;
c[++cnt]=bp(2,w,prepare(w,lst+1,n));
}
for (int i=k;i<k+B && i<=m;++i){
modify(op[i].x,a[op[i].x],1);
a[op[i].x]=op[i].h;
modify(op[i].x,a[op[i].x],0);
write(query(1,1,tot)-1+(calc_to_front()+calc_to_back())/2);
}
}
}
int main(){
init();
work();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9468kb
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: 5ms
memory: 10012kb
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: 0ms
memory: 9940kb
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: 1ms
memory: 10012kb
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: 4ms
memory: 8832kb
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: 5ms
memory: 9700kb
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: 4ms
memory: 9892kb
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: 0ms
memory: 9968kb
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: 2ms
memory: 9420kb
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...