[CF1535E]Gold Transfer

98 篇文章 0 订阅

题目

传送门 to CF

思路

由于 c i > c p i c_i>c_{p_i} ci>cpi,所以总是先买靠近根的 g o l d \rm gold gold

另外,由于买金子是永久性的,所以我们可以暴力寻找这个点。反正每次会把一个点买空(每次询问有 1 1 1 个点可能买不空,其他都会买空),所以我们只需要寻找 O ( n + q ) \mathcal O(n+q) O(n+q) 次。

怎么找呢?我由于一开始看成了 c i < c p i c_i<c_{p_i} ci<cpi,准备用并查集,结果嗝屁了。于是就往着树剖的方向想。打 L C T \tt LCT LCT 当然是不可能的!于是我准备打 替罪羊 树剖。

运用同样的原理,如果某个 “轻儿子” 的重量超过了当前子树的 α \alpha α 倍,就把这颗子树内的链剖分重新做一次。然后我发现没法维护 s i z e size size 啊,加入一个点得把它的祖先都改了,我又不是 L C T \tt LCT LCT,咋改?

于是我只好不维护 s i z e size size,只更改链头的 s i z e size size 。没想到这就过了?可能没有人会想到有这么蠢的做法吧……

这个做法的复杂度是错误的。很容易 h a c k \rm hack hack,只需要先接出一条长链——此时,由于 s i z e size size 更新不及时,所有点的 s i z e size size 都是二——然后从下往上依次给每个点接一个轻儿子,触发重构。此时是 O ( n 2 ) \mathcal O(n^2) O(n2) 的。

那么正经的做法是什么呢?每次加入一个叶子,倍增的复杂度仍然正确。多简单啊!我就是没想到!

代码

我就把我的错误做法代码贴上来好了,图个乐子。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
// #include <assert.h>
// # define assert(x) { if(!(x)) puts("shit"), exit(0); }
# define assert(x) {} // do nothing
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef long long int_;
inline int readint(){
	int a = 0; char c = getchar(), f = 1;
	for(; c<'0'||c>'9'; c=getchar())
		if(c == '-') f = -f;
	for(; '0'<=c&&c<='9'; c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
}
inline void writeint(int_ x){
	if(x > 9) writeint(x/10);
	putchar((x-x/10*10)^48);
}

const int MaxN = 300005;
const double alpha = 0.7; // Scapegoat

int bel[MaxN]; // belong to which chain
deque<int> q[MaxN]; // record chains
int siz[MaxN]; // to adjust the chain
int fa[MaxN]; // parent on tree
vector<int> G[MaxN]; // all childs
int son[MaxN]; // heavy son (for chain)
int a[MaxN], c[MaxN];

void destroy(int x){
	assert(!q[bel[x]].empty());
	for(int y : G[x]) destroy(y);
	assert(q[bel[x]].back() == x);
	q[bel[x]].pop_back();
	siz[x] = 1; // recalc
	for(int y : G[x])
		siz[x] += siz[y];
}
void rebuild(int x){
	assert(a[x] != 0); // not a deleted node
	q[bel[x]].push_back(x);
	if(G[x].empty()) return ;
	son[x] = G[x].back(); // reset
	for(int y : G[x])
		if(siz[y] > siz[son[x]])
			son[x] = y;
	bel[son[x]] = bel[x];
	rebuild(son[x]);
	for(int y : G[x])
		if(y != son[x])
			rebuild(bel[y] = y);
}
void adjust(int x){
	bel[x] = x; // single
	q[bel[x]].push_back(x);
	int goat = -1; // scapegoat
	for(; true; x=q[bel[x]].front()){
		++ siz[x]; // newly added node
		if(!(~fa[x]) || !a[fa[x]]) break;
		if(siz[x] > alpha*siz[fa[x]])
			goat = fa[x]; // to fuck
		x = fa[x]; // go up till root
	}
	if(goat != -1){
		destroy(goat);
		rebuild(goat);
	}
}

int findRoot(int x){
	for(; true; x=fa[x]){
		assert(!q[bel[x]].empty());
		x = q[bel[x]].front();
		if(!(~fa[x]) || !a[fa[x]])
			return x; // found
	}
	return -1;
}

int main(){
	int n = readint();
	a[0] = readint(), c[0] = readint();
	siz[0] = 1, fa[0] = -1; // root
	if(a[0]) q[0].push_back(0);
	for(int opt,x,v,i=1; i<=n; ++i){
		opt = readint(), x = readint();
		if(opt == 1){
			a[i] = readint();
			c[i] = readint();
			G[x].push_back(i);
			fa[i] = x, adjust(i);
		}
		if(opt == 2){
			v = readint(); // expected
			int cnt = 0; int_ sum = 0;
			while(cnt != v){
				if(!a[x] || q[bel[x]].empty())
					break; // not enough
				int y = findRoot(x);
				assert(a[y] != 0);
				int t = min(v-cnt,a[y]);
				a[y] -= t, cnt += t;
				sum += c[y]*1ll*t;
				if(!a[y]){ // delete it
					assert(!q[bel[y]].empty());
					q[bel[y]].pop_front();
				}
			}
			writeint(cnt), putchar(' ');
			writeint(sum), putchar('\n');
			fflush(stdout); // online mode
		}
	}
	return 0;
}

这里附上生成 h a c k \rm hack hack 数据的代码。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
// #include <assert.h>
// # define assert(x) { if(!(x)) puts("shit"), exit(0); }
# define assert(x) {} // do nothing
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef long long int_;
inline int readint(){
	int a = 0; char c = getchar(), f = 1;
	for(; c<'0'||c>'9'; c=getchar())
		if(c == '-') f = -f;
	for(; '0'<=c&&c<='9'; c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
}
inline void writeint(int_ x){
	if(x > 9) writeint(x/10);
	putchar((x-x/10*10)^48);
}

int main(){
	freopen("hack.in","w",stdout);
	int n = 99999; // multiple of 3
	printf("%d %d %d\n",n,1,1);
	for(int i=1; i<=(n/3); ++i)
		printf("1 %d %d %d\n",i-1,1,i+1);
	for(int i=(n/3),t=(n/3); i>=1; --i){
		printf("1 %d %d %d\n",i,1,n+2), ++ t;
		printf("1 %d %d %d\n",t,1,n+3), ++ t;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值