码迷,mamicode.com
首页 > 其他好文 > 详细

acdream 1157Segments cdq分治

时间:2016-07-25 00:01:50      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

题目链接

 

#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;
}

 

acdream 1157Segments cdq分治

标签:

原文地址:http://www.cnblogs.com/yohaha/p/5701902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!