日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(线段树 点更新 区间求和)lightoj1112

發布時間:2023/12/18 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (线段树 点更新 区间求和)lightoj1112 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#problem/D (密碼0817)

?

Description

Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick. He keeps?n?sacks where he keeps this money. The sacks are numbered from?0?to?n-1.

Now each time?he can he can do one of the three tasks.

1)??????????????????Give all the money of the?ith?sack to the poor, leaving the sack empty.

2)??????????????????Add new amount (given in input) in the?ith?sack.

3)??????????????????Find the total amount of money from?ith?sack to?jth?sack.

Since he is not a?programmer, he seeks your help.

Input

Input starts with an integer?T (≤ 5), denoting the number of test cases.

Each case contains two integers?n (1 ≤ n ≤ 105)?and?q (1 ≤ q ≤ 50000). The next line contains?n?space separated integers in the range?[0, 1000]. The?ith?integer denotes the initial amount of money in the?ith?sack?(0 ≤ i < n).

Each of the next?q?lines contains a task in one of the following form:

1 i????????Give all the money of the?ith(0 ≤ i < n)?sack to the poor.

2 i v?????Add money?v (1 ≤ v ≤ 1000)?to the?ith(0 ≤ i < n)?sack.

3 i j??????Find the total amount of money from?ith?sack to?jth?sack?(0?≤ i ≤ j < n).

Output

For each test case, print the case number first. If the query type is?1, then print the amount of money given to the poor. If the query type is?3, print the total amount from?ith?to?jth?sack.

Sample Input

1

5 6

3 2 1 4 5

1 4

2 3 4

3 0 3

1 2

3 0 4

1 1

Sample Output

Case 1:

5

14

1

13

2

?

?

?

代碼:

#include <cstdio> #include <cstring> #include <algorithm> using namespace std;#define Lson (r<<1) #define Rson (r<<1|1) #define Mid e[r].mid()const int N = 100005;struct node {int L, R, sum;int mid(){return (L+R)/2;} } e[N<<2];int a[N], sum;void BuildTree(int r, int L, int R) {e[r].L = L , e[r].R = R;if(L==R){e[r].sum = a[L];return ;}BuildTree(Lson, L, Mid);BuildTree(Rson, Mid+1, R);e[r].sum = e[Lson].sum + e[Rson].sum; }void Oper(int r, int i, int w) {if(e[r].L == e[r].R){e[r].sum = w;return ;}if(i<=Mid)Oper(Lson, i, w);elseOper(Rson, i, w);e[r].sum = e[Lson].sum + e[Rson].sum; }int Query(int r, int L, int R) {if(e[r].L==L && e[r].R==R)return e[r].sum;if(R<=Mid)return Query(Lson, L, R);else if(L>Mid)return Query(Rson, L, R);else{int LL = Query(Lson, L, Mid);int RR = Query(Rson, Mid+1, R);return LL + RR;}}int main() {int t, n, m, iCase=1;scanf("%d", &t);while(t--){int i, L, R, w, x;scanf("%d%d", &n, &m);for(i=1; i<=n; i++)scanf("%d", &a[i]);BuildTree(1, 1, n);printf("Case %d:\n", iCase++);while(m--){scanf("%d", &x);if(x==3){scanf("%d%d", &L, &R);sum = 0;L++, R++;printf("%d\n", Query(1, L, R));}else{scanf("%d", &i);i++;if(x==1){printf("%d\n", a[i]);a[i] = 0;}else{scanf("%d", &w);a[i] += w;}Oper(1, i, a[i]); ///由于都是點的操作,可以直接操做后在去操作樹,感覺和直接操作樹是一樣的,不過對于///這題來說,這種似乎更好些, 因為有加和清零的,對點的操作是不一樣的, 然而區間查詢就很簡單了,不說了}}}return 0; }

?

?

轉載于:https://www.cnblogs.com/YY56/p/4738471.html

總結

以上是生活随笔為你收集整理的(线段树 点更新 区间求和)lightoj1112的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。