ST

时间限制:1000 ms  |  内存限制:65535 KB
难度:1
描写叙述

“麻雀”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(段树 为段更新+间隔总和)的更多相关文章

  1. 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& ...

  2. ZOJ1610_Count the Colors(段树/为段更新)

    解决报告 意甲冠军: 一定长度8000段染.寻求染色完成后,.. 思路: 区间问题用线段树.成段的更新区间.最后把全部的区间下压到叶子结点,统计叶子结点的颜色. #include <iostre ...

  3. 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 ...

  4. POJ 3264-Balanced Lineup(段树:单点更新,间隔查询)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34522   Accepted: 16224 ...

  5. ccnu-线段树联系-单点更新2-B

    B - 单点更新2 Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...

  6. HDU 1698.Just a Hook-线段树(成段替换、输出总和tree[1])

    HDU1698.Just a Hook 这个题是最最基础的成段更新的线段数的题目,直接贴代码吧. 代码: #include<iostream> #include<cstring> ...

  7. POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)

    POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...

  8. POJ 3225.Help with Intervals-线段树(成段替换、区间异或、简单hash)

    POJ3225.Help with Intervals 这个题就是对区间的各种操作,感觉这道题写的一点意思都没有,写到后面都不想写了,而且更神奇的是,自己的编译器连结果都输不出来,但是交上就过了,也是 ...

  9. POJ 2528.Mayor's posters-线段树(成段替换、离散数据、简单hash)

    POJ2528.Mayor's posters 这道题真的是线段数的经典的题目,因为数据很大,直接建树的话肯定不可以,所以需要将数据处理一下,没有接触离散化的时候感觉离散化这个东西相当高级,其实在不知 ...

随机推荐

  1. 开启本地MySql数据库远程连接

    解决MySQL不允许从远程访问的方法 开启 MySQL 的远程登陆帐号有两大步: 1.确定服务器上的防火墙没有阻止 3306 端口. MySQL 默认的端口是 3306 ,需要确定防火墙没有阻止 33 ...

  2. cocos2dx 3.1从零学习(六)——CocosStudio(VS2013project导入及环境设置)

    导入libCocosStudio.libExtensions.libGUI 新建的project例如以下图: 加入现有项目 右键解决方式.例如以下操作: watermark/2/text/aHR0cD ...

  3. 道破Redis的VM

    原创文章是freas_1990.转载请注明出处:http://blog.csdn.net/freas_1990/article/details/42052813 Redis唯一的那个key的value ...

  4. phpcms v9框架的目录结构分析

    phpcms v9框架的目录结构分析:      了解v9框架的目录结构,有助于帮助我们快速建立起对v9框架的一个整体认识 打开"mycms"项目,有如下文件和目录      使用 ...

  5. CodeForces 52C Circular RMQ(间隔周期段树,间隔更新,间隔总和)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://codeforces.com/problemset/problem/52/C You are g ...

  6. MySQL 批量Dll操作(转)

    概述 本章节介绍使用游标来批量进行表操作,包括批量添加索引.批量添加字段等.如果对存储过程.变量定义.预处理还不是很熟悉先阅读我前面写过的关于这三个概念的文章,只有先了解了这三个概念才能更好的理解这篇 ...

  7. Linux 编程学习笔记----ANSI C 文件I/O管理

    转载请注明出处:http://blog.csdn.net/suool/article/details/38129201 问题引入 文件的种类 依据数据存储的方式不同,能够将文件分为文本文件和二进制文件 ...

  8. 前端构建工具gulp

    前端构建工具gulp使用   前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gul ...

  9. GRUB2配置详解:默认启动项,超时时间,隐藏引导菜单,配置文件详解,图形化配置

    配置文件详解: /etc/default/grub # 设定默认启动项,推荐使用数字 GRUB_DEFAULT=0 # 注释掉下面这行将会显示引导菜单 #GRUB_HIDDEN_TIMEOUT=0 # ...

  10. [Windows Phone] 实作不同的地图显示模式

    原文:[Windows Phone] 实作不同的地图显示模式 前言 本文章主要示范如何让地图有不同的模式产生,例如平面图.地形图.鸟瞰图.鸟瞰图含街道等. 这部分主要是调整 Map.Cartograp ...