POJ3468(KB7-C 线段树)
A Simple Problem with Integers
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
Source
//2017-05-17
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define mid ((st[id].l+st[id].r)>>1)
#define lson (id<<1)
#define rson ((id<<1)|1) using namespace std; const ll N = ;
ll arr[N];
struct Node{
ll l, r, sum, lazy;
}st[N<<]; void build(int id, int l, int r)
{
st[id].l = l; st[id].r = r; st[id].lazy = ;
if(l == r){
st[id].sum = arr[l];
return;
}
build(lson, l, mid);
build(rson, mid+, r);
st[id].sum = st[lson].sum+st[rson].sum;
} void push_down(int id)
{
if(st[id].lazy != ){
st[lson].lazy += st[id].lazy;
st[rson].lazy += st[id].lazy;
st[id].sum += (st[id].r-st[id].l+)*st[id].lazy;
st[id].lazy = ;
}
return;
} ll query(int id, int l, int r)
{
if(st[id].l == l && st[id].r == r)return st[id].sum+(r-l+)*st[id].lazy;
push_down(id);
if(r <= mid)return query(lson, l, r);
else if(l > mid)return query(rson, l, r);
else return query(lson, l, mid)+query(rson, mid+, r);
} void update(int id, int l, int r, int w)
{
if(st[id].l == l && st[id].r == r){
st[id].lazy += w;
return;
}
st[id].sum += (r-l+)*w;
if(r <= mid)update(lson, l, r, w);
else if(l > mid)update(rson, l, r, w);
else{
update(lson, l, mid, w);
update(rson, mid+, r, w);
}
} int main()
{
ll n, q;
while(scanf("%lld%lld", &n, &q)!=EOF){
for(int i = ; i <= n; i++)
scanf("%lld", &arr[i]);
build(, , n);
char op[];
ll a, b, c;
while(q--){
scanf("%s", op);
if(op[] == 'Q'){
scanf("%lld%lld", &a, &b);
printf("%lld\n", query(, a, b));
}else if(op[] == 'C'){
scanf("%lld%lld%lld", &a, &b, &c);
update(, a, b, c);
}
}
} return ;
}
//2018-03-28 import java.util.*; public class Main { public static void main(String[] args) {
Scanner cin = new Scanner(System.in); int n, q;
while(cin.hasNext()) {
n = cin.nextInt();
q = cin.nextInt();
SegmentTree st = new SegmentTree(n);
for(int i = 1; i <= n; i++)
st.arr[i] = cin.nextLong();
st.build(1, 1, n);
char op;
int a, b;
long c;
while(q-- > 0) {
op = cin.next().charAt(0);
if(op == 'Q') {
a = cin.nextInt();
b = cin.nextInt();
System.out.println(st.query(1, a, b));
}else if(op == 'C') {
a = cin.nextInt();
b = cin.nextInt();
c = cin.nextLong();
st.updata(1, a, b, c);
}
}
}
}
} class SegmentTree{
static class Node{
public int l, r;
long sum, lazy;
Node(int _l, int _r, long _sum, long _lazy){
this.l = _l;
this.r = _r;
this.sum = _sum;
this.lazy = _lazy;
}
} public int n;
public long [] arr;
public Node [] nodes; SegmentTree(int _n){
this.n = _n;
arr = new long[n+1];
nodes = new Node[n<<2];
} int lson(int id) {
return (id<<1);
} int rson(int id) {
return ((id<<1)|1);
} int mid(int id) {
return (nodes[id].l + nodes[id].r)>>1;
}
void build(int id, int l, int r) {
nodes[id] = new Node(l, r, 0, 0);
if(l == r) {
nodes[id].sum = arr[l];
return;
}
build(lson(id), l, mid(id));
build(rson(id), mid(id)+1, r);
nodes[id].sum = nodes[lson(id)].sum + nodes[rson(id)].sum;
} void pushDown(int id) {
if(nodes[id].lazy != 0) {
nodes[lson(id)].lazy += nodes[id].lazy;
nodes[rson(id)].lazy += nodes[id].lazy;
nodes[id].sum += (nodes[id].r-nodes[id].l+1)*nodes[id].lazy;
nodes[id].lazy = 0;
}
} long query(int id, int l, int r) {
if(nodes[id].l == l && nodes[id].r == r)
return nodes[id].sum+(r-l+1)*nodes[id].lazy;
pushDown(id);
if(r <= mid(id))return query(lson(id), l, r);
else if(l > mid(id))return query(rson(id), l, r);
else return query(lson(id), l, mid(id))+query(rson(id), mid(id)+1, r);
} void updata(int id, int l, int r, long w) {
if(nodes[id] == null)return;
if(nodes[id].l == l && nodes[id].r == r) {
nodes[id].lazy += w;
return;
}
nodes[id].sum += (r-l+1)*w;
if(r <= mid(id))updata(lson(id), l, r, w);
else if(l > mid(id))updata(rson(id), l, r, w);
else {
updata(lson(id), l, mid(id), w);
updata(rson(id), mid(id)+1, r, w);
}
}
}
POJ3468(KB7-C 线段树)的更多相关文章
- poj3468 线段树的懒惰标记
题目链接:poj3468 题意:给定一段数组,有两种操作,一种是给某段区间加c,另一种是查询一段区间的和 思路:暴力的方法是每次都给这段区间的点加c,查询也遍历一遍区间,复杂度是n*n,肯定过不去,另 ...
- poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- 洛谷P3372/poj3468(线段树lazy_tag)(询问区间和,支持区间修改)
洛谷P3372 //线段树 询问区间和,支持区间修改 #include <cstdio> using namespace std; struct treetype { int l,r; l ...
- POJ3468 线段树(区间更新,区间求和,延迟标记)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 97196 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
- 【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 ...
- poj3468 线段树+lazy标记
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92921 ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
随机推荐
- <转>php中heredoc与nowdoc的使用方法
http://www.361way.com/php-heredoc-nowdoc/3008.html 一.heredoc结构及用法 Heredoc 结构就象是没有使用双引号的双引号字符串,这就是说在 ...
- [vuejs] 终端npm run dev 不能自动打开浏览器运行项目解决办法
终端执行: npm run dev 出现: I Your application is running here: http://localhost:8080 但并没有打开浏览器运行项目 解决办法: ...
- MySQL查询50例
创建表和关系 /* 创建表 */ /*年级表*/ DROP TABLE IF EXISTS `class_grade`; CREATE TABLE `class_grade` ( `gid` int( ...
- [Leetcode]123.买卖股票的最佳时机3
[原题链接][https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/] 分析:动态规划+二分法.以第i天为分界线,计 ...
- iOS UIFileSharingEnabled
一.让iOS App通过iTunes进行文件交换Documents 让iOS App通过iTunes进行文件交换 有一些App需要通过使用iTunes让用户上传和下载文档.要让iOS程序支持iTune ...
- Spring框架的演变
什么是Spring 如果想要解释Spring,那么最难的部分就是对其进行分类.通常情况下,Spring被描述为构建Java应用程序的轻量级框架,但这种描述带来了两个有趣的观点. 首先,与许多其他框架( ...
- Choose GitLab for your next open source project
原文:https://b.agilob.net/choose-gitlab-for-your-next-project/ GitLab.com is a competitor of GIthub. I ...
- 第一个WCF程序
WCF的服务需要寄宿在进程中,我们把服务端的叫做宿主,为服务指定宿主指定的过程叫服务寄宿.有两种方式一种是自我寄宿(Self-Hosting),一种是IIS寄宿方式.Self-Hosting我们通过一 ...
- 删除Myeclipse中废弃的workspace记录
一个不用的workspace被我们删除后,但是在工作空间切换时File --> Switch Workspace,旧的 workspace 选项还会存在,看着很碍眼.删除遗留 workspace ...
- PPT-常用快捷键
Ctrl+Shift+C 复制文本格式 Ctrl+Shift+V 粘贴文本格式 Ctrl+B 应用粗体格式Ctrl+U 应用下划线Ctrl+l 应用斜体格式 全选 Ctrl + A 光标之后 ...