标签:test dir oid hdu printf desc get span ges
#include<bits/stdc++.h> using namespace std; const int maxn = 50010; #define lson l , m , num << 1 #define rson m + 1 , r , num << 1|1 int n , m , k , maxo[maxn << 2] , mino[maxn << 2];char s[2]; void up(int num){maxo[num] = max(maxo[num << 1] , maxo[num << 1|1]); mino[num] = min(mino[num << 1] ,mino[num << 1|1]);} void built(int l , int r , int num){ if(l == r) {maxo[num] = 0 ; mino[num] = n + 1;return;} int m = (l + r)>>1; built(lson);built(rson); up(num); return; } void des(int k , int l , int r , int num){ if(l == r){maxo[num] = k; mino[num] = k;return;} int m = (l + r)>>1; if(k <= m) des(k,lson); if(k > m)des(k,rson);up(num); } void red(int k , int l , int r , int num){ if(l == r){maxo[num] = 0; mino[num] = n + 1;return;} int m = (l + r)>>1; if(k <= m) red(k,lson); if(k > m)red(k,rson);up(num); } int getl(int L , int R , int l , int r , int num){ if(L <= l && r <= R){return maxo[num];} int ans = 0;int m = (l + r) >> 1; if(L <= m) ans = max(ans , getl(L , R , lson)); if(R > m) ans = max(ans , getl(L , R , rson)); return ans; } int getr(int L , int R , int l , int r , int num){ if(L <= l && r <= R){return mino[num];} int ans = n + 1;int m = (l + r) >> 1; if(L <= m) ans = min(ans , getr(L , R , lson)); if(R > m) ans = min(ans , getr(L , R , rson)); return ans; } int main() { while(~scanf("%d%d",&n,&m)){ built(1 , n , 1); stack<int>q; while(m--){ scanf("%s",s); if(s[0] == ‘D‘){ scanf("%d",&k); des(k , 1 , n , 1); q.push(k); } if(s[0] == ‘R‘){ if(q.empty()){ continue; } k = q.top(); q.pop(); red(k , 1 , n , 1); } if(s[0] == ‘Q‘){ scanf("%d",&k); int ll = getl(1 , k , 1 , n , 1); int rr = getr(k , n , 1 , n , 1); int ans = rr - ll - 1; if(ans < 0) ans = 0; printf("%d\n",ans); } } } }
HDU 1540 Tunnel Warfare(线段树,单点更新,区间查询)
标签:test dir oid hdu printf desc get span ges
原文地址:http://www.cnblogs.com/rtyfcvb/p/6536507.html