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


無語了,把 main 函式第3行寫成

“ wiile( ~scanf("%d", &T) ) { } ”;

結果 WA 了無數遍;

改成:

“ wiile( EOF != scanf("%d", &T) ) { } ”;

就行了。

可惡 >_< 

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MAXN 100100
typedef long long LL;
LL sum[MAXN<<2], add[MAXN<<2];
int a[MAXN];

void push_up(int rt){
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

void push_down(int rt, int m){
    if( add[rt] ){
        add[rt<<1] += add[rt];
        add[rt<<1|1] += add[rt];
        sum[rt<<1] += (m-(m>>1)) * add[rt];
        sum[rt<<1|1] += (m>>1) * add[rt];
        add[rt] = 0;
    }
}

void build(int rt, int l, int r){
    add[rt] = 0;
    if( l==r ){
        sum[rt] = a[l];
        return ;
    }
    int mid = (l+r)>>1;
    build(rt<<1, l, mid);
    build(rt<<1|1, mid+1, r);
    push_up(rt);
}

void updata(int rt, int l, int r, int x, int y, int p){
    if( x<=l && r<=y ){
        add[rt] += p;
        sum[rt] += (LL)(r-l+1)*p;
        return;
    }
    push_down(rt, r-l+1);
    int mid = (l+r)>>1;
    if( x<=mid )    updata(rt<<1, l, mid, x, y, p);
    if( mid<y )     updata(rt<<1|1, mid+1, r, x, y, p);
    push_up(rt);
}

LL query(int rt, int l, int r, int x, int y){
    if( x<=l && r<=y )
        return sum[rt];
    push_down(rt, r-l+1);
    int mid = (l+r)>>1;
    LL r1=0, r2=0;
    if( x<=mid )    r1 = query(rt<<1, l, mid, x, y);
    if( mid<y )     r2 = query(rt<<1|1, mid+1, r, x, y);
    return r1+r2;
}

int main(){       
    int n, m, x, y;
    LL p;
    while( EOF != scanf("%d%d", &n, &m) ){
        for(int i=1; i<=n; i++)
            scanf("%d", &a[i]);
        build(1,1,n);
        char s[11];
        while( m-- ){
            scanf("%s%d%d", s, &x, &y);
            if( 'Q'==s[0] ) printf("%lld\n", query(1,1,n,x,y));
            else if( 'C'==s[0] ){
                scanf("%d", &p);
                updata(1,1,n,x,y,p);
            }
        }
    }
    return 0;
}


相關推薦

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

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

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線段 單點更新+區間求和

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

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

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

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

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

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

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

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

A Simple Problem with Integers線段區間更新

【題意】 給定長度為 NNN 的序列 AAA,有 QQQ次操作,操作分兩種 CabcC \ a \ b \ cCabc 表示將序列中 A[a,b]A[a,b]A[a,b]所有元素都加上一個數字 ccc QabQ \ a \ bQab 表示查詢序列 A[a,b]

A Simple Problem with Integers(線段區間操作)

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 145755 Accepted: 45323 Case

A Simple Problem with Integers線段應用型別三)【區間增加 區間查詢】【模板基礎題】

題目連結:http://poj.org/problem?id=3468 參考部落格:https://blog.csdn.net/u013480600/article/details/22202711 Description You have N integers,

C - A Simple Problem with Integers POJ - 3468 線段模版(區間查詢區間修改)

線段樹模版 amp else 計算 更新 namespace scanf spa ger 參考qsc大佬的視頻 太強惹 先膜一下 視頻在b站 直接搜線段樹即可 1 #include<cstdio> 2 using namespace std; 3 con

A Simple Problem with Integers線段區間求和+區間更新

A Simple Problem with Integers 連結:傳送門 You have NN integers, A1,A2,⋯,ANA1,A2,⋯,AN. You need to deal with two kinds of operations. O

Poj】-A Simple Problem with Integers線段區間更新

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 100877 Accepted: 31450 Case Time Limit: 2

POJ 3468 A Simple Problem with Integers線段區間更新區間查詢)

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 Accepted: 28818 Case Time Limit: 20

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 gi

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線段、段變化+段查詢、模板)

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