Submission #110153

# Submission time Handle Problem Language Result Execution time Memory
110153 2019-05-09T15:35:43 Z Saboon Construction of Highway (JOI18_construction) C++14
0 / 100
31 ms 5248 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 10;

int n, c[maxn], pos[maxn], par[maxn], up[maxn], sz[maxn], st[maxn], Time = 1;
vector<int> t[maxn];
pair<int,int> edge[maxn];
int mxm[4 * maxn], mnm[4 * maxn], lzy[4 * maxn];

void propagate(int, int, int);

pair<int,int> get(int id, int L, int R, int l, int r, int x){
	if (r <= L or R <= l)
		return {-1, -1};
	if (mxm[id] == x and mnm[id] == x)
		return {-1, -1};
	if (L + 1 == R)
		return {L, mxm[id]};
	propagate(id, L, R);
	int mid = (L + R) >> 1;
	auto it = get(2 * id + 1, mid, R, l, r, x);
	if (it.first != -1)
		return it;
	return get(2 * id + 0, L, mid, l, r, x);
}

void change(int id, int L, int R, int l, int r, int x){
	if (L == l and R == r){
		mxm[id] = x;
		mnm[id] = x;
		lzy[id] = x;
		return;
	}
	propagate(id, L, R);
	int mid = (L + R) >> 1;
	if (l < mid)
		change(2 * id + 0, L, mid, l, min(mid, r), x);
	if (mid < r)
		change(2 * id + 1, mid, R, max(l, mid), r, x);
	mxm[id] = max(mxm[2 * id + 0], mxm[2 * id + 1]);
	mnm[id] = min(mnm[2 * id + 0], mnm[2 * id + 1]);
}

void propagate(int id, int L, int R){
	if (lzy[id] == 0)
		return;
	int mid = (L + R) >> 1;
	change(2 * id + 0, L, mid, L, mid, lzy[id]);
	change(2 * id + 1, mid, R, mid, R, lzy[id]);
	lzy[id] = 0;
}

void change(int v){
	int val = c[v];
	while (v != 0){
		int u = up[v];
		change(1, 1, n + 1, st[u], st[v] + 1, val);
		v = par[u];
	}
}

int fen[maxn];

int getfen(int idx){
	int ret = 0;
	for (; idx; idx -= idx & -idx)
		ret += fen[idx];
	return ret;
}

void add(int idx, int x){
	for (; idx <= n; idx += idx & -idx){
		fen[idx] += x;
		if (x == -1)
			fen[idx] = 0;
	}
}

int vitex[maxn];

ll get(int v){
	ll sum = 0;
	int cnt = 0;
	int tmp = c[v];
	int ptr = 0;
	while (v != 0){
		int u = up[v];
		auto it = get(1, 1, n + 1, st[u], st[v] + 1, tmp);
		if (it.first == -1){
			cnt += st[v] - st[u] + 1;
			v = par[u];
		}
		else{
			cnt += st[v] - it.first;
			sum += 1ll * cnt * getfen(tmp - 1);
			add(tmp, cnt);
			vitex[ptr ++] = tmp;
			cnt = 0;
			v = pos[it.first];
			tmp = it.second;
		}
	}
	sum += 1ll * cnt * getfen(tmp - 1);
	while (ptr > 0){
		add(vitex[--ptr], -1);
	}
	return sum;
}

bool cmp(int fi, int se){
	return sz[fi] > sz[se];
}

void dfs(int v){
	pos[Time] = v;
	st[v] = Time ++;
	sort(t[v].begin(), t[v].end(), cmp);
	bool heavy = 1;
	for (auto u : t[v]){
		up[u] = (heavy == 1 ? up[v] : u);
		heavy = 0;
		dfs(u);
	}
}

int dfssz(int v){
	sz[v] = 1;
	for (auto u : t[v])
		sz[v] += dfssz(u);
	return sz[v];
}

int main(){
	ios_base::sync_with_stdio(false);
	scanf("%d", &n);
	vector<int> v;
	for (int i = 1; i <= n; i++){
		scanf("%d", &c[i]);
		v.push_back(c[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	for (int i = 1; i <= n; i++)
		c[i] = lower_bound(v.begin(), v.end(), c[i]) - v.begin() + 1;
	for (int i = 1; i <= n - 1; i++){
		int v, u;
		cin >> v >> u;
		edge[i] = {v, u};
		par[u] = v;
		t[v].push_back(u);
	}
	dfssz(1);
	up[1] = 1;
	dfs(1);
	for (int v = 1; v <= n; v++)
		change(1, 1, n + 1, st[v], st[v] + 1, c[v]);
	for (int i = 1; i <= n - 1; i++){
		int v = edge[i].first, u = edge[i].second;
		printf("%lld\n", get(v));
		change(u);
	}
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:138:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
construction.cpp:141:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &c[i]);
   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 31 ms 5248 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 31 ms 5248 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 31 ms 5248 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -