题目描述

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--线段树/树状数组 改段求段的更多相关文章

  1. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  2. POJ3468 A Simple Problem with Integers(线段树延时标记)

    题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...

  3. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  4. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  5. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  6. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  7. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  8. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...

  9. 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 ...

  10. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

随机推荐

  1. nefu 1191 平行宇宙 (bfs)

    Description 小k是时空贸易者,他经常在两个平行宇宙之间往来经商,现在他要从S点到达E点,问最少需要多长时间.(已知小k在同一个宇宙中只能向上下左右四个方向移动,每次移动需要1个单位时间,且 ...

  2. 2015 Multi-University Training Contest 4

    1001 Olympiad 签到题1. # include <iostream> # include <cstdio> using namespace std; ]={}; b ...

  3. 移动端touch事件影响click事件的相关解决方法

    preventDefault()的方法,阻止事件的默认行为. 在移动端,手指点击一个元素,会经过:touchstart --> touchmove -> touchend -->cl ...

  4. 多线程synchronized用例解析

    当用synchronized来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码.即使在执行过程中,CPU切换到别的线程了,因为有锁的缘故,其他线程也不会进来执行代码,而 ...

  5. robot framework -重点记录

    1.s2l打开的driver和webdriver打开的driver不是同一个driver. rf打开的浏览器.调用的是s2l的driver,而自己的库用的是webdriver的driver,这样在rf ...

  6. VC MFC工具栏(CToolBar)控件

    一.工具栏 工具栏控件在控件面板里没有对应的选项(图标),但有一个工具栏控件类CToolBar,所以我们如果要创建一个工具栏控件并显示在窗口里的话,只能用代码来完成,事实上任何一种控件,都可以用代码创 ...

  7. 翻译-你必须知道的28个HTML5特征、窍门和技术

    摘自by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpress/?p=1058 前端的发 ...

  8. js键盘键值大全

    原文地址:http://blog.csdn.net/avenccssddnn/article/details/7950524 js键盘键值 keycode 8 = BackSpace BackSpac ...

  9. json 数组 对象 xml 之间转换(待补充)

    json 数组  xml 对象   之间转换(待补充) 1 把对象的类型或者数组转换成字符串类型(或者更确切的说是json类型的). 此处参考链接http://www.jb51.net/article ...

  10. Codeforces Round #364 (Div. 2) D. As Fast As Possible

     D. As Fast As Possible time limit per test 1 second memory limit per test 256 megabytes input stand ...