QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#297367 | #7872. 崩坏天际线 | zhouhuanyi | 0 | 28ms | 21052kb | C++20 | 5.0kb | 2024-01-04 12:32:13 | 2024-01-04 12:32:14 |
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,op[N+1],l[N+1],r[N+1],x[N+1],pst[N+1],fa[N+1],cnt[N+1],cnt2[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],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];
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,1ll*MD2(pst[pv[ds]]-l)*MD2(cnt[sl]-cnt[fa[ds]])%mod);
Adder(rst,1ll*MD2(r-pst[nt[ds]])*MD2(cnt2[sr]-cnt2[fa[ds]])%mod);
Adder(rst,MD2(s[sr]-s[sl-1]));
Adder(rst,MD2(s2[sr]-s2[sl-1]));
Adder2(res,-1ll*rst*invpw[d]%mod*pw[depth[ds]-1]%mod);
}
return res;
}
int main()
{
//freopen("sky.in","r",stdin);
//freopen("sky.out","w",stdout);
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(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) 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]=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],dfs2(dque[1]),pst[0]=1,pst[lengths+1]=n;
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,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;
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[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;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 18440kb
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:
629358146
result:
wrong answer 1st lines differ - expected: '855279801', found: '629358146'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 28ms
memory: 21052kb
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:
854270023
result:
wrong answer 1st lines differ - expected: '733099543', found: '854270023'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%