Submission #107020

# Submission time Handle Problem Language Result Execution time Memory
107020 2019-04-21T13:24:32 Z eriksuenderhauf Designated Cities (JOI19_designated_cities) C++11
23 / 100
130 ms 17144 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const ll INF = 1e16 + 7;
const int MAXN = 2e5 + 5;
const double eps = 1e-9;
pil edg[MAXN];
vi adj[MAXN];
ll sm[MAXN], mx[MAXN], ans[MAXN], depth[MAXN];
ll seg[MAXN*4], lazy[MAXN*4], parW[MAXN];
int cnt = 0, disc[MAXN], st[MAXN], en[MAXN];
int vis[MAXN], par[MAXN];

void dfs1(int u, int p) {
	for (int i: adj[u]) {
		int v; ll w;
		tie(v, w) = edg[i];
		if (v == p) continue;
		dfs1(v, u);
		sm[u] += sm[v] + w;
		mx[u] = max(mx[u], mx[v] + w);
	}
}

void dfs2(int u, int p, ll m) {
	int i1 = -1, i2 = -1;
	ll w1 = 0, w2 = 0;
	mx[u] = max(mx[u], m);
	for (int i: adj[u]) {
		int v; ll w;
		tie(v, w) = edg[i];
		if (v == p) continue;
		sm[v] = sm[u] - w + edg[i ^ 1].se;
		if (mx[v] + w > w1) {
			tie(i2, w2) = mp(i1, w1);
			tie(i1, w1) = mp(v, mx[v] + w);
		} else if (mx[v] + w > w2) {
			tie(i2, w2) = mp(v, mx[v] + w);
		}
	}
	for (int i: adj[u]) {
		int v; ll w;
		tie(v, w) = edg[i];
		if (v == p) continue;
		if (v == i1)
			dfs2(v, u, max(m, w2) + edg[i ^ 1].se);
		else
			dfs2(v, u, max(m, w1) + edg[i ^ 1].se);
	}
}

void dfs3(int u, int p, ll d) {
	disc[cnt] = u;
	st[u] = cnt++;
	depth[u] = d;
	for (int i: adj[u]) {
		int v; ll w;
		tie(v, w) = edg[i];
		if (v == p) continue;
		par[v] = u;
		parW[v] = w;
		dfs3(v, u, d + w);
	}
	en[u] = cnt; 
}

void prop(int l, int r, int k) {
	//seg[k] += lazy[k];
	if (l != r) {
		lazy[k*2] += lazy[k];
		seg[k*2] += lazy[k];
		lazy[k*2+1] += lazy[k];
		seg[k*2+1] += lazy[k];
	}
	lazy[k] = 0;
}

void build(int l, int r, int k) {
	if (l == r) {
		seg[k] = depth[disc[l]];
		return;
	}
	int m = (l + r) / 2;
	build(l, m, k*2);
	build(m+1, r, k*2+1);
	seg[k] = max(seg[k*2], seg[k*2+1]);
}

int qry(int l, int r, int k) {
	if (seg[k] == 0) return -1;
	if (l == r) return disc[l];
	int m = (l+r) / 2;
	prop(l,r,k);
	if (seg[k*2] == seg[k])
		return qry(l,m,k*2);
	return qry(m+1,r,k*2+1);
}

void upd(int l, int r, int k, int a, int b, ll val) {
	if (a <= l && r <= b) {
		seg[k] += val;
		lazy[k] += val;
		return;
	}
	prop(l,r,k);
	int m = (l+r) / 2;
	if (a <= m) upd(l,m,k*2,a,b,val);
	if (b > m) upd(m+1,r,k*2+1,a,b,val);
	seg[k] = max(seg[k*2], seg[k*2+1]);
}

