Task: | Kyselyt |
Sender: | hltk |
Submission time: | 2020-10-16 19:47:10 +0300 |
Language: | C++ (C++17) |
Status: | READY |
Result: | 100 |
group | verdict | score |
---|---|---|
#1 | ACCEPTED | 12 |
#2 | ACCEPTED | 34 |
#3 | ACCEPTED | 54 |
test | verdict | time | group | |
---|---|---|---|---|
#1 | ACCEPTED | 0.01 s | 1, 2, 3 | details |
#2 | ACCEPTED | 0.39 s | 2, 3 | details |
#3 | ACCEPTED | 0.40 s | 3 | details |
#4 | ACCEPTED | 0.33 s | 3 | details |
#5 | ACCEPTED | 0.38 s | 3 | details |
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 (y <= l || r <= x)return 0;if (l <= x && y <= r)return p[s];push(s, y - x);int m = (x + y) / 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 (y <= l || r <= x)return;if (l <= x && y <= r)return apply(s, y - x, k);push(s, y - x);int m = (x + y) / 2;change(s * 2, l, r, x, m, k);change(s * 2 + 1, l, r, m, y, k);pull(s);}void push(int s, int len) {apply(s * 2, len / 2, l[s]);apply(s * 2 + 1, len / 2, l[s]);l[s] = 0;}void apply(int s, int len, long x) {p[s] += x * len;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<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<long> ans(q);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();}tree.change(i, stack.back(), 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: 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 ... Truncated |
Test 2
Group: 2, 3
Verdict: ACCEPTED
input |
---|
200000 200000 98 99 29 92 29 81 100 52 89 80... |
correct output |
---|
1497732 2810356 9532632 6655773 5403513 ... |
user output |
---|
1497732 2810356 9532632 6655773 5403513 ... Truncated |
Test 3
Group: 3
Verdict: ACCEPTED
input |
---|
200000 200000 818377786 934884634 816080381 ... |
correct output |
---|
86877225712611 94684086875470 92703793485296 38149694892093 61948503092286 ... |
user output |
---|
86877225712611 94684086875470 92703793485296 38149694892093 61948503092286 ... 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: ACCEPTED
input |
---|
200000 200000 200000 199999 199998 199997 19... |
correct output |
---|
15920862903 3193483321 18874982071 4846348926 3970697055 ... |
user output |
---|
15920862903 3193483321 18874982071 4846348926 3970697055 ... Truncated |