#include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const int maxn = 2e5 + 10;
int n;
ll a[maxn];
int m;
vector<pair<pair<ll,ll>,ll>> stars; //((y,x),c)
vector<int> byX[maxn];
int pre[maxn], nex[maxn];
struct segtree {
int n;
vector<ll> t, o;
void init(int _n) {
n=_n+10;
t.assign(4*n,0);
o.assign(4*n,0);
}
void push(int v, int tl, int tr) {
if (!o[v]) return;
int tm=(tl+tr)/2;
t[2*v] += 1ll*(tm-tl+1)*o[v];
t[2*v+1] += 1ll*(tr-tm)*o[v];
o[2*v] += o[v];
o[2*v+1] += o[v];
o[v] = 0;
}
ll qry(int v, int tl, int tr, int l, int r) {
if (l>tr || r<tl) return 0;
if (l<=tl && tr<=r) return t[v];
push(v,tl,tr);
int tm=(tl+tr)/2;
return qry(2*v,tl,tm,l,r)+qry(2*v+1,tm+1,tr,l,r);
}
void upd(int v, int tl, int tr, int l, int r, ll dx) {
if (l>tr || r<tl) return;
if (l<=tl && tr<=r) {
t[v] += 1ll*(tr-tl+1)*dx;
o[v] += dx;
} else {
push(v,tl,tr);
int tm=(tl+tr)/2;
upd(2*v,tl,tm,l,r,dx);
upd(2*v+1,tm+1,tr,l,r,dx);
t[v]=t[2*v]+t[2*v+1];
}
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n;
for (int i=1; i<=n; i++) {
cin>>a[i];
a[i]=n-a[i];
}
ll sum = 0;
cin>>m;
stars.resize(m);
for (int i=0; i<m; i++) {
cin>>stars[i].first.second>>stars[i].first.first>>stars[i].second;
stars[i].first.first = n-stars[i].first.first+1;
sum += stars[i].second;
}
sort(stars.rbegin(),stars.rend());
for (int i=0; i<m; i++) {
byX[stars[i].first.second].push_back(i);
}
set<pair<int,int>> st; //(a[i],i)
stack<int> stk;
st.insert({0,0});
stk.push(0); // monoincreasing
for (int i=1; i<=n; i++) {
while (!stk.empty() && a[i]<=a[stk.top()]) {
st.erase({a[stk.top()],stk.top()});
stk.pop();
}
for (int idx: byX[i]) {
int y = stars[idx].first.first;
pre[idx] = prev(st.upper_bound({y,-1}))->second+1;
}
stk.push(i);
st.insert({a[i],i});
}
while (stk.size()) stk.pop();
st.clear();
st.insert({0,n+1});
stk.push(n+1);
for (int i=n; i>=1; i--) {
while (!stk.empty() && a[i]<=a[stk.top()]) {
st.erase({a[stk.top()],stk.top()});
stk.pop();
}
for (int idx: byX[i]) {
int y = stars[idx].first.first;
nex[idx] = prev(st.upper_bound({y,-1}))->second-1;
}
stk.push(i);
st.insert({a[i],i});
}
// for (int i=0; i<m; i++) {
// cout<<i+1<<": "<<pre[i]<<" "<<nex[i]<<endl;
// }
segtree tree;
tree.init(n+10);
for (int i=0; i<m; i++) {
auto star = stars[i];
int x = star.first.second;
ll c = star.second;
ll val = c + tree.qry(1,1,n,x,x);
if (val>0) {
sum -= val;
tree.upd(1,1,n,pre[i],nex[i],-val);
}
}
cout<<sum<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5228 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
5 ms |
5376 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
22 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5228 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
5 ms |
5376 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
22 |
Correct |
4 ms |
5100 KB |
Output is correct |
23 |
Correct |
6 ms |
5356 KB |
Output is correct |
24 |
Correct |
7 ms |
5356 KB |
Output is correct |
25 |
Correct |
7 ms |
5356 KB |
Output is correct |
26 |
Correct |
6 ms |
5356 KB |
Output is correct |
27 |
Correct |
6 ms |
5356 KB |
Output is correct |
28 |
Correct |
6 ms |
5356 KB |
Output is correct |
29 |
Correct |
5 ms |
5356 KB |
Output is correct |
30 |
Correct |
6 ms |
5356 KB |
Output is correct |
31 |
Correct |
6 ms |
5356 KB |
Output is correct |
32 |
Correct |
6 ms |
5356 KB |
Output is correct |
33 |
Correct |
6 ms |
5356 KB |
Output is correct |
34 |
Correct |
6 ms |
5484 KB |
Output is correct |
35 |
Correct |
6 ms |
5356 KB |
Output is correct |
36 |
Correct |
5 ms |
5356 KB |
Output is correct |
37 |
Correct |
6 ms |
5356 KB |
Output is correct |
38 |
Correct |
6 ms |
5504 KB |
Output is correct |
39 |
Correct |
6 ms |
5356 KB |
Output is correct |
40 |
Correct |
7 ms |
5356 KB |
Output is correct |
41 |
Correct |
6 ms |
5368 KB |
Output is correct |
42 |
Correct |
6 ms |
5356 KB |
Output is correct |
43 |
Correct |
6 ms |
5356 KB |
Output is correct |
44 |
Correct |
6 ms |
5356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5228 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
5 ms |
5376 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
22 |
Correct |
4 ms |
5100 KB |
Output is correct |
23 |
Correct |
6 ms |
5356 KB |
Output is correct |
24 |
Correct |
7 ms |
5356 KB |
Output is correct |
25 |
Correct |
7 ms |
5356 KB |
Output is correct |
26 |
Correct |
6 ms |
5356 KB |
Output is correct |
27 |
Correct |
6 ms |
5356 KB |
Output is correct |
28 |
Correct |
6 ms |
5356 KB |
Output is correct |
29 |
Correct |
5 ms |
5356 KB |
Output is correct |
30 |
Correct |
6 ms |
5356 KB |
Output is correct |
31 |
Correct |
6 ms |
5356 KB |
Output is correct |
32 |
Correct |
6 ms |
5356 KB |
Output is correct |
33 |
Correct |
6 ms |
5356 KB |
Output is correct |
34 |
Correct |
6 ms |
5484 KB |
Output is correct |
35 |
Correct |
6 ms |
5356 KB |
Output is correct |
36 |
Correct |
5 ms |
5356 KB |
Output is correct |
37 |
Correct |
6 ms |
5356 KB |
Output is correct |
38 |
Correct |
6 ms |
5504 KB |
Output is correct |
39 |
Correct |
6 ms |
5356 KB |
Output is correct |
40 |
Correct |
7 ms |
5356 KB |
Output is correct |
41 |
Correct |
6 ms |
5368 KB |
Output is correct |
42 |
Correct |
6 ms |
5356 KB |
Output is correct |
43 |
Correct |
6 ms |
5356 KB |
Output is correct |
44 |
Correct |
6 ms |
5356 KB |
Output is correct |
45 |
Correct |
375 ms |
34796 KB |
Output is correct |
46 |
Correct |
364 ms |
34284 KB |
Output is correct |
47 |
Correct |
367 ms |
34796 KB |
Output is correct |
48 |
Correct |
374 ms |
34540 KB |
Output is correct |
49 |
Correct |
383 ms |
34084 KB |
Output is correct |
50 |
Correct |
362 ms |
34028 KB |
Output is correct |
51 |
Correct |
370 ms |
34156 KB |
Output is correct |
52 |
Correct |
379 ms |
34836 KB |
Output is correct |
53 |
Correct |
353 ms |
34412 KB |
Output is correct |
54 |
Correct |
455 ms |
38252 KB |
Output is correct |
55 |
Correct |
467 ms |
36972 KB |
Output is correct |
56 |
Correct |
476 ms |
36204 KB |
Output is correct |
57 |
Correct |
424 ms |
35820 KB |
Output is correct |
58 |
Correct |
340 ms |
34028 KB |
Output is correct |
59 |
Correct |
337 ms |
34028 KB |
Output is correct |
60 |
Correct |
231 ms |
43500 KB |
Output is correct |
61 |
Correct |
269 ms |
34284 KB |
Output is correct |
62 |
Correct |
547 ms |
37976 KB |
Output is correct |
63 |
Correct |
247 ms |
34180 KB |
Output is correct |
64 |
Correct |
265 ms |
33672 KB |
Output is correct |
65 |
Correct |
526 ms |
37996 KB |
Output is correct |
66 |
Correct |
256 ms |
34284 KB |
Output is correct |