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 ...
随机推荐
- datetime与timestamp相互转换
select unix_timestamp('2019-12-05 12:26:35'); );
- [JZOJ4763] 【NOIP2016提高A组模拟9.7】旷野大计算
题目 题目大意 给你一个数列,有很多个询问,询问一段区间内,某个数乘它的出现次数的最大值,也就是带权众数. 思考历程 第一次看到这道题,立马想到了树套树之类的二位数据结构,发现不行.(就算可以也很难打 ...
- angularjs中使用swiper时不起作用,最后出现空白位
controller.js中定义swipers指令: var moduleCtrl = angular.module('newscontroller',['infinite-scroll','ngTo ...
- vue的无缝滚动插件vue-seamless-scroll的使用
https://chenxuan0000.github.io/component-document/index_prod.html#/component/seamless-others 在vue环境下 ...
- C++ 字符串、string、char *、char[]、const char*的转换和区别
1.字符串 字符串本质就是一串字符,在C++中大家想到字符串往往第一反应是std::string(后面简称string) 字符串得从C语言说起,string其实是个类,C语言是没有class的,所以C ...
- 滚动字幕标记<marquee></marquee>
<marquee>滚动内容</marquee> 常用属性: Direction : 滚动方向 取值 up, down left right width :滚动宽度 heigh ...
- 数论专场 Day9 部分题解
// 2019年西电暑期集训 // [7月9日]基础数论:https://cn.vjudge.net/contest/309097 A - Visible Lattice Points 题目大意: 平 ...
- pandas一些基本操作(DataFram和Series)_1
##生成一个一维数组import numpy as np;nb7 = np.arange(0,100,2);print(nb7)print("======================== ...
- php冒泡算法
1.冒泡算法 网上搜了很多,但是总是对于每次循环的边界值思路讲的比较笼统. 不是很容易被新手记住,我自己平时也是硬记下来的. 但是对于算法,硬记,时间长了还是容易忘记,所以自己写了一次,把每次思路尽量 ...
- 【codeforces 508D】Tanya and Password
[题目链接]:http://codeforces.com/problemset/problem/508/D [题意] 给你一个字符的所有连续3个的子串; 让你复原出原串; (包含小写.大写字母以及数字 ...