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 自己实现 NavigationView [Design Support Library(1)]
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46405409: 本文出自:[张鸿洋的博客] 一.概述 Google I/O 2 ...
- HDU5015 233 Matrix(矩阵高速幂)
HDU5015 233 Matrix(矩阵高速幂) 题目链接 题目大意: 给出n∗m矩阵,给出第一行a01, a02, a03 ...a0m (各自是233, 2333, 23333...), 再给定 ...
- 小试Office OneNote 2010的图片文字识别功能(OCR)
原文:小试Office OneNote 2010的图片文字识别功能(OCR) 自Office 2003以来,OneNote就成为了我电脑中必不可少的软件,它集各种创新功能于一身,可方便的记录下各种类型 ...
- mmc生产任务分配问题
mmc生产任务分配问题,本题目简单.
- C语言cgi(1)
1Columbia Universitycs3157 – Advanced ProgrammingSummer 2014, Lab #2, 60ish pointsJune 9, 2014Follow ...
- POJ1502(Dijkstra)
MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5538 Accepted: 3451 题目链 ...
- POJ1300(欧拉回路)
Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2139 Accepted: 858 Descripti ...
- VirtualBox安装ubuntu14.04和文件共享
因为机器的VMware使用很卡,占用更多的内存,所以我想,以取代VirtualBox.已安装ubuntu14.04使用与VMware在相同的. VirtualBox下载链接:https://www.v ...
- 推荐15个月 Node.js 开发工具
Node.js 越来月流行.这个基于 Google V8 引擎建立的平台, 用于方便地搭建响应速度快.易于扩展的网络应用.在本文中.我们列出了2015年最佳的15个 Node.js 开发工具.这些工具 ...
- 使用LabVIEWPC的制备
使用LabVIEWPC的制备 1.下载.安装LabVIEW 如今,互联网搜索,你可以搜索出一大推LabVIEW下载并安装破解教程.因此,这里没有具体描述的.请自行百度~~.另外.提醒一下,下载LabV ...