Submission #518926

# Submission time Handle Problem Language Result Execution time Memory
518926 2022-01-25T07:40:37 Z Koosha_mv Worst Reporter 4 (JOI21_worst_reporter4) C++14
100 / 100
648 ms 180672 KB
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll

const int N=2e5+99,inf=1e9,lg=20;

int n,t,cnt,a[N],sz[N],ft[N],cost[N],par[N],mark[N],vis[N],rap[lg][N];
ll ans;
set<pair<int,int> > s[N];
vector<pair<int,int> > vec;
vector<int> v,vtx,g[N];

void Dfs(int u){
	vtx.pb(u);
	vis[u]=1;
	for(auto v : g[u]){
		if(!vis[v]){
			Dfs(v);
		}
	}
	ft[u]=cnt++;
	if(mark[u]){
		ft[u]=inf;
	}
	vec.pb(mp(a[u],ft[u]));
}
void dfs(int u){
	int b=mark[u];
	mark[u]=1;
	sz[u]=1;
	f(i,0,g[u].size()){
		if(mark[g[u][i]]){
			g[u].erase(g[u].begin()+i);
			break;
		}
	}
	int x=0;
	for(auto v : g[u]){
		dfs(v);
		sz[u]+=sz[v];
		if(sz[x]<sz[v]){
			x=v;
		}
	}
	if(x>0){
		swap(s[u],s[x]);
		for(auto v : g[u]){
			if(v==x) continue ;
			for(auto p : s[v]){
				if(s[u].lower_bound(mp(p.F,0))!=s[u].end() && (*s[u].lower_bound(mp(p.F,0))).F==p.F){
					pair<int,int> e=*s[u].lower_bound(p);
					s[u].erase(e);
					e.S+=p.S;
					s[u].insert(e);
					
				}
				else{
					s[u].insert(p);
				}
			}
		}
	}
	if(b && 1){
		return ;
	}
	pair<int,int> p=mp(a[u],0);
	if(s[u].lower_bound(p)!=s[u].end() && (*s[u].lower_bound(p)).F==a[u]){
		p=*s[u].lower_bound(p);
		s[u].erase(p);
	}
	if(s[u].lower_bound(p)!=s[u].end()){
		pair<int,int> e=*s[u].lower_bound(p);
		s[u].erase(e);
		e.S-=cost[u];
		s[u].insert(e);
	}
	while(s[u].lower_bound(p)!=s[u].end() && (*s[u].lower_bound(p)).S<=0){
		pair<int,int> t,e=*s[u].lower_bound(p);
		s[u].erase(e);
		if(s[u].lower_bound(e)==s[u].end()) break;
		t=*s[u].lower_bound(e);
		s[u].erase(t);
		t.S+=e.S;
		s[u].insert(t);
	}
	p.S+=cost[u];
	s[u].insert(p);
	
	/*cout<<u<<" : "<<endl;
	for(auto p : s[u]){
		cout<<p.F<<" "<<p.S<<endl;
	}
	cout<<endl;*/
}
void solve(int rt){
	map<int,int> val;
	mark[rt]=1;
	v.pb(rt);
	for(int u=par[rt];u!=rt;u=par[u]){
		mark[u]=1;
		v.pb(u);
	}
	Dfs(rt);
	sort(all(vec));
	for(auto u : vtx){
		a[u]=lower_bound(all(vec),mp(a[u],ft[u]))-vec.begin();
	}
	
	vector<pair<int,int> > noname;
	for(auto u : v){
		val[a[u]]+=cost[u];
		dfs(u);
		for(auto p : s[u]){
			noname.pb(p);
		}
	}
	val[inf+inf]=0;
	sort(all(noname));
	/*for(auto e : noname){
		erorp(e);
	}
	cout<<endl;*/
	int p=0,sum=0,res=0;
	for(auto e : val){
		//erorp(e);
		while(p<noname.size() && noname[p].F<=e.F){
			sum+=noname[p].S;
			p++;
		}
		maxm(res,sum+e.S);
	}
	ans+=res;
	
	v.clear();
	vtx.clear();
	vec.clear();
}

main(){
	ll sum=0;
	cin>>n;
	f(i,1,n+1){
		int p;
		cin>>par[i]>>a[i]>>cost[i];
		sum+=cost[i];
		a[i]=inf-a[i];
		rap[0][i]=par[i];
		g[par[i]].pb(i);
	}	
	f(l,1,lg){
		f(i,1,n+1){
			rap[l][i]=rap[l-1][rap[l-1][i]];
		}
	}
	f(i,1,n+1){
		if(!mark[rap[lg-1][i]]){
			solve(rap[lg-1][i]);
		}
	}
	/*ll ans=0;
	for(auto p : s[1]){
		ans+=p.S;
	}*/
	cout<<sum-ans;
}
/*
6
1 1 10
1 1 10
1 1 10
3 1 10
2 1 10
2 1 10
*/

Compilation message

