UESTC_秋实大哥与花 2015 UESTC Training for Data Structures<Problem B>
B - 秋实大哥与花
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
秋实大哥是一个儒雅之人,昼听笙歌夜醉眠,若非月下即花前。
所以秋实大哥精心照料了很多花朵。现在所有的花朵排成了一行,每朵花有一个愉悦值。
秋实大哥每天要对着某一段连续的花朵歌唱,然后这些花朵的愉悦值都会增加一个相同的值v(v可能为负)。
同时他想知道每次他唱完歌后这一段连续的花朵的愉悦值总和是多少。
Input
第一行有一个整数n,表示花朵的总数目。
第二行包含n个整数ai,表示第i朵花初始的愉悦值。
第三行包含一个整数m,表示秋实大哥唱了m天的歌。
接下来m行,每行包含三个整数l r v,表示秋实大哥对着[l,r]这个区间内的花朵歌唱,每朵花的愉悦值增加了v。
1≤n,m,ai,|v|≤100000,1≤l≤r≤n。
Output
输出共m行,第i行表示秋实大哥完成第i天的歌唱后,那一段花朵的愉悦值总和。
Sample input and output
| Sample Input | Sample Output |
|---|---|
3 |
2 |
解题报告:
没啥好说的,线段树模板基础题。。唯一需要注意的就是long long了.
当然我是使用的树状数组:
题目中操作属于区间更新 - 区间查询<查询点也是区间嘛>类型,可使用树状数组来实现.
令C[i]为从1至i号花的共同更新之和.
首先考虑更新,add[l,r,v] → c[r] += v , c[l-1] -=v;
Sum[x] = 从 1 至 x 号花的value之和.
Sum[x] = c[1]*1 + c[2]*2 + c[3] * 3 ..... + c[x] * x + (c[x+1] + .... C[n] ) *x;
= (segema)f(x) + (segema)g(x) * x;
= 两个树状数组,一个维护 c[i]*i ,一个维护c[i]
之后考虑区间查询:
Query(l , r)
= sum[r] - sum[l-1]
#include <iostream>
#include <cstring>
typedef long long ll;
using namespace std;
const int maxn = 1e5 + ;
ll n;
ll f[maxn],g[maxn]; inline ll lowbit(ll idx)
{
return idx & (-idx);
} void updataf(ll idx,ll res)
{
if (!idx)
return;
while(idx <= n)
{
f[idx] += res;
idx += lowbit(idx);
}
} void updatag(ll idx,ll res)
{
if (!idx)
return;
while(idx <= n)
{
g[idx] += res;
idx += lowbit(idx);
}
} ll sumf(ll idx)
{
ll res = ;
while(idx > )
{
res += f[idx];
idx -= lowbit(idx);
}
return res;
} ll sumg(ll idx)
{
ll res = ;
while(idx > )
{
res += g[idx];
idx -= lowbit(idx);
}
return res;
} int main(int argc,char *argv[])
{
ll m;
scanf("%lld%lld",&n,&m);
memset(f,,sizeof(f));
memset(g,,sizeof(g));
for(int j = ; j <= n ; ++ j)
{
ll v;
scanf("%lld",&v);
updataf(j,v*j);
updataf(j-,-v*(j-));
updatag(j,v);
updatag(j-,-v);
}
while(m--)
{
ll i,j,v;
scanf("%lld%lld%lld",&i,&j,&v);
updataf(j,v*j);
updataf(i-,-v*(i-));
updatag(j,v);
updatag(i-,-v);
i--;
ll res1 = sumf(i) + i*(sumg(n)-sumg(i));
ll res2 = sumf(j) + j*(sumg(n)-sumg(j));
printf("%lld\n",res2 - res1);
}
return ;
}
UESTC_秋实大哥与花 2015 UESTC Training for Data Structures<Problem B>的更多相关文章
- UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>
N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>
H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>
G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥与家 2015 UESTC Training for Data Structures<Problem E>
E - 秋实大哥与家 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>
D - 秋实大哥与战争 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>
C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥与小朋友 2015 UESTC Training for Data Structures<Problem A>
A - 秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥掰手指 2015 UESTC Training for Dynamic Programming<Problem B>
B - 秋实大哥掰手指 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 2048/1024KB (Java/Others) Submit ...
- UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>
M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
随机推荐
- 数据库中的索引Index
索引就像一本书的目录,而书中的索引是对一个词语的列表,其中注明了包含各个词的页码.数据库中的索引 是某一个表中一列或者若干列值的集合和相应的只想表中物理标识这些值的数据页的逻辑指针清单. 索引的作用: ...
- Linux 块设备驱动 (二)
linux下Ramdisk驱动 1 什么是Ramdisk Ramdisk是一种模拟磁盘,其数据实际上是存储在RAM中,它使用一部分内存空间来模拟出一个磁盘设备,并以块设备的方式来组织和访问这片内存.对 ...
- Block内的强引用
众所周知,当某个对象持有着一个Block的时候,如果在Block内部使用强引用反过来持有这个对象,就会导致引用循环.为了避免引用循环,可以使用__weak修饰符,苹果的官方文档在用代码演示__weak ...
- [RxJS] Reactive Programming - What is RxJS?
First thing need to understand is, Reactive programming is dealing with the event stream. Event stre ...
- BOOST 线程完全攻略 - 扩展 - 可被关闭的线程类
本文假设读者已经基本了解boost线程库的使用方法. boost是个开源工程,线程这一块也在不断完善之中,到现在这个阶段,boost::thread仅仅实现了一个完美的技术框架,但是读者在实际使用中会 ...
- [CSAPP笔记][第一章计算机系统漫游]
计算机系统漫游 我们通过追踪hello程序的生命周期来开始对系统的学习—–从它被程序员创建,到系统上运行,输出简单的消息,然后终止.我们沿着这个程序的生命周期,简要介绍一些逐步出现的概念,专业术语和组 ...
- NuGet学习笔记(3)——搭建属于自己的NuGet服务器(转)
在上一篇NuGet学习笔记(2) 使用图形化界面打包自己的类库 中讲解了如何打包自己的类库,接下来进行最重要的一步,从零开始搭建属于自己的NuGet服务器,诚然园子里及其它很多地方已经有完全写好的Nu ...
- Query获取值常用
Query获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code...}); //为Sel ...
- Android Path
外置SDCard(TF卡) 1. SDCard的挂载路径(根据系统不同的ROM挂载的路径不同,以下列举几个是从旧系统到新系统的表现形式) /sdcard/external_sd /mnt/extSdC ...
- JS判断RadioButtonList是否有选中项
提交表单之前对RadioButtonList控件的选中项进行判断: 方法一: <script type="text/javascript"> function chec ...