int main() {
	mem(ans, 0x3f);
	int n;
	ni(n);
	for (int i = 1; i < n; i++) {
		int a, b, c, d;
		scanf("%d %d %d %d", &a, &b, &c, &d);
		a--, b--;
		edg[cnt] = mp(b, c);
		adj[a].pb(cnt++);
		edg[cnt] = mp(a, d);
		adj[b].pb(cnt++);
	}
	dfs1(0, -1);
	dfs2(0, -1, 0);
	int root = 0;
	for (int i = 0; i < n; i++) {
		ans[1] = min(ans[1], sm[i]);
		if (ans[2] > sm[i] - mx[i])
			root = i, ans[2] = sm[i] - mx[i];
	}
	cnt = 1;
	dfs3(root, -1, 0);
	par[root] = root;
	build(1, n, 1);
	ll cur = sm[root];
	vis[root] = 1;
	for (int i = 2; i <= n; i++) {
		ll val = seg[1];
		cur -= val;
		ans[i] = min(ans[i], cur);
		int ind = qry(1,n,1);
		if (ind < 0) continue;
		while (!vis[ind]) {
			vis[ind] = 1;
			upd(1,n,1,st[ind],en[ind]-1,-parW[ind]);
			ind = par[ind];
		}
	}
	int q; ni(q);
	while (q--) {
		int e; ni(e);
		prl(ans[e]);
	}
    return 0;
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:8:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define ni(n) scanf("%d", &(n))
               ~~~~~^~~~~~~~~~~~
designated_cities.cpp:144:2: note: in expansion of macro 'ni'
  ni(n);
  ^~
designated_cities.cpp:147:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d %d", &a, &b, &c, &d);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:8:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define ni(n) scanf("%d", &(n))
               ~~~~~^~~~~~~~~~~~
designated_cities.cpp:180:9: note: in expansion of macro 'ni'
  int q; ni(q);
         ^~
designated_cities.cpp:8:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define ni(n) scanf("%d", &(n))
               ~~~~~^~~~~~~~~~~~
designated_cities.cpp:182:10: note: in expansion of macro 'ni'
   int e; ni(e);
          ^~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6656 KB Output is correct
2 Correct 9 ms 6656 KB Output is correct
3 Correct 8 ms 6656 KB Output is correct
4 Correct 9 ms 6656 KB Output is correct
5 Correct 9 ms 6656 KB Output is correct
6 Correct 9 ms 6656 KB Output is correct
7 Correct 8 ms 6656 KB Output is correct
8 Correct 8 ms 6656 KB Output is correct
9 Correct 9 ms 6684 KB Output is correct
10 Correct 9 ms 6656 KB Output is correct
11 Correct 8 ms 6656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6656 KB Output is correct
2 Execution timed out 130 ms 17136 KB Time limit exceeded (wall clock)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6656 KB Output is correct
2 Execution timed out 115 ms 17144 KB Time limit exceeded (wall clock)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6656 KB Output is correct
2 Correct 9 ms 6656 KB Output is correct
3 Correct 8 ms 6656 KB Output is correct
4 Correct 9 ms 6656 KB Output is correct
5 Correct 9 ms 6656 KB Output is correct
6 Correct 9 ms 6656 KB Output is correct
7 Correct 8 ms 6656 KB Output is correct
8 Correct 8 ms 6656 KB Output is correct
9 Correct 9 ms 6684 KB Output is correct
10 Correct 9 ms 6656 KB Output is correct
11 Correct 8 ms 6656 KB Output is correct
12 Correct 9 ms 6656 KB Output is correct
13 Correct 15 ms 7040 KB Output is correct
14 Correct 13 ms 7116 KB Output is correct
15 Correct 14 ms 6912 KB Output is correct
16 Correct 11 ms 6912 KB Output is correct
17 Correct 12 ms 7040 KB Output is correct
18 Correct 13 ms 7040 KB Output is correct
19 Correct 14 ms 6912 KB Output is correct
20 Correct 14 ms 7040 KB Output is correct
21 Correct 11 ms 7040 KB Output is correct
22 Correct 14 ms 7040 KB Output is correct
23 Correct 14 ms 6912 KB Output is correct
24 Correct 13 ms 7040 KB Output is correct
25 Correct 11 ms 7168 KB Output is correct
26 Correct 10 ms 7040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6656 KB Output is correct
2 Execution timed out 130 ms 17136 KB Time limit exceeded (wall clock)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6656 KB Output is correct
2 Correct 9 ms 6656 KB Output is correct
3 Correct 8 ms 6656 KB Output is correct
4 Correct 9 ms 6656 KB Output is correct
5 Correct 9 ms 6656 KB Output is correct
6 Correct 9 ms 6656 KB Output is correct
7 Correct 8 ms 6656 KB Output is correct
8 Correct 8 ms 6656 KB Output is correct
9 Correct 9 ms 6684 KB Output is correct
10 Correct 9 ms 6656 KB Output is correct
11 Correct 8 ms 6656 KB Output is correct
12 Correct 8 ms 6656 KB Output is correct
13 Execution timed out 130 ms 17136 KB Time limit exceeded (wall clock)
14 Halted 0 ms 0 KB -