QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#665410#7417. Honorable Mentionuser10086WA 1623ms52516kbC++236.0kb2024-10-22 12:24:592024-10-22 12:25:00

Judging History

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

  • [2024-10-22 12:25:00]
  • 评测
  • 测评结果:WA
  • 用时:1623ms
  • 内存:52516kb
  • [2024-10-22 12:24:59]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(x) (x).begin(), (x).end()

#define printf

const int N = 3.5e4 + 10, inf = 1e10;

void chkmax(int &x, int y)
{
	if (y > x) x = y;
}

int cnt;

namespace tree
{
	int n, len[N << 2];
	vector<int> d[N << 2][2][2], sum[N << 2][2][2]; // sum[0] = f(1), f(0) = 0
	int v0[N << 2][2][2];
	
	void pushup(int i)
	{
		for (int c00 : {0, 1}) for (int c11 : {0, 1})
			{
				v0[i][c00][c11] = LLONG_MIN;
				d[i][c00][c11].resize(len[i], -inf);
				sum[i][c00][c11].resize(len[i], -inf);
				for (int c01 : {0, 1}) for (int c10 : {0, 1})
				{
					vector<int> tmp(d[i << 1][c00][c01].size() + d[i << 1 | 1][c10][c11].size());
					merge(all(d[i << 1][c00][c01]), all(d[i << 1 | 1][c10][c11]), tmp.begin(), greater<int>());
					int tmpv0 = v0[i << 1][c00][c01] + v0[i << 1 | 1][c10][c11];
					chkmax(v0[i][c00][c11], tmpv0);
					int cur = tmpv0;
					for (int x = 0; x < tmp.size(); x++) cur += tmp[x], chkmax(sum[i][c00][c11][x], cur);
					if (c01 && c10)
					{
					 // pop front
						cur = tmpv0 + tmp[0];
						for (int x = 0; x + 1 < tmp.size(); x++) cur += tmp[x + 1], chkmax(sum[i][c00][c11][x], cur);
//					if (i == 4 && c00 == 1 && c11 == 1) 
//					{
//						cout << tmpv0 << '/';
//						for (int x : tmp) cout << x << ' '; cout << endl;
//						assert(tmpv0 <= -2 * inf), assert(sum[i][c00][c11][0] < 0);
//					}
					}
				}
				for (int x = (int)sum[i][c00][c11].size() - 1; x >= 1; x--) d[i][c00][c11][x] = sum[i][c00][c11][x] - sum[i][c00][c11][x - 1];
				d[i][c00][c11].front() = sum[i][c00][c11].front() - v0[i][c00][c11];
				for (int x = 0; x < d[i][c00][c11].size(); x++)
				{
					if (d[i][c00][c11][x] < -5e9)
					{
						for (int j = x; j < d[i][c00][c11].size(); j++) d[i][c00][c11][j] = -inf;
						break;
					}
				}
			}
//		for (int x = 0; x <= len[i]; x++) 
//			for (int c0 : {0, 1}) for (int c1 : {0, 1})
//				f[i][x][c0][c1] = -inf;
//		for (int x = 0; x <= len[i << 1]; x++)
//			for (int y = 0; y <= len[i << 1 | 1]; y++)
//				for (int c00 : {0, 1}) for (int c01 : {0, 1}) for (int c10 : {0, 1}) for (int c11 : {0, 1})
//				{
//					if (f[i << 1][x][c00][c01] <= -inf || f[i << 1 | 1][y][c10][c11] <= -inf) continue;
//					chkmax(f[i][x + y - (c01 && c10)][c00][c11], f[i << 1][x][c00][c01] + f[i << 1 | 1][y][c10][c11]);
//				}
	}
	
	void build(int i, int *a, int li, int ri)
	{
		len[i] = ri - li + 1;
		if (li == ri)
		{
			d[i][0][0].resize(1, -inf), d[i][1][1].resize(1, a[li] + inf), d[i][0][1].resize(1, 0), d[i][1][0].resize(1, 0);
			v0[i][0][0] = 0, v0[i][1][0] = v0[i][0][1] = v0[i][1][1] = -inf;
			for (int c0 : {0, 1})
				for (int c1 : {0, 1}) sum[i][c0][c1] = d[i][c0][c1], sum[i][c0][c1][0] += v0[i][c0][c1];
			return;
		}
		int mid = (li + ri) >> 1;
		build(i << 1, a, li, mid), build(i << 1 | 1, a, mid + 1, ri);
		pushup(i);
	}
	
