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 这道题真的是线段数的经典的题目,因为数据很大,直接建树的话肯定不可以,所以需要将数据处理一下,没有接触离散化的时候感觉离散化这个东西相当高级,其实在不知 ...
随机推荐
- Android 学习资源[转]
因为一些大家都知道的原因,android很多官方出品的优秀开发资源在国内无法访问. 国内的同行们对此也做出了很多努力,有很多朋友通过各种手段把很多优秀的资源搬运到了国内,为国内android开发者提供 ...
- php中遍历数组的方法
参考网址:http://www.jb51.net/article/29949.htm 这三种方法中效率最高的是使用foreach语句遍历数组.从PHP4开始就引入了foreach结构,是PHP中专门为 ...
- 国内云存储对比: 阿里云、腾讯云、Ucloud、首都在线
阿里云的数据存储<http://www.aliyun.com/product/rds/> RDS — 关系型数据库服务(Relational Database Service,简称RDS) ...
- jstl的小问题
jstl试了半天,终于知道错在哪里了! 这是jsp中的代码 从select中取得user_id:看清楚了 是user_id:小写第一位; <table width="50%" ...
- 共享库方案解决WAS中JAR包冲突
实现步骤: 1. 准备共享库JAR包 commons-httpclient-3.1.jar httpclient-4.3.3.jar httpcore-4.3.2.jar httpmim ...
- 微端 代码project as air 分享
分享 ^_^ 1. 使用 air , as . 2. 微端下载和更新技术 成功上线棋牌游戏.它可用于传统的游戏开发. 地址: http://download.csdn.net/detail/stone ...
- 一般报java.lang.NullPointerException的原因有以下几种
一般报java.lang.NullPointerException的原因有以下几种: ·字符串变量未初始化: ·接口类型的对象没有用具体的类初始化,比如: List lt; 会报错 List lt = ...
- shell加法
echo 1597+1469+1468+2591+1260+1068+1019+993|bc http://bbs.chinaunix.net/thread-161085-1-1.html http: ...
- 从零開始学android<ImageSwitcher图片切换组件.二十六.>
ImageSwitcher组件的主要功能是完毕图片的切换显示,比如用户在进行图片浏览的时候.能够通过button点击一张张的切换显示的图片,并且使用ImageSwitcher组件在每次切换的时候也能够 ...
- android中listview分页载入数据
前段时间做的新浪微博项目一直想实现listview分页载入数据,今天最终实现了,哈哈!感觉挺好的,今天又写了个demo给大家分享下. 首先说下listview的优化方案,这也是面试中常考的题目.优化方 ...