2827: 千山鸟飞绝 非旋treap

国际惯例的题面:
2827: 千山鸟飞绝  非旋treap
看起来很不可做的样子,我们先来整理一下题意吧。
就是,维护每个点曾经拥有过的最大的两个属性值,支持把点的位置移动。
我们用map对每个位置进行离散化,对每个位置建立一个平衡树。为了方便分离,这个平衡树的关键字是节点编号。
然后把每个点当做一个节点,放进其所在位置的平衡树里。
剩下要做的就是平衡树分离出一个点,合并一个点,还有打标记了。
对于士气值的标记,我们维护平衡树中的max,每次合并的时候,用这个新点的威武值去给整棵树打标记,再用树中的max给这个新点打标记。
团结值的标记,合并后一起打就好了。另外其实我们不需要在平衡树上维护size的,再开个map维护下就好了。
最后查询的时候,为了让标记全部下放,我们强行把每个点都拆出来就行了。
非旋treap常数略大(可能我的姿势不是很正确QAQ)

代码:

 #pragma GCC optimize(2)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cctype>
typedef long long int lli;
const int maxn=5e5+1e2; struct Point {
int x,y;
friend bool operator < (const Point &a,const Point &b) {
return a.x != b.x ? a.x < b.x : a.y < b.y;
}
}; std::map<int,int> siz;
int w[maxn],hsw[maxn],hss[maxn],fix[maxn],bel[maxn];
int lson[maxn],rson[maxn],lazyw[maxn],lazys[maxn],mxw[maxn];
int root[maxn]; typedef std::pair<int,int> pii;
__inline pii mp(const int &x,const int &y) { return std::make_pair(x,y); } inline void maintain(int pos) {
mxw[pos] = std::max( std::max(mxw[lson[pos]],mxw[rson[pos]]) , w[pos] );
}
inline void applyw(int pos,const int &ww) {
if( !pos ) return;
lazyw[pos] = std::max( lazyw[pos] , ww ) , hsw[pos] = std::max( hsw[pos] , ww );
}
inline void applys(int pos,const int &ss) {
if( !pos ) return;
lazys[pos] = std::max( lazys[pos] , ss ) , hss[pos] = std::max( hss[pos] , ss );
}
inline void push(int pos) {
if( lazyw[pos] ) applyw(lson[pos],lazyw[pos]) , applyw(rson[pos],lazyw[pos]) , lazyw[pos] = ;
if( lazys[pos] ) applys(lson[pos],lazys[pos]) , applys(rson[pos],lazys[pos]) , lazys[pos] = ;
}
inline pii split(int pos,int tar) { // split first tar nodes into left .
if( !pos ) return mp(,);
push(pos);
if( tar < pos ) { // split left .
pii spl = split(lson[pos],tar);
lson[pos] = spl.second , maintain(pos);
return mp(spl.first,pos);
} else {
pii spr = split(rson[pos],tar);
rson[pos] = spr.first , maintain(pos);
return mp(pos,spr.second);
}
}
inline int merge(int x,int y) {
if( x == y || !x || !y ) return x | y;
push(x) , push(y);
if( x > y ) std::swap(x,y);
if( fix[x] < fix[y] ) { // x will be the father .
rson[x] = merge(rson[x],y) , maintain(x);
return x;
} else {
lson[y] = merge(x,lson[y]) , maintain(y);
return y;
}
} inline void remove(int x) { // split x from it's tree into a single node .
pii spr = split(root[bel[x]],x) , spl = split(spr.first,x-);
root[bel[x]] = merge(spl.first,spr.second) , --siz[bel[x]];
}
inline void insert(int x,int id) { // insert x into root[id] .
applyw(root[id],w[x]) , applyw(x,mxw[root[id]]);
pii sp = split(root[id],x);
root[id] = merge(merge(sp.first,x),sp.second) , applys(root[id],siz[id]++);
} inline int getpos(const Point &p) {
static std::map<Point,int> cov;
static int cnt = ;
if( cov.find(p) == cov.end() ) return cov[p] = ++cnt;
else return cov[p];
} inline char nextchar() {
static const int BS = << ;
static char buf[BS],*st=buf+BS,*ed=st;
if( st == ed ) ed = buf + fread(st=buf,,BS,stdin);
return st == ed ? - : *st++;
}
inline int getint() {
int ret = , fix = , ch;
while( !isdigit(ch=nextchar()) ) fix = ch == '-' ? -fix : fix;
do ret=ret*+ch-''; while( isdigit(ch=nextchar()) );
return ret * fix;
} int main() {
static int n,t;
n = getint();
for(int i=,x,y;i<=n;i++) mxw[i] = w[i] = getint() , x = getint() , y = getint() , insert(i,bel[i]=getpos((Point){x,y})) , fix[i] = i;
t = getint() , std::random_shuffle(fix+,fix++n);
for(int i=,p,x,y;i<=t;i++) p = getint() , x = getint() , y = getint() , remove(p) , insert(p,bel[p]=getpos((Point){x,y}));
for(int i=;i<=n;i++) remove(i);
for(int i=;i<=n;i++) printf("%lld\n",(lli)hss[i]*hsw[i]);
return ;
}

遠くても 届かなくても
即使已经远逝 已经无法传达
今宵、確かな夢を刻む
今宵、仍然能刻画出一个真切的梦
始まりの朝 一人ぼっち同士の誰かも
在新的早晨 让那些同样独身的沦落人
笑顔になってゆく
也能逐渐绽放出笑颜

カタチ取る この胸の中
将这情景在心中幻化成形
ずっと、いつでも描けるよう
一直、无论何时都能描绘出来
ささやかな嘘 さみしくないよ、なんて 微笑った
微微笑着说出一个小小的谎言 我并不寂寞哦
風に 揺れるけど
在风中 依旧不变的摇曳着

上一篇:vagrant系列教程(三):vagrant搭建的php7环境(转)


下一篇:BZOJ_1212_[HNOI2004]L语言_哈希