Submission #85966

# Submission time Handle Problem Language Result Execution time Memory
85966 2018-11-23T13:05:50 Z bash Construction of Highway (JOI18_construction) C++17
100 / 100
576 ms 111228 KB
/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>

#define F first
#define S second
#define endl '\n'
#define deb(x) cout<<#x<<' '<<x<<endl;
#define pb push_back

/*
#ifdef IZI_KATKA
#define int __int64_t
#else
#define int __int64
#endif
*/

const long long MOD = 1e9 + 7;
const long long MAXN = 1e5 + 1;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

deque <pair <int, int> > chain[MAXN];
vector <int> g[MAXN];
int par[MAXN];
int deg[MAXN];
int nxt[MAXN];
int in[MAXN];
int out[MAXN];
int a[MAXN];
int sz[MAXN];


void dfs_sz(int v = 1) {
	sz[v] = 1;
	for (int &i : g[v]) {
		par[i] = v;
		deg[i] = deg[v] + 1;
		dfs_sz(i);
		sz[v] += sz[i];
		if (sz[i] > sz[g[v][0]]) {
			swap(i, g[v][0]);
		}
	}
}

void dfs_hld(int v = 1) {
	for (int i : g[v]) {
		nxt[i] = (i == g[v][0] ? nxt[v] : i);
		dfs_hld(i);
	}
}


int fen[MAXN];
int N;

void init(int n)
{
	N = n;
	for(int i = 0; i <= N; i++) fen[i] = 0;
}

void update(int pos, int v)
{
	pos++;
	for(;pos <= N;pos += (pos&(-pos)))
	{
		fen[pos] += v;
	}
}


int query(int pos)
{
	pos++;
	int sum = 0;
	for(;pos; pos -= (pos&(-pos)))
	{
		sum += fen[pos];
	}
	return sum;
}


ll calc_path(int u) {
	vector <pair <int, int> > V;
	vector <int> coord;
	while(u != -1) {
		int next_u = nxt[u];
		int Sz = deg[u] - deg[next_u] + 1;
		vector <pair <int, int> > tmp;
		for (int i = 0; i < chain[next_u].size(); i++) {
			int cnt = chain[next_u][i].first;
			int v = chain[next_u][i].second;
			if (cnt <= Sz) {
				tmp.pb({cnt, v});
				Sz -= cnt;
			} else {
				tmp.pb({Sz, v});
				break;
			}
		}
		for(int i = int(tmp.size()) - 1; i >= 0; i--) {
			V.pb(tmp[i]);
			coord.pb(tmp[i].second);
		}
		u = par[next_u];
	}
	reverse(V.begin(), V.end());
	sort(coord.begin(), coord.end());
	coord.erase(unique(coord.begin(), coord.end()), coord.end());
	init(int(coord.size()));
	ll res = 0, tot = 0;
	for (pair <int, int> i : V) {
		i.second = lower_bound(coord.begin(), coord.end(), i.second) - coord.begin();
		res += i.first * (tot - query(i.second));
		tot += i.first;
		update(i.second, i.first);
	}
	return res;
}


void addedge(int u, int v)
{
	while(u != -1) {
		int next_u = nxt[u];
		int Sz = deg[u] - deg[next_u] + 1;
		while(chain[next_u].size() > 0) {
			int cnt = chain[next_u][0].first;
			if (cnt <= Sz) {
			    chain[next_u].pop_front();
			    Sz -= cnt;
			} else {
				chain[next_u].front().first -= Sz;
				break;
			}
		}
		chain[next_u].push_front({deg[u] - deg[next_u] + 1, v});
		u = par[next_u];
	}
}


int main() {
	#ifdef IZI_KATKA
	assert(freopen("input", "r", stdin));
    assert(freopen("output", "w", stdout));
    #endif
    int n = readInt();
    for (int i = 1; i <= n; i++) {
    	a[i] = readInt();
    }
    nxt[1] = 1;
    chain[1].pb({1, a[1]});
    par[1] = -1;
    vector <pair <int, int> > Q;
    for (int i = 1; i < n; i++) {
    	int u = readInt(), v = readInt();
    	g[u].pb(v);
    	Q.pb({u, v});
    }
    dfs_sz();
    dfs_hld();
    for (int i = 0; i < n - 1; i++) {
    	cout << calc_path(Q[i].first) << endl;
    	addedge(Q[i].second, a[Q[i].second]);
    }
    return 0;
}

Compilation message

