链接:

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

自己第一次在没有看题解AC出来的线段树,写的可能不是太好,再贴个学长的代码,学习一下

发现自己的Update部分写了很多废话,直接用a[r]里的值就好了,我还传了那么多的值,真傻!

代码:

 #include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
using namespace std; #define Lson r<<1
#define Rson r<<1|1 const int N = 1e6+; struct SegmentTree
{
int L, R;
int sum, e;
int Mid()
{
return (R+L)>>;
}
}a[N<<]; void BuildSegTree(int r, int L, int R)
{
a[r].L=L, a[r].R=R;
a[r].e=; if(L==R)
{
scanf("%d", &a[r].sum);
return ;
} BuildSegTree(Lson, L, a[r].Mid());
BuildSegTree(Rson, a[r].Mid()+, R); a[r].sum = a[Lson].sum + a[Rson].sum;
} void Update(int r, int L, int R, int i, int e)
{
a[r].sum += e; if(L==R && L==i)
return ; if(i<=a[r].Mid())
Update(Lson, L, a[r].Mid(), i, e);
else if(i>a[r].Mid())
Update(Rson, a[r].Mid()+, R, i, e);
} int Query(int r, int L, int R)
{
if(a[r].L==L && a[r].R==R)
return a[r].sum; if(R<=a[r].Mid())
return Query(Lson, L, R);
else if(L>a[r].Mid())
return Query(Rson, L, R);
else
{
long long Lsum = Query(Lson, L, a[r].Mid());
long long Rsum = Query(Rson, a[r].Mid()+, R); return Lsum + Rsum;
}
} int main()
{
int t, k=;
scanf("%d", &t);
while(t--)
{
int n;
scanf("%d", &n);
BuildSegTree(, , n); char s[];
int L, R, i, e; printf("Case %d:\n", k++); while(scanf("%s", s), strcmp(s, "End"))
{
if(strcmp(s, "Query")==)
{
scanf("%d%d", &L, &R);
printf("%d\n", Query(, L, R));
}
else if(strcmp(s, "Add")==)
{
scanf("%d%d", &i, &e);
Update(, , n, i, e);
}
else
{
scanf("%d%d", &i, &e);
Update(, , n, i, -e);
}
}
}
return ;
}

学长的代码:

 #include<stdio.h>
#include<math.h>
#include<string.h> #define maxn 50005 struct node
{
int L, R, sum;//左右子树,sum记录区间和
int Mid(){return (L+R)/;}
}tree[maxn*];//为了保险起见一般定义是四倍
int val[maxn];//保存每个阵地原来的人数 void Build(int root, int L, int R)//建树
{
tree[root].L = L, tree[root].R = R; if(L == R)
{
tree[root].sum = val[L];
return ;
} Build(root<<, L, tree[root].Mid());//<<1 运算符相当于乘上 2,因为是数往左移一位
Build(root<<|, tree[root].Mid()+, R);//|1, 因为左移后最后一位是0, 所以与1进行|相当于+1 tree[root].sum = tree[root<<].sum+tree[root<<|].sum;//区间和等于左右区间的和
}
//k表示需要更新的点,e表示需要更新的值
void Insert(int root, int k, int e)
{
tree[root].sum += e;
if(tree[root].L == tree[root].R)
return ; if(k <= tree[root].Mid())
Insert(root<<, k, e);
else
Insert(root<<|, k, e);
}
//查询区间LR的和
int Query(int root, int L, int R)
{
if(tree[root].L == L && tree[root].R == R)
return tree[root].sum; //如果在左子树区间
if(R <= tree[root].Mid())
return Query(root<<, L, R);
else if(L > tree[root].Mid())//如果在右子树区间
return Query(root<<|, L, R);
else
{//在左右子树
int Lsum = Query(root<<, L, tree[root].Mid());
int Rsum = Query(root<<|, tree[root].Mid()+, R); return Lsum + Rsum;
}
} int main()
{
int T, t=; scanf("%d", &T); while(T--)
{
int i, N, x, y; scanf("%d", &N); for(i=; i<=N; i++)
scanf("%d", &val[i]);
Build(, , N); char s[]; printf("Case %d:\n", t++); while(scanf("%s", s), s[] != 'E')
{
scanf("%d%d", &x, &y); if(s[] == 'A')
Insert(, x, y);
else if(s[] == 'S')
Insert(, x, -y);
else
{
int ans = Query(, x, y);
printf("%d\n", ans);
}
}
} return ;
}

