zoukankan      html  css  js  c++  java
  • 码队的新桌游

    码队的新桌游

    树状数组、离散化、二维偏序

    #include <bits/stdc++.h>
    ///即找x.a<y.b<x.c&&y.a<x.b<y.c的组数
    using namespace std;
    
    struct Item {
        int a, b, c;
        int id;
        int ans;
    
        explicit Item(int _a = 0, int _b = 0, int _c = 0) : a(_a), b(_b), c(_c) {}
    };
    
    struct cmpa {
        bool operator()(const Item &lhs, const Item &rhs) const {
            return lhs.a > rhs.a || (lhs.a == rhs.a && lhs.b < rhs.b)
                   || (lhs.a == rhs.a && lhs.b == rhs.b && lhs.c < rhs.c);
        }
    };
    
    struct cmpc {
        bool operator()(const Item &lhs, const Item &rhs) const {
            return lhs.c > rhs.c || (lhs.c == rhs.c && lhs.b < rhs.b)
                   || (lhs.c == rhs.c && lhs.b == rhs.b && lhs.a < rhs.a);
        }
    };
    
    priority_queue<Item, vector<Item>, cmpa> Qin;
    priority_queue<Item, vector<Item>, cmpc> Qout;
    
    const int MAXN = 300100;
    int ca[MAXN];
    int lsh[MAXN], lsc;
    Item za[MAXN];
    unordered_map<int, int> lss;
    
    void addZ(int x, int v) {
        while (x <= lsc) {
            ca[x] += v;
            x += (x & -x);
        }
    }
    
    int getZ(int x) {
        int ans = 0;
        while (x) {
            ans += ca[x];
            x -= (x & -x);
        }
        return ans;
    }
    
    int main() {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i) {
            scanf("%d%d%d", &za[i].a, &za[i].b, &za[i].c);
            Qin.push(za[i]);
            lsh[lsc++] = za[i].a;
            lsh[lsc++] = za[i].b;
            lsh[lsc++] = za[i].c;
            za[i].id = i;
        }
        sort(lsh, lsh + lsc);
        lsc = unique(lsh, lsh + lsc) - lsh;
        for (int i = 0; i < lsc; ++i) {
            lss[lsh[i]] = i + 1;///离散化
        }
        sort(za, za + n, [](const Item &a, const Item &b) { return a.b < b.b; });
        for (int i = 0; i < n; ++i) {
            while (!Qin.empty()) {
                Item xx = Qin.top();
                if (za[i].b > xx.a) {
                    Qin.pop();///a小的先出来
                    addZ(lss[xx.b], 1);///加入树状数组,若z[a].b<=xx.a,则za[i]必输
                    Qout.push(xx);
                } else break;
            }
            while (!Qout.empty()) {
                Item xx = Qout.top();
                if (za[i].b >= xx.c) {
                    Qout.pop();///c小的先出来
                    addZ(lss[xx.b], -1);///减去,因为xx.c<=Z[a].b,则za[i]必赢
                    ///树状数组中元素都满足xx.c>za[i].b
                } else break;
            }
            za[i].ans = getZ(lss[za[i].c]) - getZ(lss[za[i].a]);
        }
    
        sort(za, za + n, [](const Item &a, const Item &b) { return a.id < b.id; });
        for (int i = 0; i < n; ++i)printf("%d
    ", za[i].ans-1);
    
        return 0;
    }
  • 相关阅读:
    Dubbo入门微服务框架学习
    CSS学习笔记:溢出文本省略(text-overflow)
    ThreadLocal
    redis事务及乐观锁
    redis的三个特殊数据结构(geospatial、bitmaps、hyperloglogs)
    js日期时间格式化
    SimpleDateFormat 中的yyyy-MM-dd HH:mm:ss.SSS说明
    js中window.location.search的用法和作用。
    MySQL查询表中某个字段的重复数据
    完全卸载Oracle方法(超详细)
  • 原文地址:https://www.cnblogs.com/liulex/p/11393412.html
Copyright © 2011-2022 走看看