(线段树模板)A Simple Problem with Integers --POJ--3468
链接:
http://poj.org/problem?id=3468
代码:
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
using namespace std; #define Lson r<<1
#define Rson r<<1|1 const int N = 1e5+; struct SegmentTree
{
int L, R;
long long sum, e;
int Mid()
{
return (L+R)>>;
}
int len()
{
return R-L+;
}
}a[N<<]; void BuildSegTree(int r, int L, int R)
{
a[r].L=L, a[r].R=R;
a[r].e=; if(L==R)
{
scanf("%lld", &a[r].sum);
return ;
} BuildSegTree(Lson, L, a[r].Mid());
BuildSegTree(Rson, a[r].Mid()+, R); a[r].sum = a[Lson].sum + a[Rson].sum;
}
void PushDown(int r)
{
a[Lson].sum += a[r].e*a[Lson].len();
a[Lson].e += a[r].e;
a[Rson].sum += a[r].e*a[Rson].len();
a[Rson].e += a[r].e; a[r].e=;
}
void Update(int r, int L, int R, int e)
{
a[r].sum += (R-L+)*e; if(a[r].L==L && a[r].R==R)
{
a[r].e += e;
return ;
} PushDown(r); if(R<=a[r].Mid())
Update(Lson, L, R, e);
else if(L>a[r].Mid())
Update(Rson, L, R, e);
else
{
Update(Lson, L, a[r].Mid(), e);
Update(Rson, a[r].Mid()+, R, e);
}
}
long long Query(int r, int L, int R)
{
if(a[r].L==L && a[r].R==R)
return a[r].sum; PushDown(r); if(R<=a[r].Mid())
return Query(Lson, L, R);
else if(L > a[r].Mid())
return Query(Rson, L, R);
else
{
long long Lsum = Query(Lson, L, a[r].Mid());
long long Rsum = Query(Rson, a[r].Mid()+, R); return Lsum+Rsum;
}
} int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
BuildSegTree(, , n); while(m--)
{
char s[];
int L, R, e; scanf("%s", s); if(s[]=='Q')
{
scanf("%d%d", &L, &R);
printf("%lld\n", Query(, L, R));
}
else
{
scanf("%d%d%d", &L, &R, &e);
Update(, L, R, e);
}
}
}
return ;
}
(线段树模板)A Simple Problem with Integers --POJ--3468的更多相关文章
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)
A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...
- A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 69589 ...
- 【POJ3468】【zkw线段树】A Simple Problem with Integers
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)
参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...
- A Simple Problem with Integers POJ - 3468 (线段树)
思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...
- A Simple Problem with Integers~POJ - 3468
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- A Simple Problem with Integers POJ - 3468 (分块)
题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...
- 【算法系列学习】线段树 区间修改,区间求和 [kuangbin带你飞]专题七 线段树 C - A Simple Problem with Integers
https://vjudge.net/contest/66989#problem/C #include<iostream> #include<cstdio> #include& ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
随机推荐
- 漫画描述HDFS工作原理
作者不详,内容浅显易懂,特拿来与大家分享.
- r.js打包
久闻r.js的大名,但实际没有用它做过任何东西.今天用它时,发现网上许多教程都不对.研究一下,把我的实际经验分享给大家. 例子1 先是HTML页面 <!DOCTYPE html> < ...
- springboot+cxf 开发webservice
参考 https://www.cnblogs.com/fuxin41/p/6289162.html pom.xml <?xml version="1.0" encoding= ...
- SpringCloud 简单理解
0.SpringCloud,微服务架构.包括 服务发现(Eureka),断路器(Hystrix),服务网关(Zuul),客户端负载均衡(Ribbon).服务跟踪(Sleuth).消息总线(Bus).消 ...
- string类小结
要想使用标准C++中string类,必须要包含 #include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 ...
- Python3 bytes 函数
Python3 bytes 函数 Python3 内置函数 描述 bytes 函数返回一个新的 bytes 对象,该对象是一个 0 <= x < 256 区间内的整数不可变序列.它是 b ...
- 7.Reverse Integer (INT; Overflow)
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路:要注意溢出 ...
- ios 进入后台 一段时间在进入前台 动画消失
http://www.cnblogs.com/YouXianMing/p/3670846.html
- IBM MQ + WebSphere + Spring JMS配置方法
IBM MQ + WebSphere + Spring JMS配置方法 首先要在WAS里面配置IBM MQ作为JMS消息的提供者,在WAS管理控制台: Resources->JMS Provi ...
- Spring依赖注入servlet会话监听器
Spring提供了一个 “ContextLoaderListener” 监听器,以使 Spring 依赖注入到会话监听器. 在本教程中,通过添加一个 Spring 依赖注入一个bean 到会话监听器修 ...