首页 > 代码库 > Hdu1540Tunnel Warfare线段树
Hdu1540Tunnel Warfare线段树
#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>#include<math.h>using namespace std;#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1const int maxn = 55555;int sum[maxn << 2];int lsum[maxn << 2];int rsum[maxn << 2];void up(int rt, int m){ lsum[rt] = lsum[rt << 1]; rsum[rt] = rsum[rt << 1 | 1]; if (lsum[rt] == (m - (m >> 1))) lsum[rt] += lsum[rt << 1 | 1]; if (rsum[rt] == (m >> 1)) rsum[rt] += rsum[rt << 1]; sum[rt] = max(max(sum[rt << 1], sum[rt << 1 | 1]), rsum[rt << 1] + lsum[rt << 1 | 1]);}void build(int l, int r, int rt){ if (l == r){ sum[rt] = lsum[rt] = rsum[rt] = 1; return; } int mid = (l + r) >> 1; build(lson); build(rson); up(rt, r - l + 1);}void update(int key, int add, int l, int r, int rt){ if (l == r){ if (add) sum[rt] = lsum[rt] = rsum[rt] = 0; if (!add) sum[rt] = lsum[rt] = rsum[rt] = 1; return; } int mid = (l + r) >> 1; if (key <= mid) update(key, add, lson); else update(key, add, rson); up(rt, r - l + 1);}int ask(int key, int l, int r, int rt){ if (l == r){ if (sum[rt] == 1) return 1; return 0; } int mid = (l + r) >> 1; int a = l + lsum[rt] - 1; int b = r - rsum[rt] + 1; int ans = 0; if (key >= l&&key <= a) ans = lsum[rt]; if (key >= b&&key <= r) ans = max(ans, rsum[rt]); int c = mid - rsum[rt << 1] + 1; int d = lsum[rt << 1 | 1] + mid; if (key >= c&&key <= d) ans = max(ans, rsum[rt << 1] + lsum[rt << 1 | 1]); if (ans) return ans; if (key <= mid) return ask(key, lson); else return ask(key, rson);}int q[maxn<<1];int main(){ int n, m; int a; char str[100]; while (scanf("%d%d",&n,&m)!=EOF){ build(1, n, 1); int ans = 0; for (int i = 0; i < m; i++){ scanf("%s%d", str, &a); if (str[0] == ‘D‘){ update(a, 1, 1, n, 1); q[ans++] = a; } if (str[0] == ‘Q‘){ printf("%d\n", ask(a, 1, n, 1)); } if (str[0] == ‘R‘){ if(ans<=0) continue; int t = q[--ans]; update(t, 0, 1, n, 1); } } } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。