QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#93837#6192. Interval ProblemKlaus26AC ✓650ms48796kbC++144.9kb2023-04-03 05:42:042023-04-03 05:42:05

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-03 05:42:05]
  • 评测
  • 测评结果:AC
  • 用时:650ms
  • 内存:48796kb
  • [2023-04-03 05:42:04]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "../debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template <class T> struct Stree {
	int n; T neut = -1e9;
	vector <T> tree;
	Stree(int n) : n(n) { tree.assign((n + 1) << 1, neut); }
	T merge(T &a, T &b) { return max(a, b); }
	void update(int k, T x) {
		k += n;
		tree[k] = merge(tree[k], x);
		for (k >>= 1; k; k >>= 1) tree[k] = merge(tree[k << 1], tree[k << 1 | 1]);
	}
	T query(int l, int r) {
		l += n, r += n;
		T ans = neut;
		while (l <= r) {
			if (l & 1) ans = merge(ans, tree[l++]);
			if (!(r & 1)) ans = merge(ans, tree[r--]);
			l >>= 1, r >>= 1;
		}
		return ans;
	}
};

template <class T> struct Stree2 {
	int n; T neut = 1e9;
	vector <T> tree;
	Stree2(int n) : n(n) { tree.assign((n + 1) << 1, neut); }
	T merge(T &a, T &b) { return min(a, b); }
	void update(int k, T x) {
		k += n;
		tree[k] = merge(tree[k], x);
		for (k >>= 1; k; k >>= 1) tree[k] = merge(tree[k << 1], tree[k << 1 | 1]);
	}
	T query(int l, int r) {
		l += n, r += n;
		T ans = neut;
		while (l <= r) {
			if (l & 1) ans = merge(ans, tree[l++]);
			if (!(r & 1)) ans = merge(ans, tree[r--]);
			l >>= 1, r >>= 1;
		}
		return ans;
	}
};

struct Fen{
	int n;
	vector<long long> tree;
	Fen(int n): n(n){
		tree.assign(n+10,0);
	}
	void upd(int i, int v){
		while(i<=n){
			tree[i] += v;
			i += i&-i;
		}
	}
	long long get(int i){
		long long ans = 0;
		while(i>=1){
			ans += tree[i];
			i -= i&-i;
		}
		return ans;
	}
	long long get(int l, int r){
		return get(r) - get(l-1);
	}
};



