首页 > 代码库 > HDU - 1166 敌兵布阵 (线段树---点修改)
HDU - 1166 敌兵布阵 (线段树---点修改)
题意:
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 表示结束,这条命令在每组数据最后出现。
#include<cstdio>#include<cstring>#include<cstdlib>#include<cctype>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<set>#include<map>#include<stack>#include<deque>#include<queue>#include<list>#define lowbit(x) (x & (-x))const double eps = 1e-8;inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1;}typedef long long LL;typedef unsigned long long ULL;const int INT_INF = 0x3f3f3f3f;const int INT_M_INF = 0x7f7f7f7f;const LL LL_INF = 0x3f3f3f3f3f3f3f3f;const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};const int MOD = 1e9 + 7;const double pi = acos(-1.0);const int MAXN = 50000 + 10;const int MAXT = 10000 + 10;using namespace std;int a[MAXN];int sum[MAXN << 2];char s[10];void build(int id, int L, int R){ if(L == R){ sum[id] = a[L]; } else{ int mid = L + (R - L) / 2; build(id << 1, L, mid); build(id << 1 | 1, mid + 1, R); sum[id] = sum[id << 1] + sum[id << 1 | 1]; }}void update(int k, int id, int L, int R, int v){ if(L == R){ sum[id] += v; } else{ int mid = L + (R - L) / 2; if(k <= mid) update(k, id << 1, L, mid, v); else update(k, id << 1 | 1, mid + 1, R, v); sum[id] = sum[id << 1] + sum[id << 1 | 1]; }}int query(int l, int r, int id, int L, int R){ if(l <= L && R <= r){ return sum[id]; } int mid = L + (R - L) / 2; int ans = 0; if(l <= mid) ans += query(l, r, id << 1, L, mid); if(r > mid) ans += query(l, r, id << 1 | 1, mid + 1, R); return ans;}int main(){ int T; scanf("%d", &T); int kase = 0; while(T--){ memset(a, 0, sizeof a); memset(sum, 0, sizeof sum); int N; scanf("%d", &N); for(int i = 1; i <= N; ++i){ scanf("%d", &a[i]); } build(1, 1, N); printf("Case %d:\n", ++kase); while(scanf("%s", s) == 1){ if(s[0] == ‘E‘) break; int i, j; scanf("%d%d", &i, &j); if(s[0] == ‘A‘){ update(i, 1, 1, N, j); } else if(s[0] == ‘S‘){ update(i, 1, 1, N, -j); } else{ printf("%d\n", query(i, j, 1, 1, N)); } } } return 0;}
HDU - 1166 敌兵布阵 (线段树---点修改)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。