	void init(int *a, int sz)
	{
		n = sz;
		build(1, a, 1, n);
	}

	void print(int i = 1, int li = 1, int ri = n)
	{
		printf("node %lld : [%lld, %lld]\n", i, li, ri);
		for (int c0 : {0, 1})
			for (int c1 : {0, 1})
			{
				printf("v0(%lld, %lld, %lld) = %lld\n", i, c0, c1, v0[i][c0][c1]);
				printf("sum(%lld, %lld, %lld): ", i, c0, c1);
				for (int x : sum[i][c0][c1]) printf("%lld ", x); printf("\n");
				printf("d(%lld, %lld, %lld): ", i, c0, c1);
				for (int x : d[i][c0][c1]) printf("%lld ", x); printf("\n");
			}
		if (li == ri) return;
		int mid = (li + ri) >> 1;
		print(i << 1, li, mid), print(i << 1 | 1, mid + 1, ri);
	}
	
	typedef array<array<array<int, 2>, 2>, 2> info; // info[c0][c1] = {x, f(x) - px}
	
	info null = {};

	info merge(info x, info y, int p)
	{
		if (x == null) swap(x, y);
		if (y == null) return x;
		
		info res;
		for (int c0 : {0, 1}) for (int c1 : {0, 1}) 
			res[c0][c1] = {0, -inf};
		for (int c00 : {0, 1}) for (int c11 : {0, 1})
		{
			for (int c01 : {0, 1}) for (int c10 : {0, 1})
			{
				int val = x[c00][c01][1] + y[c10][c11][1];
				if (c01 && c10 && val + p > res[c00][c11][1]) res[c00][c11] = {x[c00][c01][0] + y[c10][c11][0] - 1, val + p};
				if (val > res[c00][c11][1]) res[c00][c11] = {x[c00][c01][0] + y[c10][c11][0], val};
			}
		}
		return res;
	}	
	
	info getinfo(int l, int r, int p, int i = 1, int li = 1, int ri = n)
	{
		printf("getinfo(%lld, %lld, %lld, %lld, %lld, %lld)\n", l, r, p, i, li, ri);
		if (l <= li && ri <= r)
		{
			info res;
			for (int c0 : {0, 1}) for (int c1 : {0, 1})
			{
				int id = lower_bound(all(d[i][c0][c1]), p, greater<int>()) - d[i][c0][c1].begin() - 1;
				assert(-1 <= id && id < (int)sum[i][c0][c1].size());
				if (id < 0) res[c0][c1] = {0, v0[i][c0][c1]};
				else res[c0][c1] = {id + 1, sum[i][c0][c1][id]};
				res[c0][c1][1] -= res[c0][c1][0] * p;
			}
			return res;
		}
		int mid = (li + ri) >> 1;
		info cur = null;
		if (l <= mid) cur = getinfo(l, r, p, i << 1, li, mid);
		if (r > mid) cur = merge(cur, getinfo(l, r, p, i << 1 | 1, mid + 1, ri), p);
		return cur;
	}
}

int n, q, a[N];

//int F(int q, int n)
//{
//	if (q == 0) return 1;
//	if (q == 1) return __lg(n) + 1;
//	if (n == 1) return q;
//	return F(q / 2, n / 2) + F((q + 1) / 2, (n + 1) / 2) + min(q * (__lg(n) + 1) * (__lg(n) + 1), 4 * n);
//}

const int V = 3.5e4 + 10;