(线段树)敌兵布阵--hdu--1166 (入门)的更多相关文章

  1. 敌兵布阵 HDU 1166 线段树

    敌兵布阵 HDU 1166 线段树 题意 这个题是用中文来描写的,很简单,没什么弯. 解题思路 这个题肯定就是用线段树来做了,不过当时想了一下可不可用差分来做,因为不熟练就还是用了线段树来做,几乎就是 ...

  2. A - 敌兵布阵 HDU - 1166 线段树(多点修改当单点修改)

    线段树板子题练手用 #include<cstdio> using namespace std; ; int a[maxn],n; struct Node{ int l,r; long lo ...

  3. 敌兵布阵 HDU - 1166 (树状数组模板题,线段树模板题)

    思路:就是树状数组的模板题,利用的就是单点更新和区间求和是树状数组的强项时间复杂度为m*log(n) 没想到自己以前把这道题当线段树的单点更新刷了. 树状数组: #include<iostrea ...

  4. A - 敌兵布阵 - hdu 1166

    Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些 ...

  5. 敌兵布阵 HDU - 1166 板子题

    #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...

  6. HDU 1754 线段树 单点跟新 HDU 1166 敌兵布阵 线段树 区间求和

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. A - 敌兵布阵(HDU 1166)

    A - 敌兵布阵 HDU - 1166 思路:线段树单点修改+区间查询. #include<cstdio> #include<cstring> #include<iost ...

  8. hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. hdu acm 1166 敌兵布阵 (线段树)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

随机推荐

  1. 解决Linux命令行为什么变成-bash-3.2$

    在Linux服务器上创建了一个新用户probe,是这样创建的: [root@localhost home]# groupadd -g 501 probe [root@localhost home]# ...

  2. Lodash踩坑记录

    一直在用lodash 这个框架,最近踩了一个坑 reverse 这个函数是mutable的 ,后边去查了文档 Note: This method mutates array and is based ...

  3. 迷你MVVM框架 avalonjs 1.4.1发布

    以后有关avalon的版本升级消息,全部改放到这里 重构parseHTML,让其支持xhtml 强化 ms-duplex-number拦截器 添加data-duplex-number辅助指令 值为st ...

  4. Nsis Sqlite Plugin

    1.https://stackoverflow.com/questions/15346338/nsis-and-sqlite-integration 2.http://nsis.sourceforge ...

  5. java 里定义的方法参数 (final String... args)

    定义成final是为了防止在方法类里面修改参数,final String... args 为JDK新的特性,为可变长参数.编译的时候被解释为:public DCMException(final Str ...

  6. MySql初步II

    [MySql初步II] 1.Order By 你可以使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列. 默认情况下,它是按升排列. 实例: 2.Join语法 Join不是一个关键字 ...

  7. 如何学习mybatis

    最近几天学习了mybatis框架,我是mybatis视频学习的.看这篇文章,我建议首先要会熟练使用MVC架构,再学习这个框架. 在我们写传统的MVC模式写Bean,Dao,Servlet时,我们每次调 ...

  8. 165. Compare Version Numbers比较版本号的大小

    [抄题]: Compare two version numbers version1 and version2.If version1 > version2 return 1; if versi ...

  9. javascript的数据检测总结

    目录 javaScript的数据检测 1.typeof 2.instanceof 3.constructor 4.Object.prototype.toString.call()--------- 一 ...

  10. fragment 事务回滚 ---动态创建fragment

    import java.util.Date; import java.util.LinkedList; import com.qianfeng.gp08_day23_fragment5.fragmen ...