Submission #227309

# Submission time Handle Problem Language Result Execution time Memory
227309 2020-04-26T18:04:12 Z ryansee Magic Tree (CEOI19_magictree) C++14
100 / 100
1683 ms 36884 KB
#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }

typedef long long ll; 
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;

#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (100006)
ll n, m, k, old[MAXN];
vector<int> v[MAXN];
pi A[MAXN];
struct node{
	ll prior;
	int sz, key;
	ll val, lazy, mx;
	node*l=0,*r=0;
};
typedef node* pnode;
vector<pnode> treap;
pnode init(ll _key,ll nval){
	pnode tmp=new node;
	tmp->prior=rng(), tmp->sz=1, tmp->key=_key, tmp->val=nval, tmp->lazy=0, tmp->mx=nval;
	tmp->l=0, tmp->r=0;
	return tmp;
}
void push(pnode t){
	if(!t)return;
	t->val += t->lazy, t->mx += t->lazy;
	if(t->l) t->l->lazy+=t->lazy;
	if(t->r) t->r->lazy+=t->lazy;
	t->lazy=0;
}
void pull(pnode t){
	if(!t)return;
	if(t->l) push(t->l);
	if(t->r) push(t->r);
	t->mx=max({(t->l?t->l->mx:0),(t->r?t->r->mx:0),t->val});
}
void upd(pnode t){
	if(!t) return;
	t->sz = (t->l?t->l->sz:0) + (t->r?t->r->sz:0) + 1;
}
void split(pnode t,pnode&l,pnode&r,ll nval){
	if(!t) return void(l=r=0);
	push(t);
	if(t->key <= nval) split(t->r, t->r, r, nval), l=t;
	else split(t->l,l,t->l,nval),r=t;
	upd(t), pull(t);
}
void merge(pnode&t, pnode l,pnode r){
	if(!l || !r) return void(t=l?l:r);
	push(l),push(r);
	if(l->prior<r->prior) merge(r->l,l,r->l), t=r;
	else merge(l->r,l->r,r),t=l;
	upd(t), pull(t);
}
void unite(pnode&t,pnode l,pnode r){
	if(!l || !r) return void(t=l?l:r);
	push(l),push(r);
	if(l->prior < r->prior) swap(l,r);
	pnode L,R;
	split(r,L,R,l->key);
	unite(l->l,l->l,L);
	unite(l->r,l->r,R);
	t=l;
	upd(t);
	pull(t);
}
bool find(pnode&t,ll key){
	if(!t) return 0;
	if(t->key==key) return 1;
	else if(t->key>key) return find(t->l,key);
	else return find(t->r,key);
}
void update(pnode&t,ll l,ll r,ll nval){
	pnode L,R;
	split(t,L,t,l-1);
	split(t,t,R,r);
	if(t) t->lazy += nval;
	merge(t,t,R);
	merge(t,L,t);
}
ll rmq(pnode&t,ll l,ll r){
	pnode L,R;
	split(t,L,t,l-1);
	split(t,t,R,r);
	ll ans=t ? t->mx+t->lazy : 0;
	merge(t,t,R);
	merge(t,L,t);
	return ans;
}
vector<int> s;
void it(pnode t){
	if(!t) return;
	it(t->l);
	s.pb(t->key);
	it(t->r);
}
ll memo;
void dfs(int x,int p){
	old[x]=-1;
	for(auto i:v[x]) if(i^p) {
		dfs(i, x);
		if(old[x]==-1 || (treap[old[i]]?treap[old[i]]->sz:0)>(treap[old[x]]?treap[old[x]]->sz:0)) old[x]=old[i];
	}
	if(old[x]==-1){
		old[x]=treap.size();
		treap.eb(pnode());
	}
	// i is updated by j, where j is largest < i
	for(auto i:v[x]) if((i^p) && (old[i]^old[x])) {
		s.clear();
		it(treap[old[i]]);
		vector<ll> pre;
		for(auto j=s.begin();j!=s.end();++j){
			pre.eb(rmq(treap[old[x]],0,*j));
		}
		ll co=0;
		for(auto j=s.begin();j!=s.end();++j){
			ll myval=rmq(treap[old[i]],*j,*j);
			ll b4=rmq(treap[old[x]],0,*j);
			if(!find(treap[old[x]],*j))unite(treap[old[x]],treap[old[x]],init(*j,myval+pre[co])),memo=myval+pre[co];
			else if(myval+pre[co]>(memo=rmq(treap[old[x]],*j,*j))){
				update(treap[old[x]],*j,*j,-memo), 
				update(treap[old[x]],*j,*j,myval+pre[co]);
				memo=myval+pre[co];
			}
			if(b4<memo)
				update(treap[old[x]],(*j)+1, INF, memo-b4);
			++ co;
		}
	}
	if(A[x].f){
		ll pre=rmq(treap[old[x]],0,A[x].f);
		if(!find(treap[old[x]],A[x].f))unite(treap[old[x]],treap[old[x]],init(A[x].f,A[x].s+pre));
		else if((memo=rmq(treap[old[x]],A[x].f,A[x].f))==pre) update(treap[old[x]],A[x].f,A[x].f,A[x].s);
		else update(treap[old[x]],A[x].f,A[x].f,-memo),update(treap[old[x]],A[x].f,A[x].f,pre+A[x].s);
	}
	if(x==1){
		cout<<rmq(treap[old[x]],0,INF)<<'\n';
	}
}
#define gc getchar_unlocked
void in(ll&x){
	x=0;
	char ch=gc();
	while(ch<'0'||ch>'9')ch=gc();
	while(ch>='0'&&ch<='9'){
		x=(x<<3)+(x<<1)+(ch&15);
		ch=gc();
	}
}
int main(){
	FAST
	in(n),in(m),in(k);
	FOR(i,2,n){
		ll p;in(p);
		v[p].eb(i), v[i].eb(p);
	}
	FOR(i,1,m){
		ll a;in(a);
		in(A[a].f),in(A[a].s);
	}
	dfs(1,1);
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2688 KB Output is correct
4 Correct 7 ms 2688 KB Output is correct
5 Correct 6 ms 2688 KB Output is correct
6 Correct 6 ms 2688 KB Output is correct
7 Correct 6 ms 2688 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 415 ms 16496 KB Output is correct
2 Correct 188 ms 15860 KB Output is correct
3 Correct 1633 ms 33936 KB Output is correct
4 Correct 719 ms 20844 KB Output is correct
5 Correct 724 ms 22396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2944 KB Output is correct
2 Correct 8 ms 2944 KB Output is correct
3 Correct 7 ms 2944 KB Output is correct
4 Correct 201 ms 26336 KB Output is correct
5 Correct 176 ms 26340 KB Output is correct
6 Correct 535 ms 26336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 12776 KB Output is correct
2 Correct 74 ms 11760 KB Output is correct
3 Correct 69 ms 15352 KB Output is correct
4 Correct 76 ms 15348 KB Output is correct
5 Correct 54 ms 22520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2688 KB Output is correct
4 Correct 7 ms 2688 KB Output is correct
5 Correct 6 ms 2688 KB Output is correct
6 Correct 6 ms 2688 KB Output is correct
7 Correct 6 ms 2688 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2688 KB Output is correct
10 Correct 334 ms 16780 KB Output is correct
11 Correct 235 ms 15320 KB Output is correct
12 Correct 148 ms 15224 KB Output is correct
13 Correct 207 ms 15212 KB Output is correct
14 Correct 124 ms 22392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 4224 KB Output is correct
2 Correct 40 ms 8948 KB Output is correct
3 Correct 38 ms 8956 KB Output is correct
4 Correct 46 ms 8948 KB Output is correct
5 Correct 21 ms 9616 KB Output is correct
6 Correct 37 ms 11936 KB Output is correct
7 Correct 33 ms 14328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2688 KB Output is correct
4 Correct 7 ms 2688 KB Output is correct
5 Correct 6 ms 2688 KB Output is correct
6 Correct 6 ms 2688 KB Output is correct
7 Correct 6 ms 2688 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2688 KB Output is correct
10 Correct 7 ms 2944 KB Output is correct
11 Correct 8 ms 2944 KB Output is correct
12 Correct 7 ms 2944 KB Output is correct
13 Correct 201 ms 26336 KB Output is correct
14 Correct 176 ms 26340 KB Output is correct
15 Correct 535 ms 26336 KB Output is correct
16 Correct 334 ms 16780 KB Output is correct
17 Correct 235 ms 15320 KB Output is correct
18 Correct 148 ms 15224 KB Output is correct
19 Correct 207 ms 15212 KB Output is correct
20 Correct 124 ms 22392 KB Output is correct
21 Correct 228 ms 7588 KB Output is correct
22 Correct 627 ms 18384 KB Output is correct
23 Correct 841 ms 23788 KB Output is correct
24 Correct 1660 ms 36440 KB Output is correct
25 Correct 721 ms 20172 KB Output is correct
26 Correct 828 ms 23180 KB Output is correct
27 Correct 706 ms 23376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2688 KB Output is correct
4 Correct 7 ms 2688 KB Output is correct
5 Correct 6 ms 2688 KB Output is correct
6 Correct 6 ms 2688 KB Output is correct
7 Correct 6 ms 2688 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2688 KB Output is correct
10 Correct 415 ms 16496 KB Output is correct
11 Correct 188 ms 15860 KB Output is correct
12 Correct 1633 ms 33936 KB Output is correct
13 Correct 719 ms 20844 KB Output is correct
14 Correct 724 ms 22396 KB Output is correct
15 Correct 7 ms 2944 KB Output is correct
16 Correct 8 ms 2944 KB Output is correct
17 Correct 7 ms 2944 KB Output is correct
18 Correct 201 ms 26336 KB Output is correct
19 Correct 176 ms 26340 KB Output is correct
20 Correct 535 ms 26336 KB Output is correct
21 Correct 102 ms 12776 KB Output is correct
22 Correct 74 ms 11760 KB Output is correct
23 Correct 69 ms 15352 KB Output is correct
24 Correct 76 ms 15348 KB Output is correct
25 Correct 54 ms 22520 KB Output is correct
26 Correct 334 ms 16780 KB Output is correct
27 Correct 235 ms 15320 KB Output is correct
28 Correct 148 ms 15224 KB Output is correct
29 Correct 207 ms 15212 KB Output is correct
30 Correct 124 ms 22392 KB Output is correct
31 Correct 15 ms 4224 KB Output is correct
32 Correct 40 ms 8948 KB Output is correct
33 Correct 38 ms 8956 KB Output is correct
34 Correct 46 ms 8948 KB Output is correct
35 Correct 21 ms 9616 KB Output is correct
36 Correct 37 ms 11936 KB Output is correct
37 Correct 33 ms 14328 KB Output is correct
38 Correct 228 ms 7588 KB Output is correct
39 Correct 627 ms 18384 KB Output is correct
40 Correct 841 ms 23788 KB Output is correct
41 Correct 1660 ms 36440 KB Output is correct
42 Correct 721 ms 20172 KB Output is correct
43 Correct 828 ms 23180 KB Output is correct
44 Correct 706 ms 23376 KB Output is correct
45 Correct 224 ms 7844 KB Output is correct
46 Correct 654 ms 19804 KB Output is correct
47 Correct 811 ms 23832 KB Output is correct
48 Correct 1683 ms 36884 KB Output is correct
49 Correct 700 ms 20856 KB Output is correct
50 Correct 823 ms 24048 KB Output is correct
51 Correct 691 ms 24048 KB Output is correct