QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#294682#2681. 序列yhk100170 911ms143588kbC++144.4kb2023-12-30 15:45:212023-12-30 15:45:22

Judging History

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

  • [2023-12-30 15:45:22]
  • 评测
  • 测评结果:70
  • 用时:911ms
  • 内存:143588kb
  • [2023-12-30 15:45:21]
  • 提交

answer

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

// #define Debug

const int N = 1e5;
const long long P = 998244353;

const int Lg = 17, SZ = N * Lg * 5;
typedef pair<int, int> pir;

int n, m;
long long a[N + 5];

struct Node
{
	int ls, rs;
	pir interval;//l, r
} node[SZ + 5];
int tot;

#define ls(p) node[p].ls
#define rs(p) node[p].rs

void modify(int p, int q, int l, int r, int pos, pir inte)
{
	if (l == r)
		return node[p].interval = inte, void();

	int mid = (l + r) >> 1;
	if (pos <= mid)
	{
		ls(p) = ++tot, rs(p) = rs(q);
		modify(ls(p), ls(q), l, mid, pos, inte);
	}
	else
	{
		rs(p) = ++tot, ls(p) = ls(q);
		modify(rs(p), rs(q), mid + 1, r, pos, inte);
	}
	return ;
}
pir query(int p, int l, int r, int pos)
{
	if (l == r)
		return node[p].interval;

	int mid = (l + r) >> 1;
	if (pos <= mid)
		return query(ls(p), l, mid, pos);
	return query(rs(p), mid + 1, r, pos);
}

int rootP[N + 5], rootS[N + 5];
int szP[N + 5], szS[N + 5];
long long ansP[N + 5], ansS[N + 5];

long long sum[N + 5];//sum ai, should not mod P
long long sqr[N + 5];//sum ai^2
long long inv[N + 5];

long long calc(int l, int r)//sum (ai - ave)^2 = sum ai^2 + len * ave^2 - 2 * ave * sum ai
{
	long long ave = (sum[r] - sum[l - 1]) % P * inv[r - l + 1] % P;
	long long res = (sqr[r] - sqr[l - 1] + P) % P;
	res = (res + ave * ave % P * (r - l + 1) % P) % P;
	res = (res - ave * 2 * (sum[r] - sum[l - 1]) % P + P) % P;
	return res;
}

pir stk[N + 5];
int top;

int get_pos(int x, long long val, int R)
{
	int l = 1, r = szP[x - 1], L = 1;
	while (l <= r)
	{
		int mid = (l + r) >> 1;
		pir res = query(rootP[x - 1], 1, n, mid);
		int lb = res.first, rb = res.second;

		if ((sum[rb] - sum[lb - 1]) * (R - rb) < 
			(sum[R] - sum[rb] + val - a[x]) * (rb - lb + 1))
		{
			L = rb + 1;
			l = mid + 1;
		}
		else
			r = mid - 1;
	}
	return L;
}
long long solve(int x, long long val)
{
	int l = 1, r = szS[x + 1], R = n;//[x, x]
	while (l <= r)
	{
		int mid = (l + r) >> 1;
		
		pir res = query(rootS[x + 1], 1, n, mid);
		int lb = res.first, rb = res.second;
		int L0 = get_pos(x, val, lb - 1);

		if ((sum[rb] - sum[lb - 1]) * (lb - L0) >= 
			(sum[lb - 1] - sum[L0 - 1] + val - a[x]) * (rb - lb + 1))
		{
			R = lb - 1;
			l = mid + 1;
		}
		else
			r = mid - 1;
	}
	int L = get_pos(x, val, R);//if R = n, won't execute the while-loop

	long long newSum = (sum[R] - sum[L - 1] + val - a[x]) % P;
	long long newSqr = (sqr[R] - sqr[L - 1] + val * val % P - a[x] * a[x] % P + P * 2) % P;

	int len = R - L + 1;
	long long ave = newSum * inv[len] % P;
	
	long long res = (ansP[L - 1] + ansS[R + 1]) % P;
	res = (res + newSqr) % P;
	res = (res + ave * ave % P * len % P) % P;
	res = (res - ave * 2 * newSum % P + P) % P;
	return res;
}

