HDU1166-敌兵布阵

来源:互联网 发布:淘宝零食大礼包划算吗 编辑:程序博客网 时间:2024/05/17 06:12

题目链接

http://acm.split.hdu.edu.cn/showproblem.php?pid=1166

思路

最基本的线段树,单点修改,区间查询

代码

#include <iostream>#include <cstring>#include <stack>#include <vector>#include <set>#include <map>#include <queue>#include <sstream>#include <iomanip>#include <fstream>#include <cstdio>#include <cstdlib>#include <climits>#include <deque>#include <bitset>#include <algorithm>using namespace std;#define PI acos(-1.0)#define LL long long#define PII pair<int, int>#define PLL pair<LL, LL>#define mp make_pair#define IN freopen("in.txt", "r", stdin)#define OUT freopen("out.txt", "wb", stdout)#define scan(x) scanf("%d", &x)#define scan2(x, y) scanf("%d%d", &x, &y)#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)#define sqr(x) (x) * (x)#define pr(x) cout << #x << " = " << x << endl#define lc o << 1#define rc o << 1 | 1#define pl() cout << endl#define CLR(a, x) memset(a, x, sizeof(a))#define FILL(a, n, x) for (int i = 0; i < n; i++) a[i] = xconst int maxn = 50000 << 2;LL sumv[maxn];int y1, y2, p, v;void pushup(int o) {    sumv[o] = sumv[lc] + sumv[rc];}void update(int o, int L, int R) {    if (L == R) {        sumv[o] += v;        return;    }    int M = L + (R - L) / 2;    if (p <= M) update(lc, L, M);    else update(rc, M + 1, R);    pushup(o);}LL query(int o, int L, int R) {    if (y1 <= L && y2 >= R) return sumv[o];    int M = L + (R - L) / 2;    LL res = 0;    if (y1 <= M) res += query(lc, L, M);    if (y2 > M) res += query(rc, M + 1, R);    return res;}void build(int o, int L, int R) {    if (L == R) {        scan(sumv[o]);        return;    }    int M = L + (R - L) / 2;    build(lc, L, M);    build(rc, M + 1, R);    pushup(o);}int main() {    int T, n, kase = 0;    char s[10];    scan(T);    while (T--) {        scan(n);        build(1, 1, n);        printf("Case %d:\n", ++kase);        while (scanf("%s", s) && s[0] != 'E') {            if (s[0] == 'Q') {                scan2(y1, y2);                printf("%lld\n", query(1, 1, n));            }            if (s[0] == 'A') {                scan2(p, v);                update(1, 1, n);            }            if (s[0] == 'S') {                scan2(p, v);                v = -v;                update(1, 1, n);            }        }    }    return 0;}
0 0
原创粉丝点击