This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 ll mod = 1e9+7;
const int maxn = 1e5 + 5;
const ll inf = 1e18;
struct MaxLazySegmentTree {
vector<ll> t, o;
void init(int n) {
n += 10;
t.resize(4*n);
o.resize(4*n);
}
void push(int v) {
t[2*v]+=o[v];
t[2*v+1]+=o[v];
o[2*v]+=o[v];
o[2*v+1]+=o[v];
o[v]=0;
}
ll queryMax(int v, int tl, int tr, int l, int r) {
if (l>tr||r<tl) return -inf;
if (l<=tl&&tr<=r) {
return t[v];
} else {
push(v);
int tm=(tl+tr)/2;
return max(queryMax(2*v,tl,tm,l,r),queryMax(2*v+1,tm+1,tr,l,r));
}
}
void rangeAdd(int v, int tl, int tr, int l, int r, ll dx) {
if (l>r) return;
if (l>tr||r<tl) return;
if (l<=tl&&tr<=r) {
t[v]+=dx;
o[v]+=dx;
} else {
push(v);
int tm=(tl+tr)/2;
rangeAdd(2*v,tl,tm,l,r,dx);
rangeAdd(2*v+1,tm+1,tr,l,r,dx);
t[v]=max(t[2*v],t[2*v+1]);
}
}
};
int n, x[maxn], y[maxn];
int m;
int a[maxn], b[maxn], c[maxn];
vector<int> ev[maxn];
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n;
for (int i=0; i<n-1; i++) {
cin>>x[i]>>y[i];
assert(x[i]==i+1);
assert(y[i]==x[i]+1);
}
cin>>m;
for (int i=0; i<m; i++) {
cin>>a[i]>>b[i]>>c[i];
if (a[i]>b[i]) swap(a[i],b[i]);
ev[b[i]].push_back(i);
}
MaxLazySegmentTree dp;
dp.init(n+10);
auto qry = [&](int i) {
if (i==0) return 0ll;
return dp.queryMax(1,1,n,1,i);
};
auto upd = [&](int i, ll val) {
ll cur = dp.queryMax(1,1,n,i,i);
if (val <= cur) return;
ll dx = val-cur;
dp.rangeAdd(1,1,n,i,i,dx);
};
for (int i=1; i<=n; i++) {
for (int idx: ev[i]) {
int l = a[idx];
int r = b[idx];
int val = c[idx];
upd(r, val+qry(l-1));
}
}
ll res = qry(n);
cout<<res<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |