HDU 6521 Party (线段树 二分)

Posted xorxor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 6521 Party (线段树 二分)相关的知识,希望对你有一定的参考价值。

线段树

题目链接

不知道为什么,别人的跑500ms,我的跑了2000ms...

技术图片
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define rep(i, s, e) for(int i = s; i < e; ++i)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define P pair<int, int>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
static const int N = 2020;
static const int MAX_N = 5e5 + 5;
static const ll Mod = 1e9 + 7;
int n;
int lazy[MAX_N << 2], maxv[MAX_N << 2];
ll sumv[MAX_N << 2];
void push_down(int l, int r, int rt){
    int m = l + r >> 1;
    if(lazy[rt]){
        lazy[rt << 1] = max(lazy[rt << 1], lazy[rt]);
        lazy[rt << 1 | 1] = max(lazy[rt << 1 | 1], lazy[rt]);
        maxv[rt << 1] = max(maxv[rt << 1], lazy[rt]);
        maxv[rt << 1 | 1] = max(maxv[rt << 1 | 1], lazy[rt]);
        sumv[rt << 1] = 1LL * (m - l + 1) * maxv[rt << 1];
        sumv[rt << 1 | 1] = 1LL * (r - m) * maxv[rt << 1 | 1];
        lazy[rt] = 0;
    }
}
void push_up(int rt){
    maxv[rt] = max(maxv[rt << 1], maxv[rt << 1 | 1]);
    sumv[rt] = sumv[rt << 1] + sumv[rt << 1 | 1];
}
void build(int l, int r, int rt){
    lazy[rt] = 0;
    if(l == r){
        sumv[rt] = maxv[rt] = l;
        return ;
    }
    int m = l + r >> 1;
    build(lson);
    build(rson);
    push_up(rt);
}
void update(int l, int r, int rt, int L, int R, int v){
    if(l >= L && r <= R){
        lazy[rt] = max(lazy[rt], v);
        maxv[rt] = max(maxv[rt], v);
        sumv[rt] = 1LL * (r - l + 1) * maxv[rt];
        return ;
    }
    push_down(l, r, rt);
    int m = l + r >> 1;
    if(L <= m) update(lson, L, R, v);
    if(m < R) update(rson, L, R, v);
    push_up(rt);
}
ll query(int l, int r, int rt, int L, int R){
    if(l >= L && r <= R) return sumv[rt];
    push_down(l, r, rt);
    int m = l + r >> 1;
    ll ans = 0;
    if(L <= m) ans += query(lson, L, R);
    if(m < R) ans += query(rson, L, R);
    push_up(rt);
    return ans;
}
int get_pos(int l, int r, int rt, int p){
    push_down(l, r, rt);
    if(l == r) return maxv[rt];
    int m = l + r >> 1;
    if(p <= m) return get_pos(lson, p);
    return get_pos(rson, p);
}
int bin_search(int l, int r, int L, int R){
    int p = 0;
    while(l <= r){
        int m = l + r >> 1;
        if(get_pos(1, n, 1, m) >= R) r = m - 1;
        else{
            p = m;
            l = m + 1;
        }
    }
    return p;
}
void solve(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    int q;
    while(scanf("%d%d", &n, &q) != EOF){
        build(1, n, 1);
        while(q--){
            int L, R;
            scanf("%d%d", &L, &R);
            int p = min(bin_search(1, n, L, R), R);
            if(p < L) puts("0");
            else{
                printf("%lld\n", 1LL * (p - L + 1) * R - query(1, n, 1, L, p));
                update(1, n, 1, L, p, R);
            }
        }
    }
}
int main() {
    solve();
    return 0;
}
View Code

 

以上是关于HDU 6521 Party (线段树 二分)的主要内容,如果未能解决你的问题,请参考以下文章

hdu6521 吉司机线段树

HDU 5649 DZY Loves Sorting(二分答案+线段树线段树合并+线段树分割)

HDU 4614 线段树+二分查找

hdu 4614 线段树 二分

HDU-6070-二分+线段树

HDU - 4614 二分+线段树维护