F - A Simple Problem with Integers

Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

Description

给出了一个序列,你需要处理如下两种询问。

"C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。

"Q a b" 询问[a, b]区间中所有值的和。

Input

第一行包含两个整数N, Q。1 ≤ N,Q ≤ 100000.

第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000)。

接下来Q行询问,格式如题目描述。

Output

对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。

Sample Input

  1. 10 5
  2. 1 2 3 4 5 6 7 8 9 10
  3. Q 4 4
  4. Q 1 10
  5. Q 2 4
  6. C 3 6 3
  7. Q 2 4

Sample Output

  1. 4
  2. 55
  3. 9
  4. 15

//自己写,又超时了,网上看了别人的才过的,在类里面要增加一个积累量这个数据,这样,增加命令的时候只要将特定区间积累量加起来并且num+总和,不必遍历到每一个叶节点,但是在查询时,还是要释放,因为,可能查的区间,在积累的这个区间只占一部分。

这样节约了时间,好像叫lazy的思想

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct
  5. {
  6. int l;
  7. int r;
  8. double num;
  9. double Inc;
  10. }shu[];
  11.  
  12. void Init (int left,int right,int k)
  13. {
  14. shu[k].l=left;
  15. shu[k].r=right;
  16. shu[k].Inc=;
  17. if(left==right)
  18. {
  19. scanf("%lf",&shu[k].num);
  20. return;
  21. }
  22. int mid=(left+right)/;
  23. Init(left,mid,*k);
  24. Init(mid+,right,*k+);
  25. shu[k].num=shu[*k].num+shu[*k+].num;
  26. }
  27.  
  28. void insert(int left,int right,double c,int k)
  29. {
  30. if (shu[k].l==left&&shu[k].r==right)//到某个区间
  31. {
  32. shu[k].Inc+=c;
  33. return;
  34. }
  35. shu[k].num+=(right-left+)*c;
  36. int mid=(shu[k].l+shu[k].r)/;
  37. if (right<=mid)
  38. insert(left,right,c,*k);
  39. else if (left>mid)
  40. insert (left,right,c,*k+);
  41. else
  42. {
  43. insert(left,mid,c,*k);
  44. insert(mid+,right,c,*k+);
  45. }
  46. }
  47. double query(int left,int right,int k)
  48. {
  49. if (shu[k].l==left&&shu[k].r==right)//
  50. {
  51.  
  52. return shu[k].num+(shu[k].r-shu[k].l+)*shu[k].Inc;
  53. }
  54. int mid=(shu[k].l+shu[k].r)/;
  55. if(shu[k].Inc!=)//将这个区间的积累量释放
  56. {
  57. shu[k].num+=(shu[k].r-shu[k].l+)*shu[k].Inc;//自己先加上
  58. insert(shu[k].l,mid,shu[k].Inc,*k);
  59. insert(mid+,shu[k].r,shu[k].Inc,*k+);
  60. shu[k].Inc=;
  61. }
  62. if (left>mid) return query(left,right,*k+);
  63. else if (right<=mid) return query(left,right,*k);
  64. else
  65. return query(left,mid,*k)+query(mid+,right,*k+);
  66.  
  67. }
  68.  
  69. int main()
  70. {
  71. int Q;
  72. int all_p,a,b;
  73. double c;
  74. char comend;
  75. while (scanf("%d%d",&all_p,&Q)!=EOF)
  76. {
  77. Init(,all_p,);
  78. while (Q--)
  79. {
  80. getchar();
  81. scanf("%c",&comend);
  82. scanf("%d%d",&a,&b);
  83.  
  84. if (comend=='Q') printf("%.0lf\n",query(a,b,));
  85. if (comend=='C')
  86. {
  87. scanf("%lf",&c);
  88. insert(a,b,c,);
  89. }
  90. }
  91. }
  92. return ;
  93. }

A Simple Problem with Integers(线段树)的更多相关文章

  1. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  2. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  3. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  4. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...

  5. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  6. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  7. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  8. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

  9. A Simple Problem with Integers(线段树,区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83822   ...

  10. POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 105742 ...

随机推荐

  1. 每日一个机器学习算法——LR(逻辑回归)

    本系列文章用于汇集知识点,查漏补缺,面试找工作之用.数学公式较多,解释较少. 1.假设 2.sigmoid函数: 3.假设的含义: 4.性质: 5.找一个凸损失函数 6.可由最大似然估计推导出 单个样 ...

  2. EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题

    public static DataTable ToDataTable<T>(this IEnumerable<T> varlist, CreateRowDelegate< ...

  3. 哈希—— POJ 3349 Snowflake Snow Snowflakes

    相应POJ题目:点击打开链接 Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions ...

  4. 深入分析JavaWeb Item22 -- 国际化(i18n)

    一.国际化开发概述 软件的国际化:软件开发时,要使它能同一时候应对世界不同地区和国家的訪问,并针对不同地区和国家的訪问.提供对应的.符合来訪者阅读习惯的页面或数据. 国际化(international ...

  5. 联想电脑Win8升级win10后Wlan关闭无法开启解决办法

    官网下载电源驱动,下载无线网上驱动 开启电脑 按fn+f5 电源管理界面就出来了 把无线网卡打开 就ok了 这样就开启了无线! 如果还不行,可进行如下尝试,希望有所帮助: 1.开机进bios(一般是按 ...

  6. 利用python批量缩放图片

    废话少说,上代码: import matplotlib as mpl mpl.use('Agg') import os import matplotlib.pyplot as plt from sci ...

  7. leetcode_Power of Two_easy

    Given an integer, write a function to determine if it is a power of two. 题目意思:推断某个数是否是2的幂. 方法:直接进行bi ...

  8. 【Nginx-反向代理server】基础知识(二)之多进程模式

    Nginx的多进程模式 nginx在启动后.会有一个master进程和多个worker进程.master进程主要用来管理worker进程,包括:接收来自外界的信号.向各worker进程发送信号,监控w ...

  9. 在ubuntu下安装ns2-allinone-2.35.tar.gz

    1.软件下载 首先先下载ns-allinone-2.35.tar.gz (下载路径http://sourceforge.net/projects/nsnam/files/),将其放到你/home/my ...

  10. Linux 脚本点滴知识积累

    1.以openwrt中的/etc/hotplug.d/button/00-button为例 . /lib/functions.sh do_button () { local button   ---- ...