HDU 3954 Level Up segment tree

Source: Internet
Author: User

Level Up Time limit:10000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3674 Accepted Submission (s): 994


Problem Description level 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.
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

2 3 3 5 1 2 W 1 1 1 w 1 2 1 Q 1 3 W 1 3 1 Q 1 3 5 5 8 2 5 W 5 9 3 W 4 5 1 W 1 2 2 (3 2) 3 5 1 Q 1 2 Q 3 5
Sample Output
Case 1:3 6 Case 2:9-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)
Author notonlysuccess

Topic:

There are many heroes, experienced, can upgrade, give two operations. W, choose a range of heroes, add experience is e*level (refers to the hero's rank)

The hero's experience will be upgraded to a certain extent, and it is immediately upgraded to that level.

Another operation is Q, which is the most experienced value for the L,r interval.


Analysis:

Set up 10 lesson line tree. Represents the 1-k level, respectively. For inquiry Q, the maximum value of K tree can be output.

Initializes, the 1th segment tree has a value of 0, and the other segment trees are negative infinity. For w operation, the corresponding E*level value is added to each tree's l,r interval. Because if the impact is not on this level, add any value is still negative infinity, do not be afraid.

Then enumerate the K tree to see if there is a hero's experience greater than the limit of this tree, if any, to upgrade the hero to the corresponding level. Then the corresponding node value after the upgrade is set to its experience, the original tree's hero experience is set to negative infinity.




#include <iostream> #include <cstring> #include <algorithm> #include <set> #include <cstdio
> Using namespace std;
#define LL Long long #define MAXN 400007 int LC[MAXN],RC[MAXN];
ll LZ[MAXN],MA[MAXN];
int cnt;
ll inf = -10000000000LL;
    void Init () {lc[0] = rc[0] = 0;
    Lz[0] = ma[0] = inf;
CNT = 1;
    } void Update (int u) {if (Rc[u]! = 0) Lz[rc[u]] + = Lz[u];
    if (lc[u]! = 0) Lz[lc[u]] + = Lz[u];
    Lz[u] = 0;
    Ma[u] = inf;
    if (rc[u]! = 0) Ma[u] = max (Ma[u],ma[rc[u]]+lz[rc[u]);
if (lc[u]! = 0) Ma[u] = max (Ma[u],ma[lc[u]]+lz[lc[u]);
    } void Build (int u,int l,int R,ll v) {Lz[u] = Ma[u] = 0;
        if (L = = r) {Lc[u] = Rc[u] = 0;
        Lz[u] = v;
        Ma[u] = 0;
    return;
    } int mid = (l+r)/2;
    Lc[u] = cnt++;
    Rc[u] = cnt++;
    Build (LC[U],L,MID,V);
    Build (RC[U],MID+1,R,V);
Update (U);
  } void Add (int u,int l,int R, int l, int r,ll v) {if (L = = L && r = = r) {      Lz[u] + = V;
    return;
    } int mid = (l+r)/2;
    If (mid >= R) Add (lc[u],l,mid,l,r,v);
    else if (Mid < L) Add (rc[u],mid+1,r,l,r,v);
    else Add (lc[u],l,mid,l,mid,v), add (RC[U],MID+1,R,MID+1,R,V);
Update (U);
    } ll query (int u,int l,int r,int l,int R) {if (L = = L && r = r) {return Lz[u] + ma[u];
    } update (U);
    ll ans;
    int mid = (l+r)/2;
    If (mid >= R) ans = query (lc[u],l,mid,l,r);
    else if (Mid < L) ans = query (rc[u],mid+1,r,l,r);
    else ans = max (query (lc[u],l,mid,l,mid), query (Rc[u],mid+1,r,mid+1,r));
    Update (U);
return ans; }
//look for a node value equal to V of the junction number int find (int u,int l,int R,ll v) {if (L = = r) {return L;
    } int mid = (l+r)/2;
    Update (U);
    int c1 = Lc[u];
    if (MA[C1] + lz[c1] = = v) return to find (C1,L,MID,V);
return find (RC[U],MID+1,R,V);
        }//Set node p to V-value void sett (int u,int l,int r,int P,ll v) {if (L = = r) {Lz[u] = V;
    return;
    } update (U);
    int mid = (l+r)/2;
    if (P > Mid) Sett (rc[u],mid+1,r,p,v);
    else sett (lc[u],l,mid,p,v);
Update (U);
} int k;
int LEVE[MAXN];
int need[20];
int tree[20];
    int main () {int t,n,q;
    scanf ("%d", &t);
        for (int tt = 1; TT <= t; tt++) {scanf ("%d%d%d", &n,&k,&q);
        for (int i = 1;i < K; i++) scanf ("%d", &need[i]); Init (); 
//initialize for (int i = 1;i <= k; i++) {tree[i] = cnt++;
            if (i = = 1) build (tree[i],1,n,0);
        else build (Tree[i],1,n,inf);
        } Char x[2];
        int l,r,e;
        printf ("Case%d:\n", TT);
            while (q--) {scanf ("%s", x);
                if (x[0] = = ' W ') {scanf ("%d%d%d", &l,&r,&e); for (int i = 1;i <= k; i++) {
Adding add
                    (tree[i],1,n,l,r,i*e);
                }
                for (int i = 1;i < K; i++) {While
                    (query (tree[i],1,n,1,n) >= Need[i]) {//See if the current tree has a hero need to upgrade
                        ll XL = query (Tree[i] , 1,n,1,n);
                        int KK = i;
                        while (Need[kk] <= xl && KK < k) kk++; Locate the upgrade to the first level of
                        int u = Find (TREE[I],1,N,XL);
                        Sett (TREE[KK],1,N,U,XL);   Sets the XP value
                        sett (tree[i],1,n,u,inf) on the new hierarchy tree;  The original level tree experience value is set to negative infinity
                    }}
            }
            else {
                scanf ("%d%d", &l,&r);
                ll ans = 0;
                for (int i =1; I <= K; i++)
                    ans = max (ans,query (tree[i],1,n,l,r));
                printf ("%i64d\n", ans);
            }
            ll tm = 0;
        }
        printf ("\ n");
    }
    return 0;
}










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.