P3130 [USACO15DEC]计数haybalesCounting Haybales

Posted tony-double-sky

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3130 [USACO15DEC]计数haybalesCounting Haybales相关的知识,希望对你有一定的参考价值。

P3130 [USACO15DEC]计数haybalesCounting Haybales

1)给定一段连续的田地,给每一个田地都增加一些新的草包。

2)给定一段连续的田地,找出草包最少的田地有多少草包。

3)给定一段连续的田地,统计一共有多少草包。


错误日志: (pushdown) 时记录懒标记用了 (int) , 下次(在没有把握的情况下)应全部替换


Solution

线段树, 懒标记, 靠前练手

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
#define LL long long
using namespace std;
LL RD(){
    LL out = 0,flag = 1;char c = getchar();
    while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
    while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
    return flag * out;
    }
const LL maxn = 400019;
LL num, na;
LL v[maxn];
#define lid (id << 1)
#define rid (id << 1) | 1
struct seg_tree{
    LL l, r;
    LL min, sum, lazy;
    }tree[maxn << 2];
void pushup(LL id){
    tree[id].sum = tree[lid].sum + tree[rid].sum;
    tree[id].min = min(tree[lid].min, tree[rid].min);
    }
void build(LL id, LL l, LL r){
    tree[id].l = l, tree[id].r = r;
    if(l == r){
        tree[id].min = tree[id].sum = v[l];
        return ;
        }
    LL mid = (l + r) >> 1;
    build(lid, l, mid), build(rid, mid + 1, r);
    pushup(id);
    }
void pushdown(LL id){
    if(tree[id].lazy != 0){
        LL val = tree[id].lazy;
        tree[lid].sum += val * (tree[lid].r - tree[lid].l + 1);
        tree[rid].sum += val * (tree[rid].r - tree[rid].l + 1);
        tree[lid].min += val;
        tree[rid].min += val;
        tree[lid].lazy += val;
        tree[rid].lazy += val;
        tree[id].lazy = 0;
        }
    }
void update(LL id, LL val, LL l, LL r){
    pushdown(id);
    if(tree[id].l == l && tree[id].r == r){
        tree[id].lazy += val;
        tree[id].sum += val * (tree[id].r - tree[id].l + 1);
        tree[id].min += val;
        return ;
        }
    LL mid = (tree[id].l + tree[id].r) >> 1;
    if(mid < l)update(rid, val, l, r);
    else if(mid >= r)update(lid, val, l, r);
    else update(lid, val, l, mid), update(rid, val, mid + 1, r);
    pushup(id);
    }
LL get_sum(LL id, LL l, LL r){
    pushdown(id);
    if(tree[id].l == l && tree[id].r == r){
        return tree[id].sum;
        }
    LL mid = (tree[id].l + tree[id].r) >> 1;
    if(mid < l)return get_sum(rid, l, r);
    else if(mid >= r)return get_sum(lid, l, r);
    else return get_sum(lid, l, mid) + get_sum(rid, mid + 1, r);
    }
LL get_min(LL id, LL l, LL r){
    pushdown(id);
    if(tree[id].l == l && tree[id].r == r){
        return tree[id].min;
        }
    LL mid = (tree[id].l + tree[id].r) >> 1;
    if(mid < l)return get_min(rid, l, r);
    else if(mid >= r)return get_min(lid, l, r);
    else return min(get_min(lid, l, mid), get_min(rid, mid + 1, r));
    }
int main(){
    num = RD(), na = RD();
    for(LL i = 1;i <= num;i++)v[i] = RD();
    build(1, 1, num);
    char cmd;
    while(na--){
        cin>>cmd;
        LL l = RD(), r = RD();
        if(cmd == 'M')printf("%lld
", get_min(1, l, r));
        else if(cmd == 'S')printf("%lld
", get_sum(1, l, r));
        else{
            LL val = RD();
            update(1, val, l, r);
            }
        }
    return 0;
    }

以上是关于P3130 [USACO15DEC]计数haybalesCounting Haybales的主要内容,如果未能解决你的问题,请参考以下文章

洛谷 P3128 [USACO15DEC]最大流Max Flow

洛谷 P3128 [USACO15DEC]最大流Max Flow

[USACO15DEC]最大流Max Flow

题解[USACO19DEC]Tree Depth

[Luogu 3128] USACO15DEC Max Flow

P3128 [USACO15DEC]最大流Max Flow