Submission #337383

# Submission time Handle Problem Language Result Execution time Memory
337383 2020-12-20T13:05:17 Z CaroLinda Fireworks (APIO16_fireworks) C++14
100 / 100
406 ms 100460 KB
#include <bits/stdc++.h>

#define ll long long
#define sz(x) (int)(x.size() )
#define all(x) x.begin(),x.end()

const int MAXN = 3e5+10 ;

using namespace std ;

int N , M ;
int sub[MAXN] ;
vector< int > adj[MAXN] ;
ll sumOfEverything[MAXN] , edgeParent[MAXN] ;
map<ll,ll> *mp[MAXN] ; 

void dfs(int x)
{
	sub[x] = 1 ;
	int bc = -1 ;

	for(auto e : adj[x] )
	{
		dfs(e) ;
		sub[x] += sub[e] ;

		if( bc == -1 || sub[e] > sub[bc] ) bc = e ;
	}

	if(bc == -1) 
	{
		mp[x] = new map<ll,ll> ;
		
		(*mp[x]).insert(make_pair(0,-1) ) ;
		(*mp[x]).insert(make_pair(edgeParent[x],2) ) ;

		sumOfEverything[x] = 1 ;

		return ;

	}
	else 
	{
		mp[x] = mp[ bc ] ;
		sumOfEverything[x] = sumOfEverything[bc] ;
	}

  	map<ll,ll> &ptr = *mp[x] ;

  	for(auto e : adj[x] )
  	{
  		if(e == bc ) continue ;

  		for(auto ee : (*mp[e]) ) ptr[ee.first] += ee.second ;
  		(*mp[e]).clear() ;

  		sumOfEverything[x] += sumOfEverything[bc] ;

  	}

  	if(x == 1 ) return ;

  	ll threwAway = -1 ;

  	while( true )
  	{
  		auto it = ptr.end() ;
  		it-- ;

  		if( sumOfEverything[x] <= 0 ) break ;

  		sumOfEverything[x] -= it->second ;
  		threwAway = it->first ;
  		ptr.erase(it) ;
  	}

  	if( sumOfEverything[x] != 0 )
  	{
  		//That means it didn't have a zero to begin with
  		//So what?
  		ptr.insert( make_pair(threwAway, -1LL-sumOfEverything[x] ) ) ;
  		ptr.insert( make_pair(threwAway + edgeParent[x], 2) ) ;
  		sumOfEverything[x] = 1 ;
  	}
  	else 
  	{
  		auto it = ptr.end() ;
  		it-- ;

  		//There can be many zeroes!
  		//You dumb!

		while( it->second == 0 )
		{
			ptr.erase(it) ;
			it = ptr.end() ;
			it-- ;
		}

		auto aux = *it ;
		ptr.erase(it) ;
		aux.second-- ;

		ptr.insert(aux) ;
		ptr.insert( make_pair(aux.first + edgeParent[x],1) ) ;
		ptr.insert( make_pair(threwAway+edgeParent[x],1 ) ) ;

		sumOfEverything[x] = 1 ;

  	}


}

int main()
{
	ll _sum = 0LL ;

	scanf("%d %d", &N, &M ) ;
	for(int i = 2 , parent ; i <= N+M ; i++ )
	{
		scanf("%d %d", &parent, &edgeParent[i]) ;
		_sum += edgeParent[i] ;
		adj[ parent ].push_back( i ) ;
	}

	dfs(1) ;

	map<ll,ll> &ptr = (*mp[1]) ;

	ll slope = 0LL , last = 0 ;

	for(auto e : ptr )
	{
		_sum += slope * (e.first-last) ;
		slope += e.second ;
		last=e.first;

		if(slope >= 0 )
		{
			printf("%lld\n", _sum ) ;
			return 0 ;
		}
	}


}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:122:14: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'long long int*' [-Wformat=]
  122 |   scanf("%d %d", &parent, &edgeParent[i]) ;
      |             ~^            ~~~~~~~~~~~~~~
      |              |            |
      |              int*         long long int*
      |             %lld
