Submission #127165

# Submission time Handle Problem Language Result Execution time Memory
127165 2019-07-09T02:40:52 Z wilwxk Designated Cities (JOI19_designated_cities) C++14
100 / 100
815 ms 78360 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAXN=2e5+5;
vector<tuple<int, ll, ll> > g[MAXN];
pair<ll, int> mprof[MAXN][2];
pair<ll, int> seg[MAXN*4];
ll lz[MAXN*4];
ll dp[MAXN], respf[MAXN], ppai[MAXN], somaf;
int tini[MAXN], tfim[MAXN], tcara[MAXN], pai[MAXN];
bool marc[MAXN];
int n, q, raiz, contt;

void melhora(int cur, pair<ll, int> val) {
	if(val.first>=mprof[cur][0].first) {
		mprof[cur][1]=mprof[cur][0];
		mprof[cur][0]=val;
	}
	else if(val.first>mprof[cur][1].first) {
		mprof[cur][1]=val;
	}
}

void predfs(int cur, int p) {	
	mprof[cur][0]={0, cur};
	for(auto aresta : g[cur]) {
		int viz; ll peso, rpeso;
		tie(viz, peso, rpeso)=aresta;
		if(viz==p) continue;
		predfs(viz, cur);

		dp[cur]+=dp[viz]+rpeso;
		melhora(cur, {mprof[viz][0].first+peso, mprof[viz][0].second});
	}
	tfim[cur]=contt;
}

void gira(int cur, int p) {
	respf[1]=max(respf[1], dp[cur]);
	if(dp[cur]+mprof[cur][0].first>respf[2]) {
		respf[2]=dp[cur]+mprof[cur][0].first;
		raiz=cur;
	}
	// printf("dp[%d]= %lld // mprof[%d][0]= %lld, %d\n", cur, dp[cur], cur, mprof[cur][0].first, mprof[cur][0].second);
	for(auto aresta : g[cur]) {
		int viz; ll peso, rpeso;
		tie(viz, peso, rpeso)=aresta;
		if(viz==p) continue;

		ll dpc=dp[cur], dpv=dp[viz];
		auto mprofc0=mprof[cur][0], mprofc1=mprof[cur][1];
		auto mprofv0=mprof[viz][0], mprofv1=mprof[viz][1];

		dp[cur]-=(dp[viz]+rpeso);
		dp[viz]+=(dp[cur]+peso);
		if(mprof[cur][0].second==mprof[viz][0].second) mprof[cur][0]=mprof[cur][1];
		melhora(viz, {mprof[cur][0].first+rpeso, mprof[cur][0].second});

		gira(viz, cur);

		dp[cur]=dpc; dp[viz]=dpv;
		mprof[cur][0]=mprofc0; mprof[cur][1]=mprofc1;
		mprof[viz][0]=mprofv0; mprof[viz][1]=mprofv1;
	}
}

void refresh(int sind, int ini, int fim) {
	seg[sind].first+=lz[sind];
	if(ini==fim) {
		lz[sind]=0;
		seg[sind].second=tcara[ini];
		return;
	}
	int e=sind*2; int d=e+1;
	lz[e]+=lz[sind];
	lz[d]+=lz[sind];
	lz[sind]=0;
}
void update(int sind, int ini, int fim, int qini, int qfim, ll val) {
	refresh(sind, ini, fim);
	if(qini>fim||qfim<ini) return;
	if(qini<=ini&&qfim>=fim) {
		lz[sind]+=val;
		refresh(sind, ini, fim);
		return;
	}
	int m=(ini+fim)/2; int e=sind*2; int d=e+1;
	update(e, ini, m, qini, qfim, val);
	update(d, m+1, fim, qini, qfim, val);
	seg[sind]=max(seg[e], seg[d]);
}

void dfs(int cur, int p, ll d) {
	tini[cur]=++contt;
	for(auto aresta : g[cur]) {
		int viz; ll peso, rpeso;
		tie(viz, peso, rpeso)=aresta;
		if(viz==p) continue;
		dfs(viz, cur, d+peso);
		pai[viz]=cur;
		ppai[viz]=peso;
	}
	tfim[cur]=contt;
	tcara[tini[cur]]=cur;
	update(1, 1, n, tini[cur], tini[cur], d);
	// printf("update %d +%lld\n", tini[cur], d);
} 

