Luogu P2068 统计和
P2068 统计和
题目描述
给定一个长度为n(n<=100000),初始值都为0的序列,x(x<=10000)次的修改某些位置上的数字,每次加上一个数,然后提出y (y<=10000)个问题,求每段区间的和。时间限制1秒。
输入输出格式
输入格式:
第一行1个数,表示序列的长度n
第二行1个数,表示操作的次数w
后面依次是w行,分别表示加入和询问操作
其中,加入用x表示,询问用y表示
x的格式为"x a b" 表示在序列a的位置加上b
y的格式为"y a b" 表示询问a到b区间的加和
输出格式:
每行一个数,分别是每次询问的结果
输入输出样例
5
4
x 3 8
y 1 3
x 4 9
y 3 4
8
17
线段树中最低级的模板,只有区间查询和单点修改。
请放心食用。
#include <iostream>
#include <cstring>
#include <cstdio>
#define MAXN 100007
#define lson (k<<1)
#define rson (k<<1)+1
#define int long long using namespace std; int n, m, a, b, Ans;
char c;
struct node
{
int l, r;
int sum;
}tree[MAXN*4]; void build(int k, int ll, int rr)
{
tree[k].l = ll, tree[k].r = rr;
if(ll == rr)
{
tree[k].sum = 0;
return ;
}
int mid = (ll+rr)/2;
build(lson, ll, mid);
build(rson, mid+1, rr);
tree[k].sum = tree[lson].sum+tree[rson].sum;
} void change(int k)
{
if(tree[k].l == tree[k].r && tree[k].l == a)
{
tree[k].sum += b;
return ;
}
int mid = (tree[k].l+tree[k].r)/2;
if(mid >= a) change(lson);
if(mid < a) change(rson);
tree[k].sum = tree[lson].sum+tree[rson].sum;
} void find(int k) {
if(tree[k].l >= a && tree[k].r <= b)
{
Ans += tree[k].sum;
return ;
}
int mid = (tree[k].l+tree[k].r)/2;
if(mid >= a) find(lson);
if(mid < b) find(rson);
} main()
{
scanf("%lld%lld", &n, &m);
build(1, 1, n);
for(int i=1; i<=m; i++)
{
Ans = 0;
cin>>c>>a>>b;
if(c == 'x')
{
change(1);
}
else
{
find(1);
printf("%lld\n", Ans);
}
}
}
Luogu P2068 统计和的更多相关文章
- 洛谷——P2068 统计和
P2068 统计和 题目描述 给定一个长度为n(n<=100000),初始值都为0的序列,x(x<=10000)次的修改某些位置上的数字,每次加上一个数,然后提出y (y<=1000 ...
- P2068 统计和
P2068 统计和 这题真的非常水了 如果不会 右转[模板]树状数组 2 基本上是一模一样的 #include <bits/stdc++.h> #define lowbit(x) x&am ...
- P2068 统计和(树状数组模板)
这是一道树状数组题 #include<iostream> using namespace std; ; int tree[maxn], n, m; char h; int x, y; vo ...
- 洛谷P2068 统计和
题目描述 给定一个长度为\(n(n \leq 100000)\),初始值都为\(0\)的序列,\(x(x \leq 10000)\)次的修改某些位置上的数字,每次加上一个数,然后提出\(y (y \l ...
- 洛谷 P2068 统计和
题目描述 给定一个长度为n(n<=100000),初始值都为0的序列,x(x<=10000)次的修改某些位置上的数字,每次加上一个数,然后提出y (y<=10000)个问题,求每段区 ...
- 洛谷P2068 统计和题解
题目描述 给定一个长度为n(n<=100000),初始值都为0的序列,x(x<=10000)次的修改某些位置上的数字,每次加上一个数,然后提出y (y<=10000)个问题,求每段区 ...
- 线段tree~讲解+例题
最近学习了线段树这一重要的数据结构,有些许感触.所以写一篇博客来解释一下线段树,既是对自己学习成果的检验,也希望可以给刚入门线段树的同学们一点点建议. 首先声明一点,本人是个蒟蒻,如果在博客中有什么不 ...
- [Luogu] 树状数组
https://www.luogu.org/problemnew/show/P3374 单点修改,区间查询 #include <iostream> #include <cstdio& ...
- 树状数组板子 x
树状数组! 参考 http://www.cnblogs.com/zzyh/p/6992148.html 洛谷 P3374 [模板]树状数组 1 题目描述 如题,已知一个数列,你需要进行下面两种操作: ...
随机推荐
- Arduino程序-光敏电阻
尽管造书去做的.但还是有莫名的成就感 从串口显示出,电压变化, void setup() { // put your setup code here, to run once: Serial. ...
- UVa 10954 Add All(优先队列)
题意 求把全部数加起来的最小代价 a+b的代价为(a+b) 越先运算的数 要被加的次数越多 所以每次相加的两个数都应该是剩下序列中最小的数 然后结果要放到序列中 也就是优先队列了 #inc ...
- FFmpeg滤镜使用指南
文件夹 1. FFmpeg滤镜文档 2. 演示样例 2.1 缩放 2.2 视频加速 2.3 滤镜图,链和滤镜关系 2.4 多个输入覆盖同一个2x2 网格 2.5 转义字符 2. ...
- ES6 一些常用使用
//1.解构数组 let arr1 = ['apple', 'coffee', 'cake']; let [fruit, drink, dessert] = arr1; console.log(fru ...
- jenkins对接gitlab和git
1 需要的插件 jenkins的git插件和jenkins的gitlab插件. 2 对接gitlab 在系统配置中,随便起一个连接的名字,设置url,可以直接用ip地址,端口号默认是80,不需要写明. ...
- Kindergarten Election
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3715 题意:有N个孩子投票选举leader,不能自己选自己.Sheldon ...
- 虫食算 2004年NOIP全国联赛提高组(dfs)
1064 虫食算 2004年NOIP全国联赛提高组 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Descrip ...
- cmd执行Java程序
先创建一个文本,里面内容为 public class hello{ public static void main(String[] arg) { System.out.println("H ...
- Linux下MySql数据的导入导出
1,每天4点备份mysql数据: 2,为节省空间,删除超过3个月的所有备份数据: 3,删除超过7天的备份数据,保留3个月里的 10号 20号 30号的备份数据: mysqldump -u用戶名 -p密 ...
- linux上搭建svn
参照网址:http://www.cnblogs.com/LusYoHo/p/6056377.html(如何在linux下搭建svn服务) http://www.cnblo ...