More than 2019 cattle off summer school camp (seventh) - Governing sand

Segment tree

According to highly sort after, the price range for construction segment tree, and the number of total maintenance costs.

Every time a highly enumeration, assuming that the height of the tree has m trees, higher than its cut all the trees obviously, the direct use of the prefix and statistical costs, but also assume lower than his tree has n trees,

We want to cut the number is n - m + 1, n-m + query before the total cost of one tree to tree line in the range.


#include <bits/stdc++.h>
#define INF 2333333333333333333
#define full(a, b) memset(a, b, sizeof a)
#define __fastIn ios::sync_with_stdio(false), cin.tie(0)
#define range(x) (x).begin(), (x).end()
#define pb push_back
using namespace std;
typedef long long LL;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int ret = 0, w = 0; char ch = 0;
    while(!isdigit(ch)){
        w |= ch == '-', ch = getchar();
    }
    while(isdigit(ch)){
        ret = (ret << 3) + (ret << 1) + (ch ^ 48);
        ch = getchar();
    }
    return w ? -ret : ret;
}
template <typename A>
inline A lcm(A a, A b){ return a / __gcd(a, b) * b; }
template <typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 200005;
int n, cnt;
LL sum[N<<2], pre[N], num[N], tree[N<<2], b[N];
struct Tree{
    int h, c, p;
    Tree(){}
    Tree(int h, int c, int p): h(h), c(c), p(p){}
    bool operator < (const Tree &rhs) const {
        return h < rhs.h;
    }
}t[N];

void init(){
    cnt = 0;
    full(b, 0), full(pre, 0), full(num, 0);
    full(tree, 0), full(sum, 0);
}

void buildTree(int rt, int l, int r){
    if(l == r){
        tree[rt] = sum[rt] = 0;
        return;
    }
    int mid = (l + r) >> 1;
    buildTree(rt << 1, l, mid);
    buildTree(rt << 1 | 1, mid + 1, r);
}

void insert(int rt, int l, int r, int val, int k){
    tree[rt] += 1LL * k, sum[rt] += 1LL * val * k;
    if(l == r) return;
    int mid = (l + r) >> 1;
    if(val <= mid) insert(rt << 1, l, mid, val, k);
    else insert(rt << 1 | 1, mid + 1, r, val, k);
}

LL query(int rt, int l, int r, LL k){
    if(l == r) return l * k;
    int mid = (l + r) >> 1;
    if(k <= tree[rt << 1]) return query(rt << 1, l, mid, k);
    return sum[rt << 1] + query(rt << 1 | 1, mid + 1, r,  k - tree[rt << 1]);
}

int main(){

    while(~scanf("%d", &n)){
        init();
        for(int i = 1; i <= n; i ++){
            scanf("%d%d%d", &t[i].h, &t[i].c, &t[i].p);
        }
        sort(t + 1, t + n + 1);
        int j;
        for(int i = 1; i <= n; i = j){
            cnt ++, j = i;
            pre[cnt] += pre[cnt - 1], num[cnt] += num[cnt - 1];
            while(j <= n && t[j].h == t[i].h){
                pre[cnt] += 1LL * t[j].c * t[j].p;
                num[cnt] += t[j].p;
                b[cnt] += t[j].p;
                j ++;
            }
        }
        LL ans = INF;
        int tot = 0;
        for(int i = 1; i <= n; i = j){
            tot ++, j = i;
            LL cur = pre[cnt] - pre[tot];
            LL x = num[tot - 1];
            if(x - b[tot] + 1 > 0)
                cur += query(1, 1, 200, x - b[tot] + 1);
            ans = min(ans, cur);
            while(j <= n && t[j].h == t[i].h) insert(1, 1, 200, t[j].c, t[j].p), j ++;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/onionQAQ/p/11322963.html