#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;
FOR(j,1,siz(S[i])-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';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16896 KB |
Output is correct |
2 |
Incorrect |
15 ms |
16768 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16896 KB |
Output is correct |
2 |
Incorrect |
15 ms |
16768 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16896 KB |
Output is correct |
2 |
Incorrect |
15 ms |
16768 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |