bzoj3133 [Baltic2013]ballmachine 倍增+优先队列

题目链接:传送门

题目解析

考虑树中没有球的情况:
我们一个个把球放进去,共放nn个球,把第ii个节点的球是第几个球记为dfn[i]dfn[i]
比如样例:

nn个球放进去,nn个球分别落到5,8,6,3,7,4,2,15,8,6,3,7,4,2,1,称这个排列为掉落顺序。
不难发现若掉落顺序靠前的点为空(即没有球),则新的球不珂能掉到掉落顺序靠后的点。
举例:若33为空,那么新的球不珂能掉落到7,4,2,17,4,2,1
所以一个新的球一定会落到为空的点中掉落顺序最靠前的点qwq。
所以对于操作1,建一个以dfndfn值为关键字的小根堆(优先队列),存储所有非空的点,新加入一个球就把堆顶取出来,打上标记表示这里有球即可qwq。
在根放入numnum个球就暴力放numnum次即珂。
操作2更简单,易证把一个球删掉就相当于它到根上有球的深度最浅的节点的球删掉。
因为如果一个节点没有球,那它的祖先也没有球(不然就掉下来了qwq)
所以用倍增(树剖珂能珂以?没试过qwq)求出要删除的节点到根的最浅的有球的节点,把这个节点的标记去掉,再把这个节点加入堆。

复杂度分析

空间复杂度O(nlogn)...O(nlogn)...(anc数组(倍增数组)要开nlognnlogn
时间复杂度:
预处理掉落顺序:最坏情况应该是菊花图,复杂度O(nlogn)O(nlogn)
插入球和删除球都是O(logn)O(logn)(对堆操作和倍增都是O(logn)O(logn)
使用摊还分析,发现一次删除会使最多插入的球数+1,所以删除的摊还代价为O(1logn)=O(logn)O(1*logn)=O(logn)
总共qq次,所以时间复杂度O(qlogn)O(qlogn)
总时间复杂度O((n+q)logn)O((n+q)logn) qwq

毒瘤代码

#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define re register int
#define rl register ll
using namespace std;
typedef long long ll;
int read() {
	re x=0,f=1;
	char ch=getchar();
	while(ch<'0' || ch>'9') {
		if(ch=='-')	f=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9') {
		x=(x<<1)+(x<<3)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(const int x) {
	if(x>9)	write(x/10);
	putchar(x%10+'0');
}
const int Size=200005;
int n,q,cnt,head[Size];
struct Edge {
	int v,next;
} w[Size<<1]; 
void AddEdge(int u,int v) {
	w[++cnt].v=v;
	w[cnt].next=head[u];
	head[u]=cnt;
}
int deep[Size],anc[Size][21],minv[Size];
void dfs(int x,int fa) {
	deep[x]=deep[fa]+1;
	anc[x][0]=fa;
	for(re i=1; i<=17; i++) {
		anc[x][i]=anc[anc[x][i-1]][i-1];
	}
	minv[x]=x;
	for(int i=head[x]; i; i=w[i].next) {
		int nxt=w[i].v;
		dfs(nxt,x);
		if(minv[nxt]<minv[x]) {
			minv[x]=minv[nxt];
		}
	}
}
int tim,dfn[Size];
struct node {
	int x,t;
};
inline bool operator < (const node a,const node b) {
	return a.t<b.t;
}
inline bool operator > (const node a,const node b) {
	return a.t>b.t;
}
void dfs2(int x) {
	vector<node> son;
	for(re i=head[x]; i; i=w[i].next) {
		int nxt=w[i].v;
		if(nxt!=anc[x][0]) {
			son.push_back((node){nxt,minv[nxt]});
		}
	}
	sort(son.begin(),son.end());
	int len=son.size();
	for(int i=0; i<len; i++) {
		int nxt=son[i].x;
		dfs2(nxt);
	}
	dfn[x]=++tim;
}
priority_queue<node,vector<node>,greater<node> > Heap;	//小根堆 
//小根堆里记录所有没有球的节点,以dfn为关键字 
bool ball[Size];
inline int insert() {
	node tp=Heap.top();
	Heap.pop();
	ball[tp.x]=true;
	return tp.x;		//返回落到的位置 
}
inline int pop(int num) {
	int x=num;
	for(re i=17; i>=0; i--) {
		if(ball[anc[x][i]]) {
			x=anc[x][i];
		}
	}
	ball[x]=false;
	Heap.push((node){x,dfn[x]});
	return deep[num]-deep[x];
}
int main() {
//	freopen("15.in","r",stdin);
	n=read();
	q=read();
	int root=0;
	for(re i=1; i<=n; i++) {
		int fa=read();
		if(!fa) {
			root=i;
		} else {
			AddEdge(fa,i);
		}
	}
	dfs(root,0);
	dfs2(root);
	for(re i=1; i<=n; i++) {
//		printf("%d ",dfn[i]);
		Heap.push((node){i,dfn[i]});
	}
//	putchar(10);
	while(q--) {
		int op=read();
		int num=read();
		if(op==1) {
			for(re i=1; i<num; i++) {
				insert();
			}
			printf("%d\n",insert());
		} else {
			printf("%d\n",pop(num));
		}
	}
	return 0;
}
/*
15 8
5 9 11 3 10 3 13 13 11 12 12 0 10 5 9
1 8
2 13
2 7
2 7
2 8
2 2
2 1
1 4
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章