QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#716294 | #9465. 基础 01 练习题 | yhddd | 15 | 102ms | 201596kb | C++14 | 4.8kb | 2024-11-06 14:53:36 | 2024-11-06 14:53:36 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define mod 998244353ll
#define pii pair<int,int>
#define fi first
#define se second
#define mems(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define db double
using namespace std;
const int maxn=200010;
const int inf=1e18;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch-48);ch=getchar();}
return x*f;
}
bool Mbe;
int n,q,op;
vector<pii> updl[maxn],updu[maxn];
int val[2],bas,pw[maxn];
mt19937 rnd(time(0));
int hsh[maxn<<5][2],tag[maxn<<2];
int rt[maxn];
int lc[maxn<<5],rc[maxn<<5],idx;
#define mid (l+r>>1)
#define ls lc[nd]
#define rs rc[nd]
void build(int &nd,int l,int r){
if(!nd)nd=++idx;
if(l==r){
hsh[nd][0]=val[0],hsh[nd][1]=val[1];
return ;
}
build(ls,l,mid),build(rs,mid+1,r);
hsh[nd][0]=(hsh[ls][0]*pw[r-mid]+hsh[rs][0])%mod;
hsh[nd][1]=(hsh[ls][1]*pw[r-mid]+hsh[rs][1])%mod;
}
void updata(int &nd,int l,int r,int ql,int qr){
int lst=nd;nd=++idx;
hsh[nd][0]=hsh[lst][0],hsh[nd][1]=hsh[lst][1],tag[nd]=tag[lst];
ls=lc[lst],rs=rc[lst];
if(l>=ql&&r<=qr){
swap(hsh[nd][0],hsh[nd][1]);tag[nd]^=1;
return ;
}
if(ql<=mid)updata(ls,l,mid,ql,qr);
if(qr>mid)updata(rs,mid+1,r,ql,qr);
hsh[nd][0]=(hsh[ls][0]*pw[r-mid]+hsh[rs][0])%mod;
hsh[nd][1]=(hsh[ls][1]*pw[r-mid]+hsh[rs][1])%mod;
if(tag[nd])swap(hsh[nd][0],hsh[nd][1]);
}
bool cmp(int u,int v,int l,int r,bool fl0=0,bool fl1=0){
// cout<<u<<" "<<v<<" "<<l<<" "<<r<<endl;
if(hsh[u][fl0]==hsh[v][fl1])return 0;
if(l==r){
return (hsh[u][fl0]==val[1])<(hsh[v][fl1]==val[1]);
}
fl0^=tag[u],fl1^=tag[v];
if(hsh[lc[u]][fl0]==hsh[lc[v]][fl1])return cmp(rc[u],rc[v],mid+1,r,fl0,fl1);
return cmp(lc[u],lc[v],l,mid,fl0,fl1);
}
#undef ls
#undef rs
int id[maxn],pos[maxn];
#define ls nd<<1
#define rs nd<<1|1
int mn[maxn<<2][2],mx[maxn<<2][2];
void down(int nd){
swap(mn[ls][0],mn[ls][1]),swap(mx[ls][0],mx[ls][1]),tag[ls]^=1;
swap(mn[rs][0],mn[rs][1]),swap(mx[rs][0],mx[rs][1]),tag[rs]^=1;
tag[nd]=0;
}
void up(int nd){
mn[nd][0]=min(mn[ls][0],mn[rs][0]);
mn[nd][1]=min(mn[ls][1],mn[rs][1]);
mx[nd][0]=max(mx[ls][0],mx[rs][0]);
mx[nd][1]=max(mx[ls][1],mx[rs][1]);
}
void init(int nd,int l,int r){
tag[nd]=0;
if(l==r){
mn[nd][0]=mx[nd][0]=pos[l];
mn[nd][1]=inf,mx[nd][1]=-inf;
return ;
}
init(ls,l,mid),init(rs,mid+1,r);
up(nd);
}
void modif(int nd,int l,int r,int ql,int qr){
if(l>=ql&&r<=qr){
swap(mn[nd][0],mn[nd][1]),swap(mx[nd][0],mx[nd][1]),tag[nd]^=1;
return ;
}
if(tag[nd])down(nd);
if(ql<=mid)modif(ls,l,mid,ql,qr);
if(qr>mid)modif(rs,mid+1,r,ql,qr);
up(nd);
}
int fa[maxn],siz[maxn],num[maxn];
int fd(int x){
if(fa[x]==x)return x;
return fa[x]=fd(fa[x]);
}
int ans[maxn];
priority_queue<int>Q[maxn];
void merge(int u,int v){
if(Q[u].size()<Q[v].size())Q[u].swap(Q[v]);
while(!Q[v].empty()){
Q[u].push(Q[v].top()),Q[v].pop();
}
}
// bool vis[110][110];
void work(){
n=read();q=read();op=read();
for(int i=1;i<=q;i++){
int x=read(),xx=read(),y=read(),yy=read();
updl[x].pb({y,yy}),updl[xx+1].pb({y,yy});
updu[y].pb({x,xx}),updu[yy+1].pb({x,xx});
// for(int j=x;j<=xx;j++){
// for(int k=y;k<=yy;k++)vis[j][k]^=1;
// }
}
val[0]=rnd()%mod,val[1]=rnd()%mod,bas=rnd()%mod;
pw[0]=1;for(int i=1;i<=n;i++)pw[i]=pw[i-1]*bas%mod;
build(rt[0],1,n);
for(int i=1;i<=n;i++){
rt[i]=rt[i-1];
for(auto[l,r]:updl[i])updata(rt[i],1,n,l,r);
}
for(int i=1;i<=n;i++)id[i]=i;
stable_sort(id+1,id+n+1,[&](int u,int v){return cmp(rt[u],rt[v],1,n);});
// for(int i=1;i<=n;i++)cout<<id[i]<<" ";cout<<"\n";
for(int i=1;i<=n;i++)pos[id[i]]=i;
for(int i=1;i<=n;i++)fa[i]=i,siz[i]=1;
init(1,1,n);
int res=0;
for(int i=1;i<=n;i++){
res+=n;
for(auto[l,r]:updu[i])modif(1,1,n,l,r);
int l=mn[1][1],r=mx[1][0];
// cout<<i<<" "<<l<<" "<<r<<"\n";
if(l<=r){
int p=fd(r);
res+=max(0ll,siz[p]*num[p]-1);
num[p]++;
res-=max(0ll,siz[p]*num[p]-1);
while(p>l){
int np=fd(p-1);
res+=max(0ll,siz[p]*num[p]-1)+max(0ll,siz[np]*num[np]-1);
num[np]+=num[p],siz[np]+=siz[p];fa[p]=np;merge(np,p);
while(!Q[np].empty()&&Q[np].top()>=np)Q[np].pop(),num[np]++;
res-=max(0ll,siz[np]*num[np]-1);p=np;
}
}
else if(l<=n&&r>=1){
int p=fd(l);
if(p<=r){
res+=max(0ll,siz[p]*num[p]-1);
++num[p];
res-=max(0ll,siz[p]*num[p]-1);
}
else Q[p].push(r);
}
ans[i]=res;
}
if(!op)printf("%lld\n",ans[n]);
else for(int i=1;i<=n;i++)printf("%lld ",ans[i]);
}
// \
444
bool Med;
int T;
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
// cerr<<(&Mbe-&Med)/1048576.0<<" MB\n";
T=1;
while(T--)work();
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 0ms
memory: 38092kb
input:
4 1000 0 2 3 1 2 1 3 1 3 1 2 1 2 1 2 3 4 1 4 2 4 1 3 1 2 1 4 1 2 1 3 1 4 3 3 2 3 1 2 2 4 4 4 1 3 3 3 3 4 3 4 3 4 2 3 1 1 1 2 2 4 1 4 3 4 3 4 1 2 1 2 2 3 3 4 3 3 1 2 4 4 4 4 2 4 1 4 1 1 1 1 1 3 2 3 2 3 1 1 2 4 2 3 2 4 3 3 1 4 3 3 3 3 1 3 3 3 2 3 2 4 3 3 2 2 1 3 2 4 1 3 1 2 3 4 1 2 2 3 1 3 1 1 1 2 1 2...
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 5
Accepted
time: 3ms
memory: 34140kb
input:
4 1000 0 1 4 3 3 2 3 4 4 3 4 3 4 3 4 1 2 1 4 2 4 2 3 1 3 3 4 2 4 2 3 3 3 3 4 1 3 1 3 1 4 2 3 1 3 1 1 2 2 1 4 3 4 1 4 1 3 1 2 3 4 1 2 1 2 2 3 1 4 2 2 2 2 1 3 1 3 2 2 2 4 1 2 1 4 1 1 1 1 1 2 3 4 4 4 1 3 2 4 1 3 1 1 1 3 1 4 2 2 2 3 1 2 2 2 1 2 1 2 1 4 1 4 2 4 1 2 1 3 1 2 1 3 2 4 2 2 1 2 1 1 1 2 1 3 2 4...
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 5
Accepted
time: 0ms
memory: 36360kb
input:
4 1000 0 1 4 1 2 1 4 2 2 1 4 3 4 2 4 4 4 2 3 3 4 2 4 2 4 1 2 2 2 4 4 2 4 1 3 1 3 1 4 1 4 3 3 3 4 4 4 2 3 2 3 1 4 2 2 1 3 2 3 2 4 2 2 1 4 1 2 2 3 1 4 1 3 4 4 1 4 3 4 1 4 1 2 1 2 1 2 1 3 2 2 3 3 1 2 1 4 1 1 1 4 2 2 1 4 1 4 3 4 2 4 2 4 2 2 1 4 3 4 1 3 2 3 2 4 1 3 1 4 1 3 1 4 3 3 1 3 1 2 1 3 3 3 1 4 1 4...
output:
5
result:
ok 1 number(s): "5"
Subtask #2:
score: 10
Accepted
Test #4:
score: 10
Accepted
time: 102ms
memory: 201596kb
input:
50 200000 0 1 45 2 6 29 44 2 6 31 37 2 50 2 37 1 19 7 13 8 38 38 46 19 38 10 30 30 46 22 42 1 45 5 35 24 27 10 36 19 31 20 47 17 35 7 9 23 42 15 26 31 42 7 8 7 42 1 26 33 48 2 5 30 36 17 44 21 44 5 44 24 36 19 47 15 17 29 36 2 42 31 34 11 41 9 24 12 30 30 43 8 20 2 12 13 20 11 12 10 15 14 22 3 29 2 ...
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 10
Accepted
time: 0ms
memory: 33964kb
input:
50 70 0 1 50 1 50 24 50 1 1 50 50 2 2 34 50 3 3 36 50 4 4 32 50 5 5 18 50 6 6 12 50 7 7 6 50 8 8 28 50 9 9 38 50 10 10 4 50 11 11 26 50 12 12 14 50 13 13 46 50 14 14 2 50 15 15 8 50 16 16 44 50 17 17 10 50 18 18 30 50 19 19 22 50 20 20 48 50 21 21 20 50 22 22 42 50 23 23 40 50 24 24 16 50 25 25 16 5...
output:
2280
result:
ok 1 number(s): "2280"
Test #6:
score: 10
Accepted
time: 0ms
memory: 35884kb
input:
50 100 0 2 49 1 1 23 28 2 2 19 32 3 3 21 30 4 4 20 31 5 5 22 29 6 6 12 39 7 7 15 36 8 8 7 44 9 9 3 48 10 10 10 41 11 11 5 46 12 12 14 37 13 13 13 38 14 14 4 47 15 15 6 45 16 16 17 34 17 17 25 26 18 18 1 50 19 19 9 42 20 20 11 40 21 21 16 35 22 22 24 27 23 23 8 43 24 24 18 33 25 25 11 40 26 26 14 37 ...
output:
339
result:
ok 1 number(s): "339"
Test #7:
score: 10
Accepted
time: 0ms
memory: 34316kb
input:
50 500 0 1 2 1 14 3 4 1 3 5 6 1 12 7 8 1 9 9 10 1 15 11 12 1 11 13 14 1 13 15 16 1 17 17 18 1 16 19 20 1 20 21 22 1 2 23 24 1 10 25 26 1 4 27 28 1 8 29 30 1 19 31 32 1 21 33 34 1 24 35 36 1 23 37 38 1 6 39 40 1 18 41 42 1 25 43 44 1 5 45 46 1 22 47 48 1 1 49 50 1 7 35 36 26 26 41 42 26 26 27 28 27 2...
output:
51
result:
ok 1 number(s): "51"
Subtask #3:
score: 0
Runtime Error
Test #8:
score: 0
Runtime Error
input:
5000 200000 0 1438 2561 3478 4930 1740 4634 87 3003 590 3275 1376 1681 2035 2793 2004 4945 567 3159 550 4470 61 3039 3431 3519 2654 3834 3460 4960 591 3560 409 443 345 2599 746 2891 1288 4570 1577 4402 249 377 1951 4534 2411 2455 294 1192 1679 3153 1645 4259 1735 1856 601 668 477 4881 411 2094 424 1...
output:
result:
Subtask #4:
score: 0
Runtime Error
Test #14:
score: 0
Runtime Error
input:
5000 200000 1 565 4401 1659 1826 429 1640 2999 3495 572 3994 9 3863 3844 4284 2307 3144 1054 1943 358 2592 727 4248 29 1171 1685 2392 4559 4929 1149 2787 1204 1947 2349 2619 405 998 1910 2786 25 1275 912 3475 4384 4387 3822 4895 1849 4548 3082 4749 3457 4220 3174 4885 117 1085 2517 3919 4325 4869 17...
output:
result:
Subtask #5:
score: 0
Runtime Error
Test #21:
score: 0
Runtime Error
input:
200000 200000 1 1 2 1 6 3 4 1 1 5 6 1 5 7 8 1 3 9 10 1 3 11 12 1 6 13 14 1 5 15 16 1 6 17 18 1 6 19 20 1 1 21 22 1 4 23 24 1 5 25 26 1 2 27 28 1 4 29 30 1 3 31 32 1 2 33 34 1 6 35 36 1 3 37 38 1 2 39 40 1 2 41 42 1 3 43 44 1 1 45 46 1 2 47 48 1 3 49 50 1 4 51 52 1 5 53 54 1 1 55 56 1 5 57 58 1 5 59 ...
output:
result:
Subtask #6:
score: 0
Runtime Error
Test #28:
score: 0
Runtime Error
input:
200000 200000 0 91264 123676 6826 154505 121351 188051 108158 131448 65413 163961 26771 116304 93852 110556 34929 187363 31794 142162 33578 38712 26574 67763 178013 197235 46436 146042 95 122860 11683 50463 60177 195245 60862 194711 37817 97212 144366 176271 113551 171098 120095 170517 73555 167299 ...
output:
result:
Subtask #7:
score: 0
Runtime Error
Test #37:
score: 0
Runtime Error
input:
100000 200000 1 1 22878 1 2 1 7957 3 4 1 21779 5 6 1 34321 7 8 1 41692 9 10 1 49473 11 12 1 10254 13 14 1 43995 15 16 1 46975 17 18 1 668 19 20 1 25996 21 22 1 24975 23 24 1 43259 25 26 1 4174 27 28 1 39330 29 30 1 35462 31 32 1 27523 33 34 1 5574 35 36 1 47955 37 38 1 47013 39 40 1 3846 41 42 1 276...