int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin>>n;
	map<pair<int,int>,int> idx;
	vector<int> a(2*n);
	vector<int> cop(2*n+10);
	for(int i=0; i<n; i++){
		int l, r;
		cin>>l>>r;
		idx[make_pair(l,r)] = i;
		a[2*i] = l;
		a[2*i+1] = r;
		cop[l] = r;
		cop[r] = l;
	}
	sort(a.begin(),a.end());
	vector<long long> ans(n);
	Fen cnt(2*n+10);
	for(int i=0; i<int(a.size()); i++){
		int l = a[i];
		int r = cop[a[i]];
		if(l < r){
			int ix = idx[make_pair(l,r)];
			ans[ix] += cnt.get(r+1,2*n+2);
		}
		cnt.upd(r,1);
	}
	debug(ans);
	vector<long long> betw(2*n);
	for(int i=0; i<2*n; i++){
		int l = a[i];
		int r = cop[a[i]];
		if(l < r){
			betw[i] = 1;;
		}
		if(i) betw[i] += betw[i-1];
	}
	for(int i=0; i<2*n; i++){
		int l = a[i];
		int r = cop[a[i]];
		if(l < r){
			int j = lower_bound(a.begin(),a.end(),r) - a.begin();
			int ix = idx[make_pair(l,r)];
			ans[ix] += betw[j] - betw[i];
		}
	}
	Stree<int> mx(2*n);
	for(int i=0; i<2*n; i++){
		int l = a[i];
		int r = cop[a[i]];
		if(l < r){
			mx.update(i,r);
		}
	}
	vector<pair<long long,long long>> dp(2*n,make_pair(-1,-1));
	function<pair<long long,long long>(int)> go = [&](int i){
		pair<long long, long long>& ans = dp[i];
		if(ans != make_pair(-1ll,-1ll)) return ans;
		int l = 0;
		int r = i;
		int nxtv = mx.query(l,r);
		int j = lower_bound(a.begin(),a.end(),nxtv)-a.begin();
		if( j == r ){
			return ans = make_pair(0,0);
		}else{
			long long add = betw[j] - (i-1>=0?betw[i-1] : 0);
			ans = make_pair(go(j).first + add, go(j).second);
			ans.second += ans.first;
		}
		return ans;
	};
	debug(ans);
	debug(betw);
	for(int i=0; i<2*n; i++){
		int l = a[i];
		int r = cop[a[i]];
		if( l > r){
			swap(l,r);
			int ix = idx[make_pair(l,r)];
			ans[ix]  += go(i).second + go(i).first;
		}
	}
	debug(ans);
	Stree2<int> mn(2*n);
	for(int i=0; i<2*n; i++){
		int r = a[i];
		int l = cop[a[i]];
		if(r > l){
			mn.update(i,l);
		}
	}
	for(int i=0; i<2*n; i++){
		betw[i] = 0;
		int l = a[i];
		int r = cop[a[i]];
		if(!(l < r)){
			betw[i] = 1;;
		}
		if(i) betw[i] += betw[i-1];
	}
	vector<pair<long long, long long>> dp2(2*n, make_pair(-1,-1));
	function<pair<long long, long long>(int)> go2 = [&](int i){
		pair<long long, long long>& ans = dp2[i];
		if(ans != make_pair(-1ll,-1ll)) return ans;
		int l = i;
		int r = 2*n;
		int nxtv = mn.query(i,r);
		int j = lower_bound(a.begin(),a.end(),nxtv)-a.begin();
		if(j == l){
			return ans = make_pair(0,0);
		}else{
			long long add = betw[i] - (j-1>=0 ? betw[j-1] : 0);
			ans = make_pair(go2(j).first + add, go2(j).second);
			ans.second += ans.first;
		}
		return ans;
	};
	debug(ans);
	for(int i=0; i<2*n; i++){
		int l = a[i];
		int r = cop[a[i]];
		if(l < r){
			int ix = idx[make_pair(l,r)];
			ans[ix] += go2(i).second + go2(i).first;
		}
	}
	debug(ans);
	Fen fk(2*n);
	for(int i = 2*n-1; i>=0; i--){
		int l = a[i];
		int r = cop[a[i]];
		if( l < r){
			int ix = idx[make_pair(l,r)];
			ans[ix] += fk.get(0,l-1);
		}else{
			swap(l,r);
			int ix = idx[make_pair(l,r)];
			ans[ix] -= fk.get(0,l-1);
			fk.upd(l,1);
		}
	}
	for(auto& i : ans){
		cout<<i<<' ';
	}
	cout<<'\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3480kb

input:

5
2 3
6 7
1 9
5 10
4 8

output:

7 5 4 5 5 

result:

ok 5 number(s): "7 5 4 5 5"

Test #2:

score: 0
Accepted
time: 484ms
memory: 48392kb

input:

200000
342504 342505
248314 248317
259328 259334
234817 234821
225726 225732
371424 371425
260562 260565
34752 34753
132098 132100
262130 262134
7254 7255
228491 228492
43488 43492
188408 188410
11691 11692
350334 350335
327350 327352
360684 360685
182051 182052
72131 72135
194666 194668
61303 61313...

output:

12 21 17 6 16 33 2 1 1 19 6 0 25 19 7 11 2 20 14 8 2 11 0 1 0 2 38 35 28 6 4 0 23 22 17 10 26 10 5 9 1 16 14 24 7 1 2 40 18 18 19 13 4 26 2 28 0 0 39 19 0 37 4 13 18 19 23 14 2 4 0 40 10 0 1 6 52 7 1 18 3 6 1 40 7 24 16 13 31 43 4 10 23 20 1 0 4 4 0 1 8 3 1 17 37 14 43 5 17 32 60 0 0 0 28 0 18 16 7 ...

result:

ok 200000 numbers