int main()
{
	// freopen("sequence.in", "r", stdin);
	// freopen("sequence.out", "w", stdout);

	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld", a + i);

		sum[i] = sum[i - 1] + a[i];
		sqr[i] = (sqr[i - 1] + a[i] * a[i] % P) % P;
	}

	inv[1] = 1;
	for (int i = 2; i <= n; i++)
		inv[i] = (P - P / i) * inv[P % i] % P;

	for (int i = 1; i <= n; i++)
	{
		int bound = i;
		while (top)
		{
			int l = stk[top].first, r = stk[top].second;
			if ((sum[r] - sum[l - 1]) * (i - r) >= 
				(sum[i] - sum[r]) * (r - l + 1))
			{
				bound = l;
				top--;
			}
			else
				break;
		}
		stk[++top] = make_pair(bound, i);

		int fa = stk[top - 1].second;

		rootP[i] = ++tot;
		szP[i] = szP[fa] + 1;
		ansP[i] = (ansP[fa] + calc(bound, i)) % P;

		modify(rootP[i], rootP[fa], 1, n, szP[i], stk[top]);
	}

	top = 0;
	for (int i = n; i > 0; i--)
	{
		int bound = i;
		while (top)
		{
			int l = stk[top].first, r = stk[top].second;
			if ((sum[r] - sum[l - 1]) * (l - i) <= 
				(sum[l - 1] - sum[i - 1]) * (r - l + 1))
			{
				bound = r;
				top--;
			}
			else
				break;
		}
		stk[++top] = make_pair(i, bound);

		int fa = stk[top - 1].first;

		rootS[i] = ++tot;
		szS[i] = szS[fa] + 1;
		ansS[i] = (ansS[fa] + calc(i, bound)) % P;
		
		modify(rootS[i], rootS[fa], 1, n, szS[i], stk[top]);
	}

	printf("%lld\n", ansP[n]);
	for (int i = 1; i <= m; i++)
	{
		int pos;
		long long val;
		scanf("%d%lld", &pos, &val);
		printf("%lld\n", solve(pos, val));
	}
	return 0;
}

详细

Test #1:

score: 5
Accepted
time: 0ms
memory: 137756kb

input:

9 4
5 9 3 1 2 9 6 8 1
2 4
1 10
6 4
1 24

output:

78
48
108
66
412

result:

ok 5 lines

Test #2:

score: 5
Accepted
time: 7ms
memory: 137640kb

input:

10 10
1 2 3 4 5 6 7 8 9 10
1 16
2 1
3 6
4 1
5 6
6 3
7 17
8 2
9 18
10 4

output:

0
130
0
2
2
0
2
50
14
32
14

result:

ok 11 lines

Test #3:

score: 5
Accepted
time: 7ms
memory: 138320kb

input:

70 50
35 86 5 92 30 86 99 4 36 66 41 27 40 21 20 5 98 7 10 10 44 40 47 27 37 54 61 72 13 10 30 19 27 96 54 94 88 29 45 7 96 21 98 77 46 35 8 3 59 94 61 7 23 87 17 28 35 7 40 62 27 9 53 51 13 53 2 74 26 43
30 80
62 27
60 81
69 9
29 24
40 1
37 74
43 84
51 70
10 45
65 96
58 37
53 12
51 45
12 30
59 76
5...

output:

401315650
900438359
636195698
430676780
323816444
182949232
225155371
283874120
283873840
137074867
182949012
223806330
48992716
195795352
636196406
682071808
929798863
107715801
931633050
2016536
526096112
401315650
665555218
182953214
606838401
931634241
674549557
432511491
900439346
432509578
401...

result:

ok 51 lines

Test #4:

score: 5
Accepted
time: 12ms
memory: 137380kb

input:

73 99
97 50 68 1 80 68 10 67 1 7 34 71 77 11 19 10 24 53 13 50 43 47 2 56 42 16 88 23 26 79 14 1 34 69 4 34 45 29 59 82 25 13 31 29 16 89 36 54 78 84 92 49 99 51 1 85 99 92 43 1 26 69 40 45 97 65 74 77 87 25 49 39 4
20 17
44 58
47 15
1 88
60 98
64 41
47 73
35 41
66 20
18 4
55 81
50 86
30 12
65 54
15...

output:

652877985
586328646
926999318
390483610
852525876
496959534
795484448
260710184
619097816
403318303
519779633
856326579
510271753
519777657
831134414
519781695
219117902
795484451
541961992
545923800
187030406
187031896
656677478
241736641
403317433
985625652
741610831
82455180
253580135
320130998
6...

result:

ok 100 lines

Test #5:

score: 5
Accepted
time: 8ms
memory: 138732kb