worst_reporter2.cpp: In function 'void dfs(long long int)':
worst_reporter2.cpp:8:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   50 |  f(i,0,g[u].size()){
      |    ~~~~~~~~~~~~~~~             
worst_reporter2.cpp:50:2: note: in expansion of macro 'f'
   50 |  f(i,0,g[u].size()){
      |  ^
worst_reporter2.cpp: In function 'void solve(long long int)':
worst_reporter2.cpp:145:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |   while(p<noname.size() && noname[p].F<=e.F){
      |         ~^~~~~~~~~~~~~~
worst_reporter2.cpp: At global scope:
worst_reporter2.cpp:158:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  158 | main(){
      | ^~~~
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:162:7: warning: unused variable 'p' [-Wunused-variable]
  162 |   int p;
      |       ^
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14540 KB Output is correct
2 Correct 8 ms 14520 KB Output is correct
3 Correct 8 ms 14520 KB Output is correct
4 Correct 7 ms 14540 KB Output is correct
5 Correct 18 ms 16972 KB Output is correct
6 Correct 18 ms 16956 KB Output is correct
7 Correct 18 ms 16940 KB Output is correct
8 Correct 22 ms 17080 KB Output is correct
9 Correct 21 ms 16956 KB Output is correct
10 Correct 17 ms 17044 KB Output is correct
11 Correct 17 ms 17332 KB Output is correct
12 Correct 19 ms 16948 KB Output is correct
13 Correct 20 ms 16960 KB Output is correct
14 Correct 18 ms 16584 KB Output is correct
15 Correct 17 ms 16556 KB Output is correct
16 Correct 20 ms 17468 KB Output is correct
17 Correct 20 ms 17440 KB Output is correct
18 Correct 19 ms 17996 KB Output is correct
19 Correct 18 ms 16708 KB Output is correct
20 Correct 18 ms 16720 KB Output is correct
21 Correct 17 ms 16708 KB Output is correct
22 Correct 15 ms 16716 KB Output is correct
23 Correct 15 ms 16700 KB Output is correct
24 Correct 18 ms 16744 KB Output is correct
25 Correct 18 ms 16716 KB Output is correct
26 Correct 14 ms 16964 KB Output is correct
27 Correct 17 ms 16844 KB Output is correct
28 Correct 17 ms 16840 KB Output is correct
29 Correct 16 ms 17072 KB Output is correct
30 Correct 17 ms 17080 KB Output is correct
31 Correct 17 ms 17088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14540 KB Output is correct
2 Correct 8 ms 14520 KB Output is correct
3 Correct 8 ms 14520 KB Output is correct
4 Correct 7 ms 14540 KB Output is correct
5 Correct 18 ms 16972 KB Output is correct
6 Correct 18 ms 16956 KB Output is correct
7 Correct 18 ms 16940 KB Output is correct
8 Correct 22 ms 17080 KB Output is correct
9 Correct 21 ms 16956 KB Output is correct
10 Correct 17 ms 17044 KB Output is correct
11 Correct 17 ms 17332 KB Output is correct
12 Correct 19 ms 16948 KB Output is correct
13 Correct 20 ms 16960 KB Output is correct
14 Correct 18 ms 16584 KB Output is correct
15 Correct 17 ms 16556 KB Output is correct
16 Correct 20 ms 17468 KB Output is correct
17 Correct 20 ms 17440 KB Output is correct
18 Correct 19 ms 17996 KB Output is correct
19 Correct 18 ms 16708 KB Output is correct
20 Correct 18 ms 16720 KB Output is correct
21 Correct 17 ms 16708 KB Output is correct
22 Correct 15 ms 16716 KB Output is correct
23 Correct 15 ms 16700 KB Output is correct
24 Correct 18 ms 16744 KB Output is correct
25 Correct 18 ms 16716 KB Output is correct
26 Correct 14 ms 16964 KB Output is correct
27 Correct 17 ms 16844 KB Output is correct
28 Correct 17 ms 16840 KB Output is correct
29 Correct 16 ms 17072 KB Output is correct
30 Correct 17 ms 17080 KB Output is correct
31 Correct 17 ms 17088 KB Output is correct
32 Correct 18 ms 16972 KB Output is correct
33 Correct 622 ms 126884 KB Output is correct
34 Correct 636 ms 125832 KB Output is correct
35 Correct 602 ms 123860 KB Output is correct
36 Correct 648 ms 126584 KB Output is correct
37 Correct 587 ms 127868 KB Output is correct
38 Correct 575 ms 137748 KB Output is correct
39 Correct 554 ms 104000 KB Output is correct
40 Correct 528 ms 104000 KB Output is correct
41 Correct 466 ms 108112 KB Output is correct
42 Correct 529 ms 86760 KB Output is correct
43 Correct 514 ms 86848 KB Output is correct
44 Correct 638 ms 154212 KB Output is correct
45 Correct 615 ms 154272 KB Output is correct
46 Correct 613 ms 180672 KB Output is correct
47 Correct 616 ms 100376 KB Output is correct
48 Correct 576 ms 99504 KB Output is correct
49 Correct 432 ms 101040 KB Output is correct
50 Correct 420 ms 96280 KB Output is correct
51 Correct 405 ms 96236 KB Output is correct
52 Correct 635 ms 101460 KB Output is correct
53 Correct 551 ms 100376 KB Output is correct
54 Correct 300 ms 103896 KB Output is correct
55 Correct 482 ms 105988 KB Output is correct
56 Correct 447 ms 112944 KB Output is correct
57 Correct 429 ms 116468 KB Output is correct
58 Correct 485 ms 111360 KB Output is correct
59 Correct 485 ms 111484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14540 KB Output is correct
2 Correct 8 ms 14520 KB Output is correct
3 Correct 8 ms 14520 KB Output is correct
4 Correct 7 ms 14540 KB Output is correct
5 Correct 18 ms 16972 KB Output is correct
6 Correct 18 ms 16956 KB Output is correct
7 Correct 18 ms 16940 KB Output is correct
8 Correct 22 ms 17080 KB Output is correct
9 Correct 21 ms 16956 KB Output is correct
10 Correct 17 ms 17044 KB Output is correct
11 Correct 17 ms 17332 KB Output is correct
12 Correct 19 ms 16948 KB Output is correct
13 Correct 20 ms 16960 KB Output is correct
14 Correct 18 ms 16584 KB Output is correct
15 Correct 17 ms 16556 KB Output is correct
16 Correct 20 ms 17468 KB Output is correct
17 Correct 20 ms 17440 KB Output is correct
18 Correct 19 ms 17996 KB Output is correct
19 Correct 18 ms 16708 KB Output is correct
20 Correct 18 ms 16720 KB Output is correct
21 Correct 17 ms 16708 KB Output is correct
22 Correct 15 ms 16716 KB Output is correct
23 Correct 15 ms 16700 KB Output is correct
24 Correct 18 ms 16744 KB Output is correct
25 Correct 18 ms 16716 KB Output is correct
26 Correct 14 ms 16964 KB Output is correct
27 Correct 17 ms 16844 KB Output is correct
28 Correct 17 ms 16840 KB Output is correct
29 Correct 16 ms 17072 KB Output is correct
30 Correct 17 ms 17080 KB Output is correct
31 Correct 17 ms 17088 KB Output is correct
32 Correct 18 ms 16972 KB Output is correct
33 Correct 622 ms 126884 KB Output is correct
34 Correct 636 ms 125832 KB Output is correct
35 Correct 602 ms 123860 KB Output is correct
36 Correct 648 ms 126584 KB Output is correct
37 Correct 587 ms 127868 KB Output is correct
38 Correct 575 ms 137748 KB Output is correct
39 Correct 554 ms 104000 KB Output is correct
40 Correct 528 ms 104000 KB Output is correct
41 Correct 466 ms 108112 KB Output is correct
42 Correct 529 ms 86760 KB Output is correct
43 Correct 514 ms 86848 KB Output is correct
44 Correct 638 ms 154212 KB Output is correct
45 Correct 615 ms 154272 KB Output is correct
46 Correct 613 ms 180672 KB Output is correct
47 Correct 616 ms 100376 KB Output is correct
48 Correct 576 ms 99504 KB Output is correct
49 Correct 432 ms 101040 KB Output is correct
50 Correct 420 ms 96280 KB Output is correct
51 Correct 405 ms 96236 KB Output is correct
52 Correct 635 ms 101460 KB Output is correct
53 Correct 551 ms 100376 KB Output is correct
54 Correct 300 ms 103896 KB Output is correct
55 Correct 482 ms 105988 KB Output is correct
56 Correct 447 ms 112944 KB Output is correct
57 Correct 429 ms 116468 KB Output is correct
58 Correct 485 ms 111360 KB Output is correct
59 Correct 485 ms 111484 KB Output is correct
60 Correct 8 ms 14500 KB Output is correct
61 Correct 7 ms 14524 KB Output is correct
62 Correct 10 ms 14552 KB Output is correct
63 Correct 7 ms 14540 KB Output is correct
64 Correct 616 ms 109636 KB Output is correct
65 Correct 618 ms 105872 KB Output is correct
66 Correct 636 ms 103744 KB Output is correct
67 Correct 637 ms 108532 KB Output is correct
68 Correct 572 ms 106964 KB Output is correct
69 Correct 505 ms 107520 KB Output is correct
70 Correct 498 ms 91744 KB Output is correct
71 Correct 421 ms 79404 KB Output is correct
72 Correct 588 ms 102524 KB Output is correct
73 Correct 389 ms 90228 KB Output is correct
74 Correct 500 ms 92336 KB Output is correct
75 Correct 423 ms 86040 KB Output is correct
76 Correct 382 ms 86068 KB Output is correct
77 Correct 461 ms 99460 KB Output is correct
78 Correct 414 ms 95176 KB Output is correct
79 Correct 544 ms 112832 KB Output is correct
80 Correct 554 ms 108268 KB Output is correct
81 Correct 509 ms 109492 KB Output is correct
82 Correct 469 ms 102720 KB Output is correct
83 Correct 276 ms 68004 KB Output is correct
84 Correct 550 ms 97704 KB Output is correct
85 Correct 510 ms 97788 KB Output is correct
86 Correct 556 ms 95496 KB Output is correct
87 Correct 543 ms 98560 KB Output is correct
88 Correct 565 ms 99580 KB Output is correct