Submission #691860

# Submission time Handle Problem Language Result Execution time Memory
691860 2023-01-31T18:52:27 Z Antekb Worst Reporter 4 (JOI21_worst_reporter4) C++14
100 / 100
1203 ms 206432 KB
#include<bits/stdc++.h>
 
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")
 
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
 
using namespace std;
 
///~~~~~~~~~~~~~~~~~~~~~~~~~~
 
template <typename H, typename T> 
ostream& operator<<(ostream& os, pair<H, T> m){
	return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H> 
ostream& operator<<(ostream& os, vector<H> V){
	os<<"{";
	for(int i=0; i<V.size(); i++){
		if(i)os<<" ";
		os<<V[i];
	}
	os<<"}";
	return os;
}
 
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
//#define deb(x...) ;
 
///~~~~~~~~~~~~~~~~~~~~~~~~~
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

 
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
 
const int N=1<<19, INF=1e9+5, mod=1e9+7, mod2=998244353;

struct modint{
	int n=0;
	modint(){}
	modint(ll x){
		n=x%mod;
		if(n<0)n+=mod;
	}
	operator int(){
		return n;
	}
	modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;}
	modint operator+=(modint a){return (*this)=(*this)+a;}
	modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;}
	modint operator-=(modint a){return (*this)=(*this)-a;}
	modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;}
	modint operator*=(modint a){return (*this)=(*this)*a;}
	modint operator^(const ll &m)const{
		modint a(1);
		if(m==0)return a;
		if(m==1)return (*this);
		a=(*this)^(m/2);
		a*=a;
		return a*((*this)^(m&1));
	}
	modint odw(){
		return (*this)^((ll)mod-2);
	}
	modint operator/(modint a){return (*this)*a.odw();}
	modint operator/=(modint a){return (*this)=(*this)/a;}
	bool operator==(modint a){return a.n==n;}
	friend ostream& operator<<(ostream& os, modint m) {
		return os << m.n; 
	}
};
modint fact[N], fact2[N];
typedef vector<modint> vm;
void factinit(){
    fact[0]=1;
    for(int i=1; i<N; i++){
        fact[i]=(fact[i-1]*modint(i))%mod;
    }
    fact2[N-1]=fact[N-1].odw();
    for(int i=N-2; i>=0; i--){
    	fact2[i]=(fact2[i+1]*modint(i+1))%mod;
    }
}
modint npok(int _n, int _k){
    return fact[_n]*fact2[_k]*fact2[_n-_k];
}
int deg[N], A[N], H[N], C[N], czy[N];
vi E[N];
struct node{
	node *l=nullptr,*r=nullptr;
	ll val=0, lazy=0;
	node(){};
	void prop(){
		//deb(val, lazy, l, r);
		if(l){
			assert(l);
			l->val=max(l->val+lazy, val);
			l->lazy+=lazy;
		}
		if(r){
			r->val=max(r->val+lazy, val);
			r->lazy+=lazy;
		}
		lazy=0;
	}
};
node* merge(node*u, node*v){
	if(!u)return v;
	if(!v)return u;
	if(!u->l){
		v->val+=u->val;
		v->lazy+=u->val;
		delete u;
		return v;
	}
	if(!v->l){
		u->val+=v->val;
		u->lazy+=v->val;
		delete v;
		return u;
	}
	u->prop();
	v->prop();
	u->l=merge(u->l, v->l);
	u->r=merge(u->r, v->r);
	delete v;
	return u;
}
void add(node* v, int L, int R, int l, int r, ll c){
	if(l==L && r==R){
		v->val=max(c, v->val);
		return;
	}
	if(!v->l){
		v->l=(new node);
		v->r=(new node);
	}
	v->prop();
	int M=(L+R)>>1;
	if(l<=M)add(v->l, L, M, l, min(M,r), c);
	if(r>M)add(v->r, M+1, R, max(l, M+1), r, c);
	return;
}
ll get_val(node* v, int L, int R, int t){
	if(!v->l)return v->val;
	v->prop();
	int M=(L+R)>>1;
	if(t<=M)return max(v->val, get_val(v->l, L, M, t));
	else return max(v->val, get_val(v->r, M+1, R, t));
}
node* dfs(int v){
	//deb(v);
	node* root=0;
	for(int u:E[v]){
		node *w=dfs(u);
		assert(w);
		//sum+=get_val(w, 0, N-1, H[v]);
		root=merge(root, w);
	}
	if(!root)root=new node;
	ll sum=C[v]+get_val(root, 0, N-1, H[v]);
	add(root, 0, N-1, 0, H[v], sum);
	return root;
}
int main(){
	//factinit();
	//BOOST;
	int n; 
	cin>>n;
	vector<int> ska;
	ll sum=0;
	for(int i=1; i<=n; i++){
		cin>>A[i]>>H[i]>>C[i];
		deg[A[i]]++;
		ska.pb(H[i]);
		sum+=C[i];
	}
	sor(ska);
	ska.resize(unique(all(ska))- ska.begin());
	for(int i=1; i<=n; i++){
		H[i]=lower_bound(all(ska), H[i])-ska.begin();
	}
	vi V;
	for(int i=1; i<=n; i++){
		if(deg[i]==0){
			V.pb(i);
		}
	}
	for(int i=0; i<V.size(); i++){
		int v=V[i];
		if(!--deg[A[v]]){
			V.pb(A[v]);
		}
		E[A[v]].pb(v);
	}
	vector<vi> cykle, co;
	vector<map<int, ll> > M;
	for(int i=1; i<=n; i++){
		if(deg[i]){
			cykle.pb({});
			co.pb({});
			M.resize(M.size()+1);
			for(int j=A[i]; deg[j]; j=A[j]){
				deg[j]=0;
				M[M.size()-1][H[j]]+=C[j];
				cykle.back().pb(j);
				for(int u:E[j]){
					co.back().pb(u);
				}
			}
		}
	}
	deb(cykle);
	//deb(V);
	ll ans=0;
	for(int i=0; i<cykle.size(); i++){
		node* root=0;
		for(int j:co[i]){
			root=merge(root, dfs(j));
		}
		ll part=0;
		for(auto j:M[i]){
			if(root)part=max(part, get_val(root, 0, N-1, j.st)+j.nd);
			else part=max(part, j.nd);
		}
		if(root)part=max(part, get_val(root, 0, N-1, 0));
		ans+=part;
	}
	cout<<sum-ans;
}

