hdu--3954 (segment tree + special handling)

Source: Internet
Author: User

D-1004Time limit:MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u SubmitStatusPracticeHDU 3954

Description

Level's 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.

Input

The 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).

Output

For 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

Sample Output

Hint

Case 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)          
HH out of the question, good classic line tree. Turn to the explanation of the great God, good classics.

Test instructions is very simple, into a paragraph update, into a section of the query, but the update is not the same as the general line of the tree, each point although received the same information, but because of their own different, the final value will be different. The general delay operation is uncertain.

Breaking point in K, the range is very small, only 10, you can consider each time someone upgrade, the recursive search, the person to upgrade the operation. Since finding someone requires only logn complexity, each person is up to K times, so the complexity of the n person is O (nklogn)

Two auxiliary arrays ADD[MAXN] and Max[maxk][maxn],add are used to record the delay tag, max[k] represents the maximum XP value for the interval Class K. Initialize Add,max[1] to 0 and the other to 1, indicating no one at this level. When Max[k] When the value is greater than or equal to NEEDK, this interval is upgraded, and the segment tree operation recursively upgrades the range of people that can be upgraded.

A single operation may be Nlogn (everyone upgrades), but on average there is only nklogn.

Code:
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include < algorithm> #include <string> #include <cmath> #include <queue> #include <vector> #include <map> #include <set> #define INF 0x3f3f3f3f#define Mem (A, B) memset (A,b,sizeof (a)) using namespace Std;const int maxd=10000+5; #define Lson l,m,rt<<1#define Rson m+1,r,rt<<1 | 1typedef long ll;typedef pair<int,int> pii;//---------------------------int add[maxd<<2],_max[15][ Maxd<<2];int need[15]= {0};int n,k,op;void pushup (int rt) {for (int i=1; i<=k; ++i) _max[i][rt]=max (_max I [rt<<1],_max[i][rt<<1|1]);}        void pushdown (int rt) {if (Add[rt]) {add[rt<<1]+=add[rt];        add[rt<<1|1]+=add[rt]; for (int i=1; i<=k; ++i) {if (_max[i][rt<<1]!=-1)///_MAX[I][RT&LT;&LT;1]+=ADD[RT            ]*i; if (_max[i][rt<<1|1]!=-1) _MAX[I][RT&Lt;<1|1]+=add[rt]*i;    } add[rt]=0;    }}void Build (int l,int R,int RT) {add[rt]=0;        if (l==r) {_max[1][rt]=0;        for (int i=2; i<=k; ++i) _max[i][rt]=-1;    Return    } int m= (L+R) >>1;    Build (Lson);    Build (Rson); Pushup (RT);} void LevelUp (int ki,int l,int R,int rt) {if (l==r) {while (ki<k) {if (_max[ki][rt]>=ne                Ed[ki]) {_MAX[KI+1][RT]=_MAX[KI][RT];                _max[ki][rt]=-1;            ++ki;        } else break;    } return;    } pushdown (RT);    int m= (L+R) >>1;    if (_max[ki][rt<<1] >=need[ki]) LevelUp (Ki,lson);    if (_max[ki][rt<<1|1]>=need[ki]) LevelUp (Ki,rson); Pushup (RT);}        void update (int l,int r,int v,int l,int r,int RT) {if (l<=l && r<=r) {add[rt]+=v;  for (int i=k; i>=1;-I.) {if (_max[i][rt]!=-1) _max[i][rt]+=v*i;          if (i<k && _max[i][rt]>=need[i]) LevelUp (I,L,R,RT);    } return;    } pushdown (RT);    int m= (L+R) >>1;    if (l<=m) update (L,r,v,lson);    if (m<r) update (L,r,v,rson); Pushup (RT);}            int query (int l,int r,int l,int r,int RT) {if (l<=l && r<=r) {for (int i=k; i>=1;-I.)    if (_max[i][rt]!=-1) return _MAX[I][RT];    } pushdown (RT);    int m= (L+R) >>1;    int ret=0;    if (l<=m) Ret=max (Ret,query (L,r,lson));    if (m<r) Ret=max (Ret,query (L,r,rson)); return ret;}    int main () {int kase;    Freopen ("1.txt", "R", stdin);    scanf ("%d", &kase);        for (int. Cas=1; cas<=kase; ++cas) {printf ("Case%d:\n", CAs);         scanf ("%d%d%d", &n,&k,&op);        Build (1,n,1);        for (int i=1; i<k; ++i) scanf ("%d", &need[i]);            while (op--) {char ch[2];            int l,r,val; scanf ("%s%d%d", Ch,&l,&AMP;R);            cout<<ch<<l<<r<<endl;                if (ch[0]== ' W ') {scanf ("%d", &val);                cout<<l<<r<<val<<n<<k<<op<<endl;            Update (l,r,val,1,n,1);            } else {printf ("%d\n", Query (l,r,1,n,1));    }} printf ("\ n"); } return 0;}


hdu--3954 (segment tree + special handling)

Related Article

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.