input:

100 100
38 51 81 65 3 48 84 80 87 17 13 45 78 80 99 84 29 81 94 38 61 13 38 47 81 86 59 64 14 71 1 45 36 25 41 23 22 40 8 98 85 50 14 68 61 95 27 77 51 9 20 86 35 14 22 85 46 87 10 66 20 14 67 17 43 68 73 44 68 20 87 12 54 76 97 84 32 46 25 60 40 93 61 6 19 91 82 81 55 24 20 44 39 42 20 15 70 56 46 ...

output:

375978041
687755691
375979417
562090186
223702676
460574176
866642864
644942765
934578411
108658111
545171839
787581310
266274346
460574236
504563181
800924286
288457057
105267691
504565126
257541004
520161209
279373450
866640494
247173885
714367801
443655769
67831760
579011829
22257883
156026790
26...

result:

ok 101 lines

Test #6:

score: 5
Accepted
time: 7ms
memory: 137644kb

input:

100 100
35 100 81 90 34 35 100 68 89 88 99 79 27 42 58 41 71 65 65 72 27 10 25 96 56 55 32 64 6 7 31 63 67 89 94 69 38 91 77 69 70 30 54 44 10 58 53 73 53 3 93 95 80 22 88 17 29 16 85 54 12 65 81 30 56 37 51 7 47 77 20 63 77 21 59 20 35 5 66 33 40 19 67 92 13 6 33 56 69 79 20 70 57 59 11 55 46 43 29...

output:

544569764
181571704
322737778
836987275
685736034
988233914
625237756
181572352
625235612
413487837
988232872
544568924
292487539
80739230
907566348
20241800
726070318
73876
352988471
544570580
352986903
292488259
635321207
625238080
544570024
422010821
836982907
685737726
443737094
655485193
685737...

result:

ok 101 lines

Test #7:

score: 5
Accepted
time: 16ms
memory: 140856kb

input:

53300 0
1 4 4 5 1 3 3 5 1 3 4 4 2 1 5 5 4 9 8 6 10 9 7 9 7 10 7 14 13 15 11 13 13 12 13 13 14 14 13 13 15 13 15 13 15 19 19 16 19 20 16 17 18 17 19 16 17 20 18 17 18 16 16 19 17 21 25 25 24 21 21 22 21 25 23 30 29 28 27 26 29 28 28 30 29 29 28 33 33 32 34 35 34 34 34 34 32 32 35 33 33 34 31 32 31 39...

output:

596897851

result:

ok single line: '596897851'

Test #8:

score: 5
Accepted
time: 20ms
memory: 142924kb

input:

95722 0
3 1 3 3 2 3 4 5 3 5 5 5 1 4 8 8 9 7 8 7 7 6 10 8 9 6 7 10 9 7 7 7 6 15 12 13 11 13 14 13 11 15 19 16 18 18 18 19 20 19 16 19 18 18 16 19 22 24 24 25 22 21 23 23 21 25 21 21 24 24 23 25 21 24 22 30 30 30 28 28 29 27 30 27 30 27 28 28 28 27 28 30 35 34 31 34 31 32 35 35 35 32 33 32 33 31 31 31...

output:

992431421

result:

ok single line: '992431421'

Test #9:

score: 5
Accepted
time: 24ms
memory: 143088kb

input:

100000 0
4 4 4 5 2 1 2 5 5 1 3 5 5 3 4 3 5 8 10 7 8 8 8 9 8 8 9 7 10 6 7 10 10 8 11 12 13 15 11 12 11 12 12 11 11 15 19 19 17 20 19 16 19 16 20 18 17 17 17 17 19 23 23 21 22 25 24 24 24 21 22 23 23 23 23 23 24 24 26 26 29 26 26 29 27 29 26 30 30 28 30 26 28 27 26 35 35 32 31 33 31 35 35 34 31 35 32 ...

output:

4039218

result:

ok single line: '4039218'

Test #10:

score: 5
Accepted
time: 19ms
memory: 142976kb

input:

100000 0
1 2 5 2 2 3 1 1 4 2 1 3 4 1 1 3 3 1 5 2 1 1 3 2 5 5 1 5 2 2 3 4 3 3 5 5 2 4 4 2 4 2 4 2 4 3 4 1 5 5 4 1 1 1 2 1 2 2 4 1 2 3 1 2 4 4 5 4 3 3 4 5 3 2 4 4 2 1 2 2 1 5 4 3 2 5 4 1 1 4 3 3 1 5 2 3 1 2 3 2 5 1 1 2 5 3 5 2 4 1 2 2 4 1 4 4 1 4 5 4 3 1 4 4 4 2 3 5 3 4 2 5 2 3 4 2 3 3 4 5 5 3 3 5 1 5...