void pinta(int cur) {
	while(!marc[cur]) {
		marc[cur]=1;
		update(1, 1, n, tini[cur], tfim[cur], -ppai[cur]);
		cur=pai[cur];
	}
}

int main() {
	scanf("%d", &n);
	for(int i=1; i<n; i++) {
		int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d);
		g[a].push_back({b, c, d}); g[b].push_back({a, d, c});
		somaf+=c; somaf+=d;
	}

	predfs(1, 1);
	gira(1, 1);
	dfs(raiz, raiz, 0);
	marc[raiz]=1;
	pinta(seg[1].second);

	// for(int i=1; i<=n; i++) printf("%d >> %d %d >> %lld %d\n", i, tini[i], tfim[i], ppai[i], pai[i]);

	for(int i=3; i<=n; i++) {
		respf[i]=respf[i-1]+seg[1].first;
		int cara=seg[1].second;
		pinta(cara);
		// printf("%d usa %d\n", i, cara);
	}

	scanf("%d", &q);
	while(q--) {
		int val; scanf("%d", &val);
		printf("%lld\n", somaf-respf[val]);
	}
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:119:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:121:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d);
                   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:141:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &q);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:143:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int val; scanf("%d", &val);
            ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 5112 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 5068 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 673 ms 51172 KB Output is correct
