QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#297657 | #7872. 崩坏天际线 | zhouhuanyi | 0 | 4936ms | 21084kb | C++14 | 5.8kb | 2024-01-04 21:54:26 | 2024-01-04 21:54:27 |
Judging History
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,pst[N+1],op[N+1],l[N+1],r[N+1],x[N+1],fa[N+1],dep[N+1],dep2[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],tops[N+1],s[N+1],depth[N+1],sdepth[N+1],s2[N+1],dp[N+1],DSP[N+1],DSP2[N+1],DP[N+1],DP2[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,dfs(ls[x],l,x);
if (rs[x]) fa[rs[x]]=x,depth[rs[x]]=depth[x]+1,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],dp[x]=pst[nt[x]]-pst[pv[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]),Adder(dp[x],1ll*dp[ls[x]]*inv2%mod);
if (rs[x]) dfs2(rs[x]),Adder(dp[x],1ll*dp[rs[x]]*inv2%mod);
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,1ll*MD2(DP[sl]-DP[ds])*pw[dep[ds]-1]%mod);
Adder(rst,1ll*MD2(DP2[sr]-DP2[ds])*pw[dep2[ds]-1]%mod);
Adder(rst,1ll*MD2(1-invpw[dep[sl]-dep[ds]])*inv2%mod*MD2(-l)%mod);
Adder(rst,1ll*MD2(DSP[sl]-DSP[ds])*pw[dep[ds]-1]%mod);
Adder(rst,1ll*MD2(1-invpw[dep2[sr]-dep2[ds]])*inv2%mod*r%mod);
Adder(rst,1ll*MD2(DSP2[sr]-DSP2[ds])*pw[dep2[ds]-1]%mod);
Adder(rst,1ll*(r-l)*inv2%mod);
Adder2(res,-1ll*rst*invpw[d]%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=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(tong[i]);
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) 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]=hs[lengths]=fa[lengths]=0,sdelta[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,dfs(dque[1],0,lengths+1),tops[dque[1]]=dque[1],pst[0]=1,pst[lengths+1]=n,dfs2(dque[1]);
for (int j=lengths;j>=1;--j)
{
if (nt[j]!=lengths+1) dep[j]=dep[nt[j]]+1,DP[j]=MD(DP[nt[j]]+1ll*MD(1ll*dp[rs[j]]*inv2%mod+(pst[nt[j]]-pst[pv[j]]))*invpw[dep[j]]%mod),DSP[j]=MD(DSP[nt[j]]+1ll*invpw[dep[j]]*pst[pv[j]]%mod);
else dep[j]=1,DP[j]=1ll*MD(1ll*dp[rs[j]]*inv2%mod+(pst[nt[j]]-pst[pv[j]]))*inv2%mod,DSP[j]=1ll*invpw[dep[j]]*pst[pv[j]]%mod;
}
for (int j=1;j<=lengths;++j)
{
if (pv[j]!=0) dep2[j]=dep2[pv[j]]+1,DP2[j]=MD(DP2[pv[j]]+1ll*MD(1ll*dp[ls[j]]*inv2%mod+(pst[nt[j]]-pst[pv[j]]))*invpw[dep2[j]]%mod),DSP2[j]=MD(DSP2[pv[j]]+1ll*invpw[dep2[j]]*MD2(-pst[nt[j]])%mod);
else dep2[j]=1,DP2[j]=1ll*MD(1ll*dp[ls[j]]*inv2%mod+(pst[nt[j]]-pst[pv[j]]))*inv2%mod,DSP2[j]=1ll*invpw[dep2[j]]*MD2(-pst[nt[j]])%mod;
}
}
for (int j=1;j<=n;++j) belong[j]=0;
for (int j=(i-1)*szt+1;j<=min(i*szt,leng);++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,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[k]) ls[k]=dque[top],top--;
if (top) rs[dque[top]]=k;
cl[++len]=dque[++top]=k;
}
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;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 21084kb
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:
169902410
result:
wrong answer 1st lines differ - expected: '855279801', found: '169902410'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #13:
score: 40
Accepted
time: 3850ms
memory: 19064kb
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:
733099543
result:
ok single line: '733099543'
Test #14:
score: 0
Accepted
time: 2896ms
memory: 20516kb
input:
49951 43686 1 21796 23464 1 29304 46959 1 5034 41719 1 7779 35334 1 27566 36486 1 20347 26165 1 12508 30387 1 18363 20335 1 8540 21417 1 5728 49086 1 46038 47603 1 10371 15910 1 27293 43572 1 18915 45279 1 7388 48342 1 6802 43746 1 4361 40049 1 41177 43375 1 23287 48354 1 37097 41733 1 2406 11638 1 ...
output:
792296531
result:
ok single line: '792296531'
Test #15:
score: 0
Accepted
time: 4936ms
memory: 18564kb
input:
49914 43874 1 8935 40963 1 4425 44317 1 1769 45855 1 2436 40257 1 1778 47216 1 383 42149 1 5398 40732 1 1079 43346 1 6578 41660 1 9689 45985 1 6131 42681 1 8862 47431 1 3979 46189 1 6456 43485 1 2028 46574 1 3802 47787 1 6990 41659 1 9221 41204 1 2271 43554 1 8018 45280 1 9344 43917 1 6623 41152 1 7...
output:
831211412
result:
ok single line: '831211412'
Test #16:
score: -40
Time Limit Exceeded
input:
50000 50000 1 1310 49344 1 5755 44255 1 3582 41465 1 6800 42160 1 1651 44584 1 7967 44410 1 3116 48795 1 1855 41120 1 27 42294 1 2455 49629 1 4196 42487 1 7070 44542 1 136 42053 1 5715 44222 1 8794 43115 1 4048 45579 1 635 46703 1 9246 41055 1 3678 41276 1 4871 41715 1 1659 44679 1 1639 46392 1 2479...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%