zoukankan      html  css  js  c++  java
  • 线段树 hdu3255 Farming

    做了这么多扫描线的题,,基本都是一个思路。

    改来改去,,无非就是维护的节点的内容以及push_up越写越复杂了而已


    首先将价格排序处理一下编号,变成编号越大的powerfol越大

    然后后面加入扫描线的时候将旧编号直接转换成新编号即可了


    对于这题

    S[rt][i]维护的是。rt节点相应的区间中品种为i的长度

    S[rt][i]维护的是。rt节点相应的区间的品种为i出现的次数

    那么,假设出现了cnt[rt][3],就将S[rt][3]赋值为区间长度,S[rt][1]和S[rt][2]都赋值为0,由于已经有3覆盖了整个区间,那么另外两种已经不须要考虑了,或者说成,已经被3遮挡了

    假设出现了cnt[rt][2],首先S[rt][3]应该继承左右的子树,S[rt][2]应该是整个的区间长度减去S[rt][3]的长度,由于那一部分的种类是3,2是覆盖不了3的。须要保留下来,然后让S[rt][1]变成0,由于2可以覆盖3,假设出现cnt[rt][1],S[rt][3]和S[rt][2]都继承左右子树的,S[rt][1]的长度应该是区间长度减去S[rt][2]和S[rt][3]。

    假设没有出现cnt,就看是否是l==r的情况。这样的情况下就没有子树了。。就所有赋值为0直接返回,否则。就所有继承左右子树的


    以后遇到这样的题。每次仅仅须要考虑push_up就好了,,仅仅是有时候确实维护的方法非常难想到..

    #include<map>
    #include<set>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<cstdio>
    #include<string>
    #include<vector>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<functional>
    
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PII;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define root 1,rear,1
    
    int const MX = 1e5 + 5;
    
    int m, ID[10];
    int rear, cnt[MX << 2][4];
    int A[MX << 1], S[MX << 2][4];
    
    struct Que {
        int d, s;
        int top, L, R;
        Que() {}
        Que(int _top, int _L, int _R, int _d, int _s) {
            top = _top; L = _L; R = _R; d = _d; s = _s;
        }
        bool operator<(const Que &b)const {
            if(top == b.top) return d < b.d;
            return top < b.top;
        }
    } Q[MX << 1];
    
    struct Price {
        int id, money;
        bool operator<(const Price &b)const {
            return money < b.money;
        }
    } price[10];
    
    int BS(int x) {
        int L = 1, R = rear, m;
        while(L <= R) {
            m = (L + R) >> 1;
            if(A[m] == x) return m;
            if(A[m] > x) R = m - 1;
            else L = m + 1;
        }
        return -1;
    }
    
    void push_up(int l, int r, int rt) {
        if(cnt[rt][3]) {
            S[rt][1] = S[rt][2] = 0;
            S[rt][3] = A[r + 1] - A[l];
        } else if(cnt[rt][2]) {
            S[rt][3] = S[rt << 1][3] + S[rt << 1 | 1][3];
            S[rt][2] = A[r + 1] - A[l] - S[rt][3];
            S[rt][1] = 0;
        } else if(cnt[rt][1]) {
            S[rt][3] = S[rt << 1][3] + S[rt << 1 | 1][3];
            S[rt][2] = S[rt << 1][2] + S[rt << 1 | 1][2];
            S[rt][1] = A[r + 1] - A[l] - S[rt][3] - S[rt][2];
        } else if(l == r) S[rt][1] = S[rt][2] = S[rt][3] = 0;
        else {
            S[rt][1] = S[rt << 1][1] + S[rt << 1 | 1][1];
            S[rt][2] = S[rt << 1][2] + S[rt << 1 | 1][2];
            S[rt][3] = S[rt << 1][3] + S[rt << 1 | 1][3];
        }
    }
    
    void update(int L, int R, int d, int s, int l, int r, int rt) {
        if(L <= l && r <= R) {
            cnt[rt][s] += d;
            push_up(l, r, rt);
            return;
        }
    
        int m = (l + r) >> 1;
        if(L <= m) update(L, R, d, s, lson);
        if(R > m) update(L, R, d, s, rson);
        push_up(l, r, rt);
    }
    
    int main() {
        //freopen("input.txt", "r", stdin);
        int T, n, ansk = 0;
        scanf("%d", &T);
        while(T--) {
            rear = 0;
            memset(cnt, 0, sizeof(cnt));
            memset(S, 0, sizeof(S));
    
            scanf("%d%d", &n, &m);
            for(int i = 1; i <= m; i++) {
                scanf("%d", &price[i].money);
                price[i].id = i;
            }
            sort(price + 1, price + m + 1);
            for(int i = 1; i <= m; i++) {
                ID[price[i].id] = i;
            }
    
            for(int i = 1; i <= n; i++) {
                int x1, y1, x2, y2, s;
                scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &s);
                Q[i] = Que(y1, x1, x2, 1, s);
                Q[i + n] = Que(y2, x1, x2, -1, s);
    
                A[++rear] = x1; A[++rear] = x2;
            }
            sort(Q + 1, Q + 1 + 2 * n);
            sort(A + 1, A + 1 + rear);
            rear = unique(A + 1, A + 1 + rear) - A - 1;
    
            LL ans = 0; int last = 0;
            for(int i = 1; i <= 2 * n; i++) {
                int sum = 0;
                for(int j = 1; j <= m; j++) {
                    sum += price[j].money * S[1][j];
                }
                ans += (LL)(Q[i].top - last) * sum;
                update(BS(Q[i].L), BS(Q[i].R) - 1, Q[i].d, ID[Q[i].s], root);
                last = Q[i].top;
            }
            printf("Case %d: %I64d
    ", ++ansk, ans);
        }
        return 0;
    }
    


  • 相关阅读:
    [GO]使用map生成 json
    [GO]通过结构体生成json
    [GO]正则表达式
    [GO]字符串的使用
    [GO]revoer的应用
    [GO]panic的应用
    微信公众平台自定义菜单及高级接口PHP SDK
    论MySQL何时使用索引,何时不使用索引
    MYSQL explain详解
    Mysql两种存储引擎的优缺点
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6956796.html
Copyright © 2011-2022 走看看