QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#404167#6304. Rectangle275307894aWA 3ms21208kbC++146.0kb2024-05-03 14:44:092024-05-03 14:44:10

Judging History

你现在查看的是最新测评结果

  • [2024-05-03 14:44:10]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:21208kb
  • [2024-05-03 14:44:09]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=2e5+5,M=N*4+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-8;const int INF=1e9+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
	Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
	Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
	#ifdef LOCAL
	#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
	#else 
	#define gdb(...) void()
	#endif
}using namespace Debug;
int A,n,sx[N],sy[N],tx[N],ty[N],nx[N],xh,ny[N],yh;
void build(int *s,int *t,int *ns,int &nh){
	ns[nh=1]=1;ns[++nh]=A+1;
	for(int i=1;i<=n;i++) ns[++nh]=s[i],ns[++nh]=t[i]+1;
	sort(ns+1,ns+nh+1);nh=unique(ns+1,ns+nh+1)-ns-1;
	for(int i=1;i<=n;i++) s[i]=LB(ns+1,ns+nh+1,s[i])-ns,t[i]=LB(ns+1,ns+nh+1,t[i]+1)-ns;
}
void chkmin(int &x,int y){if(x>y) x=y;}
void chkmax(int &x,int y){if(x<y) x=y;}
ll qrys(int x,int y){return 1ll*(x+y)*(y-x+1)/2%mod;}
namespace Solve1{
	int lx[N],ly[N],rx[N],ry[N];
	const int i6=(mod+1)/6;
	ll qry(int x){
		ll tot=1ll*(x+1)*(mod-A)%mod+(A+1)*qrys(0,x)%mod;
		tot-=1ll*x*(x+1)%mod*(2*x+1)%mod*i6%mod;
		return tot%mod;
	}
	ll calc(){
		fill(lx,lx+xh+2,-INF);fill(rx,rx+xh+2,INF);
		for(int i=1;i<=n;i++) chkmin(rx[tx[i]],tx[i]),chkmax(lx[tx[i]],sx[i]);
		for(int i=1;i<=xh;i++) chkmax(lx[i],lx[i-1]),chkmin(rx[i],rx[i-1]);
		fill(ly,ly+xh+2,-INF);fill(ry,ry+xh+2,INF);
		for(int i=1;i<=n;i++) chkmin(ry[sx[i]],tx[i]),chkmax(ly[sx[i]],sx[i]);
		for(int i=xh;i;i--) chkmax(ly[i],ly[i+1]),chkmin(ry[i],ry[i+1]);
		ll ans=0;
		for(int i=1;i<xh;i++){
			if(lx[i]>rx[i]||ly[i+1]>ry[i+1]) continue;
			if(rx[i]<=i){
				if(ly[i+1]>=i+1) ans+=1ll*(nx[rx[i]]-nx[lx[i]])*(nx[ry[i+1]]-nx[ly[i+1]])%mod*(nx[i+1]-nx[i])%mod;
				else ans+=qrys(A-nx[i+1]+1,A-nx[i])*(nx[rx[i]]-nx[lx[i]])%mod;
			} else{
				if(ly[i+1]>=i+1) ans+=qrys(nx[i]-1,nx[i+1]-2)*(nx[ry[i+1]]-nx[ly[i+1]])%mod;
				else ans+=qry(nx[i+1]-1)-qry(nx[i]-1);
			}
		}
		return ans%mod;
	}
}
int chkin(int l,int r,int x){return l<=x&&x<=r;}
struct mySet{
	priority_queue<int> q1,q2;
	void emplace(int x){q1.emplace(x);}
	void erase(int x){
		q2.emplace(x);
		while(!q1.empty()&&!q2.empty()&&q2.top()==q1.top()) q1.pop(),q2.pop();
	}
	int top(){return q1.top();}
	int empty(){return q1.empty();}
	void clear(){
		priority_queue<int> ().swap(q1);
		priority_queue<int> ().swap(q2);
	}
};
mySet f[N],f1,f2;
vector<tuple<int,int,int> > S[N]; 
int ry[N];
namespace Tree{
	#define ls v<<1
	#define rs v<<1|1
	int mi[M];
	ll sum[M],g[M];
	ll ask(int x,int l,int r,int v){
		if(x<=mi[v]) return 1ll*(ny[r+1]-ny[l])*ny[min(x,yh+1)];
		if(l==r) return 1ll*(ny[r+1]-ny[r])*ny[min(x,yh+1)];
		int m=l+r>>1;
		if(mi[rs]<=x) return g[v]+ask(x,m+1,r,rs);
		return 1ll*(ny[r+1]-ny[m+1])*ny[min(x,yh+1)]+ask(x,l,m,ls);
	}
	void up(int v,int l,int r){
		mi[v]=min(mi[ls],mi[rs]);
		int m=l+r>>1;
		g[v]=ask(mi[rs],l,m,ls);
	}
	void build(int l=1,int r=yh+1,int v=1){
		mi[v]=INF;if(l==r) return;
		int m=l+r>>1;build(l,m,ls);build(m+1,r,rs);up(v,l,r);
	}
	void modify(int x,int l=1,int r=yh+1,int v=1){
		if(l==r){mi[v]=(f[l].empty()?INF:-f[l].top());return;}
		int m=l+r>>1;x<=m?modify(x,l,m,ls):modify(x,m+1,r,rs);up(v,l,r);
	}
	int qry(int x,int y,int l=1,int r=yh+1,int v=1){
		if(x<=l&&r<=y) return mi[v];int m=l+r>>1;
		return min(x<=m?qry(x,y,l,m,ls):INF,y>m?qry(x,y,m+1,r,rs):INF);
	}
	ll find(int x,int y,int &w,int l=1,int r=yh+1,int v=1){
		if(x<=l&&r<=y){
			ll p=ask(w,l,r,v);
			w=min(w,mi[v]);return p;
		}
		int m=l+r>>1;
		ll tot=(y>m?find(x,y,w,m+1,r,rs):0);
		tot+=(x<=m?find(x,y,w,l,m,ls):0);
		return tot;
	}
	int findmin(int x,int l=1,int r=yh+1,int v=1){
		if(l==r) return mi[v]>=x?l:l+1;int m=l+r>>1;
		return mi[rs]>=x?findmin(x,l,m,ls):findmin(x,m+1,r,rs);
	}
	#undef ls
	#undef rs
}
void add(int l,int r){
	f1.emplace(l);f2.emplace(-r);
	f[l].emplace(-r);Tree::modify(l);
}
void del(int l,int r){
	f1.erase(l);f2.erase(-r);
	f[l].erase(-r);Tree::modify(l);
}
ll calc(){
	f1.clear();f2.clear();
	for(int i=1;i<=yh;i++) f[i].clear();
	for(int i=1;i<=xh;i++) S[i].clear();
	Tree::build();
	for(int i=1;i<=n;i++){
		add(sy[i],ty[i]);
		S[sx[i]].emplace_back(sy[i],ty[i],-1);
		S[tx[i]].emplace_back(sy[i],ty[i],1);
	}
	ll ans=0;
	for(int i=1;i<xh;i++){
		for(auto j:S[i]){
			if(get<2>(j)==1) add(get<0>(j),get<1>(j));
			else del(get<0>(j),get<1>(j));
		}
		int mi=(f2.empty()?yh:-f2.top());
		int mx=(f1.empty()?-INF:f1.top());
		ll tot=0;
		// int l=0,r=mi,mid;while(l+1<r) mid=l+r>>1,(mx<=Tree::qry(mid+1,yh+1)?r:l)=mid;
		int liml=max(Tree::findmin(mx)-1,1),limr=mi-1;
		if(liml>limr) continue;
		mx=max(mx,liml);
		if(limr>=mx){
			tot+=qrys(A-ny[limr+1]+1,A-ny[mx]);
			limr=mx-1;
		}
		if(liml<=limr){
			tot+=1ll*(ny[limr+1]-ny[liml])*(-ny[mx])%mod;
			int mi=Tree::qry(limr+1,yh+1);
			// gdb(liml,limr);
			tot+=Tree::find(liml,limr,mi);
			/*for(int j=liml;j<=limr;j++){
				tot+=1ll*(ny[j+1]-ny[j])*ny[Tree::qry(j+1,yh+1)]%mod;
			}*/
		}
		ans+=tot%mod*(nx[i+1]-nx[i])%mod;
	}
	ans+=Solve1::calc();
	return ans%mod;
}
void Solve(){
	int i,j;scanf("%d",&n);
	for(i=1;i<=n;i++) scanf("%d%d%d%d",&sx[i],&sy[i],&tx[i],&ty[i]);
	build(sx,tx,nx,xh);build(sy,ty,ny,yh);
	ll ans=calc();
	swap_ranges(sx+1,sx+n+1,sy+1);swap_ranges(tx+1,tx+n+1,ty+1);swap_ranges(nx+1,nx+max(xh,yh)+1,ny+1);swap(xh,yh);
	ans+=calc();
	printf("%lld\n",(ans%mod+mod)%mod);
}
int main(){
	int t=1;
	scanf("%d%d",&t);A=1e9;
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 21208kb

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:

130806987
695209911
729720702

result:

wrong answer 1st numbers differ - expected: '230616300', found: '130806987'