output:

136586396

result:

ok single line: '136586396'

Test #11:

score: 5
Accepted
time: 88ms
memory: 139176kb

input:

21098 27015
344 871 1735 1049 1874 1447 545 462 1589 935 1843 722 1689 1185 805 292 40 291 475 425 960 1028 34 1423 248 1873 1322 1482 1728 4 1714 731 635 1524 813 65 11 1431 1871 1002 1241 1348 1926 557 938 2656 2208 2332 2920 2786 2724 2074 2622 3180 2972 2095 2072 2941 2876 1955 2501 2664 2643 30...

output:

216490041
282298692
119780407
444297507
351700506
829121672
890279976
396968479
367639275
948505378
519256948
366489333
520792473
334743126
818845031
504959094
409140798
659726004
529431531
120880426
229185626
752423790
155041867
106896505
205165243
710431857
244624210
953922984
28311724
296258828
3...

result:

ok 27016 lines

Test #12:

score: 5
Accepted
time: 84ms
memory: 138468kb

input:

15612 27106
2656 397 3219 4095 223 691 2334 3752 1064 407 3335 1729 2173 2883 2697 4423 1543 4269 3071 3031 112 3727 263 1087 1343 3088 2411 2382 2806 1697 1546 796 3946 1856 1985 1892 1200 3364 1065 2415 102 1236 3028 97 791 830 2512 2079 2097 1154 6769 7250 8215 5437 4674 6761 7214 6375 5246 8317 ...

output:

808442154
128813612
579058906
17374090
402126800
346424288
156312849
67279374
889124134
672776580
125187299
515130178
520195811
434801785
658489394
381800637
438413118
81984714
975046940
222204543
407898208
247562694
35272834
427556435
937098237
502144804
420410370
414057605
357312981
284336723
2671...

result:

ok 27107 lines

Test #13:

score: 5
Accepted
time: 165ms
memory: 139132kb

input:

30000 30000
200 110 109 325 49 199 69 69 174 289 222 355 274 285 1237 442 1256 487 1160 1131 824 825 841 506 802 606 1110 1267 580 1224 1315 1542 1883 1514 1682 1713 1825 1896 1796 1804 1829 1987 2243 2240 2273 2213 2124 2056 2262 2262 1977 2180 2069 2170 2568 2316 2729 2359 2354 2302 2590 2484 2369...

output:

503265000
197917701
588204518
367071920
651931154
168229178
762907387
605557381
72620882
22209069
677829697
825763434
735518432
859373930
667603448
450631930
959211316
655448697
37071184
408440757
634999539
962646370
829508995
971273317
785437702
318246770
686105854
264896605
140167186
759083953
424...

result:

ok 30001 lines

Test #14:

score: 5
Accepted
time: 155ms
memory: 139704kb

input:

30000 30000
529 417 347 160 47 191 557 324 19 507 66 626 655 652 593 667 700 691 578 604 701 996 862 1005 997 828 807 744 771 778 909 908 742 875 811 1457 1362 1509 1664 1104 1445 1589 1054 1359 1613 1415 1673 1091 1272 1343 1259 1864 1746 1746 1877 1801 1836 1679 1891 1932 1878 1771 2136 2054 2129 ...

output:

187811036
419743495
326622782
596177773
284088330
817251822
763345773
611226725
783550594
619722896
516552587
668383821
439676747
563526548
632869920
226737032
378945395
338413125
721641099
517019351
995928768
109696992
35135561
296536885
822769895
525810203
76622841
1619
860416862
699304879
6559156...

result:

ok 30001 lines

Test #15:

score: 0
Wrong Answer
time: 366ms
memory: 140308kb

input:

52146 60833
10431 108440 129958 18819 83587 30104 128604 101935 131036 81943 25938 61684 19424 88192 30119 107827 12931 323532 266938 177251 297933 190841 281098 316927 266235 287774 151380 316579 221669 332239 213299 584094 474913 550232 450906 486204 377740 396900 686427 711018 645192 622709 64000...

output:

