BZOJ4358:permu

浅谈\(K-D\) \(Tree\)https://www.cnblogs.com/AKMer/p/10387266.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=4358

把询问当做一个个点,从小到大对于每个权值,在\(K-D\) \(Tree\)上寻找包含这个权值的区间区间加一,其它的全部清零。对于每个询问,历史最大值就是答案。历史最大值的维护方法看这里:https://www.cnblogs.com/AKMer/p/10232304.html

略微卡常。

时间复杂度:\(O(n\sqrt{m})\)

空间复杂度:\(O(m)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;
#define bo11 (x2<p[u].mn[0]||x1>p[u].mx[0])
#define bo12 (y2<p[u].mn[1]||y1>p[u].mx[1])
#define bo21 (x1<=p[u].mn[0]&&p[u].mx[0]<=x2)
#define bo22 (y1<=p[u].mn[1]&&p[u].mx[1]<=y2)
#define bo31 (x1<=p[u].c[0]&&p[u].c[0]<=x2)
#define bo32 (y1<=p[u].c[1]&&p[u].c[1]<=y2)

const int maxn=5e4+5,inf=2e9;

int ans[maxn];
int n,m,pps,x1,x2,y1,y2;

int read() {
	int x=0,f=1;char ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
	return x*f;
}

struct num {
	int id,val;

	bool operator<(const num &a)const {
		return val<a.val;
	}
}a[maxn];

struct kd_tree {
	int root;
	bool bo[maxn];
	int cnt[maxn],hismx[maxn],add[maxn],cov[maxn],hiscov[maxn];

	struct point {
		int c[2],mn[2],mx[2];
		int id,ls,rs;

		bool operator<(const point &a)const {
			return c[pps]<a.c[pps];
		}
	}p[maxn];

	int build(int l,int r,int d) {
		int mid=(l+r)>>1,u=mid;pps=d;
		nth_element(p+l,p+mid,p+r+1);
		if(l<mid)p[u].ls=build(l,mid-1,d^1);
		if(r>mid)p[u].rs=build(mid+1,r,d^1);
		int ls=p[u].ls,rs=p[u].rs;
		for(int i=0;i<2;i++) {
			int mn=min(p[ls].mn[i],p[rs].mn[i]);
			p[u].mn[i]=min(p[u].c[i],mn);
			int mx=max(p[ls].mx[i],p[rs].mx[i]);
			p[u].mx[i]=max(p[u].c[i],mx);
		}
		return u;
	}

	void prepare() {
		p[0].mn[0]=p[0].mn[1]=inf;
		p[0].mx[0]=p[0].mx[1]=-inf;
		for(int i=1;i<=m;i++) {
			p[i].id=i;
			p[i].mn[0]=p[i].mx[0]=p[i].c[0]=read();
			p[i].mn[1]=p[i].mx[1]=p[i].c[1]=read();
		}
		root=build(1,m,0);
	}

	void add_tag(int u,int v) {
		if(bo[u]) {
			cov[u]+=v;
			hiscov[u]=max(hiscov[u],cov[u]);
		}
		else add[u]+=v;
		cnt[u]+=v,hismx[u]=max(hismx[u],cnt[u]);
	}	

	void cov_tag(int u) {
		if(!bo[u])bo[u]=1,hiscov[u]=0;
		cnt[u]=cov[u]=0;
	}

	void solve(int u,int v,int hisv) {
		bo[u]=1,hiscov[u]=max(hiscov[u],hisv);
		cnt[u]=cov[u]=v;
		hismx[u]=max(hismx[u],hiscov[u]);
	}

	void push_down(int u) {
		if(add[u]) {
			if(p[u].ls)add_tag(p[u].ls,add[u]);
			if(p[u].rs)add_tag(p[u].rs,add[u]);
			add[u]=0;
		}
		if(bo[u]) {
			if(p[u].ls)solve(p[u].ls,cov[u],hiscov[u]);
			if(p[u].rs)solve(p[u].rs,cov[u],hiscov[u]);
			bo[u]=0;
		}
	}

	void change(int u) {
		if(bo11||bo12) {cov_tag(u);return;}
		if(bo21&&bo22) {add_tag(u,1);return;}
		push_down(u);
		if(bo31&&bo32)
			cnt[u]++,hismx[u]=max(hismx[u],cnt[u]);
		else cnt[u]=0;
		if(p[u].ls)change(p[u].ls);
		if(p[u].rs)change(p[u].rs);
	}

	void make_ans(int u) {
		ans[p[u].id]=hismx[u];
		push_down(u);
		if(p[u].ls)make_ans(p[u].ls);
		if(p[u].rs)make_ans(p[u].rs);
	}
}T;

int main() {
	n=read(),m=read();
	for(int i=1;i<=n;i++)
		a[i].id=i,a[i].val=read();
	T.prepare();sort(a+1,a+n+1);
	for(int i=1;i<=n;i++) {
		x1=1,x2=y1=a[i].id,y2=n;
		T.change(T.root);
	}
	T.make_ans(T.root);
	for(int i=1;i<=m;i++)
		printf("%d\n",ans[i]);
	return 0;
}
posted @ 2019-02-18 11:17  AKMer  阅读(244)  评论(0编辑  收藏  举报