zoukankan      html  css  js  c++  java
  • HDU

    HDU - 5372

    我们考虑总的线段减去违法的线段, 因为是按长度从小到大加入数轴的, 所以违法线段就是Rj > Ri的个数加上 Lj < Li 的个数, 

    用树状数组维护就完事了。

    #include<bits/stdc++.h>
    #define LL long long
    #define LD long double
    #define ull unsigned long long
    #define fi first
    #define se second
    #define mk make_pair
    #define PLL pair<LL, LL>
    #define PLI pair<LL, int>
    #define PII pair<int, int>
    #define SZ(x) ((int)x.size())
    #define ALL(x) (x).begin(), (x).end()
    #define fio ios::sync_with_stdio(false); cin.tie(0);
    
    using namespace std;
    
    const int N = 4e5 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 1e9 + 7;
    const double eps = 1e-8;
    const double PI = acos(-1);
    
    template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
    template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
    template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
    template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}
    
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    
    int n;
    int addtot;
    int X[N], Xtot;
    int ans[N];
    int id[N];
    int L[N], R[N];
    int op[N], b[N];
    
    struct Bit {
        int a[N];
        void init() {
            for(int i = 1; i <= Xtot; i++) {
                a[i] = 0;
            }
        }
        inline void modify(int x, int v) {
            for(int i = x; i <= Xtot; i += i & -i) {
                a[i] += v;
            }
        }
        inline int sum(int x) {
            int ans = 0;
            for(int i = x; i; i -= i & -i) {
                ans += a[i];
            }
            return ans;
        }
        inline int query(int L, int R) {
            if(L > R) return 0;
            return sum(R) - sum(L - 1);
        }
    } bit[2];
    
    
    void init() {
        Xtot = addtot = 0;
    }
    
    int main() {
    
        int cas = 0;
    
        while(scanf("%d", &n) != EOF) {
            init();
            int now = 0;
            for(int i = 1; i <= n; i++) {
                scanf("%d%d", &op[i], &b[i]);
                if(!op[i]) {
                    addtot++;
                    ans[addtot] = now;
                    id[i] = addtot;
                    L[addtot] = b[i];
                    R[addtot] = b[i] + addtot;
                    X[++Xtot] = b[i];
                    X[++Xtot] = b[i] + addtot;
                    now++;
                }
                else {
                    now--;
                }
            }
            sort(X + 1, X + 1 + Xtot);
            Xtot = unique(X + 1, X + 1 + Xtot) - X - 1;
            for(int i = 1; i <= addtot; i++) {
                L[i] = lower_bound(X + 1, X + 1 + Xtot, L[i]) - X;
                R[i] = lower_bound(X + 1, X + 1 + Xtot, R[i]) - X;
            }
    
            bit[0].init(); bit[1].init();
    
            for(int i = 1; i <= n; i++) {
                if(op[i] == 0) {
                    ans[id[i]] -= bit[0].query(1, L[id[i]] - 1);
                    ans[id[i]] -= bit[1].query(R[id[i]] + 1, Xtot);
                    bit[0].modify(L[id[i]], 1);
                    bit[1].modify(R[id[i]], 1);
                }
                else {
                    bit[0].modify(L[b[i]], -1);
                    bit[1].modify(R[b[i]], -1);
                }
            }
    
            printf("Case #%d:
    ", ++cas);
            for(int i = 1; i <= addtot; i++) {
                printf("%d
    ", ans[i]);
            }
        }
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    Activity 之使用
    Activity 之生命周期
    Activity 关于生命周期一些问题的实践验证
    深入理解Java虚拟机-第1章-走进Java-读书笔记
    代码整洁之道-第11章-系统-读书笔记
    代码整洁之道-第10章-类-读书笔记
    代码整洁之道-第9章-单元测试-读书笔记
    代码整洁之道-第8章-边界-读书笔记
    代码整洁之道-第7章-错误处理-读书笔记
    华为交换机做端口流量统计
  • 原文地址:https://www.cnblogs.com/CJLHY/p/11194842.html
Copyright © 2011-2022 走看看