QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#103701 | #6304. Rectangle | MIT01 | 0 | 0ms | 0kb | C++17 | 8.1kb | 2023-05-07 08:46:44 | 2023-05-07 08:46:45 |
Judging History
answer
#pragma GCC optimize("-Ofast","-ffast-math","-funroll-all-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pl pair<ll, ll>
#define db double
#define mp make_pair
#define pb push_back
#define fi first
#define int long long
#define se second
typedef pair<int,int> rg;
inline rg operator & (rg a,rg b) {
return rg(max(a.fi,b.fi),min(a.se,b.se));
}
inline rg operator + (rg a,rg b) {
return rg(max(a.fi,b.fi),min(a.se,b.se));
}
inline rg operator * (rg a,rg b) {
return rg(max(a.fi,b.fi),min(a.se,b.se));
}
ll max(ll a,int b) {
if(a<b) a=b;
return a;
}
inline ll len(rg s) {
return max(s.se-s.fi+1,0);
}
struct rect {
rg x,y;
}r[123456];
#define SZ 888888
#define B 400
#define SB 200000/400+3
const int MOD=998244353;
const int lim=1e9;
const ll bk6=166374059LL;
int n;
int xs[SZ]; int xn=0;
ll C2(ll t) {
t%=MOD;
return t*(t-1)/2%MOD;
}
ll C3(ll t) {
t%=MOD;
return t*(t-1)%MOD*(t-2)%MOD*bk6%MOD;
}
vector<int> qs[SZ];
rg qz[SZ],hz[SZ];
rg qzy[SZ],hzy[SZ];
int le[SZ];
rg vv[SZ];
pair<int,rg> be[SB][150099]; int en[SZ];
#define FD(x) (lower_bound(xs+1,xs+1+xn,x)-xs)
ll ss=0;
void pd(int b) {
if(!en[b]) return;
auto&BE=be[b];
static pair<rg,int> qs[SZ]; int qn=0,sn=0;
static pair<rg,int> ss[SZ];
rg tot(1,lim);
for(int i=0;i<en[b];++i)
if(BE[i].fi) qs[qn++]=mp(BE[i].se&tot,BE[i].fi);
else tot=tot&BE[i].se;
for(int i=0;i<qn;++i) qs[i].fi.se++;
for(int i=b*B;i<b*B+B;++i) if(le[i])
ss[sn++]=make_pair(vv[i],le[i]),++ss[sn-1].fi.se;
/*
ll lowend=0,highend=0,bar=0;
{
sort(qs,qs+qn,[&](pair<rg,int> x,pair<rg,int> y) {
auto a=x.fi,b=y.fi;
return a.fi<b.fi;
});
sort(ss,ss+sn,[&](pair<rg,int> x,pair<rg,int> y) {
auto a=x.fi,b=y.fi;
return a.fi<b.fi;
});
int j=0; ll tt=0,jj=0;
for(int i=0;i<qn;++i) (tt+=qs[i].fi.fi*qs[i].se)%=MOD;
for(int i=0;i<sn;++i) {
while(j<qn&&qs[j].fi.fi<ss[i].fi.fi) ++j,(jj+=qs[j].se)%=MOD,(tt-=qs[j].fi.fi*qs[j].se)%=MOD;
lowend+=(tt+ss[i].fi.fi*(ll)jj)%MOD*ss[i].se; lowend%=MOD;
}
}
{
sort(qs,qs+qn,[&](pair<rg,int> x,pair<rg,int> y) {
auto a=x.fi,b=y.fi;
return a.se>b.se;
});
sort(ss,ss+sn,[&](pair<rg,int> x,pair<rg,int> y) {
auto a=x.fi,b=y.fi;
return a.se>b.se;
});
int j=0; ll tt=0,jj=0;
for(int i=0;i<qn;++i) (tt+=qs[i].fi.se*qs[i].se)%=MOD;
for(int i=0;i<sn;++i) {
while(j<qn&&qs[j].fi.se>ss[i].fi.se) ++j,(jj+=qs[j].se)%=MOD,(tt-=qs[j].fi.se*qs[j].se)%=MOD;
// while(j<qn&&qs[j].se>ss[i].fi.se) ++j,tt-=qs[j].fi;
highend+=(tt+ss[i].fi.se*(ll)jj)%MOD*ss[i].se; highend%=MOD;
}
}
{
sort(qs,qs+qn,[&](pair<rg,int> x,pair<rg,int> y) {
auto a=x.fi,b=y.fi;
return a.se<b.se;
});
sort(ss,ss+sn,[&](pair<rg,int> x,pair<rg,int> y) {
auto a=x.fi,b=y.fi;
return a.fi<b.fi;
});
int j=0; ll tt=0,jj=0;
for(int i=0;i<sn;++i) {
while(j<qn&&qs[j].fi.se<ss[i].fi.fi) ++j,(jj+=qs[j].se)%=MOD,(tt+=qs[j].fi.se*qs[j].se)%=MOD;
// while(j<qn&&qs[j].se<ss[i].fi.fi) ++j,tt+=qs[j].se;
bar+=(ss[i].fi.fi*(ll)jj-tt)%MOD*ss[i].se; bar%=MOD;
}
}
{
sort(qs,qs+qn,[&](pair<rg,int> x,pair<rg,int> y) {
auto a=x.fi,b=y.fi;
return a.fi>b.fi;
});
sort(ss,ss+sn,[&](pair<rg,int> x,pair<rg,int> y) {
auto a=x.fi,b=y.fi;
return a.se>b.se;
});
int j=0; ll tt=0,jj=0;
for(int i=0;i<sn;++i) {
while(j<qn&&qs[j].fi.fi>ss[i].fi.se) ++j,(jj+=qs[j].se)%=MOD,(tt+=qs[j].fi.fi*qs[j].se)%=MOD;
// while(j<qn&&qs[j].fi>ss[i].fi.se) ++j,tt+=qs[j].fi;
bar+=(tt-ss[i].fi.se*(ll)jj)%MOD*ss[i].se; bar%=MOD;
}
}
cerr<<::ss<<"??"<<lowend<<","<<highend<<","<<bar<<"\n";
(::ss+=lowend+highend-bar)%=MOD;*/
cerr<<::ss<<"->";
for(int i=0;i<qn;++i)
for(int j=0;j<sn;++j) {
auto t=ss[j].fi&qs[i].fi;
::ss+=max(t.se-t.fi,0)*1LL%MOD*ss[j].se%MOD*qs[i].se%MOD;
}
::ss%=MOD;
cerr<<::ss<<"\n";
for(int i=b*B;i<b*B+B;++i)
vv[i]=vv[i]&tot;
en[b]=0;
}
void edit(int l,int r,rg t) {
if(l>r) return;
cout<<"edt"<<l<<"~"<<r<<" "<<t.fi<<","<<t.se<<"\n";
pd(l/B); pd(r/B);
for(int i=l;i/B==l/B&&i<=r;++i)
vv[i]=vv[i]&t;
if(l/B!=r/B) {
for(int i=r;i/B==r/B&&i>=l;--i)
vv[i]=vv[i]&t;
}
for(int b=l/B+1;b<r/B;++b) {
be[b][en[b]++]=make_pair(0,t);
if(en[b]>120000) pd(b);
}
}
void add_qry(int l,int r,rg q,int s) {
if(l>r) return;
s%=MOD;
if(!s) return;
cout<<"qry"<<l<<','<<r<<" "<<q.fi<<"~"<<q.se<<"}|"<<ss<<"\n";
pd(l/B); pd(r/B);
for(int i=l;i<=r;++i)
cout<<vv[i].fi<<","<<vv[i].se<<"|";
cout<<"\n";
ll sb=0;
for(int i=l;i/B==l/B&&i<=r;++i) (sb+=len(vv[i]&q)*le[i])%=MOD;
if(l/B!=r/B) {
for(int i=r;i/B==r/B&&i>=l;--i) (sb+=len(vv[i]&q)*le[i])%=MOD;
}
cout<<sb<<"|"<<s<<"\n";
(ss+=sb*s)%=MOD;
for(int b=l/B+1;b<r/B;++b) {
be[b][en[b]++]=make_pair(s,q);
if(en[b]>120000) pd(b);
}
cout<<"rst"<<ss<<"\n";
}
int rr[SZ],rl[SZ];
ll work() {
xn=0;
for(int i=1;i<=n;++i)
xs[++xn]=r[i].x.fi,//,xs[++xn]=r[i].x.fi+1,
//xs[++xn]=r[i].x.se,
xs[++xn]=r[i].x.se+1;
sort(xs+1,xs+1+xn);
xn=unique(xs+1,xs+1+xn)-xs-1;
for(int i=0;i<=xn+1;++i)
qz[i]=hz[i]=qzy[i]=hzy[i]=rg(1,lim),
qs[i].clear();
for(int i=1;i<=n;++i) {
int s=FD(r[i].x.se+1);
rr[i]=s;
qs[s].pb(i);
qz[s]=qz[s]+r[i].x;
qzy[s]=qzy[s]+r[i].y;
s=FD(r[i].x.fi);
rl[i]=s;
hz[s]=hz[s]+r[i].x;
hzy[s]=hzy[s]+r[i].y;
}
for(int i=1;i<=xn+1;++i)
qz[i]=qz[i-1]&qz[i],
qzy[i]=qzy[i-1]&qzy[i];
for(int i=xn;i>=0;--i)
hz[i]=hz[i+1]&hz[i],
hzy[i]=hzy[i+1]&hzy[i];
ss=0;
for(int i=0;i<=xn+B;++i) le[i]=0;
for(int i=1;i<xn;++i) {
int L=max(xs[i],1),
R=min(xs[i+1]-1,lim);
if(L>R) continue;
le[i]=R-L+1;
auto lx=qz[i],rx=hz[i+1];
{
auto l=lx&rg(1,L-1);
auto r=rx&rg(R+1,lim);
ss+=len(l)*len(r)%MOD*(R-L+1)%MOD;
}
{
auto l=lx&rg(L,R);
auto r=rx&rg(R+1,lim);
ss+=C2(len(l))*len(r)%MOD;
}
{
auto l=lx&rg(1,L-1);
auto r=rx&rg(L,R);
ss+=C2(len(r))*len(l)%MOD;
}
{
auto l=lx&rx&rg(L,R);
ss+=C3(len(l))%MOD;
}
ss+=C2(R-L+1)*len(qzy[i]&hzy[i+1])%MOD;
ss%=MOD;
}
for(int i=0;i<=xn/B;++i) {
for(int j=i*B;j<i*B+B;++j) vv[j]=rg(1,lim);
en[i]=0;
}
for(int i=0;i<xn;++i)
cout<<i<<":"<<le[i]<<"["<<xs[i]<<","<<xs[i+1]<<")\n";
for(int i=1;i<xn;++i) {
int L=max(xs[i],1),
R=min(xs[i+1]-1,lim);
cout<<"CONS"<<L<<"~"<<R<<"[]"<<ss<<"\n";
// insert ones in qs[i]
for(auto g:qs[i]) {
edit(0,rl[g]-1,r[g].y);
edit(rr[g],xn,r[g].y);
}
if(L>R) continue;
add_qry(1,i-1,hzy[i+1],R-L+1);
}
for(int i=0;i<=xn/B;++i) pd(i);
cout<<"result:"<<ss<<"\n";
return ss;
}
void sol() {
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d%d%d%d",&r[i].x.fi,&r[i].y.fi,&r[i].x.se,&r[i].y.se);
ll ans=work();
for(int i=1;i<=n;++i)
swap(r[i].x,r[i].y);
ans+=work();
ans=(ans%MOD+MOD)%MOD;
printf("%d\n",(int)(ans));
}
signed main() {
cerr<<sizeof(be)/1024.0/1024.0<<"M\n";
int T;
assert(bk6*6%MOD==1);
scanf("%d",&T);
while(T--) sol();
}
/*
3
1
1 1 1000000000 1000000000
3
1 1 2 2
3 3 4 4
5 5 6 6
5
581574116 47617804 999010750 826131769
223840663 366320907 613364068 926991396
267630832 51913575 488301124 223957497
217461197 492085159 999485867 913732845
28144453 603781668 912516656 993160442
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Output Limit Exceeded
input:
3 1 1 1 1000000000 1000000000 3 1 1 2 2 3 3 4 4 5 5 6 6 5 581574116 47617804 999010750 826131769 223840663 366320907 613364068 926991396 267630832 51913575 488301124 223957497 217461197 492085159 999485867 913732845 28144453 603781668 912516656 993160442
output:
0:0[0,1) 1:1000000000[1,1000000001) CONS1~1000000000[]115308150 result:115308150 0:0[0,1) 1:1000000000[1,1000000001) CONS1~1000000000[]115308150 result:115308150 230616300 0:0[0,1) 1:2[1,3) 2:2[3,5) 3:2[5,7) CONS1~2[]8 CONS3~4[]8 edt0~0 1,2 edt2~4 1,2 qry1,1 5~6}|8 1,1000000000| 4|2 rst16 CONS5~6[]1...