HDU 1166 线段树
来源:互联网 发布:中国茶叶出口数据2017 编辑:程序博客网 时间:2024/05/05 12:01
方法一:>o<
线段树
模板题
代码如下
#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>using namespace std;const int maxn=500000+10;struct tree{ int lef,rig,mid,sum;}t[maxn];int cas,len,cnt,a;void init(int a,int b,int n){ t[n].lef=a,t[n].rig=b; t[n].mid=(a+b)/2,t[n].sum=0; if(a+1==b) return; init(a,(a+b)/2,2*n);//左子树---------左闭右开 init((a+b)/2,b,2*n+1);//右子树 }void creat(int pos,int val,int n){ t[n].sum+=val; if(t[n].lef+1==t[n].rig) return; if(pos<t[n].mid) creat(pos,val,2*n);//左子树 else creat(pos,val,2*n+1);}int query(int x,int y,int n){ if(t[n].lef==x&&t[n].rig==y) return t[n].sum; if(x<t[n].mid){ if(y<=t[n].mid) return query(x,y,n*2); else return query(x,t[n].mid,2*n)+query(t[n].mid,y,2*n+1); } else return query(x,y,2*n+1);}int main(){ scanf("%d",&cas),cnt=1; while(cas--){ char ord[10]; scanf("%d",&len),init(1,len+1,1); for(int i=1;i<=len;i++) scanf("%d",&a),creat(i,a,1); int x,y; printf("Case %d:\n",cnt++); while(scanf("%s",ord)){ if(!strcmp(ord,"End")) break; scanf("%d%d",&x,&y); if(strcmp(ord,"Query")==0) printf("%d\n",query(x,y+1,1)); else if(strcmp(ord,"Add")==0) creat(x,y,1); else if(strcmp(ord,"Sub")==0) creat(x,-y,1); } } return 0;}
1 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 线段树
- NSURLSession的基本使用
- poj3415 Common Substrings (后缀数组+单调队列)
- POJ 3744 Scout YYF I (矩阵相乘+概率DP)
- 十一、图的存储---(2)邻接矩阵和邻接表
- Leetcode - Compare Version Numbers
- HDU 1166 线段树
- NSURLSession的介绍和说明
- Android 发布项目到 jcenter 遇到的坑
- linux上修改时间
- iOS视图控制器编程指南 --- 视图控制器层次结构
- 1026. 程序运行时间(15)
- 英语的五种基本句型
- 正向代理与反向代理
- Caffe学习系列(3):im2col