#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define y1 zck_is_king
#define pii pair<ll, ll>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif
const int iinf = 1e9+10;
const ll inf = LLONG_MAX;
const ll mod = 1e9+7 ;
void GG(){cout<<"0\n"; exit(0);}
ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
ll re=1;
while (n>0){
if (n&1) re = re*a %mo;
a = a*a %mo;
n>>=1;
}
return re;
}
ll inv (ll b, ll mo = mod){
if (b==1) return b;
return (mo-mo/b) * inv(mo%b,mo) % mo;
}
const int maxn = 2e5+5;
vector<pii> stars[maxn]; // stars in my region
int lc[maxn], rc[maxn], par[maxn];
int fa[21][maxn];
int L[maxn], R[maxn];
bool on[maxn];
namespace DSU{
int par[maxn];
int find(int x) {
return x == par[x]? x : par[x] = find(par[x]);
}
void mrg(int a, int b) { // a is the new head
a = find(a); b = find(b);
MN(L[a], L[b]); MX(R[a], R[b]);
par[b] = a;
}
};
int a[maxn];
ll dp[maxn];
ll s[maxn];
void MO(int e, ll v) {
for(++e; e<maxn; e+=e&-e) s[e] += v;
}
ll QU(int e) {
ll re = 0;
for(++e; e>0; e-=e&-e) {
re += s[e];
}
return re;
}
signed main(){
IOS();
int n; cin>>n;
vector<pii> who;
REP(i,n) {
cin>>a[i]; who.pb({a[i], i}); DSU::par[i] = par[i] = L[i] = R[i] = i;
}
sort(ALL(who), [&](pii x, pii y){return x.f==y.f?x.s>y.s:x.f<y.f;});
for(pii p : who) {
int i = p.s;
lc[i] = rc[i] = n+1;
if(i && on[i-1]) {
lc[i] = DSU::find(i-1); par[lc[i]] = i;
DSU::mrg(i, i-1);
}
if(i!=n-1 && on[i+1]) {
rc[i] = DSU::find(i+1); par[rc[i]] = i;
DSU::mrg(i, i+1);
}
on[i] = 1;
bug(i, lc[i], rc[i]);
}
for(int j = 0; j<20; ++j) {
REP(i,n) {
if(j == 0) fa[j][i] = par[i];
else fa[j][i] = fa[j-1][fa[j-1][i]];
}
}
bug(par[6], par[5]);
int m; cin>>m;
ll sig = 0;
REP(i,m) {
int x,y,v; cin>>x>>y>>v; --x; sig += v;
int at = x;
for(int j = 19; j>=0; --j) {
if(a[fa[j][at]] < y) at = fa[j][at];
}
bug(i, x, at, fa[0][at]);
stars[at].pb({x,v});
}
ll re = 0;
REP(ind,n) {
int i = who[ind].s;
dp[i] = dp[lc[i]] + dp[rc[i]];
for(pii S : stars[i]) {
if(S.f == i) {
MX(dp[i], dp[lc[i]] + dp[rc[i]] + S.s);
}else if (S.f < i) {
MX(dp[i], QU(S.f) + dp[rc[i]] + S.s);
}else if (S.f > i) {
MX(dp[i], QU(S.f) + dp[lc[i]] + S.s);
}
}
MO(i, dp[lc[i]] + dp[rc[i]]);
MO(i+1, -(dp[lc[i]] + dp[rc[i]]));
bug(i, dp[i]);
MX(re, dp[i]);
if (lc[i] != n+1 && rc[i] != n+1) {
MO(L[lc[i]], dp[rc[i]]);
MO(R[lc[i]]+1, -dp[rc[i]]);
MO(L[rc[i]], dp[lc[i]]);
MO(R[rc[i]]+1, -dp[lc[i]]);
}
}
cout<<sig - re<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5324 KB |
Output is correct |
4 |
Correct |
3 ms |
5324 KB |
Output is correct |
5 |
Correct |
3 ms |
5276 KB |
Output is correct |
6 |
Correct |
3 ms |
5324 KB |
Output is correct |
7 |
Correct |
3 ms |
5324 KB |
Output is correct |
8 |
Correct |
3 ms |
5324 KB |
Output is correct |
9 |
Correct |
3 ms |
5284 KB |
Output is correct |
10 |
Correct |
3 ms |
5280 KB |
Output is correct |
11 |
Correct |
3 ms |
5324 KB |
Output is correct |
12 |
Correct |
3 ms |
5324 KB |
Output is correct |
13 |
Correct |
3 ms |
5324 KB |
Output is correct |
14 |
Correct |
3 ms |
5324 KB |
Output is correct |
15 |
Correct |
3 ms |
5224 KB |
Output is correct |
16 |
Correct |
3 ms |
5324 KB |
Output is correct |
17 |
Correct |
3 ms |
5324 KB |
Output is correct |
18 |
Correct |
3 ms |
5324 KB |
Output is correct |
19 |
Correct |
4 ms |
5324 KB |
Output is correct |
20 |
Correct |
3 ms |
5324 KB |
Output is correct |
21 |
Correct |
3 ms |
5324 KB |
Output is correct |
22 |
Correct |
3 ms |
5280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5324 KB |
Output is correct |
4 |
Correct |
3 ms |
5324 KB |
Output is correct |
5 |
Correct |
3 ms |
5276 KB |
Output is correct |
6 |
Correct |
3 ms |
5324 KB |
Output is correct |
7 |
Correct |
3 ms |
5324 KB |
Output is correct |
8 |
Correct |
3 ms |
5324 KB |
Output is correct |
9 |
Correct |
3 ms |
5284 KB |
Output is correct |
10 |
Correct |
3 ms |
5280 KB |
Output is correct |
11 |
Correct |
3 ms |
5324 KB |
Output is correct |
12 |
Correct |
3 ms |
5324 KB |
Output is correct |
13 |
Correct |
3 ms |
5324 KB |
Output is correct |
14 |
Correct |
3 ms |
5324 KB |
Output is correct |
15 |
Correct |
3 ms |
5224 KB |
Output is correct |
16 |
Correct |
3 ms |
5324 KB |
Output is correct |
17 |
Correct |
3 ms |
5324 KB |
Output is correct |
18 |
Correct |
3 ms |
5324 KB |
Output is correct |
19 |
Correct |
4 ms |
5324 KB |
Output is correct |
20 |
Correct |
3 ms |
5324 KB |
Output is correct |
21 |
Correct |
3 ms |
5324 KB |
Output is correct |
22 |
Correct |
3 ms |
5280 KB |
Output is correct |
23 |
Correct |
4 ms |
5708 KB |
Output is correct |
24 |
Correct |
5 ms |
5708 KB |
Output is correct |
25 |
Correct |
4 ms |
5708 KB |
Output is correct |
26 |
Correct |
5 ms |
5708 KB |
Output is correct |
27 |
Correct |
5 ms |
5796 KB |
Output is correct |
28 |
Correct |
4 ms |
5708 KB |
Output is correct |
29 |
Correct |
5 ms |
5708 KB |
Output is correct |
30 |
Correct |
5 ms |
5708 KB |
Output is correct |
31 |
Correct |
5 ms |
5708 KB |
Output is correct |
32 |
Correct |
5 ms |
5708 KB |
Output is correct |
33 |
Correct |
5 ms |
5708 KB |
Output is correct |
34 |
Correct |
4 ms |
5796 KB |
Output is correct |
35 |
Correct |
7 ms |
5708 KB |
Output is correct |
36 |
Correct |
4 ms |
5708 KB |
Output is correct |
37 |
Correct |
4 ms |
5836 KB |
Output is correct |
38 |
Correct |
4 ms |
5708 KB |
Output is correct |
39 |
Correct |
4 ms |
5708 KB |
Output is correct |
40 |
Correct |
4 ms |
5708 KB |
Output is correct |
41 |
Correct |
4 ms |
5708 KB |
Output is correct |
42 |
Correct |
5 ms |
5708 KB |
Output is correct |
43 |
Correct |
4 ms |
5708 KB |
Output is correct |
44 |
Correct |
4 ms |
5708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5324 KB |
Output is correct |
4 |
Correct |
3 ms |
5324 KB |
Output is correct |
5 |
Correct |
3 ms |
5276 KB |
Output is correct |
6 |
Correct |
3 ms |
5324 KB |
Output is correct |
7 |
Correct |
3 ms |
5324 KB |
Output is correct |
8 |
Correct |
3 ms |
5324 KB |
Output is correct |
9 |
Correct |
3 ms |
5284 KB |
Output is correct |
10 |
Correct |
3 ms |
5280 KB |
Output is correct |
11 |
Correct |
3 ms |
5324 KB |
Output is correct |
12 |
Correct |
3 ms |
5324 KB |
Output is correct |
13 |
Correct |
3 ms |
5324 KB |
Output is correct |
14 |
Correct |
3 ms |
5324 KB |
Output is correct |
15 |
Correct |
3 ms |
5224 KB |
Output is correct |
16 |
Correct |
3 ms |
5324 KB |
Output is correct |
17 |
Correct |
3 ms |
5324 KB |
Output is correct |
18 |
Correct |
3 ms |
5324 KB |
Output is correct |
19 |
Correct |
4 ms |
5324 KB |
Output is correct |
20 |
Correct |
3 ms |
5324 KB |
Output is correct |
21 |
Correct |
3 ms |
5324 KB |
Output is correct |
22 |
Correct |
3 ms |
5280 KB |
Output is correct |
23 |
Correct |
4 ms |
5708 KB |
Output is correct |
24 |
Correct |
5 ms |
5708 KB |
Output is correct |
25 |
Correct |
4 ms |
5708 KB |
Output is correct |
26 |
Correct |
5 ms |
5708 KB |
Output is correct |
27 |
Correct |
5 ms |
5796 KB |
Output is correct |
28 |
Correct |
4 ms |
5708 KB |
Output is correct |
29 |
Correct |
5 ms |
5708 KB |
Output is correct |
30 |
Correct |
5 ms |
5708 KB |
Output is correct |
31 |
Correct |
5 ms |
5708 KB |
Output is correct |
32 |
Correct |
5 ms |
5708 KB |
Output is correct |
33 |
Correct |
5 ms |
5708 KB |
Output is correct |
34 |
Correct |
4 ms |
5796 KB |
Output is correct |
35 |
Correct |
7 ms |
5708 KB |
Output is correct |
36 |
Correct |
4 ms |
5708 KB |
Output is correct |
37 |
Correct |
4 ms |
5836 KB |
Output is correct |
38 |
Correct |
4 ms |
5708 KB |
Output is correct |
39 |
Correct |
4 ms |
5708 KB |
Output is correct |
40 |
Correct |
4 ms |
5708 KB |
Output is correct |
41 |
Correct |
4 ms |
5708 KB |
Output is correct |
42 |
Correct |
5 ms |
5708 KB |
Output is correct |
43 |
Correct |
4 ms |
5708 KB |
Output is correct |
44 |
Correct |
4 ms |
5708 KB |
Output is correct |
45 |
Correct |
630 ms |
64020 KB |
Output is correct |
46 |
Correct |
613 ms |
63360 KB |
Output is correct |
47 |
Correct |
655 ms |
62972 KB |
Output is correct |
48 |
Correct |
637 ms |
64024 KB |
Output is correct |
49 |
Correct |
627 ms |
62392 KB |
Output is correct |
50 |
Correct |
621 ms |
62396 KB |
Output is correct |
51 |
Correct |
647 ms |
62440 KB |
Output is correct |
52 |
Correct |
654 ms |
63816 KB |
Output is correct |
53 |
Correct |
621 ms |
63548 KB |
Output is correct |
54 |
Correct |
607 ms |
64048 KB |
Output is correct |
55 |
Correct |
645 ms |
63040 KB |
Output is correct |
56 |
Correct |
613 ms |
62344 KB |
Output is correct |
57 |
Correct |
585 ms |
61836 KB |
Output is correct |
58 |
Correct |
428 ms |
62004 KB |
Output is correct |
59 |
Correct |
426 ms |
61464 KB |
Output is correct |
60 |
Correct |
142 ms |
63172 KB |
Output is correct |
61 |
Correct |
525 ms |
63184 KB |
Output is correct |
62 |
Correct |
631 ms |
62208 KB |
Output is correct |
63 |
Correct |
548 ms |
62548 KB |
Output is correct |
64 |
Correct |
510 ms |
61196 KB |
Output is correct |
65 |
Correct |
574 ms |
62748 KB |
Output is correct |
66 |
Correct |
534 ms |
61844 KB |
Output is correct |