signed main()
{
	cin >> n >> q;
	for (int i = 1; i <= n; i++) cin >> a[i];
	tree::init(a, n);
	tree::print();
	while (q--)
	{
		int l, r, k; cin >> l >> r >> k;
		int pl = -2 * inf, pr = 2 * inf;
		while (pl < pr)
		{
			int mid = (pl + pr) >> 1;
			auto res = tree::getinfo(l, r, mid);
			int val = -inf, x = 0;
			for (int c0 : {0, 1}) for (int c1 : {0, 1})
				if (res[c0][c1][1] > val) val = res[c0][c1][1], x = res[c0][c1][0];
			printf("x = %lld, val = %lld, pl = %lld, pr = %lld, mid = %lld\n", x, val, pl, pr, mid);
			if (x > k) pl = mid + 1;
			else pr = mid;
		}
		auto res = tree::getinfo(l, r, pl);
		int val = -inf;
		for (int c0 : {0, 1}) for (int c1 : {0, 1})
			chkmax(val, res[c0][c1][1]);
		cout << val + k * pl << endl;
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

4
6
5
2
-3

result:

ok 5 number(s): "4 6 5 2 -3"

Test #2:

score: 0
Accepted
time: 2ms
memory: 5632kb

input:

5 1
7 7 7 7 7
1 5 1

output:

35

result:

ok 1 number(s): "35"

Test #3:

score: 0
Accepted
time: 1618ms
memory: 51652kb

input:

25000 25000
-11889 -16468 18402 -13238 14125 12120 -3295 -12106 20152 7478 8575 9968 -13946 -13408 23715 -14670 -21837 12589 7029 20609 2868 12728 -6942 982 5884 -7561 -2058 -7892 7806 16344 14645 13904 7355 9860 15446 -539 24072 21969 19028 -24939 -21911 -11836 21045 -4727 -3882 2138 18513 -4796 -1...

output:

10341941
44514177
6687268
77971944
99605102
107722534
15473041
17383093
62015893
10703102
41214044
22949449
9407209
9022260
39351814
72311292
5178065
42027491
52700848
38135774
37045964
4761513
16326256
16812496
107985169
28306484
46710957
864270
102812861
27960495
50366764
16379719
2791377
21112728...

result:

ok 25000 numbers

Test #4:

score: 0
Accepted
time: 1582ms
memory: 52516kb

input:

25000 25000
6000 -3019 -11754 -7445 -8441 7523 7149 -9202 7895 12217 7475 3656 1303 1710 9238 7029 5141 -1777 8992 -2357 5436 8102 4094 -10002 -7052 3213 1387 -41 -5364 -8259 4860 -721 12482 3791 6804 -10687 4462 -7578 -12456 5135 10987 -9087 5706 -8716 10036 9149 -11514 -7407 4623 9555 -4053 6603 -...

output:

34173465
3726494
235962
7075586
30183529
3643608
48804222
7114899
4984843
15646757
1887647
2330595
47535902
239762
6115278
7951919
1734323
2623203
1495055
1420131
10657149
3064884
12169559
9504529
617684
25801604
3732771
6630548
9892438
1190577
24266894
3527999
23386393
33402029
16816797
62284476
37...

result:

ok 25000 numbers

Test #5:

score: 0
Accepted
time: 1623ms
memory: 52148kb

input:

25000 25000
-2969 1681 -2312 -367 -2816 2496 -2066 -636 182 -522 1159 3035 -1616 -2017 -727 -64 987 -3044 -721 1871 -465 452 -2136 -636 1252 2947 -2252 -602 1521 1299 -3070 100 -3111 -1212 -1211 132 2798 -467 1610 408 491 520 2182 2323 2984 -1105 458 -384 917 165 1019 -1424 -1740 1838 225 1043 -1643...

output:

3696093
5856268
1569949
43110
361033
1251419
5991586
11948087
10350223
3152434
10667775
4975400
8739958
9585292
6671359
6889903
10775826
200001
7290024
4142281
7861955
7191701
644506
803034
1932372
9842195
6618617
12318124
4618343
5306047
12502164
612289
2157188
11618855
7200073
5016967
2463491
3209...

result:

ok 25000 numbers

Test #6:

score: 0
Accepted
time: 1603ms
memory: 51312kb

input:

25000 25000
-240 -760 1554 200 279 1306 -394 1272 967 -1151 -13 1286 994 1110 551 445 1428 -175 -1427 854 927 911 -969 -920 968 -913 -1481 -669 1487 -600 291 -1070 -782 1504 -222 599 -939 1194 1129 -397 -185 627 772 -870 133 -695 -1092 -137 -778 1332 -158 1545 -329 273 1098 -671 947 -1095 257 -1024 ...

output:

5052771
5348425
6990219
1782518
4288908
124491
4603645
662433
3354133
2273603
2117016
908991
3981909
181606
180388
1445473
8334703
3417019
3832988
1156373
751967
816152
207595
5328156
304047
4822038
8095611
4532395
4836019
4415840
779952
5397423
5924339
1559505
2746919
3784667
3664365
1427979
207448...

result:

ok 25000 numbers

Test #7:

score: 0
Accepted
time: 1573ms
memory: 51212kb

input:

25000 25000
-235 -315 120 -376 455 154 461 150 -280 -74 -242 -432 -50 25 -303 -327 263 499 -464 -203 333 59 -265 -72 357 -213 2 275 -159 -4 160 -304 -320 103 447 382 -120 -65 364 -351 73 -307 284 -469 -118 -324 -310 93 -347 -48 -286 139 -25 401 105 -479 -377 428 -26 -313 97 -132 78 14 -64 -59 -455 -...

output:

166178
1692049
770543
2394705
1908039
1005349
356998
25023
355550
891075
214103
788750
1008340
1536696
160682
151535
300585
373884
849931
800218
2440857
1009992
311596
635872
2502637
288362
284558
538137
1553207
849175
165203
544829
230264
163844
1284945
801960
371289
637139
1249915
203702
12408
830...

result:

ok 25000 numbers

Test #8:

score: 0
Accepted
time: 1579ms
memory: 52164kb

input:

25000 25000
163 -18 16 -124 54 -71 -111 -81 46 27 139 16 -51 -78 36 21 85 -31 49 33 -42 -42 122 81 -25 -43 166 -131 2 -45 154 -68 -76 127 48 124 92 -26 128 44 -22 -95 -80 -52 -64 -113 22 104 -76 58 49 53 -115 70 -159 -67 62 83 69 164 -11 -13 -4 81 -82 -98 68 136 -7 146 17 138 89 89 18 57 143 57 51 1...

output:

494541
25814
267902
153374
150628
206276
98909
261258
5231
645604
828846
372159
655216
2523
152068
199639
515692
443552
753836
15295
70205
795943
704318
398355
396254
15170
495548
103742
129088
19442
870390
146159
8499
270179
380106
191900
682344
4356
559722
372147
426880
529125
46890
161656
291764
...

result:

ok 25000 numbers

Test #9:

score: 0
Accepted
time: 1499ms
memory: 52140kb

input:

25000 25000
10 9 3 19 -1 -5 26 11 3 15 -1 -9 23 11 -24 12 -3 21 -20 10 -10 12 -19 -16 11 -2 19 20 18 -23 27 -23 9 2 1 1 16 -24 -23 -10 -13 21 6 -1 -20 -24 -17 20 23 12 -25 -13 -3 -20 13 24 -27 20 19 5 7 7 9 -24 15 17 -27 16 -13 -15 16 -4 7 13 5 -2 16 25 10 -3 -20 -11 22 -1 -5 -16 19 4 -19 15 5 -25 5...

output:

54921
58348
91837
2814
18931
43185
74327
16895
936
59014
117622
124161
120167
3943
29622
6749
86196
11513
98358
48694
34346
87327
44195
21361
94794
23566
21059
113460
121018
5009
2641
1200
27270
37878
32708
43496
39506
33574
58612
6251
5481
10564
78957
79670
49585
21564
100771
40511
716
93538
21835
...

result:

ok 25000 numbers

Test #10:

score: -100
Wrong Answer
time: 1432ms
memory: 52248kb

input:

25000 25000
-1 6 4 3 5 5 3 4 6 -4 0 -5 3 -6 6 3 5 5 -3 3 6 2 -3 4 -4 0 6 -1 -4 -3 1 -1 -3 -3 -5 0 1 2 -4 -1 -6 5 -6 -4 0 4 -6 1 -1 -1 -4 3 -4 -2 0 4 -3 -2 -3 0 5 -4 -5 -2 -6 0 5 3 -4 -2 -4 0 -3 -6 -6 -5 -3 4 3 -1 -3 -2 -1 -4 3 -3 -3 -5 1 1 -3 -2 1 -3 3 6 -4 0 -6 -4 -2 -6 -1 -2 0 -1 2 -5 -4 -5 -4 -5 ...

output:

3471
9242
14877
3247
25345
1268
12419
4913
9553
3945
12865
6204
24807
103
6552
27546
24866
17021
9674
9271
32781
18711
2582
18824
31679
7479
1569
15391
21198
19186
4196
13914
284
15798
6722
1620
34719
122
33294
1652
5213
16791
20
26381
15749
1627
3923
13190
2548
3981
10159
10964
8728
2814
6359
5167
...

result:

wrong answer 1249th numbers differ - expected: '642', found: '643'