HDU Problem 1166 敌兵布阵 【树状数组 & 线段树】
2016-08-18 10:25
441 查看
敌兵布阵
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 78012 Accepted Submission(s): 32919
Problem Description
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
Input
第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令
Output
对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End
Sample Output
Case 1:
6
33
59
Author
Windbreaker
Recommend
Eddy | We have carefully selected several similar problems for you: 1394 1698 1754 1542 1540
#include <cmath> #include <cstdio> #include <set> #include <vector> #include <cstring> #include <iostream> #include <algorithm> //#define LOCAL #define space " " using namespace std; //typedef __int64 Int; //typedef long long Long; const int INF = 0x3f3f3f3f; const int MAXN = 100000 + 10; const double Pi = acos(-1.0); const double ESP = 1e-5; int n, bit[MAXN]; int sum(int x) { int s = 0; while (x > 0) { s += bit[x]; x -= x&-x; } return s; } void add(int x, int y) { while (x <= n) { //执行线段树上每个点的更新 bit[x] += y; x += x&-x; // cout << i << endl; } } int main() { int t, kcase = 0, a, b; scanf("%d", &t); while (t--) { scanf("%d", &n); memset(bit, 0, sizeof(bit)); for (int i = 1; i <= n; i++) { scanf("%d", &a); add(i, a); } char str[20]; printf("Case %d:\n", ++kcase); while (scanf("%s", str), str[0] != 'E') { if (str[0] == 'A') { scanf("%d%d", &a, &b); add(a, b); } else if (str[0] == 'S') { scanf("%d%d", &a, &b); add(a, -b); } else { scanf("%d%d", &a, &b); printf("%d\n", sum(b) - sum(a - 1)); } } } return 0; }
下面是线段树
#include <map> #include <set> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <iostream> #include <stack> #include <cmath> #include <string> #include <vector> #include <cstdlib> //#include <bits/stdc++.h> //#define LOACL #define space " " using namespace std; typedef long long LL; typedef __int64 Int; typedef pair<int, int> paii; const int INF = 0x3f3f3f3f; const double ESP = 1e-5; const double PI = acos(-1.0); const int MOD = 1e9 + 7; const int MAXN = 200000 + 10; int num[MAXN]; char op[9]; struct node { int l, r, value; } seg[MAXN]; int build_segm(int x, int lson, int rson) { seg[x].l = lson; seg[x].r = rson; if (lson != rson) { int a = build_segm(x << 1, lson, (rson + lson) >> 1); int b = build_segm((x << 1) + 1, ((rson + lson) >> 1) + 1, rson); return seg[x].value = a + b; } return seg[x].value = num[lson]; } int query_segm(int x, int lson, int rson) { if (seg[x].l > rson || seg[x].r < lson) return 0; if (seg[x].l >= lson && seg[x].r <= rson) return seg[x].value; int a = query_segm(x << 1, lson, rson); int b = query_segm((x << 1) + 1, lson, rson); return a + b; } int updata(int x, int pos, int val) { if (seg[x].l > pos || seg[x].r < pos) return seg[x].value; if (seg[x].l == pos && seg[x].r == pos) { return seg[x].value += val; } int a = updata(x << 1, pos, val); int b = updata((x << 1) + 1, pos, val); return seg[x].value = a + b; } int main() { int T = 0; scanf("%d", &T); int N, x, y; int Kcase = 0; while (T--) { scanf("%d", &N); for (int i = 1; i <= N; i++) scanf("%d", &num[i]); build_segm(1, 1, N); printf("Case %d:\n", ++Kcase); while(scanf("%s", op)) { if (op[0] == 'E') break; scanf("%d%d", &x, &y); if (op[0] == 'A') updata(1, x, y); else if (op[0] == 'S') {y = -y; updata(1, x, y);} else { printf("%d\n", query_segm(1, x, y)); } } } return 0; }
相关文章推荐
- HDOJ 1166 敌兵布阵 (树状数组 & 线段树)
- HDU 1166 敌兵布阵 (线段树基础&树状数组基础)
- hdu 1166 敌兵布阵【入门线段树 & 树状数组】
- HDU 1166 && HLG 1794 敌兵布阵(树状数组)
- hdu 1166 敌兵布阵 单点更新 树状数组 线段树 两种做法
- hdu 1166 敌兵布阵【线段树Or树状数组】
- hdoj&nbsp;1166&nbsp;敌兵布阵(树状数组)
- HDU 1166 敌兵布阵 树状数组||线段树
- HDU-1166 敌兵布阵(树状数组和线段树)
- 线段树(单点更新)/树状数组 HDOJ 1166 敌兵布阵
- hdu1166 敌兵布阵(树状数组 && 线段树单点更新)
- HODJ1166敌兵布阵树状数组和线段树求解
- hdu 1166 敌兵布阵(水...线段树&&树状数组)
- HDOJ 1166 敌兵布阵-------------树状数组 线段树
- hdu 1166 敌兵布阵(简单线段树or树状数组)
- hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)
- HDU - 1166 - 敌兵布阵 (树状数组 or 线段树)
- hdu 1166 敌兵布阵 线段树&树状数组
- HDU 1166 敌兵布阵(树状数组,线段树)
- HDU1166 敌兵布阵 树状数组|线段树-构造完全二叉树(理解)