POJ3468--A Simple Problem with Integers--线段树/树状数组 改段求段
题目描述
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
The sums may exceed the range of 32-bit integers.
解题思路
不同于改段求点或改点求段,这是一个改段求段的题目
有两种方法接题:
第一种为利用树状数组:
区间更新这里引进了一个数组delta数组,delta[i]表示区间 [i, n] 的共同增量,每次你需要更新的时候只需要更新delta数组就行了,因为每段区间更新的数都记录在这个数组
中,那怎么查询前缀和呐?
sum[i]=a[1]+a[2]+a[3]+......+a[i]+delta[1](i-0)+delta[2](i-1)+delta[3](i-2)+......+delta[i](i-i+1);
= sigma( a[x] ) + sigma( delta[x] * (i + 1 - x) )
= sigma( a[x] ) + (i + 1) * sigma( delta[x] ) - sigma( delta[x] * x )
(引用自吕程博客)
具体代码为
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int M=100010;
long long add_sum[M];
long long add[M];
long long ori[M];
int MN;
int Q;
int lowbit(int x)
{
return x&(-x);
}
void update(int i,int v,long long* a)
{
for(;i<=MN;i+=lowbit(i)){
a[i]+=v;
}
}
long long getsum(int r,long long *a)
{
long long resr=0;
for(;r>0;r-=lowbit(r)){
resr+=a[r];
}
return resr;
}
int main()
{
//freopen("data.in","r",stdin);
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int tem;
int l,r,val;
while(cin>>MN>>Q){
memset(ori,0,sizeof(ori));
memset(add_sum,0,sizeof(add_sum));
memset(add,0,sizeof(add));
for(int i=1;i<=MN;i++){
cin>>tem;
update(i,tem,ori);
}
string ch;
for(int i=0;i<Q;i++){
cin>>ch;
if(ch=="Q"){
cin>>l>>r;
cout<<getsum(r,ori)-getsum(l-1,ori)-l*getsum(l-1,add)+(r+1)*getsum(r,add)-getsum(r,add_sum)+getsum(l-1,add_sum)<<endl;
}
if(ch=="C"){
cin>>l>>r>>val;
update(l,val,add);
update(r+1,-val,add);
update(l,val*(l),add_sum);
update(r+1,-val*(r+1),add_sum);
}
}
}
}
注意:add_sum数组保存的是add[i]*i之后的值
第二种方法为利用线段树
可以写一个利用结构体实现的线段树,然后加懒标。我地AC代码是按照刘汝佳的板写的,我觉得他地方法比较简洁,更好玩
下面是代码(详解见算法竞赛入门经典-训练指南):
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
const int M = 4*100010;
long long sumv[M];//线段树sum数组
long long add[M];
long long x, y;//全局变量,update与query时使用
long long addval;
long long _sum;
//*********************//
void update(int id, int l, int r)
{
long long m = l + (r - l) / 2;
if (x <= l&&y >= r){
add[id] += addval;
}
else{
if (x <= m) update(2 * id, l, m);
if (y>m) update(2 * id + 1, m+1, r);
}
sumv[id]=0;//!!!!!!!!!注意此步骤
if (l<r){
sumv[id] = sumv[2 * id] + sumv[2 * id + 1];
}
sumv[id] += add[id] * (r - l + 1);
}
void query(int id, int l, int r, long long addv)
{
long long m = l + (r - l) / 2;
if (x <= l&&y >= r){
_sum += sumv[id] + addv*(r - l + 1);
}
else{
//addv += add[id];
if (x <= m){
query(2 * id, l, m, addv+add[id]);
}
if (y>m) query(2 * id + 1, m+1, r, addv+add[id]);
}
}
int main()
{
//freopen("data.in", "r", stdin);
//freopen("data.out","w",stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(0);
long long n, q;
while (cin >> n >> q){
memset(sumv, 0, sizeof(sumv));
memset(add, 0, sizeof(add));
for (int i = 1; i <= n; i++){
cin >> addval;
x = i;
y = i;
update(1, 1, n);
}
string ch;
for (int i = 0; i<q; i++){
cin >> ch;
if (ch == "Q"){
_sum = 0;
cin >> x >> y;
query(1, 1, n, 0);
cout << _sum << endl;
}
if (ch == "C"){
cin >> x >> y >> addval;
update(1, 1, n);
}
}
}
}
POJ3468--A Simple Problem with Integers--线段树/树状数组 改段求段的更多相关文章
- 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 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
随机推荐
- CevaEclipse - 编译器attribute扩展
1.函数与变量的 Section Attribute void foobar (void) __attribute__ ((section (".CSECT mmm"))); vo ...
- OC之消息调用过程
Bird * aBird = [[Bird alloc] init]; [aBird fly]; 中对 fly 的调用,编译器通过插入一些代码,将之转换为对方法具体实现 IMP 的调用,这个 IMP ...
- jvm工具的使用
打开Java VisualVm->工具->插件 如图: 关于要填写的URL地址到 http://visualvm.java.net/pluginscenters.html 找到jdk版本 ...
- 实验六 多线程编程 1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。
//继承Thread类 package zuoye; //继承Thread类 public class City extends Thread{ private String name; public ...
- Value '0000-00-00' can not be represented as java.sql.Date
Value '0000-00-00' can not be represented as java.sql.Date 时间 2014-07-30 09:00:50 ITeye-博客 原文 http: ...
- Python字符串连接方式
python中有很多字符串连接方式,总结一下: 1 最原始的字符串连接方式:str1 + str22 python 新字符串连接语法:str1, str23 奇怪的字符串方式:str1 str24 % ...
- Objective-C 2.0属性(Property)介绍
通常在声明一些成员变量时会看到如下声明方式: @property (参数1,参数2) 类型 名字: 这里我们主要分析在括号中放入的参数,主要有以下三种: setter/getter方法(assign/ ...
- 四、oracle 用户管理(Profile)
oracle 用户管理 :profile + tablespace + role + user 一.使用profile管理用户口令概述:profile是口令限制,资源限制的命令集合,当建立数据库时, ...
- 转:JMeter基础--逻辑控制器Logic Controller
1.ForEach控制器 ForEach控制器在用户自定义变量中读取一系列相关的变量.该控制器下的采样器或控制器都会被执行一次或多次,每次读取不同的变量值.所以ForEach总是和User Defin ...
- HDU 5777 domino
贪心一下.有k次机会,也就是那些数字中,最大的k-1可以不选择.答案为:sum{a[i]}-sum{最大的k-1个a[i]}+n.注意:k>=n的时候直接输出n. #pragma comment ...