2015 UESTC 数据结构专题B题 秋实大哥与花 线段树 区间加,区间查询和
B - 秋实大哥与花
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://acm.uestc.edu.cn/#/contest/show/59
Description
秋实大哥是一个儒雅之人,昼听笙歌夜醉眠,若非月下即花前。
所以秋实大哥精心照料了很多花朵。现在所有的花朵排成了一行,每朵花有一个愉悦值。
秋实大哥每天要对着某一段连续的花朵歌唱,然后这些花朵的愉悦值都会增加一个相同的值v(v可能为负)。
同时他想知道每次他唱完歌后这一段连续的花朵的愉悦值总和是多少。
Input
第二行包含n个整数ai,表示第i朵花初始的愉悦值。
第三行包含一个整数m,表示秋实大哥唱了m天的歌。
接下来m行,每行包含三个整数l r v,表示秋实大哥对着[l,r]这个区间内的花朵歌唱,每朵花的愉悦值增加了v。
1≤n,m,ai,|v|≤100000,1≤l≤r≤n。
Output
Sample Input
0 0 0
3
1 2 1
1 2 -1
1 3 1
Sample Output
0
3
HINT
题意
题解:
啊,裸题嘛,随便搞搞就好了
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define MID(a,b) (a+((b-a)>>1))
const int N=;
typedef long long LL;
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/* int buf[10];
inline void write(int i) {
int p = 0;if(i == 0) p++;
else while(i) {buf[p++] = i % 10;i /= 10;}
for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);
printf("\n");
}
*/
//**************************************************************************************
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} struct node
{
int lft,rht;
LL sum,add;
int mid(){return MID(lft,rht);}
void fun(LL tmp)
{
add+=tmp;
sum+=(rht-lft+)*tmp;
}
}; int y[N]; struct Segtree
{
node tree[N*];
void relax(int ind)
{
if(tree[ind].add)
{
tree[LL(ind)].fun(tree[ind].add);
tree[RR(ind)].fun(tree[ind].add);
tree[ind].add=;
}
}
void build(int lft,int rht,int ind)
{
tree[ind].lft=lft; tree[ind].rht=rht;
tree[ind].sum=; tree[ind].add=;
if(lft==rht) tree[ind].sum=y[lft];
else
{
int mid=tree[ind].mid();
build(lft,mid,LL(ind));
build(mid+,rht,RR(ind));
tree[ind].sum=tree[LL(ind)].sum+tree[RR(ind)].sum;
}
}
void updata(int st,int ed,int ind,int add)
{
int lft=tree[ind].lft,rht=tree[ind].rht;
if(st<=lft&&rht<=ed) tree[ind].fun(add);
else
{
relax(ind);
int mid=tree[ind].mid();
if(st<=mid) updata(st,ed,LL(ind),add);
if(ed> mid) updata(st,ed,RR(ind),add);
tree[ind].sum=tree[LL(ind)].sum+tree[RR(ind)].sum;
}
}
LL query(int st,int ed,int ind)
{
int lft=tree[ind].lft,rht=tree[ind].rht;
if(st<=lft&&rht<=ed) return tree[ind].sum;
else
{
relax(ind);
int mid=tree[ind].mid();
LL sum1=,sum2=;
if(st<=mid) sum1=query(st,ed,LL(ind));
if(ed> mid) sum2=query(st,ed,RR(ind));
return sum1+sum2;
}
}
}seg;
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
char str[];
int a,b,c;
for(int i=;i<=n;i++) scanf("%d",&y[i]);
seg.build(,n,);
scanf("%d",&m);
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
seg.updata(a,b,,c);
printf("%lld\n",seg.query(a,b,));
}
}
return ;
}
2015 UESTC 数据结构专题B题 秋实大哥与花 线段树 区间加,区间查询和的更多相关文章
- 2015 UESTC 数据结构专题A题 秋实大哥与小朋友 线段树 区间更新,单点查询,离散化
秋实大哥与小朋友 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Desc ...
- 2015 UESTC 数据结构专题E题 秋实大哥与家 线段树扫描线求矩形面积交
E - 秋实大哥与家 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...
- 2015 UESTC 数据结构专题C题 秋实大哥与快餐店 字典树
C - 秋实大哥与快餐店 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 ...
- CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询
链接: I - 秋实大哥与花 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit ...
- 2015 UESTC 数据结构专题N题 秋实大哥搞算数 表达式求值/栈
秋实大哥搞算数 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1074 Des ...
- 2015 UESTC 数据结构专题H题 秋实大哥打游戏 带权并查集
秋实大哥打游戏 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...
- 2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈
秋实大哥去打工 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...
- 2015 UESTC 数据结构专题D题 秋实大哥与战争 SET的妙用
D - 秋实大哥与战争 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 D ...
- 2015 UESTC 数据结构专题D题 秋实大哥与战争 变化版本的线段树,合并区间,单点查询
D - 秋实大哥与战争 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 D ...
随机推荐
- Perl6多线程1 Thread : new / run
先看一个小例子: ) { #默认参数 say $name; } sub B(:name($name)) { #默认参数为 any say $name; } A(); A(); B(); B(name ...
- SPI最大传输速率【转】
转自:https://www.silabs.com/community/mcu/8-bit/knowledge-base.entry.html/2017/01/13/spi_-asc0 问题 SPI作 ...
- 用户空间与内核空间数据交换的方式(9)------netlink【转】
转自:http://www.cnblogs.com/hoys/archive/2011/04/10/2011722.html Netlink 是一种特殊的 socket,它是 Linux 所特有的,类 ...
- for 、forEach 、 forof、 forin遍历对比
一.遍历内容的异同 1.for 和 for...in 是针对数组下标的遍历 2.forEach 及 for...of 遍历的是数组中的元素 二.对非数字下标的处理 由于array在js中也是对象中的一 ...
- Linux /etc/cron.d作用(转自 定时任务crontab cron.d)
原文链接:http://huangfuligang.blog.51cto.com/9181639/1608549 一.cron.d增加定时任务 当我们要增加全局性的计划任务时,一种方式是直接修改/et ...
- php cache类代码(php数据缓存类)
如果访问量大的话会给数据库造成很大的负担,所以对于变化不经常的内容要做好php 数据cache(缓存)是十分必要的,我做了一个简单的php“文件缓存”的类,希望对大家有所帮助. 思路是这样的: 对于一 ...
- log优化
isLoggable(Level level) 包含计算的日志记录用isLoggable判断下. debug info warn error ,一般记录error, 但是其他里面的计算还是 ...
- POJ 3253 Fence Repair(哈夫曼编码)
题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...
- Codeforces 822C Hacker, pack your bags!(思维)
题目大意:给你n个旅券,上面有开始时间l,结束时间r,和花费cost,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少. 解题思路:看了大佬的才会写!其实和之前Codeforces 776 ...
- 关于Windows中的硬链接
https://zhidao.baidu.com/question/748233720330351012.html linux中使用硬链接 ln a.txt b.txt 查看硬链接 ls -il 关于 ...