CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Kyselyt
Sender:Metabolix
Submission time:2020-10-17 10:02:52 +0300
Language:C++17
Status:READY
Result:12
Feedback
groupverdictscore
#1ACCEPTED12
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3details
#2--2, 3details
#3--3details
#4--3details
#50.55 s3details

Code

#include <iostream>
#include <algorithm>

#define MAXPOW 19

int n, q;
int t[200002];
int lkm[200002][MAXPOW];
int sum[200002][MAXPOW];
int max[200002][MAXPOW];

int max_valilla(int a, int b) {
	int l = b - a;
	int pow = __builtin_ctz(l & -l);
	int p = a + (1 << pow);
	if (p == b) {
		return max[p-1][pow];
	}
	return std::max(max[p-1][pow], max_valilla(p, b));
}

int summa(int a, int b) {
	int l = b - a;
	int pow = __builtin_ctz(l & -l);
	int p = a + (1 << pow);
	if (p == b) {
		return sum[p-1][pow];
	}
	return sum[p-1][pow] + summa(p, b);
}

int tulos(int a, int b) {
	int m = max_valilla(a, b);
	if (t[a] == m) {
		return (b - a) * m - summa(a, b);
	}
	int p0 = a, p1 = b;
	while (p1 - p0 > 1) {
		int p = (p0 + p1) / 2;
		if (max_valilla(a, p) == m) {
			p1 = p;
		} else {
			p0 = p;
		}
	}
	return tulos(a, p0) + tulos(p0, b);
}

int main() {
	std::cin >> n >> q;
	for (int i = 0; i < n; ++i) {
		std::cin >> t[i];
		lkm[i][0] = 1;
		sum[i][0] = t[i];
		max[i][0] = t[i];
		for (int p = 1; p < MAXPOW; ++p) {
			int j = i - (1 << (p - 1));
			max[i][p] = max[i][p-1];
			lkm[i][p] = lkm[i][p-1];
			sum[i][p] = sum[i][p-1];
			if (j >= 0) {
				max[i][p] = std::max(max[i][p], max[j][p-1]);
				lkm[i][p] += lkm[j][p-1];
				sum[i][p] += sum[j][p-1];
			}
		}
	}
	for (int i = 0; i < q; ++i) {
		int a, b;
		std::cin >> a >> b;
		std::cout << tulos(a - 1, b) << '\n';
	}
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
100 100
70 8 72 88 42 78 85 41 23 36 6...

correct output
99
0
922
2579
1892
...

user output
99
0
922
2579
1892
...

Test 2

Group: 2, 3

Verdict:

input
200000 200000
98 99 29 92 29 81 100 52 89 80...

correct output
1497732
2810356
9532632
6655773
5403513
...

user output
(empty)

Test 3

Group: 3

Verdict:

input
200000 200000
818377786 934884634 816080381 ...

correct output
86877225712611
94684086875470
92703793485296
38149694892093
61948503092286
...

user output
(empty)

Test 4

Group: 3

Verdict:

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
0
0
0
0
0
...

user output
(empty)

Test 5

Group: 3

Verdict:

input
200000 200000
200000 199999 199998 199997 19...

correct output
15920862903
3193483321
18874982071
4846348926
3970697055
...

user output
-1259006281
-1101483975
1695112887
551381630
-324270241
...