Test #3:

score: 0
Accepted
time: 483ms
memory: 48476kb

input:

200000
26851 26856
258158 258160
332869 332878
28073 28074
65883 65898
181844 181847
162628 162633
293275 293276
107389 107398
302056 302072
95036 95038
311987 311988
279551 279552
86 87
161331 161332
3744 3747
231020 231024
42346 42352
37146 37153
294389 294406
265867 265873
246877 246881
197614 19...

output:

64 88 81 99 668 386 308 28 42 250 22 139 3979 331 623 312 1530 770 469 206 284 259 767 1503 221 130 686 331 98 232 865 162 1649 43 130 1144 94 413 889 1861 211 762 28 836 116 162 548 225 86 1561 319 617 4127 474 96 50 1580 1558 1435 86 465 1700 15 2 99 103 136 723 1510 12 1400 223 1271 37 61 597 27 ...

result:

ok 200000 numbers

Test #4:

score: 0
Accepted
time: 487ms
memory: 48544kb

input:

200000
337751 337758
110061 110077
324990 324993
352241 352247
325108 325115
134703 134717
25943 25962
121564 121573
42982 42983
248049 248087
60651 60657
332816 332828
246046 246055
304157 304158
392634 392640
111165 111171
247498 247513
86504 86506
185219 185235
69067 69083
251182 251183
279617 27...

output:

16419 361082 40617 418545 43244 82277 11271 258885 34898 614083 42296 147399 500813 314012 2834 277734 562298 80980 69570 394765 218205 1044173 15936 326857 97086 3675 156364 343907 125715 937181 353740 544767 183548 441107 59345 82190 185443 72848 247562 273053 495863 86049 444566 136148 356787 152...

result:

ok 200000 numbers

Test #5:

score: 0
Accepted
time: 492ms
memory: 48796kb

input:

200000
127219 127280
223345 223361
152880 152884
40178 40179
7991 8005
266310 266319
332397 332422
36392 36426
75573 75574
60879 60884
95868 95913
217855 217886
131673 131681
256534 256554
223529 223530
232350 232375
120809 120827
266098 266114
25711 25751
229072 229087
272840 272879
92506 92550
773...

output:

379221319 339344076 353506130 547752191 642034502 371778846 481200270 557918532 464406514 497231703 425782610 337490404 374130659 361866758 339437880 343683647 386790159 371580284 587088809 341994919 379226219 432022481 460538970 435287365 347076374 583445677 337619137 435205438 397389207 340450981 ...

result:

ok 200000 numbers

Test #6:

score: 0
Accepted
time: 508ms
memory: 48384kb

input:

200000
171936 172110
4591 4626
108508 108553
226874 226900
90791 90850
238982 238992
243153 243174
240390 240497
347627 347662
228287 228289
170781 170934
51097 51352
139750 139808
94804 94815
442 448
297474 297475
244036 244044
356100 356114
245963 245994
142149 142166
246792 246800
19022 19108
134...

output:

106189288 204472003 125920212 106000251 135642421 108056086 108997814 108214268 160512585 106193968 106360793 162247947 113429983 133438779 208813035 128560690 109094447 167473840 109589801 112792068 109898387 190365960 195890606 105339565 194960387 104526312 173066763 182424123 188124166 156349884 ...

result:

ok 200000 numbers

Test #7:

score: 0
Accepted
time: 488ms
memory: 48472kb

input:

200000
67879 67940
10416 10465
327752 327767
135984 135994
73978 74069
194780 195094
391551 391561
105509 105572
396552 396608
49997 50157
368719 368738
208681 208769
216212 216310
46486 46539
194253 194539
25014 25049
234319 234521
159508 159600
392891 393243
295417 295887
220115 220217
108196 1082...

output:

64734580 85174386 62791748 49363982 62670510 44740694 85882512 54835645 88015994 70235070 76674604 44894964 45118612 71297646 44847466 79519941 46079850 46733016 86455586 54596042 45354246 54277341 71781887 46735752 59856019 58595450 60747185 45149383 65402310 83057043 47809279 45603991 70890939 560...

result:

