NYOJ 1068 ST(段树 为段更新+间隔总和)
ST
- 描写叙述
-
“麻雀”lengdan用随机数生成了后台数据。可是笨笨的他被妹纸的问题给难住了。
。。
已知lengdan生成了N(1=<N<=10005)个随机整数,妹子对这些数可能有下面几种操作或询问:
1,A a b c 表示给区间a到b内每一个数都加上c。
2,S a b 表示输出区间a到b内的和。
3,Q a b 表示区间a到b内的奇数的个数;
为了使妹纸不口渴,所以我们决定妹纸的询问次数少一点。即(1=<M<=10000,M为询问次数)。
- 输入
-
多组測试数据。
每组測试数据第一行包括两个数N,M。表示N个整数,运行M次询问或操作。
紧接着一行输入N个整数,输入数据保证在int范围内。接下来M行,每行输入一种操作。
- 输出
- 每次对于操作2和3。输出结果。
- 例子输入
-
5 5
1 2 3 4 5
Q 1 4
S 1 5
A 1 4 1
S 1 5
Q 2 5 - 例子输出
-
2
15
19
3 - 简单的线段树成段更新+区间求和,仅仅是附加了求区间内的奇数个数。由于之前没有写过线段树成段更新的题目,导致由于一条向下更新的语句忘记写,调了一个上午才发现错误。
-
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 10010;
#define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
struct node
{
int l, r;
LL sum, odd, add;
}a[N<<2];
void PushUp(int root)
{
a[root].sum = a[root<<1].sum + a[root<<1|1].sum;
a[root].odd = a[root<<1].odd + a[root<<1|1].odd;
}
void PushDown(int len, int root)
{
if(a[root].add)
{
a[root<<1].add += a[root].add;
a[root<<1|1].add += a[root].add;
a[root<<1].sum += LL(len - (len>>1)) * a[root].add;
a[root<<1|1].sum += LL(len>>1) * a[root].add;
if(a[root].add % 2 == 1)
{
a[root<<1].odd = len - (len>>1) - a[root<<1].odd;
a[root<<1|1].odd = (len>>1) - a[root<<1|1].odd;
}
a[root].add = 0;
}
}
void build_tree(int l, int r, int root)
{
a[root].l = l;
a[root].r = r;
a[root].add = 0;
a[root].odd = 0;
if(l == r)
{
scanf("%lld",&a[root].sum);
if(a[root].sum % 2 == 1)
a[root].odd = 1;
return;
}
int mid = (l + r) >> 1;
build_tree(lson);
build_tree(rson);
PushUp(root);
}
void update(int l, int r, int root, LL k)
{
if(l <= a[root].l && r >= a[root].r)
{
a[root].add += k;
a[root].sum += LL(a[root].r - a[root].l + 1) * k;
if(k % 2 == 1)
a[root].odd = (a[root].r - a[root].l + 1) - a[root].odd;
return;
}
PushDown(a[root].r - a[root].l + 1, root);
int mid = (a[root].l + a[root].r) >> 1;
if(l <= mid) update(l, r, root<<1, k);
if(r > mid) update(l, r, root<<1|1, k);
PushUp(root);
}
LL Query(int l, int r, int root, char ch)
{
if(l <= a[root].l && r >= a[root].r)
{
if(ch == 'Q') return a[root].odd;
else if(ch == 'S') return a[root].sum;
}
PushDown(a[root].r - a[root].l + 1, root);
LL ans = 0;
int mid = (a[root].l + a[root].r) >> 1;
if(l <= mid) ans += Query(l, r, root<<1, ch);
if(r > mid) ans += Query(l, r, root<<1|1, ch);
return ans;
}
int main()
{
int n, m, i, x, y;
LL z;
char ch[5];
while(~scanf("%d%d",&n,&m))
{
build_tree(1, n, 1);
while(m--)
{
scanf("%s",ch);
if(ch[0] == 'A')
{
scanf("%d%d%lld",&x,&y,&z);
update(x, y, 1, z);
}
else
{
scanf("%d%d",&x,&y);
printf("%lld\n", Query(x, y, 1, ch[0]));
}
}
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
NYOJ 1068 ST(段树 为段更新+间隔总和)的更多相关文章
- PKU A Simple Problem with Integers (段树更新间隔总和)
意甲冠军:一个典型的段树C,Q问题,有n的数量a[i] (1~n),C, a, b,c在[a,b]加c Q a b 求[a,b]的和. #include<cstdio> #include& ...
- ZOJ1610_Count the Colors(段树/为段更新)
解决报告 意甲冠军: 一定长度8000段染.寻求染色完成后,.. 思路: 区间问题用线段树.成段的更新区间.最后把全部的区间下压到叶子结点,统计叶子结点的颜色. #include <iostre ...
- poj-3468-A Simple Problem with Integers-线段树入门+区间更新
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- POJ 3264-Balanced Lineup(段树:单点更新,间隔查询)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34522 Accepted: 16224 ...
- ccnu-线段树联系-单点更新2-B
B - 单点更新2 Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...
- HDU 1698.Just a Hook-线段树(成段替换、输出总和tree[1])
HDU1698.Just a Hook 这个题是最最基础的成段更新的线段数的题目,直接贴代码吧. 代码: #include<iostream> #include<cstring> ...
- POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)
POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...
- POJ 3225.Help with Intervals-线段树(成段替换、区间异或、简单hash)
POJ3225.Help with Intervals 这个题就是对区间的各种操作,感觉这道题写的一点意思都没有,写到后面都不想写了,而且更神奇的是,自己的编译器连结果都输不出来,但是交上就过了,也是 ...
- POJ 2528.Mayor's posters-线段树(成段替换、离散数据、简单hash)
POJ2528.Mayor's posters 这道题真的是线段数的经典的题目,因为数据很大,直接建树的话肯定不可以,所以需要将数据处理一下,没有接触离散化的时候感觉离散化这个东西相当高级,其实在不知 ...
随机推荐
- 【DRP】删除递归树的操作
正如图呈现的树结构.本文从任意节点删除树形结构.提供解决方案 图中,不包括其他结点的是叶子结点.包括其他结点的是父结点,即不是叶子结点. 一 本文的知识点: (1)递归调用: 由于待删除的结点的层次是 ...
- Java UML描述
开发Java应用程序时,开发者要想有效地利用统一建模语言(UML),必须全面理解UML元素以及这些元素如何映射到Java.本文重点讨论UML类图中的元素. 类图是最常用的UML图,它用于描述系统的 ...
- 【转向Javascript系列】深入理解Generators
随着Javascript语言的发展,ES6规范为我们带来了许多新的内容,其中生成器Generators是一项重要的特性.利用这一特性,我们可以简化迭代器的创建,更加令人兴奋的,是Generators允 ...
- 静默安装MSSQL
原文地址:http://www.cnblogs.com/lyhabc/p/3511788.html 介绍 假如你有50台服务器需要安装SQLSERVER,如果你用下一步下一步的方式,用远程桌面不停切换 ...
- 查询oracle表字段信息
表字段的信息咱们可以称之为元数据,今天有人问怎么把表字段的信息导出来,说实话我还不会用plsql develper把表的结构导出来,像下图所示: 在写数据库设计说明书的时候,想要把这个表格拷贝出来,这 ...
- 天翼玩家wifi,鸡肋or神器?
昨天,天一在成都,一个举行4G体验活动.谁是背着一个婴儿每一翼4G MiFi终奌站.市民可进入用户password自由的直接经验wifi互联网. 天翼随身wifi是什么? 这样的4G MiFi就是天翼 ...
- 利用 C++ 单向链表实现队列
利用C++ 单向链表实现数据结构队列,其实和上一篇基本内容相同,仅仅是插入的时候在链表的尾部插入,取元素都是一样的,都从头部取. #pragma once #include "stdio.h ...
- c++自带倒置数组函数
#include<stdio.h> #include <vector> #include <queue> #include<algorithm> usi ...
- MD5加密,解密
using System; using System.Collections.Generic; using System.Text; using System.Globalization; using ...
- 简述负载均衡&CDN技术(转)
曾经见到知乎上有人问“为什么像facebook这类的网站需要上千个工程师维护?”,下面的回答多种多样,但总结起来就是:一个高性能的web系统需要从无数个角度去考虑他,大到服务器的布局,小到软件中某个文 ...