#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 12, MOD = (int)1e9 + 7;
#define int ll
int n, m, a[N], it[N];
pair<ll, ll> g[N];
vector<pair<int, int>> t[N];
vector<int> p[N];
ll f[N * 4], add[N * 4];
void inc(int v, int val) {
f[v] += val;
add[v] += val;
}
void push(int v) {
if(add[v]) {
inc(v + v, add[v]);
inc(v + v + 1, add[v]);
add[v] = 0;
}
}
void upd(int l, int r, int val, int v = 1, int tl = 1, int tr = n) {
if(l > r || tl > r || l > tr) return;
if(tl >= l && tr <= r) {
inc(v, val);
} else {
push(v);
int tm = (tl + tr) >> 1;
upd(l, r, val, v + v, tl, tm);
upd(l, r, val, v + v + 1, tm + 1, tr);
}
}
ll get(int pos, int v = 1, int tl = 1, int tr = n) {
if(tl == tr) {
return f[v];
} else {
push(v);
int tm = (tl + tr) >> 1;
if(pos <= tm) return get(pos, v + v, tl, tm);
return get(pos, v + v + 1, tm + 1, tr);
}
}
void test() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
a[i] = n - a[i];
p[a[i]].push_back(i);
}
cin >> m;
for(int i = 1; i <= m; i++) {
int x, y, c;
cin >> x >> y >> c;
y = n - y + 1;
t[y].push_back({x, c});
}
ll ans = 0;
set<pair<int, int>> st;
auto ins = [&](int i){
auto it = st.lower_bound({i, 0});
int l = i, r = i;
if(it != st.end() && (*it).first == i + 1) {
r = (*it).second;
st.erase(it);
}
auto j = st.lower_bound({i, 0});
if(j != st.begin()) {
j--;
if((*j).second == i - 1) {
l = (*j).first;
st.erase(j);
}
}
st.insert({l, r});
};
for(int i = n; i >= 1; i--) {
for(int j:p[i]) {
ins(j);
}
for(auto [x, c]:t[i]) {
ll v = get(x);
if(v < c) {
ans += v;
auto it = st.upper_bound({x, 1e9});
it--;
auto [l, r] = (*it);
assert(l <= x && x <= r);
upd(l, r, c - v);
} else {
ans += c;
}
}
}
cout << ans << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9712 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9676 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9916 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9784 KB |
Output is correct |
10 |
Correct |
5 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
3 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9820 KB |
Output is correct |
15 |
Correct |
3 ms |
9868 KB |
Output is correct |
16 |
Correct |
3 ms |
9820 KB |
Output is correct |
17 |
Correct |
3 ms |
9820 KB |
Output is correct |
18 |
Correct |
3 ms |
9864 KB |
Output is correct |
19 |
Correct |
4 ms |
9816 KB |
Output is correct |
20 |
Correct |
3 ms |
9880 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
5 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9712 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9676 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9916 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9784 KB |
Output is correct |
10 |
Correct |
5 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
3 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9820 KB |
Output is correct |
15 |
Correct |
3 ms |
9868 KB |
Output is correct |
16 |
Correct |
3 ms |
9820 KB |
Output is correct |
17 |
Correct |
3 ms |
9820 KB |
Output is correct |
18 |
Correct |
3 ms |
9864 KB |
Output is correct |
19 |
Correct |
4 ms |
9816 KB |
Output is correct |
20 |
Correct |
3 ms |
9880 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
5 ms |
9820 KB |
Output is correct |
23 |
Correct |
5 ms |
10076 KB |
Output is correct |
24 |
Correct |
6 ms |
10076 KB |
Output is correct |
25 |
Correct |
6 ms |
9876 KB |
Output is correct |
26 |
Correct |
7 ms |
10076 KB |
Output is correct |
27 |
Correct |
5 ms |
10076 KB |
Output is correct |
28 |
Correct |
5 ms |
10076 KB |
Output is correct |
29 |
Correct |
5 ms |
10076 KB |
Output is correct |
30 |
Correct |
5 ms |
10132 KB |
Output is correct |
31 |
Correct |
6 ms |
10076 KB |
Output is correct |
32 |
Correct |
8 ms |
10072 KB |
Output is correct |
33 |
Correct |
5 ms |
9916 KB |
Output is correct |
34 |
Correct |
5 ms |
10076 KB |
Output is correct |
35 |
Correct |
5 ms |
10076 KB |
Output is correct |
36 |
Correct |
4 ms |
10076 KB |
Output is correct |
37 |
Correct |
5 ms |
10076 KB |
Output is correct |
38 |
Correct |
5 ms |
10076 KB |
Output is correct |
39 |
Correct |
5 ms |
10076 KB |
Output is correct |
40 |
Correct |
8 ms |
9904 KB |
Output is correct |
41 |
Correct |
5 ms |
10076 KB |
Output is correct |
42 |
Correct |
5 ms |
9856 KB |
Output is correct |
43 |
Correct |
5 ms |
10076 KB |
Output is correct |
44 |
Correct |
5 ms |
10076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9712 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9676 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9916 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9784 KB |
Output is correct |
10 |
Correct |
5 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
3 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9820 KB |
Output is correct |
15 |
Correct |
3 ms |
9868 KB |
Output is correct |
16 |
Correct |
3 ms |
9820 KB |
Output is correct |
17 |
Correct |
3 ms |
9820 KB |
Output is correct |
18 |
Correct |
3 ms |
9864 KB |
Output is correct |
19 |
Correct |
4 ms |
9816 KB |
Output is correct |
20 |
Correct |
3 ms |
9880 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
5 ms |
9820 KB |
Output is correct |
23 |
Correct |
5 ms |
10076 KB |
Output is correct |
24 |
Correct |
6 ms |
10076 KB |
Output is correct |
25 |
Correct |
6 ms |
9876 KB |
Output is correct |
26 |
Correct |
7 ms |
10076 KB |
Output is correct |
27 |
Correct |
5 ms |
10076 KB |
Output is correct |
28 |
Correct |
5 ms |
10076 KB |
Output is correct |
29 |
Correct |
5 ms |
10076 KB |
Output is correct |
30 |
Correct |
5 ms |
10132 KB |
Output is correct |
31 |
Correct |
6 ms |
10076 KB |
Output is correct |
32 |
Correct |
8 ms |
10072 KB |
Output is correct |
33 |
Correct |
5 ms |
9916 KB |
Output is correct |
34 |
Correct |
5 ms |
10076 KB |
Output is correct |
35 |
Correct |
5 ms |
10076 KB |
Output is correct |
36 |
Correct |
4 ms |
10076 KB |
Output is correct |
37 |
Correct |
5 ms |
10076 KB |
Output is correct |
38 |
Correct |
5 ms |
10076 KB |
Output is correct |
39 |
Correct |
5 ms |
10076 KB |
Output is correct |
40 |
Correct |
8 ms |
9904 KB |
Output is correct |
41 |
Correct |
5 ms |
10076 KB |
Output is correct |
42 |
Correct |
5 ms |
9856 KB |
Output is correct |
43 |
Correct |
5 ms |
10076 KB |
Output is correct |
44 |
Correct |
5 ms |
10076 KB |
Output is correct |
45 |
Correct |
307 ms |
37356 KB |
Output is correct |
46 |
Correct |
282 ms |
37200 KB |
Output is correct |
47 |
Correct |
269 ms |
37460 KB |
Output is correct |
48 |
Correct |
282 ms |
37200 KB |
Output is correct |
49 |
Correct |
261 ms |
37136 KB |
Output is correct |
50 |
Correct |
274 ms |
36944 KB |
Output is correct |
51 |
Correct |
245 ms |
36944 KB |
Output is correct |
52 |
Correct |
262 ms |
37456 KB |
Output is correct |
53 |
Correct |
242 ms |
37172 KB |
Output is correct |
54 |
Correct |
107 ms |
36440 KB |
Output is correct |
55 |
Correct |
110 ms |
36208 KB |
Output is correct |
56 |
Correct |
148 ms |
35924 KB |
Output is correct |
57 |
Correct |
110 ms |
36168 KB |
Output is correct |
58 |
Correct |
104 ms |
31824 KB |
Output is correct |
59 |
Correct |
110 ms |
31872 KB |
Output is correct |
60 |
Correct |
91 ms |
31060 KB |
Output is correct |
61 |
Correct |
125 ms |
31040 KB |
Output is correct |
62 |
Correct |
137 ms |
33872 KB |
Output is correct |
63 |
Correct |
122 ms |
30512 KB |
Output is correct |
64 |
Correct |
116 ms |
30704 KB |
Output is correct |
65 |
Correct |
138 ms |
33616 KB |
Output is correct |
66 |
Correct |
119 ms |
30456 KB |
Output is correct |