zoukankan      html  css  js  c++  java
  • [Codeforces 297E]Mystic Carvings

    Description

    题库链接

    题面链接

    Solution

    这里给出主席树的版本。主席树维护直线的一个端点在前 (i) 个端点中,另一个端点在区间内的个数。

    Code

    //It is made by Awson on 2018.3.21
    #include <bits/stdc++.h>
    #define LL long long
    #define dob complex<double>
    #define Abs(a) ((a) < 0 ? (-(a)) : (a))
    #define Max(a, b) ((a) > (b) ? (a) : (b))
    #define Min(a, b) ((a) < (b) ? (a) : (b))
    #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
    #define writeln(x) (write(x), putchar('
    '))
    #define lowbit(x) ((x)&(-(x)))
    using namespace std;
    const int N = 2e5;
    void read(int &x) {
        char ch; bool flag = 0;
        for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
        for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
        x *= 1-2*flag;
    }
    void print(LL x) {if (x > 9) print(x/10); putchar(x%10+48); }
    void write(LL x) {if (x < 0) putchar('-'); print(Abs(x)); }
    
    int n, a[N+5], b[N+5], to[N+5], len;
    struct Segment_tree {
        int root[N+5], ch[N*40+5][2], key[N*40+5], pos;
        int cpynode(int o) {++pos; ch[pos][0] = ch[o][0], ch[pos][1] = ch[o][1], key[pos] = key[o]; return pos; }
        void insert(int &o, int l, int r, int loc) {
        o = cpynode(o); ++key[o];
        if (l == r) return; int mid = (l+r)>>1;
        if (loc <= mid) insert(ch[o][0], l, mid, loc);
        else insert(ch[o][1], mid+1, r, loc);
        }
        int query(int o, int l, int r, int a, int b) {
        if (!o || (a <= l && r <= b)) return key[o]; int mid = (l+r)>>1, c1 = 0, c2 = 0;
        if (a <= mid) c1 = query(ch[o][0], l, mid, a, b);
        if (b > mid) c2 = query(ch[o][1], mid+1, r, a, b);
        return c1+c2;
        }
        int query(int a, int b, int l, int r) {
        if (r < l || a > b) return 0;
        return query(root[b], 1, len, l, r)-query(root[a-1], 1, len, l, r);
        }
    }T;
    
    void work() {
        read(n);
        for (int i = 1; i <= n; i++) {
        read(a[i]), read(b[i]); to[a[i]] = b[i], to[b[i]] = a[i];
        if (a[i] > b[i]) Swap(a[i], b[i]);
        }
        len = (n<<1);
        for (int i = 1; i <= len; i++) T.root[i] = T.root[i-1], T.insert(T.root[i], 1, len, to[i]);
        LL ans = 1ll*n*(n-1)*(n-2)/2/3, t = 0;
        for (int i = 1; i <= n; i++) {
        int x = T.query(a[i]+1, b[i]-1, 1, a[i]-1)+T.query(a[i]+1, b[i]-1, b[i]+1, len);
        t += 1ll*x*(n-1-x);
        }
        ans -= t/2;
        for (int i = 1; i <= n; i++) {
        int x = T.query(a[i]+1, b[i]-1, a[i]+1, b[i]-1)/2;
        int y = (T.query(1, a[i]-1, 1, a[i]-1)+T.query(1, a[i]-1, b[i]+1, len)+T.query(b[i]+1, len, 1, a[i]-1)+T.query(b[i]+1, len, b[i]+1, len))/2;
        ans -= 1ll*x*y;
        }
        writeln(ans);
    }
    int main() {work(); return 0; }
  • 相关阅读:
    java操作生成jar包 和写入jar包
    jboss配置jndi连接池
    windows 域的LDAP查询相关举例
    LDAP error Code 及解决方法
    HDU 6417
    CF1299D Around the World
    codechef Chef and The Colored Grid
    Educational Codeforces Round 82 (Rated for Div. 2)
    CF1237F Balanced Domino Placements
    CF1254E Send Tree to Charlie
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/8619065.html
Copyright © 2011-2022 走看看