답안 #83407

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
83407 2018-11-07T13:45:26 Z tincamatei Construction of Highway (JOI18_construction) C++14
100 / 100
499 ms 104376 KB
#include <bits/stdc++.h>

using namespace std;

template <typename T, int n, typename sum = std::plus<T> >
class Aib {
private:
	T* aib;
	T neutral;
	sum adder;
	inline int lsb(int x) {
		return x & (-x);
	}
public:
	Aib(T _neutral) {
		aib = new T[1+n];
		for(int i = 1; i <= n; ++i)
			aib[i] = neutral;
		neutral = _neutral;
	}
	void update(int poz, T val) {
		while(poz <= n) {
			this->aib[poz] = adder(this->aib[poz], val);
			poz += this->lsb(poz);
		}
	}
	T query(int poz) {
		T rez = this->aib[poz];
		poz -= this->lsb(poz);
		while(poz > 0) {
			rez = adder(this->aib[poz], rez);
			poz -= this->lsb(poz);
		}
		return rez;
	}
};

const int MAX_N = 100000;

struct Cell {
	int l, r, val;
};

vector<int> graph[1+MAX_N];
deque<Cell> chain[1+MAX_N];
deque<Cell> path;
int c[1+MAX_N];
int ma[MAX_N - 1], mb[MAX_N - 1];
int subarb[1+MAX_N], depth[1+MAX_N], parent[1+MAX_N];
int heavyId[1+MAX_N], head[1+MAX_N], lastid;

void dfs(int nod, int papa = 0) {
	subarb[nod] = 1;
	parent[nod] = papa;
	for(auto it: graph[nod])
		if(it != papa) {
			depth[it] = depth[nod] + 1;
			dfs(it, nod);
			subarb[nod] += subarb[it];
		}
}

void heavyTrav(int nod, int papa, int ch) {
	int heavySon = -1;
	heavyId[nod] = ++lastid;
	head[nod] = ch;
	chain[ch].push_back({depth[nod], depth[nod], c[nod]});
	for(auto it: graph[nod])
		if(it != papa && heavySon == -1)
			heavySon = it;
		else if(it != papa && subarb[it] > subarb[heavySon])
			heavySon = it;
	
	if(heavySon != -1)
		heavyTrav(heavySon, nod, ch);
	for(auto it: graph[nod])
		if(it != papa && it != heavySon)
			heavyTrav(it, nod, it);
}

void addRange(int nod, int l, int r, int val) {
	while(chain[nod].size() > 0 && r >= chain[nod][0].r)
		chain[nod].pop_front();
	if(chain[nod].size() > 0 && r >= chain[nod][0].l)
		chain[nod][0].l = r + 1;
	chain[nod].push_front({l, r, val});
}

void update(int nod, int val) {
	while(nod != 0) {
		addRange(head[nod], depth[head[nod]], depth[nod], val);
		nod = parent[head[nod]];
	}
}

void extractRange(int nod, int r) {
	int i = 0;
	while(i + 1 < chain[nod].size() && chain[nod][i + 1].l <= r)
		++i;
	path.push_front({chain[nod][i].l, r, chain[nod][i].val});
	--i;
	while(i >= 0) {
		path.push_front(chain[nod][i]);
		--i;
	}
}

void extract(int nod) {
	while(nod != 0) {
		extractRange(head[nod], depth[nod]);
		nod = parent[head[nod]];
	}
}

Aib<int, MAX_N> aib(0);

long long query(int nod) {
	long long rez = 0LL;
	path.clear();
	extract(nod);

	for(auto it: path) {
		rez = rez + (long long)(it.r - it.l + 1) * (aib.query(MAX_N) - aib.query(it.val));
		aib.update(it.val, it.r - it.l + 1);
	}
	for(auto it: path)
		aib.update(it.val, -(it.r - it.l + 1));
	return rez;
}

int poz[1+MAX_N];

bool cmp(int a, int b) {
	return c[a] < c[b];
}

