(線段樹 點更新 區間求和)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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/459020.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/459020.shtml
英文地址,請注明出處:http://en.pswp.cn/news/459020.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

TCP/ip通信模式

TCP/IP 應用層與應用程序*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 文檔出處&#xff1a;http://blog.csdn.net/bingxx11/article…

PHP中判斷空的方法,php中類型判斷和NULL,空值檢查的方法

在一些接口和數據庫的設計中。數據庫的非必填字段可能為null或者為空。這個時候接口前端javascript去判斷的時候就會比較麻煩。為了便于統一的判斷。一律把null和 空裝換成 空.這樣前端的判斷就變得簡潔 if(aa ){........}建議使用 或者 來判斷。。以下是我簡短的一個把數據…

8 Regular Expressions You Should Know

2019獨角獸企業重金招聘Python工程師標準>>> Regular expressions are a language of their own. When you learn a new programming language, theyre this little sub-language that makes no sense at first glance. Many times you have to read another tutori…

poj 3278 catch that cow BFS(基礎水)

Catch That CowTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 61826 Accepted: 19329Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a num…

制作已編譯的html幫助文件

http://www.cnblogs.com/cm186man/archive/2008/03/10/1098896.html引用 HTML幫助文檔從結構上來看可分為兩個部分&#xff0c;運行器和文檔內容。它的一個好處是能使幫助文檔跨平臺運行&#xff0c;只要有不同平臺上的運行器和瀏覽器&#xff0c;幫助文檔不再需要重新編制&…

matlab %3c handle,volume browser (updated).htm 源代碼在線查看 - Matlab顯式三維地震數據的源代碼 資源下載 蟲蟲電子下載站...

Comments: any comments on this error:??? Error using > timesIntegers can only be combined with integers of the same class, or scalar doubles.Error in > interp3>linear at 368 F (( arg4(ndx).*(1-t) arg4(ndx1).*t ).*(1-s) ...Error in > inter…

PHPer轉戰Android的學習過程以及Android學習

原文作者&#xff1a; eoeadmin原文地址&#xff1a; http://my.eoe.cn/shuhai/archive/19684.html--------------------------------------------這篇文章主要寫了一個PHP程序猿是如何轉戰學習Android的。 第一步&#xff1a;直接跨過java的學習&#xff0c;原因有我之前看過畢…

SQL中實現截取字符串的函數

SQL中實現截取字符串的函數 如果想實現從數據庫中取數據時截取一個字段下的內容或者截取一串字符串&#xff0c;則能夠實現這種效果的函數有Left&#xff0c;Right&#xff0c;SubString三個函數。1.Left函數&#xff1a;Left&#xff08;character_expression , integer_expre…

php時區設置問題,PHP 的時區設置問題_PHP教程

裝上PHP5后你會發現這樣的問題&#xff1a;你也許會發現&#xff0c;輸出的時間和你現在的時間是不相同的。原因是假如你不在程序或配置文件中設置你的服務器當地時區的話&#xff0c;PHP所取的時間是格林威治標準時間&#xff0c;所以和你當地的時間會有出入。格林威治標準時間…

HDU 5392 BC #51

就是求最大公倍數&#xff0c;但要用分解質因子求。 自己寫的WA到爆。。。。 #include<iostream> #include<stdio.h> #include<math.h>#include<algorithm>using namespace std;#define rd(x) scanf("%d",&x) #define rd2(x,y) scanf(&q…

下拉框——把一個select框中選中內容移到另一個select框中遇到的問題

在使用jQuery實現把一個select框中選中內容移到另一個select框中功能時遇到了一個問題&#xff0c;就是點擊按鈕時內容可以到另一個select框中&#xff0c;但是到了另一個select框中的內容卻很快閃退回原來的select框中&#xff0c;代碼如下&#xff1a; <select class"…

windows API 串口編程參考

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** &#xff08;一&#xff09;Windows API串口通信編程概述 Windows環境下的串口編程與D…

jquery post php返回html,jquery ajax post 提交數據,返回的是當前網頁的html?

代碼如下用戶名:密 碼:$("#login_submit").bind("click",function(){var type "post";var url "index.php?madmin&cindex&acheckLogin";var formArrays $("#admin_form").serializeArray();var requestData …

[轉]Android Studio系列教程六--Gradle多渠道打包

轉自&#xff1a;http://www.stormzhang.com/devtools/2015/01/15/android-studio-tutorial6/ Android Studio系列教程六--Gradle多渠道打包 2015 年 01 月 15 日 devtools本文為個人原創&#xff0c;歡迎轉載&#xff0c;但請務必在明顯位置注明出處&#xff01; 由于國內Andr…

服務器上裝filezilla server后,本地的ftp客戶端連接不上去

公司一臺服務器&#xff0c;上面裝了filezilla server后&#xff0c;按平常配置好了&#xff0c;但是在本地用FTP客戶端不管怎么連接都連接不上&#xff0c;本地FTP客戶端總提示連接失敗&#xff0c;遠程filezilla server的界面也沒有提示有人連接&#xff0c; 仔細看了一下&am…

數據結構與算法之堆與堆排序

在數據結構中&#xff0c;堆其實就是一棵完全二叉樹。我們知道內存中也有一塊叫做堆的存儲區域&#xff0c;但是這與數據結構中的堆是完全不同的概念。在數據結構中&#xff0c;堆分為大根堆和小根堆&#xff0c;大根堆就是根結點的關鍵字大于等于任一個子節點的關鍵字&#xf…

非法操作 login.php,閱文游戲中心 h5游戲接入wiki

閱文游戲中心《h5游戲 CP接口規范》接口要求規范游戲方接口說明&#xff1a;游戲方需按照規范提供&#xff0c;閱文進行調用閱文接口說明&#xff1a;閱文提供&#xff0c;游戲方調用參數 time 為Unix 時間戳(January 1 1970 00:00:00 GMT 起的秒數) &#xff0c;單位為秒編碼統…

串口通信與編程:串口基礎知識

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 串口是串行接口&#xff08;serial port&#xff09;的簡稱&#xff0c;也稱為串行通信…

jmeter上傳文件搞了一天,才搞定,沒高人幫忙效率就是低,趕緊記下來,以備后用...

jmeter上傳文件搞了一天&#xff0c;才搞定&#xff0c;沒高人幫忙效率就是低&#xff0c;趕緊記下來&#xff0c;以備后用 先用谷歌瀏覽器抓包&#xff0c;抓到的包類似這樣&#xff1a; 在jmeter里添加一個http請求&#xff0c;配置好參數&#xff0c;方法&#xff0c;端口&a…

自定義dialog

2019獨角獸企業重金招聘Python工程師標準>>> R.layout.layout_insert_dialog自定義布局 View mViewLayoutInflater.from(MainActivity.this).inflate(R.layout.layout_insert_dialog, null); AlertDialog.Builder dialognew AlertDialog.Builder (MainActivity.this…