#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
typedef long long ll;
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (200006)
ll n,m,A[MAXN];
vector<pi> S[MAXN], row[MAXN];
vector<int> reveal[MAXN];
int p[MAXN], mi[MAXN], mx[MAXN];
struct ufds_ {
ufds_ () {
FOR(i,0,MAXN-1) p[i]=mi[i]=mx[i]=i;
}
void merge(int x,int y){
x=find(x),y=find(y);
if(x==y)return;
p[x]=y;
mx[y]=max(mx[y],mx[x]);
mi[y]=min(mi[y],mi[x]);
}
int find(int x) { return (p[x]==x) ? x : p[x]=find(p[x]); }
bool same(int a,int b) { return find(a)==find(b); }
} ufds;
struct node {
int s,e,m;
ll v=0,add=0;
node*l,*r;
node(int S,int E){
s=S,e=E,m=(s+e)>>1;
v=add=0;
if(s^e)l=new node(s,m),r=new node(m+1,e);
}
ll value(){
v+=add;
if(s^e){
l->add+=add;
r->add+=add;
}
add=0;
return v;
}
void update(int x,int y,ll nval){
if(x>y)return;
value();
if(s==x&&e==y){add+=nval;return;}
if(x>m)r->update(x,y,nval);
else if(y<=m)l->update(x,y,nval);
else l->update(x,m,nval),r->update(m+1,y,nval);
v=max(l->value(),r->value());
}
ll rmq(int x,int y){
value();
if(s==x&&e==y) return v;
if(x>m) return r->rmq(x,y);
else if(y<=m) return l->rmq(x,y);
else return max(l->rmq(x,m),r->rmq(m+1,y));
}
}*seg;
ll total;
int main(){
FAST
cin>>n;
FOR(i,1,n)cin>>A[i], reveal[A[i]+1].pb(i);
cin>>m;
FOR(i,1,m){
ll x,y,c;cin>>x>>y>>c;total+=c;
S[x].eb(y,c);
}
FOR(i,1,n){
sort(all(S[i]));
vector<pi> tmp;
FOR(j,0,siz(S[i])-1){
if(tmp.empty() || tmp.back().s < S[i][j].s) tmp.eb(S[i][j]);
}
S[i]=tmp;
DEC(j,siz(S[i])-1,1)S[i][j].s-=S[i][j-1].s;
for(auto j:S[i]) row[j.f].eb(i, j.s);
}
seg=new node(1,n);
FOR(i,1,n){
for(auto j:reveal[i]) {
if(j>1 && !ufds.same(j-1, j) && A[j-1]<=A[j]) { // j-1 and j
ll mx1=seg->rmq(mi[ufds.find(j-1)], mx[ufds.find(j-1)]);
ll mx2=seg->rmq(mi[ufds.find(j)],mx[ufds.find(j)]);
seg->update(mi[ufds.find(j-1)],mx[ufds.find(j-1)],mx2);
seg->update(mi[ufds.find(j)],mx[ufds.find(j)],mx1);
ufds.merge(j-1,j);
}
if(j<n && !ufds.same(j+1, j) && A[j+1]<=A[j]) { // j+1 and j
ll mx1=seg->rmq(mi[ufds.find(j+1)], mx[ufds.find(j+1)]);
ll mx2=seg->rmq(mi[ufds.find(j)],mx[ufds.find(j)]);
seg->update(mi[ufds.find(j+1)],mx[ufds.find(j+1)],mx2);
seg->update(mi[ufds.find(j)],mx[ufds.find(j)],mx1);
ufds.merge(j,j+1);
}
}
for(auto j:row[i]){
seg->update(j.f, j.f, j.s);
}
// cerr<<seg->rmq(1,n)<<'\n';
}
ll ans=0;
FOR(i,1,n)if(ufds.find(i)==i){
ans += seg->rmq(mi[i], mx[i]);
// cerr<<seg->rmq(mi[i],mx[i])<<'\n';
}
cout<<total-ans<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16896 KB |
Output is correct |
3 |
Correct |
15 ms |
17024 KB |
Output is correct |
4 |
Correct |
14 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16896 KB |
Output is correct |
6 |
Correct |
14 ms |
16896 KB |
Output is correct |
7 |
Correct |
14 ms |
16896 KB |
Output is correct |
8 |
Correct |
14 ms |
16896 KB |
Output is correct |
9 |
Correct |
14 ms |
16896 KB |
Output is correct |
10 |
Correct |
14 ms |
16896 KB |
Output is correct |
11 |
Correct |
14 ms |
16896 KB |
Output is correct |
12 |
Correct |
14 ms |
16896 KB |
Output is correct |
13 |
Correct |
14 ms |
16768 KB |
Output is correct |
14 |
Correct |
15 ms |
16896 KB |
Output is correct |
15 |
Correct |
15 ms |
16896 KB |
Output is correct |
16 |
Correct |
15 ms |
17024 KB |
Output is correct |
17 |
Correct |
14 ms |
16896 KB |
Output is correct |
18 |
Correct |
15 ms |
16896 KB |
Output is correct |
19 |
Correct |
14 ms |
16896 KB |
Output is correct |
20 |
Correct |
15 ms |
16896 KB |
Output is correct |
21 |
Correct |
14 ms |
16896 KB |
Output is correct |
22 |
Correct |
14 ms |
16896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16896 KB |
Output is correct |
3 |
Correct |
15 ms |
17024 KB |
Output is correct |
4 |
Correct |
14 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16896 KB |
Output is correct |
6 |
Correct |
14 ms |
16896 KB |
Output is correct |
7 |
Correct |
14 ms |
16896 KB |
Output is correct |
8 |
Correct |
14 ms |
16896 KB |
Output is correct |
9 |
Correct |
14 ms |
16896 KB |
Output is correct |
10 |
Correct |
14 ms |
16896 KB |
Output is correct |
11 |
Correct |
14 ms |
16896 KB |
Output is correct |
12 |
Correct |
14 ms |
16896 KB |
Output is correct |
13 |
Correct |
14 ms |
16768 KB |
Output is correct |
14 |
Correct |
15 ms |
16896 KB |
Output is correct |
15 |
Correct |
15 ms |
16896 KB |
Output is correct |
16 |
Correct |
15 ms |
17024 KB |
Output is correct |
17 |
Correct |
14 ms |
16896 KB |
Output is correct |
18 |
Correct |
15 ms |
16896 KB |
Output is correct |
19 |
Correct |
14 ms |
16896 KB |
Output is correct |
20 |
Correct |
15 ms |
16896 KB |
Output is correct |
21 |
Correct |
14 ms |
16896 KB |
Output is correct |
22 |
Correct |
14 ms |
16896 KB |
Output is correct |
23 |
Correct |
19 ms |
17152 KB |
Output is correct |
24 |
Correct |
18 ms |
17212 KB |
Output is correct |
25 |
Correct |
18 ms |
17152 KB |
Output is correct |
26 |
Correct |
18 ms |
17152 KB |
Output is correct |
27 |
Correct |
18 ms |
17152 KB |
Output is correct |
28 |
Correct |
18 ms |
17152 KB |
Output is correct |
29 |
Correct |
18 ms |
17152 KB |
Output is correct |
30 |
Correct |
19 ms |
17152 KB |
Output is correct |
31 |
Correct |
19 ms |
17152 KB |
Output is correct |
32 |
Correct |
19 ms |
17280 KB |
Output is correct |
33 |
Correct |
19 ms |
17280 KB |
Output is correct |
34 |
Correct |
18 ms |
17280 KB |
Output is correct |
35 |
Correct |
18 ms |
17280 KB |
Output is correct |
36 |
Correct |
18 ms |
17152 KB |
Output is correct |
37 |
Correct |
18 ms |
17152 KB |
Output is correct |
38 |
Correct |
18 ms |
17152 KB |
Output is correct |
39 |
Correct |
18 ms |
17152 KB |
Output is correct |
40 |
Correct |
18 ms |
17152 KB |
Output is correct |
41 |
Correct |
18 ms |
17152 KB |
Output is correct |
42 |
Correct |
18 ms |
17152 KB |
Output is correct |
43 |
Correct |
18 ms |
17152 KB |
Output is correct |
44 |
Correct |
18 ms |
17152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16896 KB |
Output is correct |
3 |
Correct |
15 ms |
17024 KB |
Output is correct |
4 |
Correct |
14 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16896 KB |
Output is correct |
6 |
Correct |
14 ms |
16896 KB |
Output is correct |
7 |
Correct |
14 ms |
16896 KB |
Output is correct |
8 |
Correct |
14 ms |
16896 KB |
Output is correct |
9 |
Correct |
14 ms |
16896 KB |
Output is correct |
10 |
Correct |
14 ms |
16896 KB |
Output is correct |
11 |
Correct |
14 ms |
16896 KB |
Output is correct |
12 |
Correct |
14 ms |
16896 KB |
Output is correct |
13 |
Correct |
14 ms |
16768 KB |
Output is correct |
14 |
Correct |
15 ms |
16896 KB |
Output is correct |
15 |
Correct |
15 ms |
16896 KB |
Output is correct |
16 |
Correct |
15 ms |
17024 KB |
Output is correct |
17 |
Correct |
14 ms |
16896 KB |
Output is correct |
18 |
Correct |
15 ms |
16896 KB |
Output is correct |
19 |
Correct |
14 ms |
16896 KB |
Output is correct |
20 |
Correct |
15 ms |
16896 KB |
Output is correct |
21 |
Correct |
14 ms |
16896 KB |
Output is correct |
22 |
Correct |
14 ms |
16896 KB |
Output is correct |
23 |
Correct |
19 ms |
17152 KB |
Output is correct |
24 |
Correct |
18 ms |
17212 KB |
Output is correct |
25 |
Correct |
18 ms |
17152 KB |
Output is correct |
26 |
Correct |
18 ms |
17152 KB |
Output is correct |
27 |
Correct |
18 ms |
17152 KB |
Output is correct |
28 |
Correct |
18 ms |
17152 KB |
Output is correct |
29 |
Correct |
18 ms |
17152 KB |
Output is correct |
30 |
Correct |
19 ms |
17152 KB |
Output is correct |
31 |
Correct |
19 ms |
17152 KB |
Output is correct |
32 |
Correct |
19 ms |
17280 KB |
Output is correct |
33 |
Correct |
19 ms |
17280 KB |
Output is correct |
34 |
Correct |
18 ms |
17280 KB |
Output is correct |
35 |
Correct |
18 ms |
17280 KB |
Output is correct |
36 |
Correct |
18 ms |
17152 KB |
Output is correct |
37 |
Correct |
18 ms |
17152 KB |
Output is correct |
38 |
Correct |
18 ms |
17152 KB |
Output is correct |
39 |
Correct |
18 ms |
17152 KB |
Output is correct |
40 |
Correct |
18 ms |
17152 KB |
Output is correct |
41 |
Correct |
18 ms |
17152 KB |
Output is correct |
42 |
Correct |
18 ms |
17152 KB |
Output is correct |
43 |
Correct |
18 ms |
17152 KB |
Output is correct |
44 |
Correct |
18 ms |
17152 KB |
Output is correct |
45 |
Correct |
948 ms |
56568 KB |
Output is correct |
46 |
Correct |
881 ms |
56056 KB |
Output is correct |
47 |
Correct |
906 ms |
55672 KB |
Output is correct |
48 |
Correct |
908 ms |
56440 KB |
Output is correct |
49 |
Correct |
872 ms |
55288 KB |
Output is correct |
50 |
Correct |
891 ms |
55288 KB |
Output is correct |
51 |
Correct |
892 ms |
55544 KB |
Output is correct |
52 |
Correct |
917 ms |
56312 KB |
Output is correct |
53 |
Correct |
891 ms |
56060 KB |
Output is correct |
54 |
Correct |
799 ms |
59000 KB |
Output is correct |
55 |
Correct |
802 ms |
58104 KB |
Output is correct |
56 |
Correct |
747 ms |
57848 KB |
Output is correct |
57 |
Correct |
732 ms |
57464 KB |
Output is correct |
58 |
Correct |
704 ms |
52600 KB |
Output is correct |
59 |
Correct |
708 ms |
52472 KB |
Output is correct |
60 |
Correct |
539 ms |
62200 KB |
Output is correct |
61 |
Correct |
662 ms |
52944 KB |
Output is correct |
62 |
Correct |
819 ms |
56568 KB |
Output is correct |
63 |
Correct |
647 ms |
52416 KB |
Output is correct |
64 |
Correct |
639 ms |
51924 KB |
Output is correct |
65 |
Correct |
831 ms |
56824 KB |
Output is correct |
66 |
Correct |
632 ms |
52176 KB |
Output is correct |