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. gcc的bug? c++模板类中友元函数的訪问权限问题

    原文地址:http://stackoverflow.com/q/23171337/3309790 在c++中,模板类中能够直接定义一个友元函数.该函数拥有訪问该模板类非public成员的权限. 比方: ...

  2. TSL230选型

    tsl230是一种可以直接将光强转化成频率值的器件.详细原理就不介绍了,数据手冊里写的都非常清楚,230系列包括非常多种,主要为下面四类:TSL230,TSL230A,TSL230B系列:TSL230 ...

  3. WebApi异常

    WebApi异常处理解决方案   前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定 ...

  4. poj3126解题报告

    题意:简单的说就是:有一个人门牌号是一个四位数的整数,并且还是一个素数,现在他想要换成另外一个四位数且是素数的门牌号,而且,每次都只能更换这个四位数的一个位置的数 ,也就是每换一次都只改变一个数字,而 ...

  5. iScreenLocker 3.1.8 安卓锁屏通知--苹果一样的体验

    *软件介绍: 苹果锁屏通知(iScreenLocker)是一款android上ios风格的锁屏软件.它颠覆安智通知设计,将原来状态栏的通知搬到锁屏界面上来,能够在桌面轻松收发短信,微博,微信等消息.它 ...

  6. 【JAVA得知】struts2 于 Actionsupport 任务

    尊重原创:http://xumiao900.iteye.com/blog/469760     Action 跟 Actionsupport 的差别      当我们在写action的时候,能够实现A ...

  7. 【转】C# String.Format数字格式化输出各种转换{0:N2} {0:D2} {0:C2}...

    ; //格式为sring输出 // Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); // Label2.Text = & ...

  8. ACdream: Sum

    Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticN ...

  9. HDU1176_免费馅饼【号码塔】

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  10. 【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(二)

    4 三维 4.1 共享三维场景         用户能够将ArcScene文档导出为3D web场景,能够被加载到ArcGIS Online.Portal或本地Web服务器上并进行分享.这样,用户可以 ...