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,最后问你原来的数 ...
随机推荐
- 浅析 GRUB 如何加载 linux kernel
前言 对于 GRUB 的加载流程,网上绝大部分都是写对 menu.lst, grub.cfg 这些 GRUB 配置文件的编写流程,就像是写脚本语言一样,用些关键字就能让 PC机能正确启动桌面 Linu ...
- Factory_Method
class Product { public: virtual ~Product() {} ; }; class ProductA : public Product { public: Product ...
- python学习第四天第一部分
1.字典的特性:无序.去重.查询速度快.比list占用内存多. 2.字典查询速度快的原因:因为他是哈希类型的. 3.什么是(hash)哈希? hash把任意长度的二进制映射为较短的固定长度的二进制,这 ...
- jvm 数据区划分学习
Java virtual machine 运行时数据存储区域划分 2015年1月25日 19:15 Pc 寄存器 Each Java Virtual Machine thread has its o ...
- PowerPoint Office Mix 插件
一个内嵌在PowerPoint里的一个教学工具,可以以PPT为核心录制视频. 点下面链接了解并安装 https://mix.office.com/ 首先这货是免费,当然是基于PowerPoint的基础 ...
- hdu 5769 Substring 后缀数组 + KMP
http://acm.hdu.edu.cn/showproblem.php?pid=5769 题意:在S串中找出X串出现的不同子串的数目? 其中1 <= |S| < $10^5$ 官方题解 ...
- parameter和argument的区别
根据网上一些资料,对parameter和argument的区别,做如下的简单说明.1. parameter是指函数定义中参数,而argument指的是函数调用时的实际参数.2. 简略描述为:param ...
- useradd命令详解
功能说明:建立用户帐号.语 法:useradd [-mMnr][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数>][-g ...
- ASP.NET MVC NonActionAttribute使用说明
默认情况下,MVC 框架将 controller 类的所有公共方法都视为操作方法. 如果您的 controller 类包含公共方法,并且您不希望它成为操作方法,则必须用 NonActionAttrib ...
- VB数据库经典实例总结(二)
大家先看一张似图非图的图. 我们先称它为“过程”也许有不对的地方,在我学数据库到这个阶段.到这个刚刚接触.初生牛犊不怕虎的阶段对它的理解是这样的.所有的都是这个过程.只是在这中间掺杂了一些知识点(我们 ...