CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Kyselyt
Sender:hltk
Submission time:2020-10-16 19:26:30 +0300
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.01 s1, 2, 3details
#20.36 s2, 3details
#30.38 s3details
#4ACCEPTED0.32 s3details
#50.36 s3details

Code

#include <iostream>
#include <vector>
using namespace std;
struct SegmentTree {
int n;
vector<long> p, l;
SegmentTree(int n) : n(n), p(n * 4), l(n * 4) {}
long query(int s, int l, int r, int x, int y) {
if (l <= x && y <= r)
return p[s];
if (y <= l || r <= x)
return 0;
push(s);
int m = (x + y + 1) / 2;
return query(s * 2, l, r, x, m) + query(s * 2 + 1, l, r, m, y);
}
void change(int s, int l, int r, int x, int y, long k) {
if (l <= x && y <= r)
return apply(s, k);
if (y <= l || r <= x)
return;
push(s);
int m = (x + y + 1) / 2;
change(s * 2, l, r, x, m, k);
change(s * 2 + 1, l, r, m, y, k);
pull(s);
}
void push(int s) {
apply(s * 2, l[s]);
apply(s * 2 + 1, l[s]);
}
void apply(int s, long x) {
p[s] += x;
l[s] += x;
}
void pull(int s) {
p[s] = p[s * 2] + p[s * 2 + 1];
}
long query(int l, int r) {
return query(1, l, r, 0, n);
}
void change(int l, int r, long k) {
change(1, l, r, 0, n, k);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> v(n);
for (auto& x : v)
cin >> x;
SegmentTree tree(n);
for (int i = 0; i < n; ++i)
tree.change(i, i + 1, -v[i]);
vector<long> ans(q);
vector<vector<pair<int, int>>> queries(n);
for (int qi = 0; qi < q; ++qi) {
int l, r;
cin >> l >> r;
queries[l-1].emplace_back(r, qi);
}
vector<int> stack{n};
for (int i = n - 1; i >= 0; --i) {
while (stack.back() < n && v[stack.back()] <= v[i]) {
int sn = int(stack.size());
tree.change(stack[sn - 1], stack[sn - 2], -v[stack[sn - 1]]);
stack.pop_back();
}
int nxt = stack.back();
tree.change(i, nxt, v[i]);
stack.push_back(i);
for (auto& [r, qi] : queries[i]) {
ans[qi] = tree.query(i, r);
}
}
for (auto& u : ans)
cout << u << '\n';
}

Test details

Test 1

Group: 1, 2, 3

Verdict:

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
4472
-1467
-446
...
Truncated

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
47617665959432
2811326406743602830
3058000851192173630
-3275266340225354533
-1483822867294
...
Truncated

Test 3

Group: 3

Verdict:

input
200000 200000
818377786 934884634 816080381 ...

correct output
86877225712611
94684086875470
92703793485296
38149694892093
61948503092286
...

user output
785712695293432231
3427498201488080311
-4557656094392947405
7596051565013302193
2088576886
...
Truncated

Test 4

Group: 3

Verdict: ACCEPTED

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
0
0
0
0
0
...
Truncated

Test 5

Group: 3

Verdict:

input
200000 200000
200000 199999 199998 199997 19...

correct output
15920862903
3193483321
18874982071
4846348926
3970697055
...

user output
7409846180908466312
-3874615635396525196
-3674906031228355284
-6879861249601282541
-201010
...
Truncated