码迷,mamicode.com
首页 > 其他好文 > 详细

P3130 [USACO15DEC]计数haybalesCounting Haybales

时间:2018-09-29 19:16:01      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:pac   计数   flag   日志   mes   ret   main   math   for   

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\n", get_min(1, l, r));
        else if(cmd == 'S')printf("%lld\n", get_sum(1, l, r));
        else{
            LL val = RD();
            update(1, val, l, r);
            }
        }
    return 0;
    }

P3130 [USACO15DEC]计数haybalesCounting Haybales

标签:pac   计数   flag   日志   mes   ret   main   math   for   

原文地址:https://www.cnblogs.com/Tony-Double-Sky/p/9725249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!