"Hdoj" 1540 tunnel Warfare

Source: Internet
Author: User

Not bad. A segment tree interval is merged. Very clever usage.

1 /*1540*/2#include <iostream>3#include <string>4#include <map>5#include <queue>6#include <Set>7#include <stack>8#include <vector>9#include <deque>Ten#include <algorithm> One#include <cstdio> A#include <cmath> -#include <ctime> -#include <cstring> the#include <climits> -#include <cctype> -#include <cassert> -#include <functional> +#include <iterator> -#include <iomanip> + using namespacestd; A //#pragma COMMENT (linker, "/stack:102400000,1024000") at  - #defineSTI set<int> - #defineStpii Set<pair<int, int> > - #defineMpii map<int,int> - #defineVI vector<int> - #definePII pair<int,int> in #defineVpii vector<pair<int,int> > - #defineRep (I, A, n) for (int i=a;i<n;++i) to #definePer (i, a, n) for (int i=n-1;i>=a;--i) + #defineCLR Clear - #definePB Push_back the #defineMP Make_pair * #defineFIR First $ #defineSEC SecondPanax Notoginseng #defineAll (x) (x). Begin (), (x). End () - #defineSZ (x) ((int) (x). Size ()) the #defineLson L, Mid, rt<<1 + #defineRson mid+1, R, rt<<1|1 A  the Const intMAXN =50005; + intls[maxn<<2], rs[maxn<<2], ms[maxn<<2]; - intS[MAXN], top; $  $ voidBuild (intLintRintRT) { -LS[RT] = Rs[rt] = Ms[rt] = r-l +1; -     if(L = =R) the         return ; -     Wuyi     intMid = (L + r) >>1; the Build (Lson); - Build (Rson); Wu } -  About voidPushup (intLintRintRT) { $     intMid = (L + r) >>1; -     intLB = rt<<1; -     intRB = lb |1; -      ALS[RT] =ls[lb]; +RS[RT] =RS[RB]; the     if(ls[lb] = = mid-l+1) -LS[RT] + =LS[RB]; $     if(RS[RB] = = Rmid) theRS[RT] + =rs[lb]; the      theMS[RT] =Max (ms[lb], MS[RB]); theMS[RT] = max (Ms[rt], rs[lb]+LS[RB]); - } in  the voidUpdateintXintDeltaintLintRintRT) { the     if(L = =r) { AboutLS[RT] = Rs[rt] = Ms[rt] =Delta; the         return ; the     } the      +     intMid = (L + r) >>1; -      the     if(x <=mid) {Bayi Update (x, delta, Lson); the}Else { the Update (x, delta, Rson); -     } -      the pushup (l, R, RT); the } the  the intQuery (intXintLintRintRT) { -     if(L==r | | ms[rt]==0|| ms[rt]==r-l+1) the         returnMs[rt]; the      the     intMid = (L + r) >>1;94     intLB = rt<<1; the     intRB = lb |1; the      the     if(x <=mid) {98         if(x >= mid-rs[lb]+1) { About             returnQuery (x, Lson) + query (mid+1, Rson); -}Else {101             returnQuery (x, Lson);102         }103}Else {104         if(x <= mid+LS[RB]) { the             returnQuery (Mid, Lson) +Query (x, Rson);106}Else {107             returnQuery (x, Rson);108         }109     } the }111  the intMain () {113Ios::sync_with_stdio (false); the #ifndef Online_judge theFreopen ("data.in","R", stdin); theFreopen ("Data.out","W", stdout);117     #endif118     119     intN, m, X; -     Charcmd[5];121     intans;122     123      while(SCANF ("%d%d", &n, &m)! =EOF) {124Build (1N1); thetop =0;126          while(m--) {127scanf"%s", cmd); -             if(cmd[0] =='D') {129scanf"%d", &x); thes[top++] =x;131Update (x,0,1N1); the}Else if(cmd[0] =='Q') {133scanf"%d", &x);134Ans = Query (x,1N1);135printf"%d\n", ans);136}Else {137                 if(Top >0) {138x = s[--top];139Update (x,1,1N1); $                 }141             }142         }143     }144     145 #ifndef Online_judge146printf"Time =%d.\n", (int) clock ());147     #endif148     149     return 0; Max}

"Hdoj" 1540 tunnel Warfare

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.