HDU 4325 Flowers (线段树+离散化)

去年多校的时候用最暴力的办法水过了,忽然看到,觉得还是得用正统的方式过了.............


#include <iostream>
#include <algorithm>
#include <cmath>
#include<functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <climits>//形如INT_MAX一类的
#define MAX 100050
#define INF 0x7FFFFFFF
#define L(x) x<<1
#define R(x) x<<1|1
using namespace std;

inline void RD(int &ret) {
    char c;
    do {
        c = getchar();
    } while(c < '0' || c > '9') ;
    ret = c - '0';
    while((c=getchar()) >= '0' && c <= '9')
        ret = ret * 10 + ( c - '0' );
}
void OT(int a) {
    if(a >= 10)OT(a / 10);
    putchar(a % 10 + '0');
}

int n,m,cnt,size;
int x[MAX * 3],xx[MAX * 3],le[MAX],ri[MAX],qes[MAX];
struct Node {
    int l,r,mid;
    int add,v;
} tr[MAX * 4];

void up(int x) {
    tr[x].v = tr[L(x)].v + tr[R(x)].v;
}

void down(int x) {
    if(tr[x].add != 0) {
        int l,r;
        tr[L(x)].add += tr[x].add;
        tr[R(x)].add += tr[x].add;
        l = tr[L(x)].l;
        r = tr[L(x)].r;
        tr[L(x)].v += (r - l + 1) * tr[x].add;
        //tr[L(x)].v = (r - l + 1) * tr[L(x)].add;
        l = tr[R(x)].l;
        r = tr[R(x)].r;
        tr[R(x)].v += (r - l + 1) * tr[x].add;
        //tr[R(x)].v = (r - l + 1) * tr[R(x)].add;
        tr[x].add = 0;
    }
}

void build(int l,int r,int x) {
    tr[x].l = l;
    tr[x].r = r;
    tr[x].mid = (l + r) >> 1;
    tr[x].v = 0;
    tr[x].add = 0;
    if(l == r) return ;

    build(l,tr[x].mid,L(x));
    build(tr[x].mid + 1,r,R(x));
}

void update(int l,int r,int x,int va) {
    if(l <= tr[x].l && r >= tr[x].r) {
        tr[x].add += va;
        tr[x].v += (tr[x].r - tr[x].l + 1) * va;
        //tr[x].v = (tr[x].r - tr[x].l + 1) * tr[x].add;
        return ;
    }
    down(x);
    int mid = tr[x].mid;
    if(r > mid) update(l,r,R(x),va);
    if(l <= mid) update(l,r,L(x),va);
    up(x);
}

int ans = 0;
void query(int l,int x) {
    if(tr[x].l == tr[x].r) {
        ans += tr[x].v;
        return ;
    }
    down(x);
    int mid = tr[x].mid;
    if(l > mid) query(l,R(x));
    if(l <= mid) query(l,L(x));
}

int main() {
    int T;
    cin >> T;
    int ca = 1;
    while(T--) {
        RD(n);
        RD(m);
        cnt = 0;
        for(int i=0; i<n; i++) {
            RD(le[i]);
            RD(ri[i]);
            x[cnt++] = le[i];
            x[cnt++] = ri[i];
        }
        for(int i=0; i<m; i++) {
            RD(qes[i]);
            x[cnt++] = qes[i];
        }
        sort(x,x+cnt);
        size = unique(x,x+cnt) - x;
        build(1,size,1);
        for(int i=0; i<n; i++) {
            int ll = lower_bound(x,x+size,le[i]) - x + 1;
            int rr = lower_bound(x,x+size,ri[i]) - x + 1;
            update(ll,rr,1,1);
        }
        printf("Case #%d:\n",ca++);
        for(int i=0; i<m; i++) {
            int q = lower_bound(x,x+size,qes[i]) - x + 1;
            ans = 0;
            query(q,1);
            OT(ans);
            puts("");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值