hdu 1166 敌兵布阵
2015-03-22 20:47
211 查看
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
线段树,区间和,单点更新,照样我写成区间更新。具体实现如下:
线段树,区间和,单点更新,照样我写成区间更新。具体实现如下:
[code]#include<stdio.h> #include<stdlib.h> #include<string.h> #define Max_N 51000 typedef struct _seg{ int val, addMark; }SegTree; SegTree seg[Max_N << 2]; int arr[Max_N]; void built(int root, int x, int y){ seg[root].addMark = 0; if (x == y){ seg[root].val = arr[x]; return; } int mid = (x + y) >> 1; built(root << 1, x, mid); built(root << 1 | 1, mid + 1, y); seg[root].val = seg[root << 1].val + seg[root << 1 | 1].val; } void pushDown(int root){ if (seg[root].addMark != 0){ seg[root << 1].addMark += seg[root].addMark; seg[root << 1 | 1].addMark += seg[root].addMark; seg[root << 1].val += seg[root].addMark; seg[root << 1 | 1].val += seg[root].addMark; seg[root].addMark = 0; } } void update(int root, int _x, int _y, int x, int y, int addVal){ if (x > _y || y < _x) return; if (x <= _x && y >= _y){ seg[root].addMark += addVal; seg[root].val += addVal; return; } pushDown(root); int mid = (_x + _y) >> 1; update(root << 1, _x, mid, x, y, addVal); update(root << 1 | 1, mid + 1, _y, x, y, addVal); seg[root].val = seg[root << 1].val + seg[root << 1 | 1].val; } int query(int root, int _x, int _y, int x, int y){ if (x > _y || y < _x) return 0; if (x <= _x && y >= _y) return seg[root].val; pushDown(root); int mid = (_x + _y) >> 1; int v1 = query(root << 1, _x, mid, x, y); int v2 = query(root << 1 | 1, mid + 1, _y, x, y); return v1 + v2; } int main(){ #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout); #endif char buf[100], s1[100]; int i, t, n, a, b, c = 1; scanf("%d", &t); while (t--){ printf("Case %d:\n", c++); scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &arr[i]); built(1, 1, n); getchar(); while (gets(buf) && strcmp(buf, "End") != 0){ sscanf(buf, "%s %d %d", s1, &a, &b); if (0 == strcmp(s1, "Query")) printf("%d\n", query(1, 1, n, a, b)); else if (0 == strcmp(s1, "Add")) update(1, 1, n, a, a, b); else update(1, 1, n, a, a, -b); } } return 0; }
相关文章推荐
- Hdu-1166 敌兵布阵【线段树(单点更新)】
- hdu--1166 敌兵布阵(线段树+单点更新和查询)
- HDU 1166 敌兵布阵(线段树区间和)
- HDU-1166-敌兵布阵-单点更新
- hdu-1166 敌兵布阵(树状数组)
- HDU 1166 - 敌兵布阵
- HDU 1166 敌兵布阵 【线段树】
- HDU 1166 敌兵布阵(树状数组)
- 线段树 hdu 1166 敌兵布阵 单点更新区间求和
- HDU 1166 敌兵布阵(区间求和+单点更新)
- HDU-1166-敌兵布阵
- HDU 1166 敌兵布阵(2)
- 两道基础线段树简解:HDU 1166 敌兵布阵,HDU 1754 I Hate it
- hdu_1166 敌兵布阵(线段树)
- HDU 1166 敌兵布阵
- hdu 1166敌兵布阵 线段树 树状数组
- hdu 1166 敌兵布阵 线段树&树状数组
- hdu 1166 敌兵布阵(树状数组裸题)
- HDU 1166 敌兵布阵(线段树)
- hdu 1166 敌兵布阵