QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#295193 | #7872. 崩坏天际线 | zhouhuanyi | 0 | 0ms | 0kb | C++20 | 5.0kb | 2023-12-30 20:51:36 | 2023-12-30 20:51:37 |
answer
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
#define N 100000
#define mod 998244353
using namespace std;
const int inv2=(mod+1)>>1;
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;
}
void Adder(int &x,int d)
{
x+=d;
if (x>=mod) x-=mod;
return;
}
void Adder2(int &x,int d)
{
x+=d;
if (x<0) x+=mod;
return;
}
int MD(int x)
{
return x>=mod?x-mod:x;
}
int MD2(int x)
{
return x<0?x+mod:x;
}
struct reads
{
int l,r,t;
};
reads tong[N+1];
struct rds
{
int x,t;
};
rds st[N+1];
int n,q,szt,ans,op[N+1],l[N+1],r[N+1],x[N+1],pst[N+1],fa[N+1],cnt[N+1],pw[N+1],invpw[N+1],block[N+1],length,leng,delta[N+1],sdelta[N+1],ls[N+1],rs[N+1],pv[N+1],nt[N+1],sps[N+1],num[N+1],num2[N+1],snum[N+1],snum2[N+1],dque[N+1],top,tongs[N+1],lengths,sz[N+1],hs[N+1],belong[N+1],sbelong[N+1],sq[N+1],cater,cl[N+1],DP[N+1],tops[N+1],s[N+1],depth[N+1],sdepth[N+1],s2[N+1],len;
vector<reads>v[N+1];
void dfs(int x,int l,int r)
{
pv[x]=l,nt[x]=r,sz[x]=1;
if (ls[x]) fa[ls[x]]=x,depth[ls[x]]=depth[x]+1,DP[ls[x]]=MD(DP[x]+invpw[depth[ls[x]]]),dfs(ls[x],l,x);
if (rs[x]) fa[rs[x]]=x,depth[rs[x]]=depth[x]+1,DP[rs[x]]=MD(DP[x]+invpw[depth[rs[x]]]),dfs(rs[x],x,r);
sz[x]=sz[ls[x]]+sz[rs[x]]+1,hs[x]=(sz[ls[x]]>sz[rs[x]])?ls[x]:rs[x];
return;
}
void dfs2(int x)
{
tops[hs[x]]=tops[x];
if (ls[x]^rs[x]^hs[x]) tops[ls[x]^rs[x]^hs[x]]=ls[x]^rs[x]^hs[x];
if (ls[x]) dfs2(ls[x]);
if (rs[x]) dfs2(rs[x]);
return;
}
void dfs3(int x)
{
if (ls[x]) sdepth[ls[x]]=sdepth[x]+1,dfs3(ls[x]);
if (rs[x]) sdepth[rs[x]]=sdepth[x]+1,dfs3(rs[x]);
return;
}
int lca(int x,int y)
{
while (tops[x]!=tops[y])
{
if (depth[tops[x]]<depth[tops[y]]) swap(x,y);
x=fa[tops[x]];
}
if (depth[x]>depth[y]) swap(x,y);
return x;
}
int calc(int l,int r,int sl,int sr,int d)
{
int res=1ll*(r-l)*invpw[d]%mod,rst=0,ds;
if (sl<=sr)
{
ds=lca(sl,sr);
Adder(rst,MD2(MD2(DP[sl]-DP[ds])-1ll*l*(depth[sl]-depth[ds])%mod));
Adder(rst,MD2(1ll*r*(depth[sr]-depth[ds])%mod-MD2(DP[sr]-DP[ds])));
Adder(rst,MD2(s[ds-1]-s[sl-1]));
Adder(rst,MD2(s2[sr]-s2[ds]));
Adder2(res,-1ll*rst*invpw[d]%mod*pw[depth[ds]-1]%mod);
}
return res;
}
int main()
{
int ps=0,sl,sr;
pw[0]=invpw[0]=1;
for (int i=1;i<=N;++i) pw[i]=2ll*pw[i-1]%mod,invpw[i]=1ll*invpw[i-1]*inv2%mod;
n=read(),q=read();
for (int i=1;i<=q;++i)
{
op[i]=read();
if (op[i]==1) l[i]=read(),r[i]=read(),tong[++length]=(reads){l[i],r[i],i};
else x[i]=read(),st[++leng]=(rds){x[i],i};
}
reverse(tong+1,tong+length+1),reverse(st+1,st+leng+1),szt=sqrt(leng);
for (int i=1;i<=leng;++i) block[i]=(i-1)/szt+1;
for (int i=1;i<=length;++i)
{
while (ps+1<=leng&&st[ps+1].t>=tong[i].t) ++ps;
if (ps) v[block[ps]].push_back((reads){tong[i].l,tong[i].r,ps});
else Adder(ans,tong[i].r-tong[i].l);
}
for (int i=1;i<=block[leng];++i)
if (!v[i].empty())
{
if (i!=1)
{
lengths=top=cater=0;
for (int j=1;j<=n;++j) num[j]=delta[j]=0;
for (int j=1;j<=(i-1)*szt;++j) delta[st[j].x]=st[j].t;
for (int j=1;j<=n;++j)
if (delta[j])
tongs[++lengths]=j,ls[lengths]=rs[lengths]=fa[lengths]=0,delta[lengths]=delta[j],pst[lengths]=j;
for (int j=1;j<=lengths;++j)
{
while (top&&sdelta[dque[top]]>sdelta[j]) ls[j]=dque[top],top--;
if (top) rs[dque[top]]=j;
dque[++top]=j;
}
depth[dque[1]]=1,cnt[1]=0,DP[1]=inv2,dfs(dque[1],0,top+1),tops[dque[1]]=dque[1],dfs2(dque[1]),pst[top+1]=n+1;
for (int j=1;j<=lengths;++j) s[j]=MD(s[j-1]+1ll*invpw[depth[j]]*(pst[nt[j]]-pst[j])%mod),s2[j]=MD(s2[j-1]+1ll*invpw[depth[j]]*(pst[j]-pst[pv[j]])%mod);
}
for (int j=1;j<=n;++j) belong[j]=0;
for (int j=(i-1)*szt+1;j<=min(i*szt,n);++j)
{
num[st[j].x]=lower_bound(tongs+1,tongs+lengths+1,st[j].x)-tongs-1,num2[st[j].x]=lower_bound(tongs+1,tongs+lengths+1,st[j].x+1)-tongs;
if (!belong[st[j].x]) belong[st[j].x]=st[j].t;
}
for (int j=1;j<=n;++j)
if (belong[j])
sq[++cater]=j,snum[cater]=num[j],snum2[cater]=num2[j],sbelong[cater]=belong[j],sps[cater]=j;
for (int j=0;j<v[i].size();++j)
{
sl=lower_bound(tongs+1,tongs+lengths+1,v[i][j].l+1)-tongs,sr=lower_bound(tongs+1,tongs+lengths+1,v[i][j].r)-tongs-1,top=len=0;
for (int k=1;k<=cater;++k) ls[k]=rs[k]=0;
for (int k=1;k<=cater;++k)
if (v[i][j].l<sq[k]&&sq[k]<v[i][j].r&&sbelong[k]>=v[i][j].t)
{
while (top&&sbelong[dque[top]]>sbelong[j]) ls[j]=dque[top],top--;
if (top) rs[dque[top]]=j;
cl[++len]=k,dque[++top]=j;
}
if (top)
{
sdepth[dque[1]]=1,dfs3(dque[1]);
Adder(ans,calc(v[i][j].l,sps[cl[1]],sl,snum[cl[1]],sdepth[cl[1]]));
for (int k=1;k<=len-1;++k) Adder(ans,calc(sps[cl[k]],sps[cl[k+1]],snum2[cl[k]],snum[cl[k+1]],max(sdepth[cl[k]],sdepth[cl[k+1]])));
Adder(ans,calc(sps[cl[len]],v[i][j].r,snum2[cl[len]],sr,sdepth[cl[len]]));
}
else Adder(ans,calc(v[i][j].l,v[i][j].r,sl,sr,0));
}
}
printf("%d\n",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
500 500 1 119 258 2 134 2 417 2 176 2 61 2 60 2 110 1 335 336 1 96 111 2 202 1 138 344 2 358 2 134 1 29 54 1 73 381 1 179 495 2 490 2 418 2 186 2 183 1 168 340 2 78 1 15 27 2 373 1 245 498 1 372 495 2 244 2 63 1 174 490 2 282 2 417 1 272 408 1 109 134 2 303 2 345 1 238 401 1 36 480 1 21 483 2 10 1 3...
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #13:
score: 0
Time Limit Exceeded
input:
50000 50000 1 24367 33007 1 14396 42256 1 6375 22327 1 7892 42501 1 10100 37998 1 6284 48524 1 7357 18164 1 16200 46424 1 18972 34131 1 16849 32591 1 1917 3018 1 19897 30272 1 45044 45753 1 18999 25448 1 5167 31033 1 6182 35335 1 7270 37270 1 12651 39965 1 28896 38022 1 13853 35426 1 35516 48244 1 1...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%