HDU 1540 Tunnel Warfare

发布时间:2016-12-11 21:59:06 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"HDU 1540 Tunnel Warfare",主要涉及到HDU 1540 Tunnel Warfare方面的内容,对于HDU 1540 Tunnel Warfare感兴趣的同学可以参考一下。

线段树,区间合并。n个村子连成一条线,鬼子炸,八路修,问x村左右共联通多少村子。三种操作分别对应炸修和问。模拟个栈来判断该修哪个,栈空判断一下。在开个数组记录一下目前各村状态,炸了就别再炸了,修了就别再修了。 建树时,三个数组的初始值都是当前区间长度。更新时,判断目标p在左边还是右边。询问时,先看p在哪边,假设p在左子树,并且在左子树的rsum内,也就是说可以一直连到右子树,那么就加上lsum[rt << 1 | 1]。反之亦然 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<cmath> ///LOOP #define REP(i, n) for(int i = 0; i < n; i++) #define FF(i, a, b) for(int i = a; i < b; i++) #define FFF(i, a, b) for(int i = a; i <= b; i++) #define FD(i, a, b) for(int i = a - 1; i >= b; i--) #define FDD(i, a, b) for(int i = a; i >= b; i--) ///INPUT #define RI(n) scanf("%d", &n) #define RII(n, m) scanf("%d%d", &n, &m) #define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k) #define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p) #define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q) #define RFI(n) scanf("%lf", &n) #define RFII(n, m) scanf("%lf%lf", &n, &m) #define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k) #define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p) #define RS(s) scanf("%s", s) ///OUTPUT #define PN printf("\n") #define PI(n) printf("%d\n", n) #define PIS(n) printf("%d ", n) #define PS(s) printf("%s\n", s) #define PSS(s) printf("%s ", n) #define PC(n) printf("Case %d: ", n) ///OTHER #define PB(x) push_back(x) #define CLR(a, b) memset(a, b, sizeof(a)) #define CPY(a, b) memcpy(a, b, sizeof(b)) #define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}} #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 using namespace std; typedef long long LL; typedef pair<int, int> P; const int MOD = 9901; const int INFI = 1e9 * 2; const LL LINFI = 1e17; const double eps = 1e-6; const double pi = acos(-1.0); const int N = 55555; const int M = 22; const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1}; int s[N], lsum[N << 2], rsum[N << 2], msum[N << 2]; bool d[N]; void pushup(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]; msum[rt] = max(lsum[rt << 1 | 1] + rsum[rt << 1], max(msum[rt << 1], msum[rt << 1 | 1])); } void build(int l, int r, int rt) { lsum[rt] = rsum[rt] = msum[rt] = r - l + 1; if(l == r)return; int m = (l + r) >> 1; build(lson); build(rson); } void update(int p, int v, int l, int r, int rt) { if(l == r) { lsum[rt] = rsum[rt] = msum[rt] = v; return; } int m = (l + r) >> 1; if(p <= m)update(p, v, lson); else update(p, v, rson); pushup(rt, r - l + 1); } int query(int p, int l, int r, int rt) { if(l == r || msum[rt] == 0 || msum[rt] == r - l + 1)return msum[rt]; int m = (l + r) >> 1; if(p <= m) { if(p + rsum[rt << 1] > m)return query(p, lson) + lsum[rt << 1 | 1]; else return query(p, lson); } else { if(p - lsum[rt << 1 | 1] <= m)return query(p, rson) + rsum[rt << 1]; else return query(p, rson); } } int main() { //freopen("input.txt", "r", stdin); char op[5]; int n, m, a, num; while(RII(n, m) != EOF) { num = 0; build(1, n, 1); REP(i, n + 1)d[i] = 1; REP(i, m) { RS(op); if(op[0] == 'D') { RI(a); if(d[a]) { d[a] = 0; update(a, 0, 1, n, 1); } s[num++] = a; } else if(op[0] == 'R') { if(!num)continue; a = s[--num]; if(!d[a]) { d[a] = 1; update(a, 1, 1, n, 1); } } else { RI(a); PI(query(a, 1, n, 1)); } } } return 0; }

上一篇:mapnik-python学习7:在基于xml绘制的地图上添加绘制
下一篇:VB.NET Timer实现功能细讲

相关文章

相关评论