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 ...
随机推荐
- 使用Squid部署代理服务
Squid是Linux系统中最为流行的一款高性能代理服务软件,通常用作Web网站的前置缓存服务,能够代替用户向网站服务器请求页面数据并进行缓存.简单来说,Squid服务程序会按照收到的用户请求向网站源 ...
- 全屏使用swiper.js过程中遇到的坑
概述 swiper.js确实是一个很好用的插件,下面记录下我在全屏使用过程中遇到的一些坑和解决办法,供以后开发时参考,相信对其他人也有用. 通用方案 一般来说,swiper需要放在body的下一层,虽 ...
- Docker三剑客之Docker Compose
一.什么是Docker Compose Compose 项目是Docker官方的开源项目,负责实现Docker容器集群的快速编排,开源代码在https://github.com/docker/comp ...
- Vue2.5开发去哪儿网App 搜索功能完成
效果展示: Search.vue: <div class="search-content" ref="search" v-show="keywo ...
- Xamarin 绑定安卓第三方库恢复原始参数问题
大家都知道在绑定xamarin android 第三方库的时候 参数名是乱码的 变成了p1 p2 p3 之类的 这样在实际使用的时候非常不方便. 其实xamarin是提供了三种方式帮助大家恢复ja ...
- (转)Linux top命令的用法详细详解
原文:https://yq.aliyun.com/articles/399004?spm=a2c4e.11153940.blogcont399002.9.3a19f00aHOA3SH# 摘要: 首先介 ...
- C# 自定义类型通过实现IFormattable接口,来输出指定的格式和语言文化的字符串(例:DateTime)
常规的调用ToString()方法,存在两个问题. (1).调用者无法控制字符串的格式 (2).调用者不能方便的选择一种特定的语言文化来格式化字符串. 在开发一些国际化的应用时,应用程序需要调用与当前 ...
- php javascript comet
简单描述: comet是用ajax实现的服务器推送,有两种实现comet的方式,长轮询和流,这里只实现长轮询. 长轮询的过程:页面发起一个服务器请求,然后服务器一直保持连接打开,直到有数据返回.返回数 ...
- ICANN认证注册商小全 英、德、法
转载 在ICANN认证注册商小全(一)和ICANN认证注册商小全(二)中,QQPCC介绍了美洲的各ICANN认证注册商.今天我们移师欧洲,介绍欧洲的ICANN认证注册商,欧洲的注册商也很多,不可能在一 ...
- 31-hadoop-hbase-mapreduce操作hbase
有一些大的文件,需要存入HBase中,其思想是先把文件传到HDFS上,利用map阶段读取<key,value>对,可在reduce把这些键值对上传到HBase中. HbaseMapper: ...