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,最后问你原来的数 ...
随机推荐
- IEEE 802.15.4协议学习之MAC层
MAC负责建立于网络的同步,支持关联和取消关联.MAC层的安全以及控制物理信道访问机制.信道访问机制主要有以下几种: 1. 有序的物理无线信道访问机制 2. 协调器启动和维 ...
- 对 Sea.js 进行配置(一) seajs.config
可以对 Sea.js 进行配置,让模块编写.开发调试更方便. seajs.config seajs.config(options) 用来进行配置的方法. seajs.config({ // 别名配置 ...
- 基于JQuery+JSP的无数据库无刷新多人在线聊天室
JQuery是一款非常强大的javascript插件,本文就针对Ajax前台和JSP后台来实现一个无刷新的多人在线聊天室,该实现的数据全部存储在服务端内存里,没有用到数据库,本文会提供所有源程序,需要 ...
- Linux操作杂记
centos7修改默认运行等级 查看当前默认运行等级: systemctl get-dafault 修改默认运行等级为5: systemctl set-default graphical.target ...
- asp.net2.0 国际化
公司业务需要在国外开展了, 因此以前的系统要做多国语言了, 从网上搜集了好多资料, 最后选择了一个比较简单的方案 1. 打开vs2005, 新建网站, 首先在配置文件中添加配置: <syste ...
- JQuery在iframe中实现 点击后选中当前栏目的样式
二级或者三级折叠菜单参考http://www.cnblogs.com/qigege/p/5178947.html <script type="text/javascript" ...
- mac OS X下git代码行统计命令
1.统计某人的代码提交量,包括增加,删除 git log --author=-- --until=-- --pretty=tformat: --numstat | awk '{ add += $1 ; ...
- mikrotik/IPSec Dynamic End points Updater.rsc
# IPSec Peer/Policy Updater for Dynamic WAN addresses # ============================================ ...
- JDBC 基本操作: CRUD
Jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可. Statement对象的executeUpdate方法,用于向 ...
- java的JVM机制
1.jre:java运行环境 提供一个JVM和一些基础类库.2.只安装jre以后,机器就具备了运行java程序的条件.但是不具备开发java程序的条件.安装JDK以后,在c:/program file ...