zoukankan      html  css  js  c++  java
  • [BZOJ2827]千山鸟飞绝

    bzoj

    sol

    每个位置维护一棵平衡树,每次插入的时候更新答案即可。
    至于位置离散的话,开了(map)不就好了么。。。

    #include<cstdio>
    #include<algorithm>
    #include<map>
    using namespace std;
    int gi(){
    	int x=0,w=1;char ch=getchar();
    	while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    	if (ch=='-') w=0,ch=getchar();
    	while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    	return w?x:-x;
    }
    #define pi pair<int,int>
    #define mk make_pair
    #define cmax(a,b) (a<b?a=b:a)
    const int N = 4e5+5;
    int n,m,w[N],val[N],pos[N],rt[N],tot,tj[N],sq[N],fa[N],ch[2][N],sz[N],tt[N],ts[N],S[N];
    map<pi,int>M;
    bool son(int x){
    	return x==ch[1][fa[x]];
    }
    void pushup(int x){
    	sz[x]=sz[ch[0][x]]+1+sz[ch[1][x]];
    	val[x]=max(w[x],max(val[ch[0][x]],val[ch[1][x]]));
    }
    void cover(int x,int _t,int _s){
    	cmax(tj[x],_t);cmax(sq[x],_s);
    	cmax(tt[x],_t);cmax(ts[x],_s);
    }
    void pushdown(int x){
    	if (ch[0][x]) cover(ch[0][x],tt[x],ts[x]);
    	if (ch[1][x]) cover(ch[1][x],tt[x],ts[x]);
    	tt[x]=ts[x]=0;
    }
    void rotate(int x){
    	int y=fa[x],z=fa[y],c=son(x);
    	ch[c][y]=ch[c^1][x];if (ch[c][y]) fa[ch[c][y]]=y;
    	fa[x]=z;if (z) ch[son(y)][z]=x;
    	ch[c^1][x]=y;fa[y]=x;pushup(y);
    }
    void splay(int x,int goal){
    	S[S[0]=1]=x;
    	for (int i=x;fa[i];i=fa[i]) S[++S[0]]=fa[i];
    	while (S[0]) pushdown(S[S[0]--]);
    	for (int y=fa[x];y!=goal;rotate(x),y=fa[x])
    		if (fa[y]!=goal) son(x)^son(y)?rotate(x):rotate(y);
    	pushup(x);if (!goal) rt[pos[x]]=x;
    }
    void insert(int y){
    	if (!rt[pos[y]]) {rt[pos[y]]=y;pushup(y);return;}
    	cover(rt[pos[y]],w[y],sz[rt[pos[y]]]);
    	cmax(tj[y],val[rt[pos[y]]]);cmax(sq[y],sz[rt[pos[y]]]);
    	int x=rt[pos[y]];
    	while (233){
    		pushdown(x);
    		if (y<x)
    			if (!ch[0][x]){
    				fa[ch[0][x]=y]=x;splay(y,0);return;
    			}else x=ch[0][x];
    		else
    			if (!ch[1][x]){
    				fa[ch[1][x]=y]=x;splay(y,0);return;
    			}else x=ch[1][x];
    	}
    }
    int pre(int y){
    	int x=ch[1][y];
    	while (ch[0][x]) pushdown(x),x=ch[0][x];
    	splay(x,y);return x;
    }
    void erase(int y){
    	splay(y,0);
    	if (!ch[1][y]) {fa[rt[pos[y]]=ch[0][y]]=0;ch[0][y]=0;return;}
    	int x=pre(y);
    	fa[rt[pos[y]]=fa[ch[0][x]=ch[0][y]]=x]=0;
    	ch[0][y]=ch[1][y]=0;pushup(x);
    }
    void dfs(int x){
    	if (!x) return;pushdown(x);
    	dfs(ch[0][x]);dfs(ch[1][x]);
    }
    int main(){
    	n=gi();
    	for (int i=1;i<=n;++i){
    		w[i]=gi();int x=gi(),y=gi();
    		if (!M[mk(x,y)]) M[mk(x,y)]=++tot;
    		pos[i]=M[mk(x,y)];insert(i);
    	}
    	m=gi();while (m--){
    		int v=gi(),x=gi(),y=gi();
    		if (!M[mk(x,y)]) M[mk(x,y)]=++tot;
    		erase(v);pos[v]=M[mk(x,y)];insert(v);
    	}
    	for (int i=1;i<=tot;++i) dfs(rt[i]);
    	for (int i=1;i<=n;++i) printf("%lld
    ",1ll*tj[i]*sq[i]);
    	return 0;
    }
    
  • 相关阅读:
    寄生组合式继承
    原型式继承
    js数学方法应用
    arguments.callee
    date日期比较和格式化方法
    转型函数 Boolean()
    javaScript高程第三版读书笔记
    《JavaScript dom 编程艺术》 placeholder占位符IE8兼容办法。
    正确设置网站title、keywords、description(转载)
    理解RESTful架构
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/9307350.html
Copyright © 2011-2022 走看看