A Simple Problem with Integers POJ - 3468 (线段树)

思路:线段树,区间更新,区间查找
#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<set>
#include<algorithm>
#include<cstdio>
#include<map>
#include<cstring>
#include<list> #define MAXSIZE 100010 using namespace std; long long tree[*MAXSIZE];
long long lz[*MAXSIZE];
int N, Q; void init()
{
memset(tree, , sizeof(tree));
memset(lz, , sizeof(lz));
} void build(int node, int l, int r)
{
if(l == r)
{
scanf("%lld", &tree[node]);
return;
} int mid = (l+r)/;
build(node*, l, mid);
build(node*+, mid+, r); tree[node] = tree[node*] + tree[node*+];
} void push_down(int node, int l, int r)
{
if(lz[node])
{
int mid = (l+r)/;
lz[node*] += lz[node];
lz[node*+] += lz[node];
tree[node*] += (mid-l+)*lz[node];
tree[node*+] += (r-mid)*lz[node];
lz[node] = ;
}
} void update_range(int node, int l, int r, int L, int R, int add)
{
if(l <= L && r >= R)
{
lz[node] += add;
tree[node] += (R-L+)*add;
return;
}
push_down(node, L, R);
int mid = (L+R)/;
if(mid >= l)
update_range(node*, l, r, L, mid, add);
if(mid < r)
update_range(node*+, l, r, mid+, R, add);
tree[node] = tree[node*] + tree[node*+];
} long long query_range(int node, int l, int r, int L, int R)
{
if(l <= L && r >= R)
{
return tree[node];
}
push_down(node, L, R);
int mid = (L+R)/;
long long sum = ;
if(mid >= l)
sum += query_range(node*, l, r, L, mid);
if(mid < r)
sum += query_range(node*+, l, r, mid+, R); return sum;
} int main()
{
scanf("%d%d", &N, &Q);
init();
build(, , N);
while(Q--)
{
char ch;
scanf(" %c", &ch);
if(ch == 'Q')
{
int a, b;
scanf("%d%d", &a, &b);
printf("%lld\n", query_range(, a, b, , N));
}
else if(ch == 'C')
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
update_range(, a, b, , N, c);
}
} return ;
}
A Simple Problem with Integers POJ - 3468 (线段树)的更多相关文章
- 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 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
- A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 69589 ...
- ACM: A Simple Problem with Integers 解题报告-线段树
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...
- POJ 3468 A Simple Problem with Integers(详细题解) 线段树
这是个线段树题目,做之前必须要有些线段树基础才行不然你是很难理解的. 此题的难点就是在于你加的数要怎么加,加入你一直加到叶子节点的话,复杂度势必会很高的 具体思路 在增加时,如果要加的区间正好覆盖一个 ...
- 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 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...
- C - A Simple Problem with Integers - poj 3468(区间更新)
题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值. 分析:很明显我们不可能对区间的每 ...
- POJ 3468 线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
随机推荐
- leetcode 132 Palindrome Pairs 2
lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...
- swiper 插件里面嵌套可滚动内容
在移动端使用swiper的整屏滚动,如果slide里面有滚动内容的话,滚动的时候会整个页面一起滚动,如果想里面的滚动区域单独滚动的话,可以在初始化swiper的时候添加上 noSwipingClass ...
- 简单科普下hosts文件原理与制作
简单科普下hosts文件原理与制作 hosts文件是一个用于储存计算机网络中各节点信息的计算机文件.这个文件负责将主机名映射到相应的IP地址.hosts文件通常用于补充或取代网络中DNS的功能.和DN ...
- Jdk8 Hashmap ConcurrentHashMap
JDK1.8 Hashmap JDK1.8 ConcurrentHashMap 不采用segment而采用 synchronized (f) f = table[i]; 减小锁的力度 设计了MOVE ...
- 把github上的项目clone到IDEA
点击clone按钮后,会弹出如下截图弹窗,点击 NO 点击open,找到刚刚从github上clone下来的项目,打开即可
- 03_Spring Bean的装配模式_基于Annotation配置方式
前言 在Spring中尽管使用XML配置文件可以实现Bean的装配工作,但如果应用中Bean的数量较多,会导致XML配置文件过于臃肿,从而给维护和升级带来一定的困难.从JDK 5开始提供了名为Anno ...
- 07_jQuery对象初识(五)事件(非常重要)
1. 目前为止学过的绑定事件的方式 1. 在标签里面写 onclick=foo(this); 2. 原生DOM的JS绑定 DOM对象.onclick=function(){...} 3. jQuery ...
- 对话框处理Enter,Esc键相应问题
在类视图里面选择你要实现的类,右键属性,在属性里面找到函数PreTranslateMessage,然后添加PreranslateMessage的消息函数,在PreTranslateMessage的消息 ...
- href 页面跳转页面 参数
$.getUrlParam = function (name) { var reg = new RegExp("(^|&)" + name + "=([^& ...
- Zookeeper的安装与使用: