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 ...
随机推荐
- C++调用API获取当前时间
#include <string> #include<iostream> #include<windows.h> #include <sstream> ...
- Python2 指定文件编码格式需要注意的地方
python2 中默认的编码格式是unicode, 开发人员经常需要根据需要,将python文件的编码格式设置为utf-8,我们可以在python文件的第一行进行设置,加入如下代码: # encodi ...
- Eclipse 导入本地 Git 项目
File --> Open Projects From File System 选择项目路径 Finish
- 转载:TCP/IP四层模型
转载:TCP/IP四层模型 一. TCP/IP参考模型示意图 ISO制定的OSI参考模型的过于庞大.复杂招致了许多批评.与此对照,由技术人员自己开发的TCP/IP协议栈获得了更为广泛的应用. 如图所示 ...
- SQLYog执行SQL脚本提示:错误代码: 1067 - Invalid default value for '数据库表'查询:解决办法
强烈建议:完全卸载当前版本MySQL,重新安装5.6及以上版本 完全卸载方法:https://jingyan.baidu.com/article/3d69c551611290f0ce02d77b.ht ...
- 微信公众平台主动推送消息(asp.net)
/// <summary> /// MD5 32位加密 /// </summary> /// <param name=" ...
- git提交的问题
1. Pull is not possible because you have unmerged files.症状:pull的时候$ git pull Pull is not possible be ...
- MySQL笔记(3)---文件
1.前言 第二章简单记录了一下InnoDB存储引擎的一个基本内容,介绍了保证高效插入的Insert Buffer,change Buffer和确保数据安全的write ahead log以及doubl ...
- Zookeeper--0300--java操作Zookeeper,临时节点实现分布式锁原理
删除Zookeeper的java客户端有 : 1,Zookeeper官方提供的原生API, 2,zkClient,在原生api上进行扩展的开源java客户端 3, 一.Zookeeper原生API ...
- 面试题之发散思维能力:如何用非常规方法求1+2+···+n
今天在<剑指offer>里看到了下面这样一个简单且有趣的题,考察程序员的发散思维能力,前提是你对C++相关知识点熟悉,否则是想不出来方案的,分享给大家. 题目:求1+2+···+n,要 ...