fireworks.cpp:119:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  119 |  scanf("%d %d", &N, &M ) ;
      |  ~~~~~^~~~~~~~~~~~~~~~~~
fireworks.cpp:122:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  122 |   scanf("%d %d", &parent, &edgeParent[i]) ;
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7424 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 5 ms 7424 KB Output is correct
9 Correct 5 ms 7404 KB Output is correct
10 Correct 5 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 6 ms 7404 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
9 Correct 5 ms 7404 KB Output is correct
10 Correct 5 ms 7404 KB Output is correct
11 Correct 6 ms 7404 KB Output is correct
12 Correct 5 ms 7404 KB Output is correct
13 Correct 5 ms 7404 KB Output is correct
14 Correct 5 ms 7404 KB Output is correct
15 Correct 5 ms 7404 KB Output is correct
16 Correct 6 ms 7404 KB Output is correct
17 Correct 6 ms 7404 KB Output is correct
18 Correct 5 ms 7404 KB Output is correct
19 Correct 5 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7424 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 5 ms 7424 KB Output is correct
9 Correct 5 ms 7404 KB Output is correct
10 Correct 5 ms 7404 KB Output is correct
11 Correct 5 ms 7404 KB Output is correct
12 Correct 5 ms 7404 KB Output is correct
13 Correct 5 ms 7404 KB Output is correct
14 Correct 5 ms 7404 KB Output is correct
15 Correct 5 ms 7404 KB Output is correct
16 Correct 5 ms 7404 KB Output is correct
17 Correct 6 ms 7404 KB Output is correct
18 Correct 5 ms 7404 KB Output is correct
19 Correct 5 ms 7404 KB Output is correct
20 Correct 5 ms 7404 KB Output is correct
21 Correct 6 ms 7404 KB Output is correct
22 Correct 5 ms 7404 KB Output is correct
23 Correct 5 ms 7404 KB Output is correct
24 Correct 5 ms 7404 KB Output is correct
25 Correct 5 ms 7404 KB Output is correct
26 Correct 6 ms 7404 KB Output is correct
27 Correct 6 ms 7404 KB Output is correct
28 Correct 5 ms 7404 KB Output is correct
29 Correct 5 ms 7404 KB Output is correct
30 Correct 5 ms 7404 KB Output is correct
31 Correct 6 ms 7532 KB Output is correct
32 Correct 7 ms 7532 KB Output is correct
33 Correct 8 ms 7660 KB Output is correct
34 Correct 8 ms 7788 KB Output is correct
35 Correct 8 ms 7788 KB Output is correct
36 Correct 10 ms 7916 KB Output is correct
37 Correct 9 ms 7916 KB Output is correct
38 Correct 10 ms 8044 KB Output is correct
39 Correct 9 ms 8044 KB Output is correct
40 Correct 7 ms 8940 KB Output is correct
41 Correct 9 ms 8940 KB Output is correct
42 Correct 9 ms 8044 KB Output is correct
43 Correct 9 ms 8300 KB Output is correct
44 Correct 9 ms 8320 KB Output is correct
45 Correct 9 ms 8300 KB Output is correct
46 Correct 9 ms 8044 KB Output is correct
47 Correct 9 ms 8104 KB Output is correct
48 Correct 9 ms 8064 KB Output is correct
49 Correct 9 ms 8172 KB Output is correct
50 Correct 8 ms 8172 KB Output is correct
51 Correct 8 ms 8172 KB Output is correct
52 Correct 8 ms 8172 KB Output is correct
53 Correct 9 ms 8172 KB Output is correct
54 Correct 9 ms 8556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7424 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 5 ms 7424 KB Output is correct
9 Correct 5 ms 7404 KB Output is correct
10 Correct 5 ms 7404 KB Output is correct
11 Correct 5 ms 7404 KB Output is correct
12 Correct 5 ms 7404 KB Output is correct
13 Correct 5 ms 7404 KB Output is correct
14 Correct 5 ms 7404 KB Output is correct
15 Correct 5 ms 7404 KB Output is correct
16 Correct 5 ms 7404 KB Output is correct
17 Correct 6 ms 7404 KB Output is correct
18 Correct 5 ms 7404 KB Output is correct
19 Correct 5 ms 7404 KB Output is correct
20 Correct 5 ms 7404 KB Output is correct
21 Correct 6 ms 7404 KB Output is correct
22 Correct 5 ms 7404 KB Output is correct
23 Correct 5 ms 7404 KB Output is correct
24 Correct 5 ms 7404 KB Output is correct
25 Correct 5 ms 7404 KB Output is correct
26 Correct 6 ms 7404 KB Output is correct
27 Correct 6 ms 7404 KB Output is correct
28 Correct 5 ms 7404 KB Output is correct
29 Correct 5 ms 7404 KB Output is correct
30 Correct 5 ms 7404 KB Output is correct
31 Correct 6 ms 7532 KB Output is correct
32 Correct 7 ms 7532 KB Output is correct
33 Correct 8 ms 7660 KB Output is correct
34 Correct 8 ms 7788 KB Output is correct
35 Correct 8 ms 7788 KB Output is correct
36 Correct 10 ms 7916 KB Output is correct
37 Correct 9 ms 7916 KB Output is correct
38 Correct 10 ms 8044 KB Output is correct
39 Correct 9 ms 8044 KB Output is correct
40 Correct 7 ms 8940 KB Output is correct
41 Correct 9 ms 8940 KB Output is correct
42 Correct 9 ms 8044 KB Output is correct
43 Correct 9 ms 8300 KB Output is correct
44 Correct 9 ms 8320 KB Output is correct
45 Correct 9 ms 8300 KB Output is correct
46 Correct 9 ms 8044 KB Output is correct
47 Correct 9 ms 8104 KB Output is correct
48 Correct 9 ms 8064 KB Output is correct
49 Correct 9 ms 8172 KB Output is correct
50 Correct 8 ms 8172 KB Output is correct
51 Correct 8 ms 8172 KB Output is correct
52 Correct 8 ms 8172 KB Output is correct
53 Correct 9 ms 8172 KB Output is correct
54 Correct 9 ms 8556 KB Output is correct
55 Correct 18 ms 8980 KB Output is correct
56 Correct 57 ms 13068 KB Output is correct
57 Correct 108 ms 17132 KB Output is correct
58 Correct 137 ms 19820 KB Output is correct
59 Correct 199 ms 24100 KB Output is correct
60 Correct 248 ms 28304 KB Output is correct
61 Correct 282 ms 31596 KB Output is correct
62 Correct 319 ms 34796 KB Output is correct
63 Correct 391 ms 39916 KB Output is correct
64 Correct 406 ms 40488 KB Output is correct
65 Correct 273 ms 100460 KB Output is correct
66 Correct 255 ms 100332 KB Output is correct
67 Correct 324 ms 48876 KB Output is correct
68 Correct 244 ms 65004 KB Output is correct
69 Correct 326 ms 61636 KB Output is correct
70 Correct 312 ms 61548 KB Output is correct
71 Correct 335 ms 50284 KB Output is correct
72 Correct 315 ms 50156 KB Output is correct
73 Correct 295 ms 49388 KB Output is correct
74 Correct 300 ms 49388 KB Output is correct
75 Correct 311 ms 49132 KB Output is correct
76 Correct 296 ms 49620 KB Output is correct
77 Correct 338 ms 49772 KB Output is correct
78 Correct 398 ms 50668 KB Output is correct
79 Correct 402 ms 76764 KB Output is correct