#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{" << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 200005;
int n,m;
pi A[MAXN];
int T;
vector<pi> V[MAXN];
struct SparseTable {
vector<vector<pi> > ST;
int N, K;
SparseTable(int _N, pi a[]): N(_N) {
K = MSB(N);
ST.resize(K);
ST[0] = vector<pi>(a, a+N);
for (int k = 1; k < K; ++k)
for (int i = 0; i+(1<<k) <= N; ++i)
ST[k].push_back( min(ST[k-1][i], ST[k-1][i+(1<<(k-1))]) ); //min
}
/* returns most significant bit of an integer */
inline int MSB(unsigned int x) { return 32-__builtin_clz(x); }
/* Min of range (x, x + 2^k-1) and (y-2^k+1, y) */
pi query(int x, int y) {
int k = MSB(y-x+1) - 1;
return min(ST[k][x], ST[k][y-(1<<k)+1]);
}
}*ST;
int twok[MAXN][20];
vector<int> adj[MAXN];
int st[MAXN];
int en[MAXN];
int cnt =1;
int Val[MAXN];
void dnc(int l, int r, int p) {
pi node = ST->query(l, r);
int idx = node.s, val=node.f;
Val[idx]=val;
twok[idx][0] = p;
FOR(i,1,18) {
if(twok[idx][i-1]==-1)break;
twok[idx][i] = twok[twok[idx][i - 1]][i - 1];
}
st[idx]=cnt++;
if(idx != l) {
dnc(l, idx-1,idx);
}
if(idx != r) {
dnc(idx+1,r,idx);
}
en[idx]=cnt-1;
}
struct fenw {
int fw[MAXN];
void update(int x, int nval) {
for(;x<MAXN;x+=x&-x)fw[x]+=nval;
}
int query(int x) {
int res =0;
for(;x;x-=x&-x)res+=fw[x];
return res;
}
void update(int x, int y, int nval) {
update(x,nval);
update(y+1,-nval);
}
} t1, t2;
vector<pi> chains[MAXN];
int dp[MAXN];
void dfs(int x) {
for(auto v:adj[x]){
dfs(v);
}
int tot = 0;
for(auto v:adj[x]) {
tot += dp[v];
}
t1.update(st[x],en[x],tot);
for(auto ch:chains[x]) {
chmax(dp[x], ch.s + t1.query(st[ch.f]) - t2.query(st[ch.f]));
}
chmax(dp[x], tot);
t2.update(st[x],en[x],dp[x]);
db2(x,dp[x]);
}
int32_t main()
{
IAMSPEED
memset(twok,-1,sizeof twok);
cin >> n;
FOR(i,1,n) {
cin >> A[i].f;
A[i].f = n-A[i].f;
A[i].s=i;
}
ST=new SparseTable(n+1,A);
dnc(1,n,-1);
int root = -1;
FOR(i,1,n) {
if(twok[i][0] != -1) {
adj[twok[i][0]].pb(i);
} else {
root = i;
}
}
cin >> m;
FOR(i,1,m) {
int x,y,c; cin >> x >> y >> c;
y=n-y;
T+=c;
int tail = x;
DEC(i,19,0) {
if(twok[x][i] != -1 && Val[twok[x][i]] > y) {
x=twok[x][i];
}
}
chains[x].pb({tail,c});
db2(x,tail);
}
dba(st,1,n);
dba(en,1,n);
dfs(root);
cout << T-dp[root];
return (0-0) ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
45908 KB |
Output is correct |
2 |
Correct |
20 ms |
45836 KB |
Output is correct |
3 |
Correct |
18 ms |
45908 KB |
Output is correct |
4 |
Correct |
19 ms |
45824 KB |
Output is correct |
5 |
Correct |
20 ms |
45864 KB |
Output is correct |
6 |
Correct |
19 ms |
45868 KB |
Output is correct |
7 |
Correct |
19 ms |
45832 KB |
Output is correct |
8 |
Correct |
19 ms |
45900 KB |
Output is correct |
9 |
Correct |
20 ms |
45864 KB |
Output is correct |
10 |
Correct |
19 ms |
45908 KB |
Output is correct |
11 |
Correct |
18 ms |
45908 KB |
Output is correct |
12 |
Correct |
19 ms |
45884 KB |
Output is correct |
13 |
Correct |
21 ms |
45824 KB |
Output is correct |
14 |
Correct |
19 ms |
45908 KB |
Output is correct |
15 |
Correct |
19 ms |
45908 KB |
Output is correct |
16 |
Correct |
19 ms |
45880 KB |
Output is correct |
17 |
Correct |
21 ms |
45912 KB |
Output is correct |
18 |
Correct |
19 ms |
45912 KB |
Output is correct |
19 |
Correct |
19 ms |
45908 KB |
Output is correct |
20 |
Correct |
19 ms |
45904 KB |
Output is correct |
21 |
Correct |
18 ms |
45880 KB |
Output is correct |
22 |
Correct |
20 ms |
45816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
45908 KB |
Output is correct |
2 |
Correct |
20 ms |
45836 KB |
Output is correct |
3 |
Correct |
18 ms |
45908 KB |
Output is correct |
4 |
Correct |
19 ms |
45824 KB |
Output is correct |
5 |
Correct |
20 ms |
45864 KB |
Output is correct |
6 |
Correct |
19 ms |
45868 KB |
Output is correct |
7 |
Correct |
19 ms |
45832 KB |
Output is correct |
8 |
Correct |
19 ms |
45900 KB |
Output is correct |
9 |
Correct |
20 ms |
45864 KB |
Output is correct |
10 |
Correct |
19 ms |
45908 KB |
Output is correct |
11 |
Correct |
18 ms |
45908 KB |
Output is correct |
12 |
Correct |
19 ms |
45884 KB |
Output is correct |
13 |
Correct |
21 ms |
45824 KB |
Output is correct |
14 |
Correct |
19 ms |
45908 KB |
Output is correct |
15 |
Correct |
19 ms |
45908 KB |
Output is correct |
16 |
Correct |
19 ms |
45880 KB |
Output is correct |
17 |
Correct |
21 ms |
45912 KB |
Output is correct |
18 |
Correct |
19 ms |
45912 KB |
Output is correct |
19 |
Correct |
19 ms |
45908 KB |
Output is correct |
20 |
Correct |
19 ms |
45904 KB |
Output is correct |
21 |
Correct |
18 ms |
45880 KB |
Output is correct |
22 |
Correct |
20 ms |
45816 KB |
Output is correct |
23 |
Correct |
21 ms |
46300 KB |
Output is correct |
24 |
Correct |
21 ms |
46372 KB |
Output is correct |
25 |
Correct |
20 ms |
46404 KB |
Output is correct |
26 |
Correct |
20 ms |
46404 KB |
Output is correct |
27 |
Correct |
20 ms |
46400 KB |
Output is correct |
28 |
Correct |
22 ms |
46416 KB |
Output is correct |
29 |
Correct |
20 ms |
46284 KB |
Output is correct |
30 |
Correct |
23 ms |
46428 KB |
Output is correct |
31 |
Correct |
22 ms |
46340 KB |
Output is correct |
32 |
Correct |
22 ms |
46404 KB |
Output is correct |
33 |
Correct |
20 ms |
46404 KB |
Output is correct |
34 |
Correct |
21 ms |
46548 KB |
Output is correct |
35 |
Correct |
21 ms |
46420 KB |
Output is correct |
36 |
Correct |
20 ms |
46456 KB |
Output is correct |
37 |
Correct |
22 ms |
46492 KB |
Output is correct |
38 |
Correct |
21 ms |
46460 KB |
Output is correct |
39 |
Correct |
21 ms |
46412 KB |
Output is correct |
40 |
Correct |
21 ms |
46444 KB |
Output is correct |
41 |
Correct |
20 ms |
46392 KB |
Output is correct |
42 |
Correct |
20 ms |
46400 KB |
Output is correct |
43 |
Correct |
21 ms |
46436 KB |
Output is correct |
44 |
Correct |
20 ms |
46264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
45908 KB |
Output is correct |
2 |
Correct |
20 ms |
45836 KB |
Output is correct |
3 |
Correct |
18 ms |
45908 KB |
Output is correct |
4 |
Correct |
19 ms |
45824 KB |
Output is correct |
5 |
Correct |
20 ms |
45864 KB |
Output is correct |
6 |
Correct |
19 ms |
45868 KB |
Output is correct |
7 |
Correct |
19 ms |
45832 KB |
Output is correct |
8 |
Correct |
19 ms |
45900 KB |
Output is correct |
9 |
Correct |
20 ms |
45864 KB |
Output is correct |
10 |
Correct |
19 ms |
45908 KB |
Output is correct |
11 |
Correct |
18 ms |
45908 KB |
Output is correct |
12 |
Correct |
19 ms |
45884 KB |
Output is correct |
13 |
Correct |
21 ms |
45824 KB |
Output is correct |
14 |
Correct |
19 ms |
45908 KB |
Output is correct |
15 |
Correct |
19 ms |
45908 KB |
Output is correct |
16 |
Correct |
19 ms |
45880 KB |
Output is correct |
17 |
Correct |
21 ms |
45912 KB |
Output is correct |
18 |
Correct |
19 ms |
45912 KB |
Output is correct |
19 |
Correct |
19 ms |
45908 KB |
Output is correct |
20 |
Correct |
19 ms |
45904 KB |
Output is correct |
21 |
Correct |
18 ms |
45880 KB |
Output is correct |
22 |
Correct |
20 ms |
45816 KB |
Output is correct |
23 |
Correct |
21 ms |
46300 KB |
Output is correct |
24 |
Correct |
21 ms |
46372 KB |
Output is correct |
25 |
Correct |
20 ms |
46404 KB |
Output is correct |
26 |
Correct |
20 ms |
46404 KB |
Output is correct |
27 |
Correct |
20 ms |
46400 KB |
Output is correct |
28 |
Correct |
22 ms |
46416 KB |
Output is correct |
29 |
Correct |
20 ms |
46284 KB |
Output is correct |
30 |
Correct |
23 ms |
46428 KB |
Output is correct |
31 |
Correct |
22 ms |
46340 KB |
Output is correct |
32 |
Correct |
22 ms |
46404 KB |
Output is correct |
33 |
Correct |
20 ms |
46404 KB |
Output is correct |
34 |
Correct |
21 ms |
46548 KB |
Output is correct |
35 |
Correct |
21 ms |
46420 KB |
Output is correct |
36 |
Correct |
20 ms |
46456 KB |
Output is correct |
37 |
Correct |
22 ms |
46492 KB |
Output is correct |
38 |
Correct |
21 ms |
46460 KB |
Output is correct |
39 |
Correct |
21 ms |
46412 KB |
Output is correct |
40 |
Correct |
21 ms |
46444 KB |
Output is correct |
41 |
Correct |
20 ms |
46392 KB |
Output is correct |
42 |
Correct |
20 ms |
46400 KB |
Output is correct |
43 |
Correct |
21 ms |
46436 KB |
Output is correct |
44 |
Correct |
20 ms |
46264 KB |
Output is correct |
45 |
Correct |
315 ms |
124756 KB |
Output is correct |
46 |
Correct |
289 ms |
123968 KB |
Output is correct |
47 |
Correct |
300 ms |
123008 KB |
Output is correct |
48 |
Correct |
278 ms |
124896 KB |
Output is correct |
49 |
Correct |
296 ms |
122332 KB |
Output is correct |
50 |
Correct |
317 ms |
122356 KB |
Output is correct |
51 |
Correct |
308 ms |
122428 KB |
Output is correct |
52 |
Correct |
316 ms |
124148 KB |
Output is correct |
53 |
Correct |
293 ms |
124124 KB |
Output is correct |
54 |
Correct |
519 ms |
135316 KB |
Output is correct |
55 |
Correct |
506 ms |
130656 KB |
Output is correct |
56 |
Correct |
489 ms |
128272 KB |
Output is correct |
57 |
Correct |
449 ms |
126636 KB |
Output is correct |
58 |
Correct |
401 ms |
131152 KB |
Output is correct |
59 |
Correct |
365 ms |
130752 KB |
Output is correct |
60 |
Correct |
221 ms |
141652 KB |
Output is correct |
61 |
Correct |
274 ms |
124480 KB |
Output is correct |
62 |
Correct |
513 ms |
135584 KB |
Output is correct |
63 |
Correct |
259 ms |
123020 KB |
Output is correct |
64 |
Correct |
260 ms |
121700 KB |
Output is correct |
65 |
Correct |
499 ms |
136704 KB |
Output is correct |
66 |
Correct |
279 ms |
122156 KB |
Output is correct |