CF 295A Greg and Array (两次建树,区间更新,单点查询)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define lson rt<<1,L,mid
#define rson rt<<1|1,mid+1,R
/*
题意:给出原始序列a[1],a[2],...a[n]
给出m个操作方式 l r d,把a[l],...a[r]都加上d
然后给出k个操作 x y 执行第x到第y个操作 思路:如果直接执行k个对应的x~y操作,会超时的。
所以,我们需要统计一下对于m个操作,每个操作需要统计多少次,然后每个操作执行一次即可。
这样就先建立一颗树,查询k次[x,y],最后再单点查询,得到第i个操作需要执行的次数cnt[i]。
然后在建立一次(因为操作都一样,所以用同一棵树即可),这次分别对m个操作进行更新,
更新值为cnt[i]*di,再单点更新得出最后的a[i]。
*/
using namespace std;
const int maxn=;
int n,m,k;
long long a[maxn]; //原始序列
long long cnt[maxn]; //cnt[i]表示第i个操作方式需要执行的次数
struct Node{
long long add;
bool lazy;
}tree[maxn<<]; struct Operation{
int l,r;
long long d;
}op[maxn]; void build(int rt,int L,int R){
tree[rt].add=;
tree[rt].lazy=false;
if(L==R)
return;
int mid=(L+R)>>;
build(lson);
build(rson);
} void pushDown(int rt){
if(tree[rt].lazy){
tree[rt<<].add+=tree[rt].add;
tree[rt<<|].add+=tree[rt].add;
tree[rt<<].lazy=tree[rt<<|].lazy=true;
tree[rt].add=;
tree[rt].lazy=false;
}
}
void update(int rt,int L,int R,int l,int r,long long val){
if(l<=L&&R<=r){
tree[rt].add+=val;
tree[rt].lazy=true;
return;
}
pushDown(rt);
int mid=(L+R)>>;
if(l<=mid)
update(lson,l,r,val);
if(r>mid)
update(rson,l,r,val);
} //单点查询cnt
void query1(int rt,int L,int R){
if(L==R){
cnt[L]+=tree[rt].add;
return;
}
pushDown(rt);
int mid=(L+R)>>;
query1(lson);
query1(rson);
}
//单点查询a
void query2(int rt,int L,int R){
if(L==R){
a[L]+=tree[rt].add;
return;
}
pushDown(rt);
int mid=(L+R)>>;
query2(lson);
query2(rson);
}
int main()
{
int x,y;
scanf("%d%d%d",&n,&m,&k); for(int i=;i<=n;i++){
scanf("%I64d",&a[i]);
}
for(int i=;i<=m;i++){
scanf("%d%d%I64d",&op[i].l,&op[i].r,&op[i].d);
}
//先对m个操作方式建树,统计每个操作方式需要执行的次数
build(,,m);
for(int i=;i<=k;i++){
scanf("%d%d",&x,&y);
update(,,m,x,y,); }
memset(cnt,,sizeof(cnt));
query1(,,m); //再对n个数建树,更新所增加的值
build(,,n);
for(int i=;i<=m;i++){
update(,,n,op[i].l,op[i].r,op[i].d*cnt[i]); //对每个操作只要查询一次即可
}
query2(,,n);
flag=true;
for(int i=;i<=n;i++){
if(flag){
printf("%I64d",a[i]);
flag=false;
}
else
printf(" %I64d",a[i]);
}
printf("\n");
return ;
}
CF 295A Greg and Array (两次建树,区间更新,单点查询)的更多相关文章
- HDU - 3974 Assign the task (DFS建树+区间覆盖+单点查询)
题意:一共有n名员工, n-1条关系, 每次给一个人分配任务的时候,(如果他有)给他的所有下属也分配这个任务, 下属的下属也算自己的下属, 每次查询的时候都输出这个人最新的任务(如果他有), 没有就输 ...
- HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)
HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...
- CodeForces Round #179 (295A) - Greg and Array 一个线段树做两次用
线段树的区间更新与区间求和...一颗这样的线段树用两次... 先扫描1~k...用线段树统计出每个操作执行的次数... 那么每个操作就变成了 op. l , op.r , op.c= times* ...
- Codeforces 295A Greg and Array
传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...
- CF#52 C Circular RMQ (线段树区间更新)
Description You are given circular array a0, a1, ..., an - 1. There are two types of operations with ...
- Codeforces 482B Interesting Array(线段树区间更新)
题目链接 Interesting Array 区间更新.然后对于每一个约数重新求一遍区间的&值,不符合就跳出. #include <bits/stdc++.h> using nam ...
- CodeForces Round #179 (295A) - Greg and Array
题目链接:http://codeforces.com/problemset/problem/295/A 我的做法,两次线段树 #include <cstdio> #include < ...
- [CF 295A]Grag and Array[差分数列]
题意: 有数列a[ ]; 操作op[ ] = { l, r, d }; 询问q[ ] = { x, y }; 操作表示对a的[ l, r ] 区间上每个数增加d; 询问表示执行[ x, y ]之间的o ...
- codeforces 482B. Interesting Array【线段树区间更新】
题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val. 就是区间l---r上的与的值为val,最后问你原来的数 ...
随机推荐
- Codevs 1205 单词翻转
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题解 题目描述 Description 给出一个英语句子,希望你把句子里的单词顺序都翻转过来 输入 ...
- void指针
指针有两个属性:指向变量/对象的地址 和长度 但是指针只存储地址,长度则取决于指针的类型 编译器根据指针的类型从指针指向的地址向后寻址 指针类型不同则寻址范围也不同,比如: int*从指定地址向后寻找 ...
- JavaScript 中undefined,null,NaN的区别
1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型.var a1;var a2 = true;va ...
- Viewport Resizer下载 谷歌前端自适应开发工具
原文链接:http://www.phpbiji.cn/article/index/id/107/cid/6.html Viewport Resizer下载 谷歌前端自适应开发工具 在前端开发过程中,随 ...
- 13.mariadb-rhce考试解题思路
1.安装mariadb ①yum install -y mariadb mariadb-server 或者 yum groupinstall -y mariadb 2.备份和还原数据库 ①备份:mys ...
- <a href="onclick="javascript:goSearch(this)" class="click" name="Java">Java</a>为什么a标签的父节点获取不到
<script> function goSearch(event) { //var select = $('#keyInput').val($(event).attr("name ...
- Mysql数据库常用的命令 数据备份 恢复 远程
远程数据库 格式: mysql -h主机地址 -u用户名 -p用户密码数据库 mysql -h 42.51.150.68 -u yang -p discuz mysql设置密码 mysql>us ...
- 有关Mysql连接问题
问题一——Mysql number Error 2003 MySQL连接错误.错误代码10061.10061一般是Mysql服务没启动.或Mysql服务器无法连接 . 在程序栏找到Mysql\Mysq ...
- 分享:mysql 随机查询数据
在mysql中查询5条不重复的数据,使用以下: 1 SELECT * FROM `table` ORDER BY RAND() LIMIT 5 就可以了.但是真正测试一下才发现这样效率非常低.一个1 ...
- static方法不能直接访问类内的非static变量和不能调用this,super语句分析
大家都知道在static方法中,不能访问类内非static成员变量和方法.可是原因是什么呢? 这首先要从static方法的特性说起.static方法,即类的静态成员经常被称为"成员变量&qu ...