ok 200000 numbers

Test #8:

score: 0
Accepted
time: 476ms
memory: 48384kb

input:

200000
286770 286880
206693 206740
224293 224808
127509 127679
305483 305814
292326 292854
226829 226949
16092 16146
382375 383028
139098 139136
42306 42462
85808 85960
391657 391743
75339 75656
374612 374790
324858 325163
126952 127012
93957 94097
133574 133760
326872 326927
95947 96935
101356 1014...

output:

23436316 19895692 20084548 22447756 25407740 23973874 20137342 35924514 36421534 21647005 31664586 25936117 38296098 27283764 35181820 27792866 22385314 25166580 21898086 28045156 24849932 24493238 22857265 22519996 37724107 25514267 20496978 20449635 27284206 37166496 20312154 25408786 26557231 364...

result:

ok 200000 numbers

Test #9:

score: 0
Accepted
time: 484ms
memory: 48380kb

input:

200000
380316 380382
211990 212332
98342 98497
273741 274028
310348 310373
15880 16509
377455 377930
30217 30927
327310 327466
34478 35491
381454 382262
133424 133425
338230 338939
201253 201418
315547 316660
129756 129908
8865 8967
290213 290918
200810 201836
363765 364427
266183 267070
150404 1505...

output:

12162580 6900400 8749178 7780778 9122073 12431434 11974806 11726400 9719653 11557873 12154465 7729331 10087943 6877024 9233382 7736819 12994846 8272476 6874728 11469762 7572458 7377010 8961745 8556622 11978523 9234003 6777657 9352548 9264121 12714264 11147516 12713648 10666576 12615729 7939879 68755...

result:

ok 200000 numbers

Test #10:

score: 0
Accepted
time: 507ms
memory: 48420kb

input:

200000
94172 94549
378164 379641
98214 99014
32105 32115
161647 162954
150359 150544
310360 310419
354635 355536
165664 167834
82402 84493
325499 325550
307332 307497
332558 333000
38917 39174
346142 350743
91220 91402
214569 215415
177931 178010
197235 197762
68559 69705
376546 380294
127708 129140...

output:

4111828 5416403 4015502 5365302 3311594 3420049 4124495 4922006 3376096 4231851 4366457 4003214 4336910 5039206 4751726 4121666 3211927 3322124 3283548 4463047 5415273 3579780 4496188 3420213 3526465 3832293 4886321 4737354 5788810 4770208 3641455 3377048 3352478 4922204 3321986 3916692 4359401 5535...

result:

ok 200000 numbers

Test #11:

score: 0
Accepted
time: 523ms
memory: 48380kb

input:

200000
1072 3118
40071 43871
127286 133342
225846 229162
107755 108681
126692 126984
282893 289606
371786 372185
323077 326187
115603 116808
371356 373473
297233 297757
69577 69663
115940 116991
222525 223825
258843 260753
116418 117598
364675 364863
390718 392788
176166 182649
309256 312248
231780 ...

output:

2825812 2456257 1722533 1619155 1938198 1780319 1839986 2651546 2095849 1802614 2642653 1969729 2171015 1802669 1667585 1734749 1802593 2480030 2829784 1583490 1975708 1591427 1975008 1881362 2160968 2037564 2192449 2022100 1932643 1975777 2464278 2455552 1661653 1802541 1976558 1846501 1797272 2463...

result:

ok 200000 numbers

Test #12:

score: 0
Accepted
time: 513ms
memory: 48204kb

input:

200000
172930 174742
27463 29558
281470 282104
378322 381194
289478 290375
293078 317404
324947 325056
105107 111280
254153 254706
32188 37595
318055 321344
112598 118650
269551 270153
343739 345290
266354 271907
363821 369134
383270 389836
251225 265879
117980 126149
4042 14067
10229 11087
116790 1...

output:

741968 1125299 873370 1176638 867970 833651 980787 856194 800813 1116499 883021 856239 806684 1015473 791463 1119436 1180387 792430 823849 1149950 1154489 747812 973592 1113694 888796 974015 974926 739982 858574 774214 935522 822716 887233 740193 739779 1109462 1114910 1010604 872778 796867 1174921 ...