635546469
743122338
858589732
565409757
664324780
693325295
33801489
760860809
230132838
82172450
162522933
659277908
675087840
480138693
889030028
31065431
494099543
358576996
60322903
269628974
970830916
576064541
723897082
987366226
84568492
740838824
564703378
833481677
381266580
626477770
85403...

result:

wrong answer 1st lines differ - expected: '215403165', found: '635546469'

Test #16:

score: 0
Wrong Answer
time: 500ms
memory: 142560kb

input:

81253 60775
100013263 100024473 100025183 100033846 100045576 100048264 100060032 100069192 100075667 100084722 100094053 100095251 100097195 100106820 100137679 100143726 100144205 100154546 100159600 100161014 100165484 100170846 100186357 100188421 100191991 100196942 100197593 100205843 10020807...

output:

949446244
694100171
743414074
16797095
803175376
482279522
578511695
104917494
102287798
518985801
549833800
157101307
147424077
108312165
948631136
905582641
535071525
78017234
477232487
694968295
329259586
326844131
938109478
149918624
595674828
238238964
823796347
303661119
58226703
834687750
472...

result:

wrong answer 1st lines differ - expected: '536218462', found: '949446244'

Test #17:

score: 0
Wrong Answer
time: 563ms
memory: 143444kb

input:

98031 64875
6055 11305 23012 46694 49231 60267 64022 66521 78013 80669 88732 116127 117622 119493 121728 126631 129670 133075 138800 144370 152157 161591 162235 168317 178889 180355 188901 191552 193715 200770 221690 229588 230335 245497 257902 267776 275916 298839 307776 335454 360115 382195 386491...

output:

299361147
730993272
518485348
528241212
687257271
258509154
925118325
299925218
886535857
988127047
186711828
975190665
855328908
802265912
343039290
851148828
682973038
745532048
199503668
431499078
280832428
737052803
205255782
40192141
986931828
795840418
876921902
701270098
144728258
780413796
1...

result:

wrong answer 1st lines differ - expected: '385426608', found: '299361147'

Test #18:

score: 0
Wrong Answer
time: 424ms
memory: 142912kb

input:

100000 100000
405143 837533 1400794 939418 634420 252212 818562 573021 1131578 533333 1033294 474659 716976 362693 1299869 30295 90842 554861 1370733 281964 528300 630202 632566 572122 813457 257710 1507505 1452401 491540 182440 793549 1020865 860442 318787 319064 1504619 957459 1228758 1335635 5353...

output:

792369142
140930812
925920179
693872909
636001561
710667135
496807053
798377717
146994394
292319309
449954860
469323540
143585172
565446503
694302142
894673476
888562631
331773545
834680801
365136014
496950905
180602808
951720856
563443091
67999508
836487967
321591849
446799075
471275578
697423390
4...

result:

wrong answer 1st lines differ - expected: '802895276', found: '792369142'

Test #19:

score: 0
Wrong Answer
time: 911ms
memory: 143588kb

input:

100000 100000
100001485 100006306 100007354 100007762 100010995 100026633 100030017 100030961 100032784 100045950 100054031 100055257 100067881 100078828 100100205 100115622 100118159 100144824 100147826 100152782 100164046 100166378 100168860 100173865 100182899 100188549 100198636 100223533 100228...

output:

274841179
193939419
286089014
918891643
878381318
431343789
173002578
644465171
221375042
785320091
142801085
566067436
485717741
599619517
503216160
65142000
197344768
374793792
164591577
18999351
671869764
845457412
288013044
335154550
169502211
551667370
33148462
340974538
89153489
910064548
6969...

result:

wrong answer 1st lines differ - expected: '457345384', found: '274841179'

Test #20:

score: 0
Wrong Answer
time: 874ms
memory: 143584kb

input:

100000 100000
5150 9533 22391 28652 28932 31263 45794 81632 96756 103461 105105 122891 133272 167978 171445 181720 181840 190167 193256 199417 205512 218429 242071 259925 271400 274907 288185 301567 314808 325172 330792 331871 335347 339657 340513 346712 350646 353735 374146 377118 399170 405307 409...

output:

78570829
647378583
827081704
216892024
572756481
14277536
630732436
889404575
277565446
63627137
232179715
621081565
73817991
581340340
791496014
249657382
797960530
576126583
765107270
35519737
49989063
492903977
272102358
493037590
215942804
711152693
956619156
140209217
84559701
928978587
2076569...

result:

wrong answer 1st lines differ - expected: '982276385', found: '78570829'