Compilation message

worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:220:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  220 |  for(int i=0; i<V.size(); i++){
      |               ~^~~~~~~~~
worst_reporter2.cpp:247:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  247 |  for(int i=0; i<cykle.size(); i++){
      |               ~^~~~~~~~~~~~~
worst_reporter2.cpp: In instantiation of 'std::ostream& operator<<(std::ostream&, std::vector<_Tp>) [with H = std::vector<int>; std::ostream = std::basic_ostream<char>]':
worst_reporter2.cpp:39:30:   required from 'void debug(H, T ...) [with H = std::vector<std::vector<int> >; T = {}]'
worst_reporter2.cpp:244:2:   required from here
worst_reporter2.cpp:29:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |  for(int i=0; i<V.size(); i++){
      |               ~^~~~~~~~~
worst_reporter2.cpp: In instantiation of 'std::ostream& operator<<(std::ostream&, std::vector<_Tp>) [with H = int; std::ostream = std::basic_ostream<char>]':
worst_reporter2.cpp:31:5:   required from 'std::ostream& operator<<(std::ostream&, std::vector<_Tp>) [with H = std::vector<int>; std::ostream = std::basic_ostream<char>]'
worst_reporter2.cpp:39:30:   required from 'void debug(H, T ...) [with H = std::vector<std::vector<int> >; T = {}]'
worst_reporter2.cpp:244:2:   required from here
worst_reporter2.cpp:29:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
# Verdict Execution time Memory Grader output
1 Correct 8 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Correct 8 ms 16724 KB Output is correct
4 Correct 8 ms 16724 KB Output is correct
5 Correct 21 ms 18036 KB Output is correct
6 Correct 24 ms 17108 KB Output is correct
7 Correct 17 ms 16988 KB Output is correct
8 Correct 20 ms 17868 KB Output is correct
9 Correct 22 ms 17040 KB Output is correct
10 Correct 18 ms 16980 KB Output is correct
11 Correct 18 ms 16980 KB Output is correct
12 Correct 17 ms 17748 KB Output is correct
13 Correct 15 ms 17256 KB Output is correct
14 Correct 22 ms 17760 KB Output is correct
15 Correct 15 ms 17108 KB Output is correct
16 Correct 20 ms 17920 KB Output is correct
17 Correct 18 ms 17000 KB Output is correct
18 Correct 17 ms 16912 KB Output is correct
19 Correct 21 ms 21408 KB Output is correct
20 Correct 22 ms 21380 KB Output is correct
21 Correct 20 ms 21452 KB Output is correct
22 Correct 19 ms 17364 KB Output is correct
23 Correct 22 ms 16896 KB Output is correct
24 Correct 18 ms 17452 KB Output is correct
25 Correct 17 ms 16980 KB Output is correct
26 Correct 21 ms 17668 KB Output is correct
27 Correct 21 ms 20304 KB Output is correct
28 Correct 23 ms 19924 KB Output is correct
29 Correct 19 ms 18772 KB Output is correct
30 Correct 18 ms 17608 KB Output is correct
31 Correct 19 ms 17508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Correct 8 ms 16724 KB Output is correct
4 Correct 8 ms 16724 KB Output is correct
5 Correct 21 ms 18036 KB Output is correct
6 Correct 24 ms 17108 KB Output is correct
7 Correct 17 ms 16988 KB Output is correct
8 Correct 20 ms 17868 KB Output is correct
9 Correct 22 ms 17040 KB Output is correct
10 Correct 18 ms 16980 KB Output is correct
11 Correct 18 ms 16980 KB Output is correct
12 Correct 17 ms 17748 KB Output is correct
13 Correct 15 ms 17256 KB Output is correct
14 Correct 22 ms 17760 KB Output is correct
15 Correct 15 ms 17108 KB Output is correct
16 Correct 20 ms 17920 KB Output is correct
17 Correct 18 ms 17000 KB Output is correct
18 Correct 17 ms 16912 KB Output is correct
19 Correct 21 ms 21408 KB Output is correct
20 Correct 22 ms 21380 KB Output is correct
21 Correct 20 ms 21452 KB Output is correct
22 Correct 19 ms 17364 KB Output is correct
23 Correct 22 ms 16896 KB Output is correct
24 Correct 18 ms 17452 KB Output is correct
25 Correct 17 ms 16980 KB Output is correct
26 Correct 21 ms 17668 KB Output is correct
27 Correct 21 ms 20304 KB Output is correct
28 Correct 23 ms 19924 KB Output is correct
29 Correct 19 ms 18772 KB Output is correct
30 Correct 18 ms 17608 KB Output is correct
31 Correct 19 ms 17508 KB Output is correct
32 Correct 20 ms 18048 KB Output is correct
33 Correct 886 ms 65572 KB Output is correct
34 Correct 484 ms 25824 KB Output is correct
35 Correct 905 ms 66600 KB Output is correct
36 Correct 497 ms 25992 KB Output is correct
37 Correct 394 ms 24764 KB Output is correct
38 Correct 397 ms 24736 KB Output is correct
39 Correct 400 ms 55816 KB Output is correct
40 Correct 334 ms 37180 KB Output is correct
41 Correct 282 ms 37088 KB Output is correct
42 Correct 394 ms 59352 KB Output is correct
43 Correct 302 ms 32584 KB Output is correct
44 Correct 804 ms 60796 KB Output is correct
45 Correct 444 ms 24928 KB Output is correct
46 Correct 350 ms 24196 KB Output is correct
47 Correct 716 ms 202760 KB Output is correct
48 Correct 573 ms 202748 KB Output is correct
49 Correct 513 ms 206432 KB Output is correct
50 Correct 535 ms 41144 KB Output is correct
51 Correct 429 ms 22952 KB Output is correct
52 Correct 475 ms 48060 KB Output is correct
53 Correct 393 ms 29368 KB Output is correct
54 Correct 318 ms 55836 KB Output is correct
55 Correct 651 ms 179296 KB Output is correct
56 Correct 493 ms 117896 KB Output is correct
57 Correct 381 ms 86244 KB Output is correct
58 Correct 314 ms 51108 KB Output is correct
59 Correct 316 ms 51092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Correct 8 ms 16724 KB Output is correct
4 Correct 8 ms 16724 KB Output is correct
5 Correct 21 ms 18036 KB Output is correct
6 Correct 24 ms 17108 KB Output is correct
7 Correct 17 ms 16988 KB Output is correct
8 Correct 20 ms 17868 KB Output is correct
9 Correct 22 ms 17040 KB Output is correct
10 Correct 18 ms 16980 KB Output is correct
11 Correct 18 ms 16980 KB Output is correct
12 Correct 17 ms 17748 KB Output is correct
13 Correct 15 ms 17256 KB Output is correct
14 Correct 22 ms 17760 KB Output is correct
15 Correct 15 ms 17108 KB Output is correct
16 Correct 20 ms 17920 KB Output is correct
17 Correct 18 ms 17000 KB Output is correct
18 Correct 17 ms 16912 KB Output is correct
19 Correct 21 ms 21408 KB Output is correct
20 Correct 22 ms 21380 KB Output is correct
21 Correct 20 ms 21452 KB Output is correct
22 Correct 19 ms 17364 KB Output is correct
23 Correct 22 ms 16896 KB Output is correct
24 Correct 18 ms 17452 KB Output is correct
25 Correct 17 ms 16980 KB Output is correct
26 Correct 21 ms 17668 KB Output is correct
27 Correct 21 ms 20304 KB Output is correct
28 Correct 23 ms 19924 KB Output is correct
29 Correct 19 ms 18772 KB Output is correct
30 Correct 18 ms 17608 KB Output is correct
31 Correct 19 ms 17508 KB Output is correct
32 Correct 20 ms 18048 KB Output is correct
33 Correct 886 ms 65572 KB Output is correct
34 Correct 484 ms 25824 KB Output is correct
35 Correct 905 ms 66600 KB Output is correct
36 Correct 497 ms 25992 KB Output is correct
37 Correct 394 ms 24764 KB Output is correct
38 Correct 397 ms 24736 KB Output is correct
39 Correct 400 ms 55816 KB Output is correct
40 Correct 334 ms 37180 KB Output is correct
41 Correct 282 ms 37088 KB Output is correct
42 Correct 394 ms 59352 KB Output is correct
43 Correct 302 ms 32584 KB Output is correct
44 Correct 804 ms 60796 KB Output is correct
45 Correct 444 ms 24928 KB Output is correct
46 Correct 350 ms 24196 KB Output is correct
47 Correct 716 ms 202760 KB Output is correct
48 Correct 573 ms 202748 KB Output is correct
49 Correct 513 ms 206432 KB Output is correct
50 Correct 535 ms 41144 KB Output is correct
51 Correct 429 ms 22952 KB Output is correct
52 Correct 475 ms 48060 KB Output is correct
53 Correct 393 ms 29368 KB Output is correct
54 Correct 318 ms 55836 KB Output is correct
55 Correct 651 ms 179296 KB Output is correct
56 Correct 493 ms 117896 KB Output is correct
57 Correct 381 ms 86244 KB Output is correct
58 Correct 314 ms 51108 KB Output is correct
59 Correct 316 ms 51092 KB Output is correct
60 Correct 8 ms 16724 KB Output is correct
61 Correct 8 ms 16724 KB Output is correct
62 Correct 9 ms 16756 KB Output is correct
63 Correct 9 ms 16720 KB Output is correct
64 Correct 893 ms 101796 KB Output is correct
65 Correct 583 ms 35912 KB Output is correct
66 Correct 893 ms 73660 KB Output is correct
67 Correct 517 ms 39784 KB Output is correct
68 Correct 420 ms 32060 KB Output is correct
69 Correct 393 ms 31024 KB Output is correct
70 Correct 806 ms 42424 KB Output is correct
71 Correct 730 ms 30392 KB Output is correct
72 Correct 841 ms 42712 KB Output is correct
73 Correct 711 ms 30436 KB Output is correct
74 Correct 734 ms 50448 KB Output is correct
75 Correct 608 ms 32164 KB Output is correct
76 Correct 607 ms 32352 KB Output is correct
77 Correct 711 ms 47708 KB Output is correct
78 Correct 608 ms 29456 KB Output is correct
79 Correct 869 ms 58004 KB Output is correct
80 Correct 631 ms 30148 KB Output is correct
81 Correct 554 ms 30200 KB Output is correct
82 Correct 743 ms 42964 KB Output is correct
83 Correct 1203 ms 86940 KB Output is correct
84 Correct 795 ms 128588 KB Output is correct
85 Correct 787 ms 128524 KB Output is correct
86 Correct 784 ms 120584 KB Output is correct
87 Correct 774 ms 128480 KB Output is correct
88 Correct 776 ms 128492 KB Output is correct