QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#119100#5421. Factories Once MoreAcestarWA 111ms17912kbC++202.4kb2023-07-04 21:52:222023-07-04 21:52:25

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-04 21:52:25]
  • 评测
  • 测评结果:WA
  • 用时:111ms
  • 内存:17912kb
  • [2023-07-04 21:52:22]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define ll long long

typedef pair<int, int> P;
#define fi first
#define se second

const int N = 1e5 + 5;

int n, m;
vector<P> G[N];

mt19937 rng(time(0));
struct FHQ
{
	int ls[N], rs[N], siz[N], rnd[N];
	ll val[N], k[N], b[N];
	int cnt, st[N], tp;

	int New(int v)
	{
		int x = tp ? st[tp--] : ++cnt;
		ls[x] = rs[x] = 0, siz[x] = 1, rnd[x] = rng();
		val[x] = v, k[x] = b[x] = 0;
		return x;
	}

	void pushup(int p)
	{
		siz[p] = siz[ls[p]] + siz[rs[p]] + 1;
	}

	void push(int p, ll K, ll B)
	{
		val[p] += K * (siz[ls[p]] + 1) + B;
		k[p] += K, b[p] += B;
	}

	void pushdown(int p)
	{
		if(k[p] || b[p])
		{
			if(ls[p]) push(ls[p], k[p], b[p]);
			if(rs[p]) push(rs[p], k[p], k[p] * (siz[ls[p]] + 1) + b[p]);
			k[p] = b[p] = 0;
		}
		return;
	}

	void Split(int p, int v, int &x, int &y)
	{
		if(!p) return x = y = 0, void();
		pushdown(p);
		if(val[p] >= v) x = p, Split(rs[p], v, rs[x], y);
		else y = p, Split(ls[p], v, x, ls[y]);
		pushup(p);
		return;
	}

	int merge(int x, int y)
	{
		if(!x || !y) return x | y;
		if(rnd[x] < rnd[y])
		{
			pushdown(x);
			rs[x] = merge(rs[x], y);
			return pushup(x), x;
		} else {
			pushdown(y);
			ls[y] = merge(x, ls[y]);
			return pushup(y), y;
		}
	}

	void Insert(int &p, int v)
	{
		int x, y; Split(p, v, x, y);
		p = merge(merge(x, New(v)), y);
		return;
	}

	void Clear(int p, vector<ll> &vec)
	{
		pushdown(p);
		if(ls[p]) Clear(ls[p], vec);
		vec.push_back(val[p]); st[++tp] = p;
		if(rs[p]) Clear(rs[p], vec);
		return;
	}
} T;
int rt[N];

int siz[N];
void dfs(int u, int fa)
{
	int son = 0; siz[u] = 1;
	for(auto p : G[u]) if(p.fi != fa)
	{
		int v = p.fi, w = p.se;
		dfs(v, u); siz[u] += siz[v];
		if(siz[v] > siz[son]) son = v;
		T.push(rt[v], -2 * w, w * (m + 1));
	}
	if(son) rt[u] = rt[son];
	T.Insert(rt[u], 0);
	for(auto p : G[u]) if(p.fi != fa && p.fi != son)
	{
		int v = p.fi; vector<ll> vec;
		T.Clear(rt[v], vec);
		for(auto it : vec) T.Insert(rt[u], it);
	}
	return;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);

	cin >> n >> m;
	for(int i = 1, u, v, w; i < n; i++)
		cin >> u >> v >> w, G[u].push_back({v, w}), G[v].push_back({u, w});
	dfs(1, 0);

	vector<ll> vec;
	T.Clear(rt[1], vec);

	ll ans = 0;
	for(int i = 0; i < m; i++) ans += vec[i];
	cout << ans << '\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8048kb

input:

6 3
1 2 3
2 3 2
2 4 1
1 5 2
5 6 3

output:

22

result:

ok 1 number(s): "22"

Test #2:

score: 0
Accepted
time: 1ms
memory: 8996kb

input:

4 3
1 2 2
1 3 3
1 4 4

output:

18

result:

ok 1 number(s): "18"

Test #3:

score: 0
Accepted
time: 0ms
memory: 9064kb

input:

2 2
1 2 1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Wrong Answer
time: 111ms
memory: 17912kb

input:

100000 17
37253 35652 9892
56367 53643 1120
47896 49255 4547
93065 88999 1745
5251 6742 5031
49828 50972 8974
31548 46729 1032
56341 56287 4812
21896 22838 1682
82124 90557 7307
76289 76949 7028
33834 45380 6856
15499 15064 2265
10127 5251 9920
87208 93945 9487
68990 72637 6891
91640 85004 2259
4748...

output:

36499779246

result:

wrong answer 1st numbers differ - expected: '4915539756', found: '36499779246'