HDU - 1166 敌兵布阵
来源:互联网 发布:什么是数据标准化 编辑:程序博客网 时间:2024/06/05 04:08
题意:省略
思路:经典的线段树更新节点
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 10;const int MAXN = 50010;struct Node{ int left,right; int sum;}node[MAXN*4];int n,num[MAXN];void init(int left,int right,int pos){ node[pos].left = left; node[pos].right = right; if (left == right){ node[pos].sum = num[left]; return; } int x = (left+right) >> 1; init(left,x,pos<<1); init(x+1,right,pos<<1|1); node[pos].sum = node[pos<<1].sum + node[pos<<1|1].sum;}void update(int index,int val,int pos){ if (node[pos].left == node[pos].right){ node[pos].sum += val; return; } int x = (node[pos].left+node[pos].right) >> 1; if (index <= x) update(index,val,pos<<1); else update(index,val,pos<<1|1); node[pos].sum = node[pos<<1].sum + node[pos<<1|1].sum;}int query(int left,int right,int pos){ if (node[pos].left == left && node[pos].right == right) return node[pos].sum; int x = (node[pos].left+node[pos].right) >> 1; if (right <= x) return query(left,right,pos<<1); else if (left > x) return query(left,right,pos<<1|1); else return query(left,x,pos<<1)+query(x+1,right,pos<<1|1);}void input(){ char str[N]; scanf("%d",&n); for (int i = 1; i <= n; i++) scanf("%d",&num[i]); init(1,n,1); getchar(); int x,y; while (scanf("%s",str) != EOF && str[0] != 'E'){ scanf("%d%d%*c",&x,&y); if (str[0] == 'Q') printf("%d\n",query(x,y,1)); else if (str[0] == 'A') update(x,y,1); else update(x,-y,1); }}int main(){ int t,cas = 1; scanf("%d",&t); while (t--){ printf("Case %d:\n",cas++); input(); } return 0;}
0 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 敌兵布阵
- hdu 1166 敌兵布阵
- HDU 1166 敌兵布阵
- HDU 1166 - 敌兵布阵
- hdu 1166 敌兵布阵
- hdu 1166 敌兵布阵
- hdu 1166 敌兵布阵
- GCC调试基础知识
- 学习TCP协议的流量控制(flow control)小结
- Android4.2.2 Camer系统架构图(HAL和回调处理)
- 如何匹配职业和兴趣
- SQLsever分页 高效率
- HDU - 1166 敌兵布阵
- android 4.0 com.android.ddmlib.InstallException: device not found
- DataSet用法(二)
- 【暴力搜索】-HDU-4282-a very hard mathematic problem
- php中数组合并array_merge和+的区别
- centos中ln
- EBS OAF开发DataBoundValues的使用示例-动态显示表列的左右对齐
- SDWebImage
- 木马免杀和PE