QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#367428#5144. Set of IntervalsBaiyu0123WA 3ms9880kbC++142.5kb2024-03-25 22:38:402024-03-25 22:38:41

Judging History

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

  • [2024-03-25 22:38:41]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:9880kb
  • [2024-03-25 22:38:40]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second 
#define pii pair<int,int>
#define mkp make_pair
using namespace std;
const int maxn=2e5+100;
int n,sum=0;
pii p[maxn],Q[maxn];
int G[maxn],a[maxn],h[maxn],z[maxn];
set<int> s;
int cnt[maxn],w[maxn];
ll ans=0;
int main() {
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int T;cin>>T;
	while (T--) {
		cin>>n;ans=0;sum=0;
		for (int i=1;i<=n;i++) {
			cin>>p[i].fi>>p[i].se;
			G[2*i-1]=p[i].fi-1;
			G[2*i]=p[i].se;
		}
		if (n==1) {
			cout<<1<<endl;
			continue;
		}
		sort(G+1,G+2*n+1);
		int Gw=unique(G+1,G+2*n+1)-G-1;
		for (int i=2;i<=Gw;i++) a[i]=G[i]-G[i-1];
		for (int i=1;i<=n;i++) {
			p[i].fi=lower_bound(G+1,G+Gw+1,p[i].fi-1)-G+1;
			p[i].se=lower_bound(G+1,G+Gw+1,p[i].se)-G;
			Q[2*i-1]=mkp(p[i].fi,+i);
			Q[2*i]=mkp(p[i].se+1,-i);
		}
		sort(Q+1,Q+2*n+1);
		assert(Q[1].fi==2);
		for (int i=2,j=0;i<=Gw+1;i++) {
			z[i-1]=z[i-2];
			while (j+1<=2*n&&Q[j+1].fi==i) {
				j++;
				if (Q[j].se>0)s.insert(Q[j].se);
				else z[i-1]++,s.erase(-Q[j].se);
			}
			cnt[i]=s.size();
			if (cnt[i]==1) w[i]=*s.begin(),h[w[i]]+=a[i];
			if (cnt[i]!=0) sum+=a[i];
		}
		for (int i=1;i<=n;i++) {
			if (h[i]!=0) {
				int L=z[p[i].fi-1],R=n-z[p[i].se];
				ans+=1ll*h[i]*(sum-1);
				if (p[i].fi==p[i].se) {
					if (L==0||R==0) ans-=1ll*h[i]*(h[i]-1);
					continue;
				}
				if (cnt[p[i].fi]==1&&L==0) ans-=1ll*a[p[i].fi]*(a[p[i].fi]-1);
				if (cnt[p[i].se]==1&&R==0) ans-=1ll*a[p[i].se]*(a[p[i].se]-1);
				if (cnt[p[i].fi]==1&&L==0&&cnt[p[i].se]==1&&R==0) 
					ans-=2ll*a[p[i].fi]*a[p[i].se];
			}
		}
		//cout<<ans<<endl;
		s.clear();
		for (int i=2;i<=Gw;i++) {
		//	cout<<i-1<<" : "<<ans<<endl;
			if (cnt[i]==0) {
				if (n==2) continue;
				int L=z[i],R=n-z[i];
				assert(L!=0&&R!=0);
				if (L>=2&&R>=2) {
					ans+=1ll*a[i]*sum;
					ans+=1ll*a[i]*(G[Gw]-G[1]-1);
				} else {
					if (L==1&&R==2) {
						ans+=2ll*a[i]*(sum-(G[i-1]-G[1]));
					}
					if (L==1&&R>2) {
						ans+=1ll*a[i]*(sum-(G[i-1]-G[1]));
						ans+=1ll*a[i]*(G[Gw]-G[i]);
					}
					if (R==1&&L==2) {
						ans+=2ll*a[i]*(sum-(G[Gw]-G[i]));
					}
					if (R==1&&L>2) {
						ans+=1ll*a[i]*(sum-(G[Gw]-G[i]));
						ans+=1ll*a[i]*(G[i-1]-G[1]);
					}
				}
				continue;
			}
			if (cnt[i]==2) {
				ans+=1ll*a[i]*(sum-1);
				continue;
			}
		}
		for (int i=0;i<=max(n,Gw)+3;i++) {
			h[i]=w[i]=G[i]=z[i]=a[i]=0;
		//	cout<<"["<<G[i-1]+1<<","<<G[i]<<"] "<<cnt[i]<<" "<<w[i]<<endl;
		}
		cout<<ans/2<<endl;
	}
	
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9880kb

input:

4
1
1 1000000000
2
1 1000000000
1 1000000000
4
1 2
3 4
5 6
7 8
4
1 3
2 4
5 8
6 7

output:

1
499999999500000000
26
28

result:

ok 4 number(s): "1 499999999500000000 26 28"

Test #2:

score: 0
Accepted
time: 0ms
memory: 9764kb

input:

10000
1
778216842 910688403
1
513404058 890988011
1
1 1000000000
1
1 1000000000
1
758932694 848837772
1
516433381 715411928
1
1 1000000000
1
1 1000000000
1
1 1000000000
1
1 1000000000
1
1 1000000000
1
652548522 898071173
1
1 1000000000
1
509357508 603420032
1
1 1000000000
1
657294869 887475066
1
1 1...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 10000 numbers

Test #3:

score: 0
Accepted
time: 3ms
memory: 7772kb

input:

10000
2
427286995 863604876
582970459 874563920
2
181948005 565025282
799528580 848659925
2
1 1000000000
716032287 836380611
2
383809946 544540272
520881396 990456979
2
156308569 178412791
731100211 963724967
2
426113388 802990296
556666621 560014605
2
1 1000000000
575838571 811122140
2
255734272 64...

output:

87849603321470913
18821102290156188
113106465274673025
75195165925255965
5141989504048811
1256173734724260
207604390726385765
56483863859672889
37024984393194673
36132096841419954
22573394495301601
143688903920213372
166852999281224709
3932968549941246
63833225117266922
49459548807361176
70057738855...

result:

ok 10000 numbers

Test #4:

score: -100
Wrong Answer
time: 3ms
memory: 9824kb

input:

10000
3
1 1000000000
521067549 666980062
562319713 714009993
3
42407212 75107815
610219669 973789020
873806856 902158965
3
1 1000000000
673648508 809445815
599994567 757240668
3
92939611 410549965
597447135 678622426
676819188 811525072
3
230740729 455081734
606008912 926990742
615223886 822068791
3...

output:

121998876414569940
269965111370246655
145720255583240455
122087055515083130
166464628402586213
198709695905927184
170198469294305433
209062603672526017
59827455776692524
211828544173036039
134334952548458322
188645471237049582
186982464464105447
274043263510447662
200274422590053105
2387307446491590...

result:

wrong answer 1st numbers differ - expected: '174329051362239765', found: '121998876414569940'