3 Correct 689 ms 74208 KB Output is correct
4 Correct 616 ms 49784 KB Output is correct
5 Correct 660 ms 50928 KB Output is correct
6 Correct 666 ms 54520 KB Output is correct
7 Correct 615 ms 49952 KB Output is correct
8 Correct 762 ms 75000 KB Output is correct
9 Correct 562 ms 49164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 709 ms 51204 KB Output is correct
3 Correct 724 ms 77968 KB Output is correct
4 Correct 625 ms 49784 KB Output is correct
5 Correct 658 ms 51244 KB Output is correct
6 Correct 709 ms 54928 KB Output is correct
7 Correct 566 ms 49116 KB Output is correct
8 Correct 750 ms 66056 KB Output is correct
9 Correct 541 ms 48792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 5112 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 5068 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 10 ms 5496 KB Output is correct
14 Correct 10 ms 5752 KB Output is correct
15 Correct 10 ms 5496 KB Output is correct
16 Correct 10 ms 5588 KB Output is correct
17 Correct 10 ms 5496 KB Output is correct
18 Correct 10 ms 5624 KB Output is correct
19 Correct 10 ms 5496 KB Output is correct
20 Correct 10 ms 5624 KB Output is correct
21 Correct 10 ms 5496 KB Output is correct
22 Correct 10 ms 5496 KB Output is correct
23 Correct 10 ms 5496 KB Output is correct
24 Correct 11 ms 5624 KB Output is correct
25 Correct 11 ms 5752 KB Output is correct
26 Correct 9 ms 5496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 673 ms 51172 KB Output is correct
3 Correct 689 ms 74208 KB Output is correct
4 Correct 616 ms 49784 KB Output is correct
5 Correct 660 ms 50928 KB Output is correct
6 Correct 666 ms 54520 KB Output is correct
7 Correct 615 ms 49952 KB Output is correct
8 Correct 762 ms 75000 KB Output is correct
9 Correct 562 ms 49164 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 709 ms 51204 KB Output is correct
12 Correct 724 ms 77968 KB Output is correct
13 Correct 625 ms 49784 KB Output is correct
14 Correct 658 ms 51244 KB Output is correct
15 Correct 709 ms 54928 KB Output is correct
16 Correct 566 ms 49116 KB Output is correct
17 Correct 750 ms 66056 KB Output is correct
18 Correct 541 ms 48792 KB Output is correct
19 Correct 6 ms 5116 KB Output is correct
20 Correct 680 ms 51120 KB Output is correct
21 Correct 704 ms 78360 KB Output is correct
22 Correct 625 ms 49836 KB Output is correct
23 Correct 695 ms 51436 KB Output is correct
24 Correct 687 ms 50452 KB Output is correct
25 Correct 672 ms 51628 KB Output is correct
26 Correct 677 ms 50424 KB Output is correct
27 Correct 667 ms 51120 KB Output is correct
28 Correct 707 ms 54520 KB Output is correct
29 Correct 694 ms 51960 KB Output is correct
30 Correct 658 ms 49964 KB Output is correct
31 Correct 611 ms 49696 KB Output is correct
32 Correct 748 ms 67192 KB Output is correct
33 Correct 548 ms 49208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 5112 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 5068 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 673 ms 51172 KB Output is correct
14 Correct 689 ms 74208 KB Output is correct
15 Correct 616 ms 49784 KB Output is correct
16 Correct 660 ms 50928 KB Output is correct
17 Correct 666 ms 54520 KB Output is correct
18 Correct 615 ms 49952 KB Output is correct
19 Correct 762 ms 75000 KB Output is correct
20 Correct 562 ms 49164 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 709 ms 51204 KB Output is correct
23 Correct 724 ms 77968 KB Output is correct
24 Correct 625 ms 49784 KB Output is correct
25 Correct 658 ms 51244 KB Output is correct
26 Correct 709 ms 54928 KB Output is correct
27 Correct 566 ms 49116 KB Output is correct
28 Correct 750 ms 66056 KB Output is correct
29 Correct 541 ms 48792 KB Output is correct
30 Correct 6 ms 5112 KB Output is correct
31 Correct 10 ms 5496 KB Output is correct
32 Correct 10 ms 5752 KB Output is correct
33 Correct 10 ms 5496 KB Output is correct
34 Correct 10 ms 5588 KB Output is correct
35 Correct 10 ms 5496 KB Output is correct
36 Correct 10 ms 5624 KB Output is correct
37 Correct 10 ms 5496 KB Output is correct
38 Correct 10 ms 5624 KB Output is correct
39 Correct 10 ms 5496 KB Output is correct
40 Correct 10 ms 5496 KB Output is correct
41 Correct 10 ms 5496 KB Output is correct
42 Correct 11 ms 5624 KB Output is correct
43 Correct 11 ms 5752 KB Output is correct
44 Correct 9 ms 5496 KB Output is correct
45 Correct 6 ms 5116 KB Output is correct
46 Correct 680 ms 51120 KB Output is correct
47 Correct 704 ms 78360 KB Output is correct
48 Correct 625 ms 49836 KB Output is correct
49 Correct 695 ms 51436 KB Output is correct
50 Correct 687 ms 50452 KB Output is correct
51 Correct 672 ms 51628 KB Output is correct
52 Correct 677 ms 50424 KB Output is correct
53 Correct 667 ms 51120 KB Output is correct
54 Correct 707 ms 54520 KB Output is correct
55 Correct 694 ms 51960 KB Output is correct
56 Correct 658 ms 49964 KB Output is correct
57 Correct 611 ms 49696 KB Output is correct
58 Correct 748 ms 67192 KB Output is correct
59 Correct 548 ms 49208 KB Output is correct
60 Correct 6 ms 5112 KB Output is correct
61 Correct 732 ms 53812 KB Output is correct
62 Correct 745 ms 76052 KB Output is correct
63 Correct 754 ms 52600 KB Output is correct
64 Correct 747 ms 54196 KB Output is correct
65 Correct 719 ms 52748 KB Output is correct
66 Correct 727 ms 54216 KB Output is correct
67 Correct 710 ms 52856 KB Output is correct
68 Correct 735 ms 53936 KB Output is correct
69 Correct 746 ms 56652 KB Output is correct
70 Correct 749 ms 54708 KB Output is correct
71 Correct 702 ms 52604 KB Output is correct
72 Correct 687 ms 53284 KB Output is correct
73 Correct 815 ms 67364 KB Output is correct
74 Correct 625 ms 53508 KB Output is correct