zoukankan      html  css  js  c++  java
  • 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
    ", 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;
    }
  • 相关阅读:
    oo第二次总结
    oo第一次总结
    OO最后一次博客儿
    OO作业总结第三弹
    OO作业总结第二弹
    初学面向对象
    hi🎈
    散列函数及其应用
    结对项目作业
    构建之法第四,第十四章读书有感 (另补第十七章)
  • 原文地址:https://www.cnblogs.com/yohaha/p/5701902.html
Copyright © 2011-2022 走看看