1. 程式人生 > >20140719 「樹狀陣列 - 區間更新,區間求和」 POJ 3468 A Simple Problem with Integers

20140719 「樹狀陣列 - 區間更新,區間求和」 POJ 3468 A Simple Problem with Integers

Language: A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 58536 Accepted: 17827
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+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.

Source

All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please 

Contact Administrator


#include <stdio.h>
#include <string.h>
#define MAXN 101000
#define lowbit(x) ((x)&(-x))
int n, m;
long long a[MAXN], b[MAXN], c[MAXN];

void add(long long s[], int i, int x){
    for( ; i<=n; i+=lowbit(i))  s[i] += x;
}

long long sum(long long s[], int i){
    long long ret = 0;
    for( ; i>0; i-=lowbit(i) )
        ret += s[i];
    return ret;
}

long long sum_ans(int i){
    return (i+1)*sum(b, i) - sum(c, i);
}

int main(){           
    long long L, R, x;
    while( ~scanf("%d%d", &n, &m) ){
        a[0] = 0;
        for(int i=1; i<=n; i++){
            scanf("%lld", &a[i]);
            a[i] += a[i-1];
        }
        char s[10];
        while( m-- ){
            scanf("%s", s);
            if( 'Q'==s[0] ){
                scanf("%lld%lld", &L, &R);
                long long ans = a[R]-a[L-1] +sum_ans(R)-sum_ans(L-1);
                printf("%lld\n", ans );
            }
            else {
                scanf("%lld%lld%lld", &L, &R, &x);
                add(b, L, x);                   add(b, R+1, -x);
                add(c, L, x*L);                 add(c, R+1, -x*(R+1));
            }
        }
    }
    return 0;
}


相關推薦

陣列的單點更新區間查詢。

基本的陣列陣列概念,樹狀陣列利用其特殊的位置可以用二進位制達到log級別的更新,如下圖 核心程式碼 int sum(int i){ int s = 0; while(i > 0){ s += dat[i];

20140719 陣列 - 區間更新區間求和 POJ 3468 A Simple Problem with Integers

Language: A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58536 Accepted: 17827 Case Time

20140719 線段 - 區間更新區間求和 POJ 3468 A Simple Problem with Integers

Language: A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58536 Accepted: 17827 Case Time

poj 3468 A Simple Problem with Integers區間修改+區間查詢(陣列)】

參考下面部落格的公式: 需要注意的是,輸入初始數列的時候要 add 兩次, L = R, add (L, x), add (R+1, -x) #include <iostream>

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 t

poj3468 A Simple Problem with Integers數組做法)

align style cee arch puppet ret 定義 pre tab 題目傳送門 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K

poj3511--A Simple Problem with Integers(線段求和

poj pac style som can com onos roman miss A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K

線段專題 POJ3468 A Simple Problem with Integers

strong print style update else algo linker clas uil 題意:n個點。m個操作。兩種操作類型。C X Y K 表示區間[x,y]上每一個點值加k。Q X Y 求區間[x,y]的和 分析:線段樹區間求和,裸模板 註意

poj 3468 A Simple Problem with Integers(原來是一道簡單的線段區間修改用來練練splay)

long 兩個 可能 style push ios stream 區間 pan 題目鏈接:http://poj.org/problem?id=3468 題解:splay功能比線段樹強大當然代價就是有些操作比線段樹慢,這題用splay實現的比線段樹慢上一倍。線段樹用l

POJ-3468 A Simple Problem with Integers(線段、段變化+段查詢、模板)

sum .org miss numbers ... bsp wid scanf accepted A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Su

POJ 3468 A Simple Problem with Integers 線段成段更新

sca query string int open print ring cnblogs pac 線段樹功能   update:成段更新 query:區間求和 #include <iostream> #include <string>

POJ 3468 A Simple Problem with Integers(線段 單點更新+區間求和

names || log shu 更新 can pro struct sim 題目鏈接:http://poj.org/problem?id=3468 題意:單點更新,區間求和。 題解:裸題 1 //POJ 3468 A Simple Problem with

bzoj3212: Pku3468 A Simple Problem with Integers(線段

while printf scan font geo main post align sim 3212: Pku3468 A Simple Problem with Integers 題目:傳送門 題解:    感謝Rose_max大佬的傾情相

POJ 3468 A Simple Problem with Integers(線段模板之區間增減更新 區間求和查詢)

return string ali accept numbers other map nts contain A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 13107

線段-----C - 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 numb

BZOJ3212 Pku3468 A Simple Problem with Integers(線段區間求和區間加模板)

題目大意: 你有N個整數,A1,A2,.,An。你需要處理兩種操作。一種操作是在給定的區間內向每個數字加上一個給定的數字。另一種是求給定區間內的數字之和。   題解: 線段樹的基本操作。 lazy標記。 附上程式碼: #include<cstdio> #includ

POJ 3468 A Simple Problem with Integers(線段

truct fin clas define class 基本 open urn display 題目鏈接:A Simple Problem with Integers 題意:N個數字,M次操作;操作分為兩種,第一種將$[L,R]$區間內的每個數都加上C,第二種為求$[L,

POJ-3468 A Simple Problem with Integers 分塊 線段區間更新

POJ-3468 A Simple Problem with Integers 題意: 給定一個數字序列, 有兩張操作: 1. 查詢[L, R] 的所有數字之和。2. 給定區間[L, R] 的所有數加C. 分析: 很明顯的線段樹區間更新問題, 但是在這裡要引入一種新的演算法---

POJ3468 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 a

A Simple Problem with Integers 【線段區間更新 區間查詢】

POJ   3468 題意就不說了,之間看程式碼; 程式碼是對的,如果大佬覺得註釋有問題可以說,會仔細更改? #include<stdio.h> #include<string.h> #include<string> #include