bzoj 2827 千山鸟飞绝(treap)

离线, 离散化坐标.当第 i 只鸟飞到坐标(x, y), 如果(x, y)有鸟, 就更新mx[i], 再打标记, 更新(x, y)里面的鸟的mx[i].然后把第 i 只鸟加入到(x, y)所在的平衡树内, 再打标记更新 tog[i].

treap区间更新的时候, 插入删除的时候, 注意要push_down(). 还有在删除的时候如果有旋转操作,也要push_down();

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

#define LL long long 
#define pii pair<int, int>
#define MP make_pair
#define ls i << 1
#define rs ls | 1
#define md (ll + rr >> 1)
#define lson ll, md, ls
#define rson md + 1, rr, rs
#define inf 0x3f3f3f3f
#define N 100010
#define M 500010

int ch[M][2];
int rand_val[M], key[M], sz[M], cnt[M], tot;
int mx[N], tog[N], w[N];
int d1[M], d2[M];

int creat(int val){
    ++tot;
    ch[tot][0] = ch[tot][1] = 0;
    key[tot] = val;
    rand_val[tot] = rand();
    sz[tot] = cnt[tot] = 1;
    return tot;
}
int cmp(int x, int val){
    if(w[key[x]] == w[val]) return key[x] < val ? 1 : 0;
    return w[key[x]] < w[val] ? 1 : 0;
}
int cmp2(int x, int val){
    if(key[x] == val) return -1;
    return w[key[x]] <= w[val] ? 1 : 0;
}
void mark_down(int x, int u, int v){
    d1[x] = max(d1[x], u);
    d2[x] = max(d2[x], v);
    mx[key[x]] = max(mx[key[x]], u);
    tog[key[x]] = max(tog[key[x]], v);
}

void push_up(int x){
    sz[x] = cnt[x] + sz[ch[x][0]] + sz[ch[x][1]];
}
void push_down(int x){
    if(!d1[x] && !d2[x] || !x) return ;
    if(ch[x][0])
        mark_down(ch[x][0], d1[x], d2[x]);
    if(ch[x][1])
        mark_down(ch[x][1], d1[x], d2[x]);
    d1[x] = d2[x] = 0;
}

void rot(int &x, int d){
    int k = ch[x][d^1];
    ch[x][d^1] = ch[k][d];
    ch[k][d] = x;
    push_up(x);
    push_up(k);
    x = k;
}
void insert(int &x, int val){
    if(x == 0){
        x = creat(val);
        return ;
    }
    push_down(x);
    int d = cmp(x, val);
    insert(ch[x][d], val);
    if(rand_val[ch[x][d]] > rand_val[x]) rot(x, d ^ 1);
    push_up(x);
}
void dele(int &x, int val){
    if(x == 0) return ;
    push_down(x);
    int d = cmp(x, val);
    if(key[x] == val) d = -1;
    if(d == -1){
        if(!ch[x][0]) x = ch[x][1];
        else if(!ch[x][1]) x = ch[x][0];
        else{
            int d2 = rand_val[ch[x][0]] > rand_val[ch[x][1]] ? 1 : 0;
            push_down(ch[x][d2^1]);
            rot(x, d2);
            dele(ch[x][d2], val);
        }
    }
    else dele(ch[x][d], val);
    if(x) push_up(x);
}
int get_max(int x){
    if(!x) return -1;
    push_down(x);
    if(ch[x][1])
        return get_max(ch[x][1]);
    return w[key[x]];
}
struct query{
    int x, y, id;
    void input(){
        scanf("%d%d%d", &id, &x, &y);
    }
};

struct point{
    int x, y;
    point(int x = 0, int y = 0) : x(x), y(y) {}
    bool operator < (const point &b) const{
        return x < b.x || x == b.x && y < b.y;
    }
    bool operator == (const point &b) const{
        return x == b.x && y == b.y;
    }
};
point p[N], san[M];
query q[M];
int z[N], rt[M], sum;
void gao(int id, int i){
    if(rt[id]){
        mx[i] = max(get_max(rt[id]), mx[i]);
        mark_down(rt[id], w[i], 0);
    }
    insert(rt[id], i);
    mark_down(rt[id], 0, sz[rt[id]] - 1);
}

int haxi(point v){
    return lower_bound(san + 1, san + 1 + sum, v) - san;
}
int main(){
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i){
        scanf("%d%d%d", &w[i], &p[i].x, &p[i].y);
        san[++sum] = p[i];
    }
    int t;
    scanf("%d", &t);
    for(int i = 0; i < t; ++i){
        q[i].input();
        san[++sum] = point(q[i].x, q[i].y);
    }
    sort(san + 1, san + sum + 1);
    sum = unique(san + 1, san + sum + 1) - san - 1;

    for(int i = 1; i <= n; ++i){
        z[i] = haxi(p[i]);
        gao(z[i], i);
    }
    for(int i = 0; i < t; ++i){
        int u = q[i].id, xx = q[i].x, yy = q[i].y;
        dele(rt[z[u]], u);
        z[u] = haxi(point(xx, yy));
        gao(z[u], u);
    }
    for(int i = 1; i <= n; ++i){
        dele(rt[z[i]], i);
    }
    for(int i = 1; i <= n; ++i){
        printf("%lld\n", 1LL * mx[i] * tog[i]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值