(线段树)敌兵布阵--hdu--1166 (入门)
链接:
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 (入门)的更多相关文章
- 敌兵布阵 HDU 1166 线段树
敌兵布阵 HDU 1166 线段树 题意 这个题是用中文来描写的,很简单,没什么弯. 解题思路 这个题肯定就是用线段树来做了,不过当时想了一下可不可用差分来做,因为不熟练就还是用了线段树来做,几乎就是 ...
- A - 敌兵布阵 HDU - 1166 线段树(多点修改当单点修改)
线段树板子题练手用 #include<cstdio> using namespace std; ; int a[maxn],n; struct Node{ int l,r; long lo ...
- 敌兵布阵 HDU - 1166 (树状数组模板题,线段树模板题)
思路:就是树状数组的模板题,利用的就是单点更新和区间求和是树状数组的强项时间复杂度为m*log(n) 没想到自己以前把这道题当线段树的单点更新刷了. 树状数组: #include<iostrea ...
- A - 敌兵布阵 - hdu 1166
Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些 ...
- 敌兵布阵 HDU - 1166 板子题
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...
- HDU 1754 线段树 单点跟新 HDU 1166 敌兵布阵 线段树 区间求和
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- A - 敌兵布阵(HDU 1166)
A - 敌兵布阵 HDU - 1166 思路:线段树单点修改+区间查询. #include<cstdio> #include<cstring> #include<iost ...
- hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu acm 1166 敌兵布阵 (线段树)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
随机推荐
- Haskell语言学习笔记(44)Lens(2)
自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...
- nginx配置【转】
转自:http://www.ha97.com/5194.html #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_proc ...
- localstorage是什么,它有哪些作用
localStorage作为HTML5本地存储web storage特性的API之一,主要作用是将数据保存在客户端中,而客户端一般是指上海网站设计用户的计算机.在移动设备上,由于大部分浏览器都支持 w ...
- mysql数据类型长度
1个字节= 8位 tinyint 为一个字节 2的8次方= 256 所以最多存储到256 日期和时间数据类型 MySQL数据类型 含义 date 3字节,日期,格式:2014-09-18 time ...
- LuoguP1226 【模板】快速幂||取余运算
题目链接:https://www.luogu.org/problemnew/show/P1226 第一次学快速幂,将别人对快速幂原理的解释简要概括一下: 计算a^b时,直接乘的话计算次数为b,而快速幂 ...
- 52. N-Queens II (Array; Back-Track)
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- 自动化预备知识上&&下--Android自动化测试学习历程
章节:自动化基础篇——自动化预备知识上&&下 主要讲解内容及笔记: 一.需要具备的能力: 测试一年,编程一年,熟悉并掌握业界自动化测试工具(monkey--压力测试.monkeyrun ...
- ubuntu 安装 google Gtest
1.安装源代码 在ubuntu的桌面上,右键选择打开终端,在终端中输入如下命令: $ sudo apt-get install libgtest-dev 下载源码后,apt将会在目录/usr/src/ ...
- dede的cfg_keywords和cfg_description无法显示
问题:在生成html文件时,网页的keywords和description的content为空,但后台显示这两项是有值的. 解决方案: 1.设置 系统->系统基本参数->站点根网址 设 ...
- linux下安装oracle数据库详细教程
一.安装yum源 下载或拷贝RedHat的iso镜像到本地,比如 /repo/iso/ rhel-server-6.6-x86_64-dvd.iso 1.建立ISO文件存放目录(/repo/iso)和 ...