Codeforces 295A Greg and Array
Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.
Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.
Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array.
Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).
Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m).
The numbers in the lines are separated by single spaces.
On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
9 18 17
1 1 1
1
1 1 1
1 1
2
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
5 18 31 20 分析
线段树。
离线预处理所有Query,统计各operation的次数。
区间Insert,注意使用lazy-tag,点Query答案。
写法
要维护两棵线段树,可并做一棵。 这是我第一次写的,TLE on test 24
#include<bits/stdc++.h>
using namespace std;
const int MAX_N=1e5+;
typedef long long ll; struct op{
int l, r;
ll v;
}o[MAX_N]; ll cnt[MAX_N], a[MAX_N]; struct Node{
int l, r;
ll v;
int mid(){return (l+r)>>;}
}T[MAX_N<<]; void Build(int id, int l, int r){
T[id].l=l, T[id].r=r, T[id].v=;
if(l==r) return;
int mid=T[id].mid();
Build(id<<, l, mid);
Build(id<<|, mid+, r);
}
void Insert(int id, int l, int r, ll v){
Node &now=T[id];
if(now.l>=l&&now.r<=r){
if(~now.v) now.v+=v;
else{
Insert(id<<, l, r, v);
Insert(id<<|, l, r, v);
}
}
else{
Node &lch=T[id<<], &rch=T[id<<|];
if(~now.v) lch.v=rch.v=now.v, now.v=-; //ERROR-PRONE
int mid=now.mid();
if(l<=mid) Insert(id<<, l, r, v);
if(r>mid) Insert(id<<|, l, r, v);
if(lch.v==rch.v) now.v=lch.v;
}
} void Qurery(int id, ll *a){
Node &now=T[id];
if(~now.v)
for(int i=now.l; i<=now.r; i++) a[i]+=now.v;
else{
Qurery(id<<, a);
Qurery(id<<|, a);
}
} int main(){
//freopen("in", "r", stdin);
int N, M, K;
scanf("%d%d%d", &N, &M, &K);
for(int i=; i<=N; i++) scanf("%lld", a+i);
for(int i=; i<=M; i++)
scanf("%d%d%lld", &o[i].l, &o[i].r, &o[i].v);
Build(, , M);
int l, r;
while(K--){
scanf("%d%d", &l, &r);
Insert(, l, r, );
}
Qurery(, cnt);
Build(, , N);
for(int i=; i<=M; i++)
if(cnt[i])
Insert(, o[i].l, o[i].r, o[i].v*cnt[i]);
Qurery(, a);
for(int i=; i<=N; i++)
printf("%lld ", a[i]);
puts("");
return ;
}
上面的代码没有lazy-tag或者说我设置的lazy-tag没起到相应的作用。我的考虑是设置一个tag,最后求答案时可不必细分到每个叶子节点,但是这种优化对降低Insert的复杂度没有太大帮助,而Insert是最耗时的,因而总的复杂度还是没降下来。
AC的姿势
#include<bits/stdc++.h>
using namespace std;
const int MAX_N=1e5+;
typedef long long ll; struct op{
int l, r, v;
}o[MAX_N]; ll cnt[MAX_N], a[MAX_N]; struct Node{
int l, r;
ll v;
int mid(){return (l+r)>>;}
}T[MAX_N<<]; void Build(int id, int l, int r){
T[id].l=l, T[id].r=r, T[id].v=;
if(l==r) return;
int mid=T[id].mid();
Build(id<<, l, mid);
Build(id<<|, mid+, r);
}
void Insert(int id, int l, int r, ll v){
Node &now=T[id];
if(now.l>=l&&now.r<=r) now.v+=v;
else{
Node &lch=T[id<<], &rch=T[id<<|];
if(now.v)
lch.v+=now.v, rch.v+=now.v, now.v=;
int mid=now.mid();
if(l<=mid) Insert(id<<, l, r, v);
if(r>mid) Insert(id<<|, l, r, v);
}
} void Qurery(int id, ll *a){
Node &now=T[id];
if(now.l==now.r) a[now.l]+=now.v;
else{
Node &lch=T[id<<], &rch=T[id<<|];
if(now.v)
lch.v+=now.v, rch.v+=now.v;
Qurery(id<<, a);
Qurery(id<<|, a);
}
} int main(){
//freopen("in", "r", stdin);
int N, M, K;
scanf("%d%d%d", &N, &M, &K);
for(int i=; i<=N; i++) scanf("%lld", a+i);
for(int i=; i<=M; i++)
scanf("%d%d%lld", &o[i].l, &o[i].r, &o[i].v);
Build(, , M);
int l, r;
while(K--){
scanf("%d%d", &l, &r);
Insert(, l, r, );
}
Qurery(, cnt);
Build(, , N);
for(int i=; i<=M; i++)
if(cnt[i]&&o[i].v)
Insert(, o[i].l, o[i].r, o[i].v*cnt[i]);
Qurery(, a);
for(int i=; i<=N; i++)
printf("%lld ", a[i]);
puts("");
return ;
}
Codeforces 295A Greg and Array的更多相关文章
- CodeForces Round #179 (295A) - Greg and Array
题目链接:http://codeforces.com/problemset/problem/295/A 我的做法,两次线段树 #include <cstdio> #include < ...
- CodeForces Round #179 (295A) - Greg and Array 一个线段树做两次用
线段树的区间更新与区间求和...一颗这样的线段树用两次... 先扫描1~k...用线段树统计出每个操作执行的次数... 那么每个操作就变成了 op. l , op.r , op.c= times* ...
- CF 295A Greg and Array (两次建树,区间更新,单点查询)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- Codeforces 296C Greg and Array
数据结构题.个人认为是比较好的数据结构题.题意:给定一个长度为n的数组a,然后给定m个操作序列,每个操作:l, r, x将区间[l, r]内的元素都增加a,然后有k个查询,查询形式是对于操作序列x,y ...
- Codeforces Round #179 (Div. 1) A. Greg and Array 离线区间修改
A. Greg and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/295/pro ...
- Greg and Array CodeForces 296C 差分数组
Greg and Array CodeForces 296C 差分数组 题意 是说有n个数,m种操作,这m种操作就是让一段区间内的数增加或则减少,然后有k种控制,这k种控制是说让m种操作中的一段区域内 ...
- Codeforces 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
- Codeforces Round #504 D. Array Restoration
Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
随机推荐
- Android SQLite (三 ) 全面详解(一)
官网 SQLite是一款轻型的数据库,是关系型数据库(RDBMS)管理系统,它包含在一个相对小的C库中.目前在很多嵌入式产品中使用了它,它占用资源非常 的低,在嵌入式设备中,可能只需要几百K的内存就够 ...
- python-基础案例
范例一: 练习:元素分类 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值 ...
- 分布式环境下Unique ID生成方法
ID即标示符,在某个搜索域内能唯一标示其中某个对象.在关系型数据库中每个表都需要定义一个主键来唯一标示一条记录.为了方便一般都会使用一个auto_increment属性的整形数做为ID.因为数据库本身 ...
- import javax.servlet.FilterConfig;
具体的使用方法你可以在google上搜索 “filter 过滤器”,FilterConfig可以获取部署描述符文件(web.xml)中分配的过滤器初始化参数.针对你的问题回答,结果就是说FilterC ...
- 研:手势与眼动相结合-手势SDK的整合
Leap提供了SDK.但是整合有很多的问题,写博客记录一下: 写一个类:SampleListener.cpp以及头文件SampleListener.h. 这里主要碰到的问题是找不到以及冲突问题: 这里 ...
- 命令行参数(argc, argv)
每个C语言程序都必须有一个称为main()的函数,作为程序启动的起点.当执行程序时,命令行参数(command-line argument)(由shell逐一解析)通过两个入参提供给main()函数. ...
- python实现简易数据库之一——存储和索引建立
最近没事做了一个数据库project,要求实现一个简单的数据库,能满足几个特定的查询,这里主要介绍一下我们的实现过程,代码放在过ithub,可参看这里.都说python的运行速度很慢,但因为时间比较急 ...
- threejs构建web三维视图入门教程
本文是一篇简单的webGL+threejs构建web三维视图的入门教程,你可以了解到利用threejs创建简单的三维图形,并且控制图形运动.若有不足,欢迎指出. 本文使用的框架是three.js gi ...
- Cocos2d-x中使用OpenGL ES2.0编写shader
这几天在看子龙山人的关于OpenGL的文章,先依葫芦画瓢,能看到些东西,才能慢慢深入了解,当入门文章不错,但是其中遇到的一些问题,折腾了一些时间,为了方便和我一样的小白们,在这篇文章中进行写补充. O ...
- XP明年就被停止技术支持,这会带来什么?谈谈如何做决策
XP是MS的一款老牌操作系统,相信大家都不陌生,甚至还有继续使用的人,当然了,在虚拟机里用它也是很好用的,不过,再漂亮的姑娘,也有嫁人的时候,作为XP的父母,MS微软明年四月将停止支持有十多年历史的 ...