acdream 1157Segments cdq分治

题目链接

 

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e5+5;
int v[maxn*2], n, b[maxn], cnt, num, sum[maxn*2], ans[maxn];
struct node
{
    int l, r, id, val;
}a[maxn];
bool cmp1(const node& lhs, const node& rhs)
{
    return lhs.id < rhs.id;
}
bool cmp2(const node& lhs, const node& rhs)
{
    if(lhs.r != rhs.r)
        return lhs.r > rhs.r;
    if(lhs.l != rhs.l)
        return lhs.l < rhs.l;
    return lhs.id < rhs.id;
}
int lowbit(int x) {return x&(-x);}
void update(int x, int val)
{
    while(x <= cnt) {
        sum[x] += val;
        x += lowbit(x);
    }
}
int query(int x)
{
    int ret = 0;
    while(x) {
        ret += sum[x];
        x -= lowbit(x);
    }
    return ret;
}
void cdq(int l, int r)
{
    if(l == r) return ;
    int m = l+r>>1;
    cdq(l, m);
    cdq(m+1, r);
    sort(a+l, a+1+r, cmp2);
    for(int i = l; i <= r; i++) {
        if(a[i].id <= m && a[i].val)
            update(a[i].l, a[i].val);
        if(a[i].id > m && !a[i].val)
            ans[a[i].id] += query(a[i].l);
    }
    for(int i = l; i <= r; i++)
        if(a[i].id <= m && a[i].val)
            update(a[i].l, -a[i].val);

}
void solve()
{
    mem(ans);
    mem(sum);
    sort(v, v+cnt);
    cnt = unique(v, v+cnt)-v;
    for(int i = 1; i <= n; i++) {
        a[i].l = lower_bound(v, v+cnt, a[i].l)-v+1;
        a[i].r = lower_bound(v, v+cnt, a[i].r)-v+1;
    }
    cdq(1, n);
    sort(a+1, a+1+n, cmp1);
    for(int i = 1; i <= n; i++) {
        if(!a[i].val)
            printf("%d\n", ans[i]);
    }
}
int main()
{
    int x;
    while(~scanf("%d", &n)) {
        cnt = num = 0;
        for(int i = 1; i <= n; i++) {
            char ch[1];
            scanf("%s", ch);
            a[i].id = i;
            if(ch[0] == 'C') {
                scanf("%d", &x);
                a[i].l = a[b[x]].l;
                a[i].r = a[b[x]].r;
                a[i].val = -1;
            } else {
                scanf("%d%d", &a[i].l, &a[i].r);
                v[cnt++] = a[i].l;
                v[cnt++] = a[i].r;
                if(ch[0] == 'D') {
                    b[++num] = i;
                    a[i].val = 1;
                } else {
                    a[i].val = 0;
                }
            }
        }
        solve();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/yohaha/p/5701902.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值