Hdoj topic 3954 Level Up (segment tree to meet update interval query)

Source: Internet
Author: User

Level UpTime limit:10000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3890 Accepted Submission (s): 1086


Problem descriptionlevel up are the task of all online games. It ' s very boooooooooring. There is a level up in the those games and except level up.
In a online game, there is N heroes numbered ID from 1 to N, each begins with level 1 and 0 Experience. They need to kill monsters to get Exp and level up.

There is many waves of monsters, each wave, the heroes with ID from Li to Ri would come to kill monsters and those hero WI Th level K'll get Ei*k Exp. If one hero ' s Exp reach needk then the hero level up to level K immediately.
After some waves, I'll query the maximum Exp from Li to Ri.
Now giving the information of all waves and needk, please tell me the answer of my query.
Inputthe first line was a number T (1<=t<=30), represents the number of case. The next T blocks follow each indicates a case.
The first line of all case contains three integers N (1<=n<=10000), K (2<=k<=10) and QW (1<=qw<=10000) each Represent hero number, the MAX level and querys/waves number.
Then a line with K-1 integers, Need2, Need3 ... Needk. (1 <= Need2 < Need3 < ... < NEEDK <= 10000).
Then QW lines follow, each line start with ' W ' contains three integers li ri ei (1<=li<=ri<=n, 1<=ei<=1000 0); Each line start with ' Q ' contains the integers li Ri (1<=li<=ri<=n).
Outputfor each case, output the number of case in first line. (as shown in the sample output)
For each query, the output of the maximum Exp from Li to Ri.
Output a black line after each case.
Sample Input
3 2W 1 1 1W 1 2 1Q 1 3W 1 3 1Q 1 5 5 5 3 9W 4 1 5W 1 2 2W 3 2Q 3 1 5W 3 8Q 1 3

Sample Output
Case 1:36case 2:91825Hintcase 1:at First, the information of each hero are 0 (1), 0 (1), 0 (1) [Exp (level)]after first Wave, 1 (2), 0 (1), 0 (1); After second wave, 3 (3), 1 (2), 0 (1); After third Wave, 6 (3), 3 (3), 1 (2); Case 2:the information of each hero Finally:18 (5) 18 (5) 25 (5) 5 (2) 9 (2)

Authornotonlysuccess
Source2011 Alibaba Programming Contest
Recommendlcy | We have carefully selected several similar problems for you:3340 3397 2871 1542 1828

That's a good question. The main idea is that there are N heroes, m grade, q operation, just start each hero is level 1, experience 0, the bottom m-1 an integer, indicating the experience required to ascend each level, the bottom with Q operation, W a b C, the interval "A, a" hero to play the blame, the experience is the hero of the class *c,q a B, The greatest experience of the heroes in the query interval "A, B"

AC Code


#include <stdio.h> #include <string.h> #define INF 0x3f3f3f3f#define Max (A, B) (a>b?a:b) #define MIN (A, B) ( a>b?b:a) struct S{__int64 level,maxnum,flag,need;void init () {level=1;maxnum=0;flag=0;} void Fun (__int64 val) {maxnum+=level*val;need-=val;flag+=val;}} Node[10005<<2];__int64 need[15];void pushdown (int tr) {if (Node[tr].flag) {Node[tr<<1].fun (Node[tr].flag ); Node[tr<<1|1].fun (node[tr].flag); node[tr].flag=0;}} void pushup (int tr) {Node[tr].maxnum=max (node[tr<<1].maxnum,node[tr<<1|1].maxnum); Node[tr].level=max ( Node[tr<<1].level,node[tr<<1|1].level); Node[tr].need=min (node[tr<<1].need,node[tr<<1|1 ].need);} void build (int l,int r,int tr) {node[tr].init (); node[tr].need=need[2];if (l==r) return;int mid= (l+r) >>1;build (L, MID,TR&LT;&LT;1); build (mid+1,r,tr<<1|1);} void update (int l,int r,int l,int r,int tr,int val) {if (l<=l&&r<=r) {if (val>=node[tr].need) {if (l==r) {_ _int64 &now=node[tr].level;node[tr].maxnum+=now*Val;while (node[tr].maxnum>=need[now+1]) Now++;__int64 Temp=need[now+1]-node[tr].maxnum;node[tr].need=temp/now + (temp%now!=0);} Else{pushdown (tr); int mid= (L+R) >>1;if (l<=mid) Update (L,r,l,mid,tr<<1,val), if (r>mid) update (L,R, Mid+1,r,tr<<1|1,val);p ushup (tr);}} Else{node[tr].fun (val);} return;} Pushdown (tr); int mid= (L+R) >>1;if (l<=mid) Update (L,r,l,mid,tr<<1,val), if (r>mid) update (l,r,mid+1 , R,tr<<1|1,val);p ushup (tr);} __int64 query (int l,int r,int l,int r,int tr) {if (l<=l&&r<=r) {return node[tr].maxnum;} Pushdown (tr); int mid= (L+R) >>1;__int64 temp1=0,temp2=0;if (l<=mid) temp1=query (l,r,l,mid,tr<<1); if (r >mid) temp2=query (l,r,mid+1,r,tr<<1|1);p ushup (TR); return Max (TEMP1,TEMP2);} int main () {int t,c=0;scanf ("%d", &t), while (t--) {int. n,m,q,i,j;scanf ("%d%d%d", &n,&m,&q); for (i=2;i <=m;i++) scanf ("%i64d", &need[i]) need[m+1]=inf;build (1,n,1);p rintf ("Case%d:\n", ++c); while (q--) {char op[5] ; scanf ("%s", op); if (op[0]== ' W') {int a,b,c;scanf ("%d%d%d", &a,&b,&c); update (A,B,1,N,1,C);} Else{int a,b;scanf ("%d%d", &a,&b);p rintf ("%i64d\n", Query (a,b,1,n,1));}} printf ("\ n");}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj topic 3954 Level Up (segment tree to meet update interval query)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.