제출 #442225

#제출 시각아이디문제언어결과실행 시간메모리
442225cheetoseWorst Reporter 4 (JOI21_worst_reporter4)C++17
100 / 100
415 ms106784 KiB
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define COMPRESS(a) sort(ALL(a));a.resize(unique(ALL(a))-a.begin())
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<ld> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 1000000007;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };

Vi v[200001];
int h[200001],pt[200001];
ll c[200001];
map<int,ll,greater<int>> M[200001];

int ind[200001],gn[200001],GN=1;
Vi st;
Vi group[200005];
bool chk[200005],fin[200005];
void merge(int next,int N){
	int &x=pt[next],&y=pt[N];
	if(M[x].size()>M[y].size())swap(x,y);
	for(auto [a,b]:M[x]){
		M[y][a]+=b;
	}
}
void dfs(int N){
	if(v[N].empty()){
		M[N][h[N]]=c[N];
		return;
	}
	if(gn[N]){
		for(int x:group[gn[N]]){
			chk[x]=1;
			for(int next:v[x]){
				if(!gn[next]){
					dfs(next);
					merge(next,N);
				}
			}
		}
		return;
	}
	for(int next:v[N]){
		dfs(next);
		merge(next,N);
	}
	int nn=pt[N];
	M[nn][h[N]]+=c[N];
	ll t=c[N];
	auto it=M[nn].upper_bound(h[N]);
	while(it!=M[nn].end()){
		if(it->Y<t){
			t-=it->Y;
			it=M[nn].erase(it);
		}else{
			it->Y-=t;
			break;
		}
	}
}
void dfs2(int N){
	st.pb(N);
	chk[N]=1;
	for(int next:v[N]){
		if(chk[next]){
			if(!fin[next]){
				int M=st.size();
				fdn(ii,M-1,0,1){
					gn[st[ii]]=GN;
					group[GN].pb(st[ii]);
					if(st[ii]==next)break;
				}
				GN++;
			}
		}
		else dfs2(next);
	}
	fin[N]=1;
	st.pop_back();
}
int main(){
	int n;
	scanf("%d",&n);
	iota(pt,pt+n+1,0);
	ll tot=0;
	fup(i,1,n,1){
		int x;
		scanf("%d%d%lld",&x,&h[i],&c[i]);
		tot+=c[i];
		if(i!=x){
			v[x].pb(i);
			ind[i]++;
		}
	}
	fup(i,1,n,1){
		if(!chk[i]){
			st.clear();
			dfs2(i);
		}
	}
	ll ans=0;
	MEM0(chk);
	fup(i,1,n,1){
		if(!chk[i] && (ind[i]==0 || gn[i])){
			dfs(i);
			if(ind[i]==0){
				for(auto [a,b]:M[pt[i]])ans+=b;
			}else{
				M[pt[i]][1000000007]+=0;
				VPll v1,v2;
				for(auto it=M[pt[i]].begin();it!=M[pt[i]].end();it++){
					v1.pb(*it);
				}
				reverse(ALL(v1));
				int N=v1.size();
				fdn(i,N-2,0,1)v1[i].Y+=v1[i+1].Y;
				//fup(i,0,N-1,1)printf("%lld %lld\n",v1[i].X,v1[i].Y);
				map<ll,ll> MM;
				for(int x:group[gn[i]]){
					MM[h[x]]+=c[x];
				}
				for(auto it=MM.begin();it!=MM.end();it++){
					v2.pb(*it);
				}
				ll tmp=0;
				bool ok=0;
				fup(i,0,(int)v2.size()-1,1){
					if(v2[i].X!=i+1){
						ok=1;
						int t=lower_bound(ALL(v1),mp((ll)i,-1ll))-v1.begin();
						tmp=max(tmp,v1[t].Y);
					}
					int t=lower_bound(ALL(v1),mp(v2[i].X,-1ll))-v1.begin();
					tmp=max(tmp,v1[t].Y+v2[i].Y);
				}
				if(!ok){
					int t=lower_bound(ALL(v1),mp((ll)v2.size(),-1ll))-v1.begin();
					tmp=max(tmp,v1[t].Y);
				}
				ans+=tmp;
			}
		}
	}
	printf("%lld\n",tot-ans);
}

컴파일 시 표준 에러 (stderr) 메시지

worst_reporter2.cpp: In function 'void dfs2(int)':
worst_reporter2.cpp:11:30: warning: unnecessary parentheses in declaration of 'ii' [-Wparentheses]
   11 | #define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
      |                              ^
worst_reporter2.cpp:101:5: note: in expansion of macro 'fdn'
  101 |     fdn(ii,M-1,0,1){
      |     ^~~
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
      |                              ^
worst_reporter2.cpp:119:2: note: in expansion of macro 'fup'
  119 |  fup(i,1,n,1){
      |  ^~~
worst_reporter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
      |                              ^
worst_reporter2.cpp:128:2: note: in expansion of macro 'fup'
  128 |  fup(i,1,n,1){
      |  ^~~
worst_reporter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
      |                              ^
worst_reporter2.cpp:136:2: note: in expansion of macro 'fup'
  136 |  fup(i,1,n,1){
      |  ^~~
worst_reporter2.cpp:11:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   11 | #define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
      |                              ^
worst_reporter2.cpp:149:5: note: in expansion of macro 'fdn'
  149 |     fdn(i,N-2,0,1)v1[i].Y+=v1[i+1].Y;
      |     ^~~
worst_reporter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
      |                              ^
worst_reporter2.cpp:160:5: note: in expansion of macro 'fup'
  160 |     fup(i,0,(int)v2.size()-1,1){
      |     ^~~
worst_reporter2.cpp:116:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |  scanf("%d",&n);
      |  ~~~~~^~~~~~~~~
worst_reporter2.cpp:121:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |   scanf("%d%d%lld",&x,&h[i],&c[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...