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;
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; --i)
#define REP(i, a, b) for(int i = (a), _b = (b); i < _b; ++i)
#define REPD(i, b, a) for(int i = (b), _a = (a); i > _a; --i)
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define all(val) val.begin(), val.end()
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(v) (int)v.size()
#define fi first
#define se second
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {return l + rng() % (r - l + 1);}
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} else return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} else return false;
}
template<class T>
T Abs(const T &x) {
return (x < 0 ? -x : x);
}
const int mod = 1e9 + 7;
const int inf = 1e9;
const int N = 2e5 + 5;
const int K = 19;
struct fenwicktree {
int n;
vector<ll> bit;
fenwicktree(int _n) {
n = _n;
bit.assign(n + 1, 0);
}
void upd(int i, ll val) {
for(; i <= n; i += i & -i) bit[i] += val;
}
void upd(int l, int r, ll val) {
upd(l, val);
upd(r + 1, -val);
}
ll get(int i) {
ll res(0);
for(; i; i -= i & -i) res += bit[i];
return res;
}
};
int n, m, timeDFS;
int a[N], P[N][K], in[N], out[N];
vector<int> g[N];
vector<pii> stars[N];
pii rmq[N][K];
ll dp[N];
int getroot(int l, int r) {
int k = 31 - __builtin_clz(r - l + 1);
return -max(rmq[l][k], rmq[r - MASK(k) + 1][k]).se;
}
int dfs(int l, int r, int par = 0) {
if(l > r) return 0;
int root = getroot(l, r);
in[root] = ++timeDFS;
g[par].push_back(root);
P[root][0] = par;
REP(i, 1, K) P[root][i] = P[P[root][i - 1]][i - 1];
dfs(l, root - 1, root);
dfs(root + 1, r, root);
out[root] = timeDFS;
return root;
}
void process() {
cin >> n;
FOR(i, 1, n) {
cin >> a[i];
rmq[i][0] = {a[i], -i};
}
REP(j, 1, K) FOR(i, 1, n - MASK(j) + 1) {
rmq[i][j] = max(rmq[i][j - 1], rmq[i + MASK(j - 1)][j - 1]);
}
int root = dfs(1, n);
ll TotalCost(0);
cin >> m;
FOR(i, 1, m) {
int x, y, w; cin >> x >> y >> w;
TotalCost += w;
int u = x;
FORD(i, K - 1, 0) if(P[u][i] > 0 && a[P[u][i]] < y)
u = P[u][i];
stars[u].emplace_back(x, w);
}
fenwicktree bit(n);
function<ll(int)> solve = [&] (int root) {
for(int v : g[root])
dp[root] += solve(v);
bit.upd(in[root], out[root], dp[root]);
for(auto [v, w] : stars[root]) {
maximize(dp[root], w + bit.get(in[v]));
}
bit.upd(in[root], out[root], -dp[root]);
return dp[root];
};
cout << TotalCost - solve(root) << '\n';
}
signed main() {
if(fopen("test.inp","r")) {
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
}
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int ntests = 1;
// cin >> ntests;
while(ntests--) process();
// cerr << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s!\n";
return 0;
}
// https://oj.uz/problem/view/JOI20_constellation3
Compilation message (stderr)
constellation3.cpp: In function 'int main()':
constellation3.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen("test.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
constellation3.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen("test.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |