제출 #218983

#제출 시각아이디문제언어결과실행 시간메모리
218983nvmdava별자리 3 (JOI20_constellation3)C++17
100 / 100
321 ms74004 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ff first #define ss second mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define N 200005 #define INF 0x3f3f3f3f #define MOD 1000000007LL int a[N]; int l[N], r[N]; vector<pair<int, int> > st[N], tmp; map<int, ll> ans[N]; ll c[N]; ll res = 0; void insert(int v, int y, ll s){ if((--ans[v].upper_bound(y)) -> ss >= s) return; ans[v][y] = s; auto it = ans[v].upper_bound(y); while(it != ans[v].end() && it -> ss <= s) it = ans[v].erase(it); } void dfs(int v){ int l = ::l[v]; int r = ::r[v]; if(l) dfs(l); if(r) dfs(r); ll L, R; L = c[l] + (--ans[l].upper_bound(a[v])) -> ss; R = c[r] + (--ans[r].upper_bound(a[v])) -> ss; c[l] += R; c[r] += L; if(ans[l].size() > ans[r].size()) swap(l, r); swap(ans[r], ans[v]); swap(c[r], c[v]); if(!l){ c[0] = 0; ans[0].clear(); ans[0][1] = 0; } else { for(auto& x : ans[l]) insert(v, x.ff, x.ss + c[l] - c[v]); } for(auto& x : st[v]) insert(v, x.ff, x.ss + L + R - c[v]); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; ans[0][1] = 0; for(int i = 1; i <= n; ++i){ ans[i][1] = 0; cin>>a[i]; while(!tmp.empty() && tmp.back().ff < a[i]){ l[i] = tmp.back().ss; tmp.pop_back(); } if(!tmp.empty()) r[tmp.back().ss] = i; tmp.push_back({a[i], i}); } int q; cin>>q; while(q--){ int x, y, s; cin>>x>>y>>s; res += s; st[x].push_back({y, s}); } dfs(tmp[0].ss); res -= c[tmp[0].ss] + (--ans[tmp[0].ss].end()) -> ss; cout<<res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...