[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=4392

[算法]

线段树

时间复杂度 : O(MlogN)

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 200010
typedef long long LL; int n , m;
LL a[MAXN]; struct SegmentTree
{
struct Node
{
int l , r;
LL sum , val;
LL tag;
} Tree[MAXN << ];
inline void build(int index , int l , int r)
{
Tree[index].l = l;
Tree[index].r = r;
Tree[index].tag = ;
if (l == r)
{
Tree[index].sum = Tree[index].val = a[l];
return;
}
int mid = (l + r) >> ;
build(index << , l , mid);
build(index << | , mid + , r);
update(index);
}
inline void update(int index)
{
Tree[index].sum = Tree[index << ].sum + Tree[index << | ].sum;
Tree[index].val = min(Tree[index << ].val , Tree[index << | ].val);
}
inline void pushdown(int index)
{
int l = Tree[index].l , r = Tree[index].r;
int mid = (l + r) >> ;
Tree[index << ].sum += Tree[index].tag * (mid - l + );
Tree[index << | ].sum += Tree[index].tag * (r - mid);
Tree[index << ].val += Tree[index].tag;
Tree[index << | ].val += Tree[index].tag;
Tree[index << ].tag += Tree[index].tag;
Tree[index << | ].tag += Tree[index].tag;
Tree[index].tag = ;
}
inline void modify(int index , int l , int r , LL value)
{
if (Tree[index].l == l && Tree[index].r == r)
{
Tree[index].sum += (r - l + ) * value;
Tree[index].val += value;
Tree[index].tag += value;
return;
}
pushdown(index);
int mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) modify(index << , l , r , value);
else if (mid + <= l) modify(index << | , l , r , value);
else
{
modify(index << , l , mid , value);
modify(index << | , mid + , r , value);
}
update(index);
}
inline LL query_sum(int index , int l , int r)
{
if (Tree[index].l == l && Tree[index].r == r) return Tree[index].sum;
pushdown(index);
int mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) return query_sum(index << , l , r);
else if (mid + <= l) return query_sum(index << | , l , r);
else return query_sum(index << , l , mid) + query_sum(index << | , mid + , r);
}
inline LL query_min(int index , int l , int r)
{
if (Tree[index].l == l && Tree[index].r == r) return Tree[index].val;
pushdown(index);
int mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) return query_min(index << , l , r);
else if (mid + <= l) return query_min(index << | , l , r);
else return min(query_min(index << , l , mid) , query_min(index << | , mid + , r));
}
} T; template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} int main()
{ read(n); read(m);
for (int i = ; i <= n; i++) read(a[i]);
T.build( , , n);
while (m--)
{
char op[];
scanf("%s",op);
if (op[] == 'P')
{
int l , r;
LL x;
read(l); read(r); read(x);
T.modify( , l , r , x);
}
if (op[] == 'S')
{
int l , r;
read(l); read(r);
printf("%lld\n" , T.query_sum( , l ,r));
}
if (op[] == 'M')
{
int l , r;
read(l); read(r);
printf("%lld\n", T.query_min( , l , r));
}
} return ;
}

[Usaco2015 DEC] Counting Haybales的更多相关文章

  1. bzoj 4747: [Usaco2016 Dec]Counting Haybales

    23333,在扒了一天题解之后发现我竟然还能秒题,虽然这是个pj的sb题... (排个序,然后upper_bound和lower_bound一用就行了(是不是有O(1)的查询方法啊??貌似要离散啊,一 ...

  2. [Usaco2016 Dec]Counting Haybales

    原题链接https://www.lydsy.com/JudgeOnline/problem.php?id=4747 先将原数组排序,然后二分查找即可.时间复杂度\(O((N+Q)logN)\). #i ...

  3. bzoj 4397: [Usaco2015 dec]Breed Counting -- 前缀和

    4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec  Memory Limit: 128 MB Description Farmer John ...

  4. bzoj4397【Usaco2015 Dec】Breed Counting

    4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 29  Solved: 25 ...

  5. bzoj4397[Usaco2015 dec]Breed Counting*

    bzoj4397[Usaco2015 dec]Breed Counting 题意: 给定一个长度为N的序列,每个位置上的数只可能是1,2,3中的一种.有Q次询问,每次给定两个数a,b,请分别输出区间[ ...

  6. BZOJ 4390: [Usaco2015 dec]Max Flow

    4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 177  Solved: 113[Submi ...

  7. Counting Haybales

    Counting Haybales 题目描述 Farmer John is trying to hire contractors to help rearrange his farm, but so ...

  8. 【BZOJ4391】[Usaco2015 dec]High Card Low Card(贪心)

    [BZOJ4391][Usaco2015 dec]High Card Low Card(贪心) 题面 BZOJ 题解 预处理前缀后缀的结果,中间找个地方合并就好了. #include<iostr ...

  9. [Usaco2015 dec]Max Flow 树上差分

    [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 353  Solved: 236[Submit][Sta ...

随机推荐

  1. WKWebView的了解

    1. http://blog.csdn.net/chenyong05314/article/details/53735215 2. http://www.jianshu.com/p/6ba250744 ...

  2. 从零开始写STL—模板元编程之tuple

    tuple Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generali ...

  3. POJ 1724 【存在附加约束的最短路问题】【优先队列】

    题意:给K个权值.给含有N个点,R条单向边的图. 每条边都有两个权值,其中一个路长,另外一个是附加权值. 要求路的附加权值之和不超过K的情况下求最短路. 思路: 自己的思路太狭隘,这题还是看了大牛的思 ...

  4. Codeforces Round Edu 36

    A.B.C 略 D(dfs+强连通分量) 题意: 给出一个n(n<=500)点m(m<=100000)边的有向图,问能否通过删去一条边使得该图无环. 分析: 最简单的想法就是枚举一条边删去 ...

  5. Nginx配置upstream实现负载均衡及keepalived实现nginx高可用

    (原文链接:http://www.studyshare.cn/blog-front//blog/details/1159/0 ) 一.准备工作 1.准备两个项目,发布到不同的服务器上,此处使用2个虚拟 ...

  6. &quot;What&#39;s New&quot; WebPart in SharePoint

    "What's New" WebPart in SharePoint 项目描写叙述         这是一个自己定义WebPart,能够显示一个列表,这个列表项目是在SharePo ...

  7. Feign简介

    Feign简介

  8. Oracle 数据库管理员的任务

    设计.实施和维护 Oracle 数据库时,按优先次序排列的任务包括:   1. 确定数据库服务器硬件   2. 安装 Oracle 软件   3. 为数据库和安全策略制定计划   4. 创建.移植和打 ...

  9. [Javascript] Link to Other Objects through the JavaScript Prototype Chain

    Objects have the ability to use data and methods that other objects contain, as long as it lives on ...

  10. Hadoop-08-Hive本地独立式安装

    1.安装mysql sudo apt-get install mysql-server mysql-client 2.使用root账户登录mysql数据库,新建存放hive元数据的数据库.如果叫hiv ...