传送门

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
  1. 3 3 3
    1 2 3
    1 2 1
    1 3 2
    2 3 4
    1 2
    1 3
    2 3
Output
  1. 9 18 17
Input
  1. 1 1 1
    1
    1 1 1
    1 1
Output
  1. 2
Input
  1. 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
  1. 5 18 31 20
  2.  
  3. 分析
    线段树
    离线预处理所有Query,统计各operation的次数。
    区间Insert,注意使用lazy-tag,点Query答案。
    写法
    要维护两棵线段树,可并做一棵。
  4.  
  5. 这是我第一次写的,TLE on test 24
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAX_N=1e5+;
  4. typedef long long ll;
  5.  
  6. struct op{
  7. int l, r;
  8. ll v;
  9. }o[MAX_N];
  10.  
  11. ll cnt[MAX_N], a[MAX_N];
  12.  
  13. struct Node{
  14. int l, r;
  15. ll v;
  16. int mid(){return (l+r)>>;}
  17. }T[MAX_N<<];
  18.  
  19. void Build(int id, int l, int r){
  20. T[id].l=l, T[id].r=r, T[id].v=;
  21. if(l==r) return;
  22. int mid=T[id].mid();
  23. Build(id<<, l, mid);
  24. Build(id<<|, mid+, r);
  25. }
  26. void Insert(int id, int l, int r, ll v){
  27. Node &now=T[id];
  28. if(now.l>=l&&now.r<=r){
  29. if(~now.v) now.v+=v;
  30. else{
  31. Insert(id<<, l, r, v);
  32. Insert(id<<|, l, r, v);
  33. }
  34. }
  35. else{
  36. Node &lch=T[id<<], &rch=T[id<<|];
  37. if(~now.v) lch.v=rch.v=now.v, now.v=-; //ERROR-PRONE
  38. int mid=now.mid();
  39. if(l<=mid) Insert(id<<, l, r, v);
  40. if(r>mid) Insert(id<<|, l, r, v);
  41. if(lch.v==rch.v) now.v=lch.v;
  42. }
  43. }
  44.  
  45. void Qurery(int id, ll *a){
  46. Node &now=T[id];
  47. if(~now.v)
  48. for(int i=now.l; i<=now.r; i++) a[i]+=now.v;
  49. else{
  50. Qurery(id<<, a);
  51. Qurery(id<<|, a);
  52. }
  53. }
  54.  
  55. int main(){
  56. //freopen("in", "r", stdin);
  57. int N, M, K;
  58. scanf("%d%d%d", &N, &M, &K);
  59. for(int i=; i<=N; i++) scanf("%lld", a+i);
  60. for(int i=; i<=M; i++)
  61. scanf("%d%d%lld", &o[i].l, &o[i].r, &o[i].v);
  62. Build(, , M);
  63. int l, r;
  64. while(K--){
  65. scanf("%d%d", &l, &r);
  66. Insert(, l, r, );
  67. }
  68. Qurery(, cnt);
  69. Build(, , N);
  70. for(int i=; i<=M; i++)
  71. if(cnt[i])
  72. Insert(, o[i].l, o[i].r, o[i].v*cnt[i]);
  73. Qurery(, a);
  74. for(int i=; i<=N; i++)
  75. printf("%lld ", a[i]);
  76. puts("");
  77. return ;
  78. }
  1. 上面的代码没有lazy-tag或者说我设置的lazy-tag没起到相应的作用。我的考虑是设置一个tag,最后求答案时可不必细分到每个叶子节点,但是这种优化对降低Insert的复杂度没有太大帮助,而Insert是最耗时的,因而总的复杂度还是没降下来。
    AC的姿势
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAX_N=1e5+;
  4. typedef long long ll;
  5.  
  6. struct op{
  7. int l, r, v;
  8. }o[MAX_N];
  9.  
  10. ll cnt[MAX_N], a[MAX_N];
  11.  
  12. struct Node{
  13. int l, r;
  14. ll v;
  15. int mid(){return (l+r)>>;}
  16. }T[MAX_N<<];
  17.  
  18. void Build(int id, int l, int r){
  19. T[id].l=l, T[id].r=r, T[id].v=;
  20. if(l==r) return;
  21. int mid=T[id].mid();
  22. Build(id<<, l, mid);
  23. Build(id<<|, mid+, r);
  24. }
  25. void Insert(int id, int l, int r, ll v){
  26. Node &now=T[id];
  27. if(now.l>=l&&now.r<=r) now.v+=v;
  28. else{
  29. Node &lch=T[id<<], &rch=T[id<<|];
  30. if(now.v)
  31. lch.v+=now.v, rch.v+=now.v, now.v=;
  32. int mid=now.mid();
  33. if(l<=mid) Insert(id<<, l, r, v);
  34. if(r>mid) Insert(id<<|, l, r, v);
  35. }
  36. }
  37.  
  38. void Qurery(int id, ll *a){
  39. Node &now=T[id];
  40. if(now.l==now.r) a[now.l]+=now.v;
  41. else{
  42. Node &lch=T[id<<], &rch=T[id<<|];
  43. if(now.v)
  44. lch.v+=now.v, rch.v+=now.v;
  45. Qurery(id<<, a);
  46. Qurery(id<<|, a);
  47. }
  48. }
  49.  
  50. int main(){
  51. //freopen("in", "r", stdin);
  52. int N, M, K;
  53. scanf("%d%d%d", &N, &M, &K);
  54. for(int i=; i<=N; i++) scanf("%lld", a+i);
  55. for(int i=; i<=M; i++)
  56. scanf("%d%d%lld", &o[i].l, &o[i].r, &o[i].v);
  57. Build(, , M);
  58. int l, r;
  59. while(K--){
  60. scanf("%d%d", &l, &r);
  61. Insert(, l, r, );
  62. }
  63. Qurery(, cnt);
  64. Build(, , N);
  65. for(int i=; i<=M; i++)
  66. if(cnt[i]&&o[i].v)
  67. Insert(, o[i].l, o[i].r, o[i].v*cnt[i]);
  68. Qurery(, a);
  69. for(int i=; i<=N; i++)
  70. printf("%lld ", a[i]);
  71. puts("");
  72. return ;
  73. }
  1.  
  1.  

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. 关于第一个Java应用

    一.创建Java源文件 Java应用由一个或多个扩展名为".java"的文件构成,这些文件被称为Java源文件,从编译的角度,则被称为编译单元(Compilation Unit). ...

  2. C语言 数组做函数参数退化为指针的技术推演

    //数组做函数参数退化为指针的技术推演 #include<stdio.h> #include<stdlib.h> #include<string.h> //一维数组 ...

  3. C语言 文件操作3--文件重定向与扫描

    //文件重定向和扫描 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //fprint ...

  4. js如何判断一组数字是否连续,得到一个临时数组[[3,4],[13,14,15],[17],[20],[22]];

    var arrange = function(arr){ var result = [], temp = []; arr.sort(function(source, dest){ return sou ...

  5. pandas 给数据打标签

    import numpy as np import pandas as pd df = pd.DataFrame(np.random.randint(0,100,100), columns=['sco ...

  6. Chrome 插件: 起动本地应用 (Native messaging)

    Chrome 插件: 起动本地应用 (Native messaging) www.MyException.Cn  网友分享于:2014-08-01  浏览:3次   Chrome 插件: 启动本地应用 ...

  7. 如何下载Hibernate

    官网: http://hibernate.org/ 打开hibernate官网,选择Hibernate ORM,点击左侧的Downloads 点击Downloads后,可以看到如下页面,右侧是各个版本 ...

  8. 安装包制作工具 SetupFactory使用2 API清单

    2014-11-19 SetupFactory中可以通过其API控制很复杂的业务需求. 下图中展示了其内置的API种类与具体分类函数.   序号 API名称 API说明 1 Application.E ...

  9. java之hashCode

    package com.simope.myTest; import java.util.HashMap; import java.util.Map; public class Test20151022 ...

  10. 第八章 self sizing cell

    本项目是<beginning iOS8 programming with swift>中的项目学习笔记==>全部笔记目录 ------------------------------ ...