result:

ok 200000 numbers

Test #13:

score: 0
Accepted
time: 545ms
memory: 48188kb

input:

200000
396480 397307
35648 37415
256941 267156
135654 139714
126330 133123
237210 241635
9328 12129
138460 145503
292776 294347
51995 53415
327655 329591
367790 368757
41948 58528
12606 56450
224903 237674
47356 51357
177950 193567
16051 34504
25665 27391
392399 392798
21152 40393
12230 38747
292065...

output:

757703 687976 530266 498832 566176 517902 728283 503183 592078 605230 603367 685120 597626 584013 513710 603917 513028 679635 724620 730945 652828 675589 574078 726230 528058 607213 530516 499502 592079 516952 522796 590211 500171 724355 453484 597863 580815 603151 522371 503484 531015 756377 497243...

result:

ok 200000 numbers

Test #14:

score: 0
Accepted
time: 579ms
memory: 48452kb

input:

200000
172883 218174
30687 56660
211338 242591
318600 323262
63226 97641
269157 284103
33990 43988
192237 196395
247512 254586
33563 38944
83379 117561
10925 12071
262370 266422
100154 111153
238382 280045
192961 220350
297213 396676
265697 302673
296937 327287
370554 390222
71247 100334
384661 3954...

output:

362419 407362 369316 412008 367790 377817 415298 382767 381560 417652 367851 436485 383164 379539 364214 371254 351223 366756 379800 413831 370459 427185 326780 371524 430064 381207 370442 417530 386226 375423 383241 411644 360743 379011 375728 413952 365596 404176 371097 420988 382996 413505 370805...

result:

ok 200000 numbers

Test #15:

score: 0
Accepted
time: 610ms
memory: 48404kb

input:

200000
103349 264305
178051 198663
74656 100424
322291 333624
62219 116581
21852 44028
251919 277878
53240 87351
245100 265874
11996 40044
212372 225176
73628 149628
142790 310770
169496 247976
50308 180800
138992 330125
289117 338022
274751 382694
80345 101657
2111 76369
33407 71723
63648 118011
18...

output:

271317 340356 338924 356175 326660 362113 340270 339117 342221 364406 344791 313916 270169 312019 291326 260668 335469 315720 340553 338288 344095 326412 301682 297778 360096 315745 385982 257349 258225 300578 330067 230799 309768 321414 322127 310177 297449 266039 328487 368521 338380 345179 341038...

result:

ok 200000 numbers

Test #16:

score: 0
Accepted
time: 638ms
memory: 48224kb

input:

200000
118304 262258
30805 129796
105610 215004
73169 305825
16186 104979
162259 187539
265974 385486
186957 352497
64058 269854
9282 141856
216553 269728
24364 78747
172872 286230
71411 360129
142836 348733
232833 282395
45897 112265
137827 213006
38139 171233
24943 118081
43184 311266
22521 112855...

output:

242013 292713 257381 218694 309268 290166 289419 247559 226922 283711 281043 329730 254674 208770 229742 286364 306217 268277 267600 300285 213136 303829 280878 282830 338182 279645 280853 291142 226678 298078 264998 309417 233019 224610 274302 344537 258601 271299 311937 238976 212577 274349 234122...

result:

ok 200000 numbers

Test #17:

score: 0
Accepted
time: 650ms
memory: 48384kb

input:

200000
201425 347551
71476 137629
140154 376332
116590 341032
210807 331824
245972 388020
5575 334517
163518 251275
138978 227516
201756 383479
8395 372874
56793 233450
294485 318492
146791 396767
164811 285272
287563 307750
145795 341549
179229 186820
176893 223994
2860 199806
270613 351304
113107 ...

output:

254146 292218 225197 221335 261420 275659 205466 260965 261245 251184 201018 238547 316664 226860 250393 313980 230771 296931 277857 250070 294536 248236 218502 319837 356166 238661 299729 218683 209705 332155 343658 308842 269694 287381 268875 278393 310296 245233 340731 291962 299710 275388 279549...

result:

ok 200000 numbers