void normalize(int n) {
	for(int i = 1; i <= n; ++i)
		poz[i - 1] = i;
	std::sort(poz, poz + n, cmp);

	int lastval = c[poz[0]], j = 1;
	for(int i = 0; i < n; ++i)
		if(c[poz[i]] == lastval)
			c[poz[i]] = j;
		else {
			lastval = c[poz[i]];
			c[poz[i]] = ++j;
		}
}

int main() {
#ifdef HOME
	FILE *fin = fopen("input.in", "r");
	FILE *fout = fopen("output.out", "w");
#else
	FILE *fin = stdin;
	FILE *fout = stdout;
#endif
	
	int n, a, b;
	fscanf(fin, "%d", &n);
	for(int i = 1; i <= n; ++i)
		fscanf(fin, "%d", &c[i]);
	
	normalize(n);

	for(int i = 0; i < n - 1; ++i) {
		fscanf(fin, "%d%d", &a, &b);
		ma[i] = a;
		mb[i] = b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	dfs(1);
	heavyTrav(1, 0, 1);
	
	for(int i = 0; i < n - 1; ++i) {
		fprintf(fout, "%lld\n", query(ma[i]));
		update(mb[i], c[mb[i]]);
	}

#ifdef HOME
	fclose(fin);
	fclose(fout);
#endif
	return 0;
}

Compilation message

construction.cpp: In function 'void extractRange(int, int)':
construction.cpp:98:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(i + 1 < chain[nod].size() && chain[nod][i + 1].l <= r)
        ~~~~~~^~~~~~~~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:162:8: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  fscanf(fin, "%d", &n);
  ~~~~~~^~~~~~~~~~~~~~~
construction.cpp:164:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   fscanf(fin, "%d", &c[i]);
   ~~~~~~^~~~~~~~~~~~~~~~~~
construction.cpp:169:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   fscanf(fin, "%d%d", &a, &b);
   ~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 68856 KB Output is correct
2 Correct 68 ms 69020 KB Output is correct
3 Correct 71 ms 69020 KB Output is correct
4 Correct 68 ms 69020 KB Output is correct
5 Correct 67 ms 69020 KB Output is correct
6 Correct 73 ms 69096 KB Output is correct
7 Correct 77 ms 69200 KB Output is correct
8 Correct 76 ms 69320 KB Output is correct
9 Correct 72 ms 69320 KB Output is correct
10 Correct 74 ms 69336 KB Output is correct
11 Correct 70 ms 69336 KB Output is correct
12 Correct 79 ms 69336 KB Output is correct
13 Correct 73 ms 69336 KB Output is correct
14 Correct 70 ms 69360 KB Output is correct
15 Correct 72 ms 69360 KB Output is correct
16 Correct 72 ms 69360 KB Output is correct
17 Correct 74 ms 69360 KB Output is correct
18 Correct 71 ms 69404 KB Output is correct
19 Correct 69 ms 69416 KB Output is correct
20 Correct 74 ms 69416 KB Output is correct
21 Correct 74 ms 69416 KB Output is correct
22 Correct 74 ms 69416 KB Output is correct
23 Correct 72 ms 69432 KB Output is correct
24 Correct 82 ms 69432 KB Output is correct
25 Correct 74 ms 69488 KB Output is correct
26 Correct 71 ms 69568 KB Output is correct
27 Correct 72 ms 69568 KB Output is correct
28 Correct 77 ms 69568 KB Output is correct
29 Correct 72 ms 69568 KB Output is correct
30 Correct 81 ms 69568 KB Output is correct
31 Correct 74 ms 69568 KB Output is correct
32 Correct 75 ms 69568 KB Output is correct
33 Correct 72 ms 69568 KB Output is correct
34 Correct 74 ms 69568 KB Output is correct
35 Correct 84 ms 69568 KB Output is correct
36 Correct 82 ms 69568 KB Output is correct
37 Correct 73 ms 69568 KB Output is correct
38 Correct 82 ms 69568 KB Output is correct
39 Correct 73 ms 69568 KB Output is correct
40 Correct 88 ms 69596 KB Output is correct
41 Correct 74 ms 69596 KB Output is correct
42 Correct 76 ms 69596 KB Output is correct
43 Correct 74 ms 69596 KB Output is correct
44 Correct 72 ms 69628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 68856 KB Output is correct
2 Correct 68 ms 69020 KB Output is correct
3 Correct 71 ms 69020 KB Output is correct
4 Correct 68 ms 69020 KB Output is correct
5 Correct 67 ms 69020 KB Output is correct
6 Correct 73 ms 69096 KB Output is correct
7 Correct 77 ms 69200 KB Output is correct
8 Correct 76 ms 69320 KB Output is correct
9 Correct 72 ms 69320 KB Output is correct
10 Correct 74 ms 69336 KB Output is correct
11 Correct 70 ms 69336 KB Output is correct
12 Correct 79 ms 69336 KB Output is correct
13 Correct 73 ms 69336 KB Output is correct
14 Correct 70 ms 69360 KB Output is correct
15 Correct 72 ms 69360 KB Output is correct
16 Correct 72 ms 69360 KB Output is correct
17 Correct 74 ms 69360 KB Output is correct
18 Correct 71 ms 69404 KB Output is correct
19 Correct 69 ms 69416 KB Output is correct
20 Correct 74 ms 69416 KB Output is correct
21 Correct 74 ms 69416 KB Output is correct
22 Correct 74 ms 69416 KB Output is correct
23 Correct 72 ms 69432 KB Output is correct
24 Correct 82 ms 69432 KB Output is correct
25 Correct 74 ms 69488 KB Output is correct
26 Correct 71 ms 69568 KB Output is correct
27 Correct 72 ms 69568 KB Output is correct
28 Correct 77 ms 69568 KB Output is correct
29 Correct 72 ms 69568 KB Output is correct
30 Correct 81 ms 69568 KB Output is correct
31 Correct 74 ms 69568 KB Output is correct
32 Correct 75 ms 69568 KB Output is correct
33 Correct 72 ms 69568 KB Output is correct
34 Correct 74 ms 69568 KB Output is correct
35 Correct 84 ms 69568 KB Output is correct
36 Correct 82 ms 69568 KB Output is correct
37 Correct 73 ms 69568 KB Output is correct
38 Correct 82 ms 69568 KB Output is correct
39 Correct 73 ms 69568 KB Output is correct
40 Correct 88 ms 69596 KB Output is correct
41 Correct 74 ms 69596 KB Output is correct
42 Correct 76 ms 69596 KB Output is correct
43 Correct 74 ms 69596 KB Output is correct
44 Correct 72 ms 69628 KB Output is correct
45 Correct 80 ms 69640 KB Output is correct
46 Correct 78 ms 69912 KB Output is correct
47 Correct 77 ms 69988 KB Output is correct
48 Correct 80 ms 70208 KB Output is correct
49 Correct 79 ms 70532 KB Output is correct
50 Correct 78 ms 70636 KB Output is correct
51 Correct 78 ms 70720 KB Output is correct
52 Correct 78 ms 70720 KB Output is correct
53 Correct 91 ms 70720 KB Output is correct
54 Correct 89 ms 70732 KB Output is correct
55 Correct 85 ms 70848 KB Output is correct
56 Correct 79 ms 70888 KB Output is correct
57 Correct 83 ms 70888 KB Output is correct
58 Correct 100 ms 70888 KB Output is correct
59 Correct 80 ms 70924 KB Output is correct
60 Correct 81 ms 71128 KB Output is correct
61 Correct 77 ms 71204 KB Output is correct
62 Correct 78 ms 71280 KB Output is correct
63 Correct 76 ms 71360 KB Output is correct
64 Correct 79 ms 71360 KB Output is correct
65 Correct 77 ms 71360 KB Output is correct
66 Correct 76 ms 71400 KB Output is correct
67 Correct 79 ms 71568 KB Output is correct
68 Correct 75 ms 71892 KB Output is correct
69 Correct 78 ms 71892 KB Output is correct
70 Correct 78 ms 71892 KB Output is correct
71 Correct 81 ms 71892 KB Output is correct
72 Correct 80 ms 71892 KB Output is correct
73 Correct 80 ms 71892 KB Output is correct
74 Correct 74 ms 71940 KB Output is correct
75 Correct 76 ms 71988 KB Output is correct
76 Correct 76 ms 71988 KB Output is correct
77 Correct 78 ms 71988 KB Output is correct
78 Correct 76 ms 72052 KB Output is correct
79 Correct 86 ms 72104 KB Output is correct
80 Correct 75 ms 72148 KB Output is correct
81 Correct 77 ms 72340 KB Output is correct
82 Correct 79 ms 72340 KB Output is correct
83 Correct 76 ms 72412 KB Output is correct
84 Correct 76 ms 72492 KB Output is correct
85 Correct 77 ms 72520 KB Output is correct
86 Correct 75 ms 72520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 68856 KB Output is correct
2 Correct 68 ms 69020 KB Output is correct
3 Correct 71 ms 69020 KB Output is correct
4 Correct 68 ms 69020 KB Output is correct
5 Correct 67 ms 69020 KB Output is correct
6 Correct 73 ms 69096 KB Output is correct
7 Correct 77 ms 69200 KB Output is correct
8 Correct 76 ms 69320 KB Output is correct
9 Correct 72 ms 69320 KB Output is correct
10 Correct 74 ms 69336 KB Output is correct
11 Correct 70 ms 69336 KB Output is correct
12 Correct 79 ms 69336 KB Output is correct
13 Correct 73 ms 69336 KB Output is correct
14 Correct 70 ms 69360 KB Output is correct
15 Correct 72 ms 69360 KB Output is correct
16 Correct 72 ms 69360 KB Output is correct
17 Correct 74 ms 69360 KB Output is correct
18 Correct 71 ms 69404 KB Output is correct
19 Correct 69 ms 69416 KB Output is correct
20 Correct 74 ms 69416 KB Output is correct
21 Correct 74 ms 69416 KB Output is correct
22 Correct 74 ms 69416 KB Output is correct
23 Correct 72 ms 69432 KB Output is correct
24 Correct 82 ms 69432 KB Output is correct
25 Correct 74 ms 69488 KB Output is correct
26 Correct 71 ms 69568 KB Output is correct
27 Correct 72 ms 69568 KB Output is correct
28 Correct 77 ms 69568 KB Output is correct
29 Correct 72 ms 69568 KB Output is correct
30 Correct 81 ms 69568 KB Output is correct
31 Correct 74 ms 69568 KB Output is correct
32 Correct 75 ms 69568 KB Output is correct
33 Correct 72 ms 69568 KB Output is correct
34 Correct 74 ms 69568 KB Output is correct
35 Correct 84 ms 69568 KB Output is correct
36 Correct 82 ms 69568 KB Output is correct
37 Correct 73 ms 69568 KB Output is correct
38 Correct 82 ms 69568 KB Output is correct
39 Correct 73 ms 69568 KB Output is correct
40 Correct 88 ms 69596 KB Output is correct
41 Correct 74 ms 69596 KB Output is correct
42 Correct 76 ms 69596 KB Output is correct
43 Correct 74 ms 69596 KB Output is correct
44 Correct 72 ms 69628 KB Output is correct
45 Correct 80 ms 69640 KB Output is correct
46 Correct 78 ms 69912 KB Output is correct
47 Correct 77 ms 69988 KB Output is correct
48 Correct 80 ms 70208 KB Output is correct
49 Correct 79 ms 70532 KB Output is correct
50 Correct 78 ms 70636 KB Output is correct
51 Correct 78 ms 70720 KB Output is correct
52 Correct 78 ms 70720 KB Output is correct
53 Correct 91 ms 70720 KB Output is correct
54 Correct 89 ms 70732 KB Output is correct
55 Correct 85 ms 70848 KB Output is correct
56 Correct 79 ms 70888 KB Output is correct
57 Correct 83 ms 70888 KB Output is correct
58 Correct 100 ms 70888 KB Output is correct
59 Correct 80 ms 70924 KB Output is correct
60 Correct 81 ms 71128 KB Output is correct
61 Correct 77 ms 71204 KB Output is correct
62 Correct 78 ms 71280 KB Output is correct
63 Correct 76 ms 71360 KB Output is correct
64 Correct 79 ms 71360 KB Output is correct
65 Correct 77 ms 71360 KB Output is correct
66 Correct 76 ms 71400 KB Output is correct
67 Correct 79 ms 71568 KB Output is correct
68 Correct 75 ms 71892 KB Output is correct
69 Correct 78 ms 71892 KB Output is correct
70 Correct 78 ms 71892 KB Output is correct
71 Correct 81 ms 71892 KB Output is correct
72 Correct 80 ms 71892 KB Output is correct
73 Correct 80 ms 71892 KB Output is correct
74 Correct 74 ms 71940 KB Output is correct
75 Correct 76 ms 71988 KB Output is correct
76 Correct 76 ms 71988 KB Output is correct
77 Correct 78 ms 71988 KB Output is correct
78 Correct 76 ms 72052 KB Output is correct
79 Correct 86 ms 72104 KB Output is correct
80 Correct 75 ms 72148 KB Output is correct
81 Correct 77 ms 72340 KB Output is correct
82 Correct 79 ms 72340 KB Output is correct
83 Correct 76 ms 72412 KB Output is correct
84 Correct 76 ms 72492 KB Output is correct
85 Correct 77 ms 72520 KB Output is correct
86 Correct 75 ms 72520 KB Output is correct
87 Correct 91 ms 73100 KB Output is correct
88 Correct 176 ms 75344 KB Output is correct
89 Correct 377 ms 82208 KB Output is correct
90 Correct 379 ms 84360 KB Output is correct
91 Correct 371 ms 86448 KB Output is correct
92 Correct 225 ms 98876 KB Output is correct
93 Correct 237 ms 100984 KB Output is correct
94 Correct 223 ms 102028 KB Output is correct
95 Correct 238 ms 102028 KB Output is correct
96 Correct 258 ms 102028 KB Output is correct
97 Correct 252 ms 102028 KB Output is correct
98 Correct 241 ms 102028 KB Output is correct
99 Correct 240 ms 102028 KB Output is correct
100 Correct 421 ms 102028 KB Output is correct
101 Correct 487 ms 102028 KB Output is correct
102 Correct 462 ms 102028 KB Output is correct
103 Correct 493 ms 102028 KB Output is correct
104 Correct 261 ms 102028 KB Output is correct
105 Correct 262 ms 102028 KB Output is correct
106 Correct 298 ms 102028 KB Output is correct
107 Correct 313 ms 102028 KB Output is correct
108 Correct 321 ms 102028 KB Output is correct
109 Correct 324 ms 102028 KB Output is correct
110 Correct 211 ms 102056 KB Output is correct
111 Correct 231 ms 102056 KB Output is correct
112 Correct 224 ms 102056 KB Output is correct
113 Correct 235 ms 102056 KB Output is correct
114 Correct 424 ms 102056 KB Output is correct
115 Correct 499 ms 102056 KB Output is correct
116 Correct 263 ms 104376 KB Output is correct
117 Correct 247 ms 104376 KB Output is correct
118 Correct 244 ms 104376 KB Output is correct
119 Correct 252 ms 104376 KB Output is correct
120 Correct 270 ms 104376 KB Output is correct
121 Correct 241 ms 104376 KB Output is correct
122 Correct 237 ms 104376 KB Output is correct
123 Correct 290 ms 104376 KB Output is correct
124 Correct 255 ms 104376 KB Output is correct
125 Correct 296 ms 104376 KB Output is correct
126 Correct 253 ms 104376 KB Output is correct
127 Correct 237 ms 104376 KB Output is correct
128 Correct 285 ms 104376 KB Output is correct