思路:线段树,区间更新,区间查找

  1 #include<iostream>
  2 #include<vector>
  3 #include<string>
  4 #include<cmath>
  5 #include<set>
  6 #include<algorithm>
  7 #include<cstdio>
  8 #include<map>
  9 #include<cstring>
 10 #include<list>
 11
 12 #define MAXSIZE 100010
 13
 14 using namespace std;
 15
 16 long long tree[4*MAXSIZE];
 17 long long lz[4*MAXSIZE];
 18 int N, Q;
 19
 20 void init()
 21 {
 22     memset(tree, 0, sizeof(tree));
 23     memset(lz, 0, sizeof(lz));
 24 }
 25
 26 void build(int node, int l, int r)
 27 {
 28     if(l == r)
 29     {
 30         scanf("%lld", &tree[node]);
 31         return;
 32     }
 33
 34     int mid = (l+r)/2;
 35     build(node*2, l, mid);
 36     build(node*2+1, mid+1, r);
 37
 38     tree[node] = tree[node*2] + tree[node*2+1];
 39 }
 40
 41 void push_down(int node, int l, int r)
 42 {
 43     if(lz[node])
 44     {
 45         int mid = (l+r)/2;
 46         lz[node*2] += lz[node];
 47         lz[node*2+1] += lz[node];
 48         tree[node*2] += (mid-l+1)*lz[node];
 49         tree[node*2+1] += (r-mid)*lz[node];
 50         lz[node] = 0;
 51     }
 52 }
 53
 54 void update_range(int node, int l, int r, int L, int R, int add)
 55 {
 56     if(l <= L && r >= R)
 57     {
 58         lz[node] += add;
 59         tree[node] += (R-L+1)*add;
 60         return;
 61     }
 62     push_down(node, L, R);
 63     int mid = (L+R)/2;
 64     if(mid >= l)
 65         update_range(node*2, l, r, L, mid, add);
 66     if(mid < r)
 67         update_range(node*2+1, l, r, mid+1, R, add);
 68     tree[node] = tree[node*2] + tree[node*2+1];
 69 }
 70
 71 long long query_range(int node, int l, int r, int L, int R)
 72 {
 73     if(l <= L && r >= R)
 74     {
 75         return tree[node];
 76     }
 77     push_down(node, L, R);
 78     int mid = (L+R)/2;
 79     long long sum = 0;
 80     if(mid >= l)
 81         sum += query_range(node*2, l, r, L, mid);
 82     if(mid < r)
 83         sum += query_range(node*2+1, l, r, mid+1, R);
 84
 85     return sum;
 86 }
 87
 88
 89
 90 int main()
 91 {
 92     scanf("%d%d", &N, &Q);
 93     init();
 94     build(1, 1, N);
 95     while(Q--)
 96     {
 97         char ch;
 98         scanf(" %c", &ch);
 99         if(ch == 'Q')
100         {
101             int a, b;
102             scanf("%d%d", &a, &b);
103             printf("%lld\n", query_range(1, a, b, 1, N));
104         }
105         else if(ch == 'C')
106         {
107             int a, b, c;
108             scanf("%d%d%d", &a, &b, &c);
109             update_range(1, a, b, 1, N, c);
110         }
111     }
112
113
114     return 0;
115 }

转载于:https://www.cnblogs.com/FengZeng666/p/11458279.html

A Simple Problem with Integers POJ - 3468 (线段树)相关推荐

  1. 2019_GDUT_新生专题V算法优化 B. A Simple Problem with Integers POJ 3468

    来源 POJ3468 题目: You have N integers, A1, A2, - , AN. You need to deal with two kinds of operations. O ...

  2. A Simple Problem with Integers POJ - 3468(线段树+区间查询+区间修改+建树+懒惰标记模板)+(树状数组)

    题意: 有一个数组,有两种操作.1: Q a b 求[a,b]的和 2:C a b c 给[a,b] 的所有元素都加上c. 题目: You have N integers, A1, A2, ... , ...

  3. POJ 3468 线段树+lazy标记

    lazy标记   Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u  Submit S ...

  4. poj 3468 线段树

    线段树的 建立build(初始化+左右相等+两个递归+别忘了sum)+更新update(递归出口+更新delta+三向递归+修正当前节点的value)+查找query(如果左右相等+更新delta+三 ...

  5. 【线段树】【模板】讲解 + 例题1 HDU - 1754 I Hate It (点修改分数)+ 例题二 POJ - 3468 A Simple Problem with Integers(区间加值)

    [线段树][模板]讲解 + 例题1 HDU - 1754 I Hate It (点修改分数)+ 例题二 POJ - 3468 A Simple Problem with Integers(区间加值) ...

  6. poj 3243:A Simple Problem with Integers

    3243:A Simple Problem with Integers 查看 提交 统计 提示 提问 总时间限制:  5000ms  单个测试点时间限制:  2000ms  内存限制:  131072 ...

  7. poj 3468 A Simple Problem with Integers(线段树区区)

    题目链接:  http://poj.org/problem?id=3468 题目大意:  给出N个数,和M次查询 C a b c  区间[a,b]的值都加上c Q a b     查询区间[a,b]值 ...

  8. 线段树专辑—— pku 3468 A Simple Problem with Integers

    http://poj.org/problem?id=3468 典型的一道基于lazy传递的线段树题目,这题和一般题目不同的地方在于,它的每次操作不是简单的覆盖线段,而是累加.记得第一次写的时候纠结了好 ...

  9. POJ 3468 A Simple Problem with Integers

    分析:这题wa了好多次(看了下discuss好多人也是这样,好题~).一处是sum值会超int32,要用int64.还有一处是toadd要累加,我不知道是受上一题影响还是怎的..pushdown的时候 ...

最新文章

  1. 计算机多媒体运用的ppt课件,《计算机多媒体》PPT课件.ppt
  2. $GLOBALS['HTTP_RAW_POST_DATA'] 和$_POST的区别
  3. 技术人员,为什么会苦逼
  4. C语言之头文件,static与const关键字
  5. 合作开发用到的几个 设计模式
  6. log4j源码阅读(一)之Logger
  7. 多线程相关的一些知识点
  8. Tomcat基础教程(一)
  9. 懒人建站 前台设计及特效
  10. 虚拟机本来有MySQL,后安装宝塔的MySQL导致MySQL不能启动报错
  11. Java Swing中键盘事件的处理
  12. JVM垃圾收集器(2)
  13. 使用APP inventor制作蓝牙串口助手【智能浇灌模型中用到】
  14. xp系统打印机服务器报错,互联网要点:Win7系统连接XP共享打印机报错0X000004如何解决...
  15. 用大写字母打印三角形
  16. Word 2016 公式编辑器中微分符号的竖线(2018.5.17)
  17. 机器学习之sklearn-KNN近邻算法分类小案例(乳腺癌预测最优模型)
  18. JAVA计算平年和闰年
  19. Cucumber eclipse plugin install
  20. LeetCode 整数转罗马数字

热门文章

  1. Logback 专题
  2. 高考前几天我们应该干什么?
  3. 一个非常巧妙的 hashcode 算法 return h (length-1);
  4. linux终端按下退格键自动覆盖上一行的问题
  5. flink編譯hadoop3.1.2(失败,这个问题没有意义,关闭)
  6. Backbone发展与语义分割网络发展
  7. CNN中的权重维度解析以及卷积核中的数值是怎么确定的
  8. 操作系统 真象还原 读书笔记
  9. 5.3 递归最小二乘法
  10. 2.12 矩阵及乘法重要总结