传送门

A. Greg and Array
time limit per test   1.5 seconds
memory limit per test   256 megabytes
input   standard input
output  standard output

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.

Input

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.

Output

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.

Sample test(s)
Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Output
9 18 17
Input
1 1 1
1
1 1 1
1 1
Output
2
Input
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
Output
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的更多相关文章

  1. CodeForces Round #179 (295A) - Greg and Array

    题目链接:http://codeforces.com/problemset/problem/295/A 我的做法,两次线段树 #include <cstdio> #include < ...

  2. CodeForces Round #179 (295A) - Greg and Array 一个线段树做两次用

    线段树的区间更新与区间求和...一颗这样的线段树用两次... 先扫描1~k...用线段树统计出每个操作执行的次数... 那么每个操作就变成了 op. l  , op.r , op.c= times* ...

  3. CF 295A Greg and Array (两次建树,区间更新,单点查询)

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...

  4. Codeforces 296C Greg and Array

    数据结构题.个人认为是比较好的数据结构题.题意:给定一个长度为n的数组a,然后给定m个操作序列,每个操作:l, r, x将区间[l, r]内的元素都增加a,然后有k个查询,查询形式是对于操作序列x,y ...

  5. 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 ...

  6. Greg and Array CodeForces 296C 差分数组

    Greg and Array CodeForces 296C 差分数组 题意 是说有n个数,m种操作,这m种操作就是让一段区间内的数增加或则减少,然后有k种控制,这k种控制是说让m种操作中的一段区域内 ...

  7. Codeforces 442C Artem and Array(stack+贪婪)

    题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...

  8. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  9. ACM - 最短路 - CodeForces 295B Greg and Graph

    CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...

随机推荐

  1. Android应用程序签名详解 简介

    转自: http://blog.csdn.net/lyq8479/article/details/6401093 本文主要讲解Android应用程序签名相关的理论知识,包括:什么是签名.为什么要给应用 ...

  2. View (五)自定义View的实现方法

    一些接触Android不久的朋友对自定义View都有一丝畏惧感,总感觉这是一个比较高级的技术,但其实自定义View并不复杂,有时候只需要简单几行代码就可以完成了. 如果说要按类型来划分的话,自定义Vi ...

  3. yield(C# 参考)

    yield(C# 参考) 在语句中使用 yield 关键字,表示在该关键字所在的方法.运算符或 get 访问器是迭代器.   通过使用 yield 定义迭代器,可在实现自定义集合类型的 IEnumer ...

  4. iBatis.Net(C#)SQL数据映射

    转载请注明 http://www.cnblogs.com/13590/archive/2013/03/01/2938126.html 摘要:本文探讨了iBatis.Net框架的XML数据映射文件各配置 ...

  5. 关于浏览器cookie的那些事儿

    昨天接到一个小需求,就是在ipad上访问某页面,页面顶部出现一个下载客户端的提示,点击关闭按钮后,提示信息消失,信息存入cookie,在cookie未过期之前,除非用户自己清除浏览器的cookie,否 ...

  6. 自定义GrildView实现单选功能

    首先看实现功能截图,这是一个自定义Dialog,并且里面内容由GrildView 绑定数据源,实现类似单选功能. 首先自定义Dialog,绑定数据源 自定义Dialog弹出框大小方法 最主要实现的就是 ...

  7. 足球运动训练心得及经验分析-c语言学习调查

    在准备预备作业02之前,我参考娄老师的提示,阅读了<[做中学(Learning By Doing)]之乒乓球刻意训练一年总结>一文. 在文章描述的字里行间,给予我的印象是系统.负责,娄老师 ...

  8. 如何远程断点调试本地localhost项目

    前言 对于一般开发网站的IDE自带的服务器是都跑在 localhost 地址上的.(如下图的asp.net) 而这种地址是只能在本机通过 localhost 或 127.0.0.1 地址访问到,而无法 ...

  9. 支持Json进行操作的Javascript类库TAFFY DB

    前段时间工作中用到Json数据,希望将一些简单的增删改查放到客户端来做,这样也能减少服务器端的压力.分别查找了几个可以对Json进行操作的javascript 类库,最终选定了TAFFY DB.原因如 ...

  10. nginx的笔记

    nginx 的安装 下载地址: http://nginx.org/download/nginx-1.4.2.tar.gz 安装准备: nginx依赖于pcre库,要先安装pcre yum instal ...