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>
#ifdef LOCAL
#include <bits/debug.hpp>
#endif // LOCAL
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Constellation 3"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
int n, m, rt;
int a[maxn];
vector<pair<int, int>> store[maxn];
int cha[maxn][18], ma[maxn][18];
int In[maxn], Out[maxn];
ll dp[maxn][2];
struct dl { int p, c[2]; } T[maxn];
struct BIT {
ll bit[maxn];
void update(int l, int r, ll val) {
for( l; l < maxn; l += (l & -l)) bit[l] += val;
for(++ r; r < maxn; r += (r & -r)) bit[r] -= val;
}
ll get(int x, ll ans = 0) {
for(; x > 0; x -= (x & -x)) ans += bit[x];
return ans;
}
} Bit;
void BuildTree() {
a[0] = 1e9;
fi(i, 1, n) {
T[i].p = i - 1;
while(a[i] > a[T[i].p]) T[i].p = T[T[i].p].p;
T[i].c[0] = T[T[i].p].c[1];
T[T[i].c[0]].p = i;
T[T[i].p].c[1] = i;
}
fi(i, 1, n) if(T[i].p == 0) return rt = i, void();
}
void pre_dfs(int u) {
static int Time = 0;
In[u] = ++Time;
for(int v : T[u].c) if(v != 0) {
cha[v][0] = u, ma[v][0] = a[u];
fi(i, 1, 17) {
cha[v][i] = cha[cha[v][i - 1]][i - 1];
ma[v][i] = max(ma[v][i - 1], ma[cha[v][i - 1]][i - 1]);
}
pre_dfs(v);
}
Out[u] = Time;
}
void Add(int x, int h, int val) {
int u = x;
fid(i, 17, 0) if(ma[u][i] < h) u = cha[u][i];
store[u].push_back({x, val});
}
void dfs(int u) {
for(int v : T[u].c) if(v != 0) {
dfs(v);
dp[u][0] += dp[v][1];
}
Bit.update(In[u], Out[u], dp[u][0]);
dp[u][1] = dp[u][0];
for(auto [v, w] : store[u]) dp[u][1] = max(dp[u][1], Bit.get(In[v]) + w);
Bit.update(In[u], Out[u], -dp[u][1]);
}
void solve()
{
cin >> n;
fi(i, 1, n) cin >> a[i];
BuildTree();
memset(ma, 60, sizeof ma);
pre_dfs(rt);
cin >> m;
ll S = 0;
fi(i, 1, m) {
int x, y, val; cin >> x >> y >> val;
Add(x, y, val);
S += val;
}
dfs(rt);
cout << S - max(dp[rt][0], dp[rt][1]);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message (stderr)
constellation3.cpp: In member function 'void BIT::update(int, int, long long int)':
constellation3.cpp:35:16: warning: statement has no effect [-Wunused-value]
35 | for( l; l < maxn; l += (l & -l)) bit[l] += val;
| ^
constellation3.cpp: In function 'int main()':
constellation3.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
constellation3.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen(Neco".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... |