QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#469407#8739. 古明地枣的袜子Rong7AC ✓5861ms53556kbC++143.9kb2024-07-09 18:52:352024-07-09 18:52:36

Judging History

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

  • [2024-07-09 18:52:36]
  • 评测
  • 测评结果:AC
  • 用时:5861ms
  • 内存:53556kb
  • [2024-07-09 18:52:35]
  • 提交

answer

// Not afraid to dark.

#include <bits/stdc++.h>
using namespace std;

#define inline __inline__ __attribute__ ((always_inline))

#define ll long long

namespace io {
	int pos;
	inline int read (int &p = pos){
		static int v; static char c;
		v = 1, c = getchar (), p = 0;
		while (! isdigit (c)){
			if (c == '-')
				v = - 1;
			c = getchar ();
		}
		while (isdigit (c)){
			p = (p << 1) + (p << 3) + (c - 48);
			c = getchar ();
		}
		return p *= v;
	}
	inline void write (ll x){
		if (x < 0)
			putchar ('-'), x = - x;
		static int sta[65], top;
		top = 0;
		do {
			sta[++ top] = x % 10;
			x /= 10;
		} while (x);
		while (top)
			putchar (sta[top --] + 48);
	}
};

inline void upd (ll &a, ll b){
	(a < b && (a = b));
}

const int N = 5e5, SQ = 750;
const ll inf = 0x3f3f3f3f3f3fll;
int n, m, B;
struct oper {
	int x, y, t;
} op[N + 5], ls[N + 5];
int ca[N + 5], cb[N + 5], gt[N + 5], y[N + 5];
struct query {
	int l, r, i;
} q[N + 5];
ll ans[N + 5], f[SQ + 5][SQ + 5], g[SQ + 5][SQ + 5], sm[N + 5];

int o;
int pr[N + 5], su[N + 5];
int pre[N + 5], s[N + 5];

void dac (int l, int r){
	if (l == r){
		if (ca[l])
			f[l - o][r - o] = op[l].y;
		return ;
	}
	int mid = (l + r) >> 1;
	dac (l, mid), dac (mid + 1, r);
//	for (int i = l;i <= r;++ i)
//		for (int j = i;j <= r;++ j)
//			printf ("(%d %d %lld)\n", i, j, f[i][j]);
//	puts ("");
	sm[mid] = 0;
	for (int i = mid + 1;i <= r;++ i)
		sm[i] = sm[i - 1] + op[i].y;
	pr[l - 1] = l - 1, su[l - 1] = mid;
	for (int i = l, x = l, y = mid + 1;i <= r;++ i){
		if (y > r || (x <= mid && op[x].t < op[y].t))
			ls[i] = op[x ++];
		else
			ls[i] = op[y ++];
		pr[i] = x - 1;
		su[i] = y - 1;
	}
	for (int i = l;i <= r;++ i)
		op[i] = ls[i];
	for (int i = l, j;i <= mid;++ i)
		for (j = i;j <= mid;++ j)
			g[i - o][j - o] = f[i - o][j - o];
	for (int i = mid + 1, j;i <= r;++ i)
		for (j = i;j <= r;++ j)
			g[i - o][j - o] = f[i - o][j - o];
	for (int i = l, j, li, lj, ri, rj;i <= r;++ i)
		for (j = i;j <= r;++ j){
			li = pr[i - 1] + 1, lj = pr[j], ri = su[i - 1] + 1, rj = su[j];
			f[i - o][j - o] = - inf;
			if (cb[mid] - cb[l - 1])
				upd (f[i - o][j - o], g[li - o][lj - o] + sm[rj] - sm[ri - 1]);
			if (cb[r] - cb[mid])
				upd (f[i - o][j - o], g[ri - o][rj - o]);
		}
}

inline void Solve (int l, int r){
	o = l - 1;
	for (int i = l;i <= r;++ i)
		for (int j = i;j <= r;++ j)
			f[i - o][j - o] = - inf;
	dac (l, r);
//	for (int i = l;i <= r;++ i)
//		for (int j = i;j <= r;++ j)
//			printf ("(%d %d %lld)\n", i, j, f[i][j]);
//	puts ("");
	pre[0] = l - 1;
	for (int i = 1;i <= n;++ i){
		pre[i] = pre[i - 1];
		if (op[pre[i] + 1].t <= i && pre[i] < r)
			++ pre[i];
		s[i] = s[i - 1] + (gt[i] > r ? y[i] : 0);
//		printf ("%d %d\n", pre[i], s[i]);
	}
	for (int i = 1, ql, qr;i <= m;++ i){
		ql = pre[q[i].l - 1] + 1, qr = pre[q[i].r];
//		printf ("%lld %lld %lld\n", ans[i], f[ql - o][qr - o], s[q[i].r] - s[q[i].l - 1]);
		if (cb[r] - cb[l - 1])
			upd (ans[i], f[ql - o][qr - o] + s[q[i].r] - s[q[i].l - 1]);
	}
}

signed main (){
	io::read (n), io::read (m);
	B = (int) sqrt (n);
	for (int i = 1;i <= n;++ i)
		io::read (op[i].x), io::read (op[i].y), op[i].t = i, y[i] = op[i].y;
	for (int i = 1;i <= m;++ i)
		io::read (q[i].l), io::read (q[i].r), q[i].i = i, ans[i] = - inf;
	sort (op + 1, op + n + 1, [] (const oper &a, const oper &b){
		return a.x != b.x ? a.x < b.x : a.t < b.t;
	});
	op[n + 1].x = n;
	op[n + 1].y = 0;
	op[n + 1].t = n + 1;
	++ n;
//	for (int i = 1;i <= n;++ i)
//		printf ("%d %d %d\n", op[i].x, op[i].y, op[i].t);
	for (int i = 1;i <= n;++ i)
		gt[op[i].t] = i;
	for (int i = 1;i <= n;++ i)
		ca[i] = (i == 1 || op[i].x != op[i - 1].x), cb[i] = cb[i - 1] + ca[i];
	for (int l = 1, r = B;l <= n;l = r + 1, r = min (l + B - 1, n))
		Solve (l, r);
	for (int i = 1;i <= m;++ i)
		io::write (ans[i]), putchar ('\n');
	return 0;
}

详细

Test #1:

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

input:

6 5
6 4
2 6
5 -5
3 6
1 2
3 6
1 6
1 6
2 6
2 6
5 6

output:

19
19
15
15
8

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 4630ms
memory: 52760kb

input:

500000 499993
388621 -1
431241 -2
98684 -3
225542 3
302101 -1
268099 0
335498 5
207789 -3
178315 5
79438 10
240218 -3
162624 -8
235325 4
366696 13
344613 8
364410 6
433264 16
279350 13
92961 -11
98083 7
278073 -12
378295 -20
403246 -20
373345 17
442503 -21
426711 16
128318 -14
397427 1
35401 11
1680...

output:

38583073
46401789
46136078
44760222
54466220
35576165
38932387
62596992
40282660
68765202
66880455
64517190
53487001
62837204
53990440
37994203
52079234
51079471
48954532
51246090
63117393
42825167
37042281
50091152
47486246
43626097
44097716
34496260
49867623
60357202
50846745
43287850
65491030
590...

result:

ok 499993 lines

Test #3:

score: 0
Accepted
time: 5849ms
memory: 53344kb

input:

500000 500000
460690 334751
484680 228847
381533 -345167
59764 263144
181478 391597
344444 -172316
469660 -73203
323170 -20371
479733 -284856
248504 -324443
199974 -387690
180081 -230691
92925 453600
463189 346973
885 -193592
464248 -218780
134467 -435168
168391 -280787
454903 -72851
167010 221459
6...

output:

164482050
55616876
195448624
45911421
47632376
194159968
38772536
35573615
270902051
192061033
65485396
90209790
69892762
52813843
164473247
143203103
86564257
237271706
156325846
216941080
110243046
45890573
33351500
214560514
146017583
104405888
160880344
147410258
42917968
49743408
58818923
57733...

result:

ok 500000 lines

Test #4:

score: 0
Accepted
time: 5204ms
memory: 53544kb

input:

499995 499990
449160 -94160
96912 313857
136215 88032
395536 77247
79848 -461846
68073 403666
192382 -271083
321684 498493
482456 -54878
48483 159902
409604 -354226
166110 -58526
384502 -457355
130930 453799
240825 -212169
54733 286008
236090 -1177
70772 -237815
11057 95693
323748 95740
299557 31065...

output:

12819561
21141640
81973143
64004804
112968353
101751279
134451883
108132480
77406711
137666443
127830811
71835383
79909923
69884165
97062469
48314499
106984313
52772306
162938162
95465491
175007799
74847507
45029539
20826655
64628779
77735319
34965864
77164437
43017721
94319986
103035323
86007739
38...

result:

ok 499990 lines

Test #5:

score: 0
Accepted
time: 5696ms
memory: 53516kb

input:

500000 500000
186058 -1
76718 -1
348254 3
468052 1
412949 2
424581 4
52350 -7
465145 -6
347630 -9
318246 -6
369352 -11
462861 -6
484170 12
3136 -14
253719 -6
41130 16
416691 11
82545 -10
327995 16
464553 2
467886 -13
62955 10
397422 19
267012 -13
407764 6
68885 -5
451820 -6
425062 13
371736 6
224786...

output:

112104196
104788677
76588880
54958058
45812004
14144830
11644867
71121036
32549765
7504283
367122
31421022
3027826
66805029
100154600
2896774
6177242
5300793
178625
246542
63899786
109703472
53680294
119120667
31038496
12409139
122381008
73770722
71595180
105194800
20763044
68191088
128779130
647445...

result:

ok 500000 lines

Test #6:

score: 0
Accepted
time: 5614ms
memory: 53556kb

input:

500000 500000
352799 -379438
72288 52304
498388 -180921
15831 -185459
437495 403444
410594 161692
150204 -478094
32356 90132
399243 -293793
246394 191734
182327 -124898
273444 149092
197163 376784
99815 -339197
83621 111523
372314 287282
264386 -51444
244376 -457231
251401 -199521
31069 -354198
4489...

output:

48430266
314022104
300864467
106446445
71186392
91898962
146481868
156959045
85297850
21679530
178162780
132121279
67579709
61222681
50194441
35189845
115635732
356943275
199669090
124261044
126237762
41191263
87751116
134121026
126395596
67385603
148809726
259095749
15028619
67689317
109790124
2876...

result:

ok 500000 lines

Test #7:

score: 0
Accepted
time: 5769ms
memory: 53316kb

input:

500000 500000
475882 0
4119 -1
487885 1
220428 3
316327 1
316982 -4
451294 -1
384915 -7
496702 -6
288278 6
442834 -11
235303 10
306369 -1
87609 -2
177976 -13
490642 4
187451 17
23964 -7
193211 -10
222639 3
283491 -13
443137 5
360143 -3
134838 -4
114781 -10
202421 14
62114 -3
486870 21
459431 -7
8049...

output:

86688367
34099134
111432908
2385813
24245240
94887505
110331526
42735627
6269059
41418744
45820970
111165754
3211884
65798286
68919107
28737282
68119419
57161295
115000170
2441944
14258625
60375477
6764716
45028166
71620627
14769210
133163435
116821408
63005349
16859887
92979513
107778370
44888962
1...

result:

ok 500000 lines

Test #8:

score: 0
Accepted
time: 5434ms
memory: 53552kb

input:

500000 500000
347179 -70946
406187 326933
461792 -290500
485956 268019
31833 -168059
488683 91292
183172 397789
412259 -104567
56870 120692
199047 232326
273659 68603
337888 238046
2213 383665
213246 -424333
14723 265433
16927 -157563
195550 170931
201801 -301940
3688 301679
335075 110210
299574 -35...

output:

59411487
28417047
42658132
47225850
37427087
53886317
64584575
87963121
58014467
65276242
125847627
36972104
31339757
35278108
91065532
108892672
40377682
68416412
44665236
55459001
37461464
54624557
62437778
70920781
24722129
42369070
81974315
77085169
52886894
54609923
51740886
29802766
58510632
5...

result:

ok 500000 lines

Test #9:

score: 0
Accepted
time: 5861ms
memory: 53260kb

input:

500000 500000
315961 -1
326005 -2
461590 1
260723 -3
205129 -5
49064 2
259065 -1
248369 -3
35707 1
479372 10
479302 -2
454071 7
153459 9
361489 -5
333252 15
171645 14
3781 -7
75580 16
358854 -19
195655 -7
352590 -12
184982 22
129475 23
101686 -17
233696 10
410459 3
243361 21
152882 -8
496702 -25
178...

output:

20762371
126373639
127096045
122312332
199926647
179474297
91755782
30060888
104481579
117443308
229300244
173648430
160091091
88906931
152013570
164498369
113464247
191125818
151112017
131598527
194518769
155110107
198663024
130840219
105891922
185075640
100355666
130048690
141769734
134816966
1532...

result:

ok 500000 lines

Test #10:

score: 0
Accepted
time: 5565ms
memory: 53080kb

input:

499991 499997
162696 1
338926 2
450378 3
254801 -3
183814 0
285671 0
397566 -6
304361 -1
270137 6
246043 10
107446 -2
377025 5
367654 12
489143 -13
125493 2
351860 -4
418377 12
260443 16
479197 -12
197870 -13
193270 15
411140 -7
366061 -22
102129 -4
365611 4
457724 -20
163029 -11
146833 7
292934 -21...

output:

18488111
17329543
1941016
18435136
21222687
34911115
12596437
8620264
2103903
12864041
19107609
15035697
2117939
21570582
16039458
19074314
2281418
17384131
14832565
20625240
25794855
17048437
17865336
14812100
17061790
9235394
18119157
16946630
13460287
2839615
6563310
18589807
12104209
14247041
81...

result:

ok 499997 lines

Test #11:

score: 0
Accepted
time: 5158ms
memory: 53064kb

input:

500000 500000
3212 -262197
425592 -55118
110982 -414055
479600 -82149
127330 133921
151048 -147548
119937 -49961
350638 362051
315664 -120394
476563 61924
481963 -277722
185454 -208890
181171 -385327
478263 165370
466652 -486283
486741 -224029
66076 23611
42635 -267898
123824 -9988
55463 174551
1677...

output:

5823176
11553849
8596910
22780737
13822207
3750381
75824261
26307568
43025258
2558926
5583807
67966245
39102418
62967603
70941760
104350925
16665556
16575034
23340574
70964476
28740152
121535190
847724
0
38941129
3260667
2971994
96113286
30362017
101673461
85437720
15937254
10633405
2742348
23926147...

result:

ok 500000 lines