链接:

http://poj.org/problem?id=3468

代码:

  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<stdlib.h>
  4 using namespace std;
  5
  6 #define Lson r<<1
  7 #define Rson r<<1|1
  8
  9 const int N = 1e5+5;
 10
 11 struct SegmentTree
 12 {
 13     int L, R;
 14     long long sum, e;
 15     int Mid()
 16     {
 17         return (L+R)>>1;
 18     }
 19     int len()
 20     {
 21         return R-L+1;
 22     }
 23 }a[N<<2];
 24
 25 void BuildSegTree(int r, int L, int R)
 26 {
 27     a[r].L=L, a[r].R=R;
 28     a[r].e=0;
 29
 30     if(L==R)
 31     {
 32         scanf("%lld", &a[r].sum);
 33         return ;
 34     }
 35
 36     BuildSegTree(Lson, L, a[r].Mid());
 37     BuildSegTree(Rson, a[r].Mid()+1, R);
 38
 39     a[r].sum = a[Lson].sum + a[Rson].sum;
 40 }
 41 void PushDown(int r)
 42 {
 43     a[Lson].sum += a[r].e*a[Lson].len();
 44     a[Lson].e += a[r].e;
 45     a[Rson].sum += a[r].e*a[Rson].len();
 46     a[Rson].e += a[r].e;
 47
 48     a[r].e=0;
 49 }
 50 void Update(int r, int L, int R, int e)
 51 {
 52     a[r].sum += (R-L+1)*e;
 53
 54     if(a[r].L==L && a[r].R==R)
 55     {
 56         a[r].e += e;
 57         return ;
 58     }
 59
 60     PushDown(r);
 61
 62     if(R<=a[r].Mid())
 63         Update(Lson, L, R, e);
 64     else if(L>a[r].Mid())
 65        Update(Rson, L, R, e);
 66     else
 67     {
 68         Update(Lson, L, a[r].Mid(), e);
 69         Update(Rson, a[r].Mid()+1, R, e);
 70     }
 71 }
 72 long long Query(int r, int L, int R)
 73 {
 74     if(a[r].L==L && a[r].R==R)
 75        return a[r].sum;
 76
 77     PushDown(r);
 78
 79     if(R<=a[r].Mid())
 80         return Query(Lson, L, R);
 81     else if(L > a[r].Mid())
 82         return Query(Rson, L, R);
 83     else
 84     {
 85         long long Lsum = Query(Lson, L, a[r].Mid());
 86         long long Rsum = Query(Rson, a[r].Mid()+1, R);
 87
 88         return Lsum+Rsum;
 89     }
 90 }
 91
 92 int main()
 93 {
 94    int n, m;
 95    while(scanf("%d%d", &n, &m)!=EOF)
 96    {
 97        BuildSegTree(1, 1, n);
 98
 99        while(m--)
100        {
101            char s[10];
102            int L, R, e;
103
104            scanf("%s", s);
105
106            if(s[0]=='Q')
107            {
108                scanf("%d%d", &L, &R);
109                printf("%lld\n", Query(1, L, R));
110            }
111            else
112            {
113                scanf("%d%d%d", &L, &R, &e);
114                Update(1, L, R, e);
115            }
116        }
117    }
118    return 0;
119 }

转载于:https://www.cnblogs.com/YY56/p/4689786.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. A Simple Problem with Integers POJ - 3468 (线段树)

    思路:线段树,区间更新,区间查找 1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #inc ...

  4. 【线段树】【模板】讲解 + 例题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(区间加值) ...

  5. POJ3468-A Simple Problem with Integers【线段树,树状数组,分块】

    正题 题目链接:我是链接 其实洛谷线段树模板也是一样的:三种方法AC评测链接 题目大意 要求支持区间修改,区间求和. 线段树 直接用一个lazy标记,在之前的博客里有说 code1 #include& ...

  6. 线段树模板(来自胡浩大牛)

    http://www.notonlysuccess.com/(今天看二叉树,想回来看看,发现大牛博客进不去...) 如果要学,就要好好学.我copy的,如有错,请看http://www.cnblogs ...

  7. poj 3243:A Simple Problem with Integers

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

  8. hdu1156(简单线段树 模板题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  9. 线段树模板hdu 1754:I Hate It

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  10. 【AHOI2009】【BZOJ1798】Seq 维护序列seq(线段树模板,易错提醒)

    problem 给定一个长为n的序列,m次询问 每次询问有3种操作 1.一段区间全部乘一个值 2.一段区间全部加一个值 3.询问一段区间和%P solution 不就一颗线段树么,看朕10分钟A掉.. ...

最新文章

  1. java如何做到判断一个字符串是否是数字
  2. Java中String类型的数据比较
  3. SAP Spartacus SimpleResponsiveBannerComponent url 的数据源
  4. WordList02
  5. [luoguP4142]洞穴遇险
  6. Java日志框架-logback的介绍及配置使用方法(纯Java工程)
  7. oracle database version,Oracle Database Version History
  8. Oracle闪回技术(Flashback)
  9. 上传网站到服务器的tomcat
  10. html页面小宠物代码大全,宠物店网页设计html代码
  11. linux下r语言画图,linux命令行下使用R语言绘图实例讲解
  12. MySQL快速解决“is marked as crashed and should be repaired“故障
  13. VBA中启动其它程序
  14. c语言变量 集体备课,(最新整理)数学集体备课活动记录2
  15. Excel行高列宽使用单位为磅(1cm=28.6磅)
  16. gitbash EndNote Snipaste Wox+Everything 火狐 火绒浏览器 Bandizip
  17. 一个神奇的分布式计算框架:jini
  18. Apache Ambari介绍
  19. unity 摄像头跟着鼠标移动_unity第三视角移动,摄像机跟随
  20. 人工智能、深度学习、机器学习常见面试题41~55

热门文章

  1. 教你从0到1搭建秒杀系统-限流
  2. php 数组存入mysql_PHP将数组存入数据库中的四种方式
  3. Spring Boot 微服务性能下降九成!使用 Arthas 定位根因
  4. centos7 安装mysql php,Centos7安装mysql与php的方法
  5. 待办事项桌面插件_求一款安卓手机上可添加小目标的桌面便签软件?
  6. Fiddler之断点调试(模拟器)
  7. linux如何使用vim显示行号语法高亮,(.vimrc简单使用)
  8. java内部float,Java中的float是什么?
  9. python数组求和函数_python数据分析之Numpy数据库第三期数组的运算
  10. 三层架构项目如何发布_以k8s集群管理为例,大牛教你如何设计优秀项目架构