construction.cpp: In function 'll calc_path(int)':
construction.cpp:138:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < chain[next_u].size(); i++) {
                   ~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 74 ms 70008 KB Output is correct
2 Correct 84 ms 70132 KB Output is correct
3 Correct 83 ms 70240 KB Output is correct
4 Correct 79 ms 70332 KB Output is correct
5 Correct 79 ms 70444 KB Output is correct
6 Correct 86 ms 70488 KB Output is correct
7 Correct 77 ms 70488 KB Output is correct
8 Correct 76 ms 70488 KB Output is correct
9 Correct 71 ms 70488 KB Output is correct
10 Correct 71 ms 70488 KB Output is correct
11 Correct 80 ms 70488 KB Output is correct
12 Correct 79 ms 70488 KB Output is correct
13 Correct 80 ms 70560 KB Output is correct
14 Correct 81 ms 70560 KB Output is correct
15 Correct 81 ms 70560 KB Output is correct
16 Correct 83 ms 70560 KB Output is correct
17 Correct 82 ms 70740 KB Output is correct
18 Correct 80 ms 70740 KB Output is correct
19 Correct 79 ms 70740 KB Output is correct
20 Correct 82 ms 70740 KB Output is correct
21 Correct 79 ms 70740 KB Output is correct
22 Correct 80 ms 70740 KB Output is correct
23 Correct 78 ms 70740 KB Output is correct
24 Correct 77 ms 70740 KB Output is correct
25 Correct 80 ms 70764 KB Output is correct
26 Correct 71 ms 70764 KB Output is correct
27 Correct 82 ms 70868 KB Output is correct
28 Correct 78 ms 70868 KB Output is correct
29 Correct 79 ms 70868 KB Output is correct
30 Correct 79 ms 70868 KB Output is correct
31 Correct 86 ms 70868 KB Output is correct
32 Correct 75 ms 70868 KB Output is correct
33 Correct 89 ms 70868 KB Output is correct
34 Correct 80 ms 70868 KB Output is correct
35 Correct 80 ms 70868 KB Output is correct
36 Correct 80 ms 70868 KB Output is correct
37 Correct 80 ms 70868 KB Output is correct
38 Correct 88 ms 70868 KB Output is correct
39 Correct 81 ms 70868 KB Output is correct
40 Correct 81 ms 70868 KB Output is correct
41 Correct 79 ms 70868 KB Output is correct
42 Correct 85 ms 70868 KB Output is correct
43 Correct 94 ms 70868 KB Output is correct
44 Correct 101 ms 70932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 70008 KB Output is correct
2 Correct 84 ms 70132 KB Output is correct
3 Correct 83 ms 70240 KB Output is correct
4 Correct 79 ms 70332 KB Output is correct
5 Correct 79 ms 70444 KB Output is correct
6 Correct 86 ms 70488 KB Output is correct
7 Correct 77 ms 70488 KB Output is correct
8 Correct 76 ms 70488 KB Output is correct
9 Correct 71 ms 70488 KB Output is correct
10 Correct 71 ms 70488 KB Output is correct
11 Correct 80 ms 70488 KB Output is correct
12 Correct 79 ms 70488 KB Output is correct
13 Correct 80 ms 70560 KB Output is correct
14 Correct 81 ms 70560 KB Output is correct
15 Correct 81 ms 70560 KB Output is correct
16 Correct 83 ms 70560 KB Output is correct
17 Correct 82 ms 70740 KB Output is correct
18 Correct 80 ms 70740 KB Output is correct
19 Correct 79 ms 70740 KB Output is correct
20 Correct 82 ms 70740 KB Output is correct
21 Correct 79 ms 70740 KB Output is correct
22 Correct 80 ms 70740 KB Output is correct
23 Correct 78 ms 70740 KB Output is correct
24 Correct 77 ms 70740 KB Output is correct
25 Correct 80 ms 70764 KB Output is correct
26 Correct 71 ms 70764 KB Output is correct
27 Correct 82 ms 70868 KB Output is correct
28 Correct 78 ms 70868 KB Output is correct
29 Correct 79 ms 70868 KB Output is correct
30 Correct 79 ms 70868 KB Output is correct
31 Correct 86 ms 70868 KB Output is correct
32 Correct 75 ms 70868 KB Output is correct
33 Correct 89 ms 70868 KB Output is correct
34 Correct 80 ms 70868 KB Output is correct
35 Correct 80 ms 70868 KB Output is correct
36 Correct 80 ms 70868 KB Output is correct
37 Correct 80 ms 70868 KB Output is correct
38 Correct 88 ms 70868 KB Output is correct
39 Correct 81 ms 70868 KB Output is correct
40 Correct 81 ms 70868 KB Output is correct
41 Correct 79 ms 70868 KB Output is correct
42 Correct 85 ms 70868 KB Output is correct
43 Correct 94 ms 70868 KB Output is correct
44 Correct 101 ms 70932 KB Output is correct
45 Correct 85 ms 70972 KB Output is correct
46 Correct 88 ms 71968 KB Output is correct
47 Correct 88 ms 72044 KB Output is correct
48 Correct 90 ms 72172 KB Output is correct
49 Correct 78 ms 72172 KB Output is correct
50 Correct 78 ms 72172 KB Output is correct
51 Correct 74 ms 72172 KB Output is correct
52 Correct 89 ms 72728 KB Output is correct
53 Correct 96 ms 72728 KB Output is correct
54 Correct 86 ms 72732 KB Output is correct
55 Correct 100 ms 72812 KB Output is correct
56 Correct 89 ms 72884 KB Output is correct
57 Correct 94 ms 72884 KB Output is correct
58 Correct 101 ms 72908 KB Output is correct
59 Correct 93 ms 73100 KB Output is correct
60 Correct 114 ms 73100 KB Output is correct
61 Correct 83 ms 73188 KB Output is correct
62 Correct 103 ms 73336 KB Output is correct
63 Correct 86 ms 73412 KB Output is correct
64 Correct 104 ms 73412 KB Output is correct
65 Correct 89 ms 73412 KB Output is correct
66 Correct 88 ms 73496 KB Output is correct
67 Correct 88 ms 73508 KB Output is correct
68 Correct 78 ms 73508 KB Output is correct
69 Correct 86 ms 73856 KB Output is correct
70 Correct 94 ms 73900 KB Output is correct
71 Correct 83 ms 73900 KB Output is correct
72 Correct 89 ms 73900 KB Output is correct
73 Correct 90 ms 73900 KB Output is correct
74 Correct 83 ms 73900 KB Output is correct
75 Correct 84 ms 73916 KB Output is correct
76 Correct 88 ms 74236 KB Output is correct
77 Correct 95 ms 74236 KB Output is correct
78 Correct 86 ms 74236 KB Output is correct
79 Correct 86 ms 74236 KB Output is correct
80 Correct 99 ms 74236 KB Output is correct
81 Correct 94 ms 74252 KB Output is correct
82 Correct 90 ms 74316 KB Output is correct
83 Correct 84 ms 74380 KB Output is correct
84 Correct 86 ms 74444 KB Output is correct
85 Correct 86 ms 74540 KB Output is correct
86 Correct 84 ms 74540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 70008 KB Output is correct
2 Correct 84 ms 70132 KB Output is correct
3 Correct 83 ms 70240 KB Output is correct
4 Correct 79 ms 70332 KB Output is correct
5 Correct 79 ms 70444 KB Output is correct
6 Correct 86 ms 70488 KB Output is correct
7 Correct 77 ms 70488 KB Output is correct
8 Correct 76 ms 70488 KB Output is correct
9 Correct 71 ms 70488 KB Output is correct
10 Correct 71 ms 70488 KB Output is correct
11 Correct 80 ms 70488 KB Output is correct
12 Correct 79 ms 70488 KB Output is correct
13 Correct 80 ms 70560 KB Output is correct
14 Correct 81 ms 70560 KB Output is correct
15 Correct 81 ms 70560 KB Output is correct
16 Correct 83 ms 70560 KB Output is correct
17 Correct 82 ms 70740 KB Output is correct
18 Correct 80 ms 70740 KB Output is correct
19 Correct 79 ms 70740 KB Output is correct
20 Correct 82 ms 70740 KB Output is correct
21 Correct 79 ms 70740 KB Output is correct
22 Correct 80 ms 70740 KB Output is correct
23 Correct 78 ms 70740 KB Output is correct
24 Correct 77 ms 70740 KB Output is correct
25 Correct 80 ms 70764 KB Output is correct
26 Correct 71 ms 70764 KB Output is correct
27 Correct 82 ms 70868 KB Output is correct
28 Correct 78 ms 70868 KB Output is correct
29 Correct 79 ms 70868 KB Output is correct
30 Correct 79 ms 70868 KB Output is correct
31 Correct 86 ms 70868 KB Output is correct
32 Correct 75 ms 70868 KB Output is correct
33 Correct 89 ms 70868 KB Output is correct
34 Correct 80 ms 70868 KB Output is correct
35 Correct 80 ms 70868 KB Output is correct
36 Correct 80 ms 70868 KB Output is correct
37 Correct 80 ms 70868 KB Output is correct
38 Correct 88 ms 70868 KB Output is correct
39 Correct 81 ms 70868 KB Output is correct
40 Correct 81 ms 70868 KB Output is correct
41 Correct 79 ms 70868 KB Output is correct
42 Correct 85 ms 70868 KB Output is correct
43 Correct 94 ms 70868 KB Output is correct
44 Correct 101 ms 70932 KB Output is correct
45 Correct 85 ms 70972 KB Output is correct
46 Correct 88 ms 71968 KB Output is correct
47 Correct 88 ms 72044 KB Output is correct
48 Correct 90 ms 72172 KB Output is correct
49 Correct 78 ms 72172 KB Output is correct
50 Correct 78 ms 72172 KB Output is correct
51 Correct 74 ms 72172 KB Output is correct
52 Correct 89 ms 72728 KB Output is correct
53 Correct 96 ms 72728 KB Output is correct
54 Correct 86 ms 72732 KB Output is correct
55 Correct 100 ms 72812 KB Output is correct
56 Correct 89 ms 72884 KB Output is correct
57 Correct 94 ms 72884 KB Output is correct
58 Correct 101 ms 72908 KB Output is correct
59 Correct 93 ms 73100 KB Output is correct
60 Correct 114 ms 73100 KB Output is correct
61 Correct 83 ms 73188 KB Output is correct
62 Correct 103 ms 73336 KB Output is correct
63 Correct 86 ms 73412 KB Output is correct
64 Correct 104 ms 73412 KB Output is correct
65 Correct 89 ms 73412 KB Output is correct
66 Correct 88 ms 73496 KB Output is correct
67 Correct 88 ms 73508 KB Output is correct
68 Correct 78 ms 73508 KB Output is correct
69 Correct 86 ms 73856 KB Output is correct
70 Correct 94 ms 73900 KB Output is correct
71 Correct 83 ms 73900 KB Output is correct
72 Correct 89 ms 73900 KB Output is correct
73 Correct 90 ms 73900 KB Output is correct
74 Correct 83 ms 73900 KB Output is correct
75 Correct 84 ms 73916 KB Output is correct
76 Correct 88 ms 74236 KB Output is correct
77 Correct 95 ms 74236 KB Output is correct
78 Correct 86 ms 74236 KB Output is correct
79 Correct 86 ms 74236 KB Output is correct
80 Correct 99 ms 74236 KB Output is correct
81 Correct 94 ms 74252 KB Output is correct
82 Correct 90 ms 74316 KB Output is correct
83 Correct 84 ms 74380 KB Output is correct
84 Correct 86 ms 74444 KB Output is correct
85 Correct 86 ms 74540 KB Output is correct
86 Correct 84 ms 74540 KB Output is correct
87 Correct 103 ms 76508 KB Output is correct
88 Correct 178 ms 83308 KB Output is correct
89 Correct 443 ms 106752 KB Output is correct
90 Correct 435 ms 107064 KB Output is correct
91 Correct 471 ms 107092 KB Output is correct
92 Correct 161 ms 107092 KB Output is correct
93 Correct 170 ms 107092 KB Output is correct
94 Correct 155 ms 107092 KB Output is correct
95 Correct 219 ms 111228 KB Output is correct
96 Correct 228 ms 111228 KB Output is correct
97 Correct 242 ms 111228 KB Output is correct
98 Correct 244 ms 111228 KB Output is correct
99 Correct 243 ms 111228 KB Output is correct
100 Correct 467 ms 111228 KB Output is correct
101 Correct 576 ms 111228 KB Output is correct
102 Correct 531 ms 111228 KB Output is correct
103 Correct 529 ms 111228 KB Output is correct
104 Correct 223 ms 111228 KB Output is correct
105 Correct 232 ms 111228 KB Output is correct
106 Correct 234 ms 111228 KB Output is correct
107 Correct 443 ms 111228 KB Output is correct
108 Correct 472 ms 111228 KB Output is correct
109 Correct 420 ms 111228 KB Output is correct
110 Correct 165 ms 111228 KB Output is correct
111 Correct 225 ms 111228 KB Output is correct
112 Correct 218 ms 111228 KB Output is correct
113 Correct 228 ms 111228 KB Output is correct
114 Correct 493 ms 111228 KB Output is correct
115 Correct 567 ms 111228 KB Output is correct
116 Correct 229 ms 111228 KB Output is correct
117 Correct 244 ms 111228 KB Output is correct
118 Correct 262 ms 111228 KB Output is correct
119 Correct 273 ms 111228 KB Output is correct
120 Correct 240 ms 111228 KB Output is correct
121 Correct 255 ms 111228 KB Output is correct
122 Correct 259 ms 111228 KB Output is correct
123 Correct 280 ms 111228 KB Output is correct
124 Correct 245 ms 111228 KB Output is correct
125 Correct 267 ms 111228 KB Output is correct
126 Correct 233 ms 111228 KB Output is correct
127 Correct 248 ms 111228 KB Output is correct
128 Correct 252 ms 111228 KB Output is correct