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

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

Sample Output

4
55
9
15

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

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

 #include <stdio.h>
#include <string.h> struct
{
int l;
int r;
double num;
double Inc;
}shu[]; void Init (int left,int right,int k)
{
shu[k].l=left;
shu[k].r=right;
shu[k].Inc=;
if(left==right)
{
scanf("%lf",&shu[k].num);
return;
}
int mid=(left+right)/;
Init(left,mid,*k);
Init(mid+,right,*k+);
shu[k].num=shu[*k].num+shu[*k+].num;
} void insert(int left,int right,double c,int k)
{
if (shu[k].l==left&&shu[k].r==right)//到某个区间
{
shu[k].Inc+=c;
return;
}
shu[k].num+=(right-left+)*c;
int mid=(shu[k].l+shu[k].r)/;
if (right<=mid)
insert(left,right,c,*k);
else if (left>mid)
insert (left,right,c,*k+);
else
{
insert(left,mid,c,*k);
insert(mid+,right,c,*k+);
}
}
double query(int left,int right,int k)
{
if (shu[k].l==left&&shu[k].r==right)//
{ return shu[k].num+(shu[k].r-shu[k].l+)*shu[k].Inc;
}
int mid=(shu[k].l+shu[k].r)/;
if(shu[k].Inc!=)//将这个区间的积累量释放
{
shu[k].num+=(shu[k].r-shu[k].l+)*shu[k].Inc;//自己先加上
insert(shu[k].l,mid,shu[k].Inc,*k);
insert(mid+,shu[k].r,shu[k].Inc,*k+);
shu[k].Inc=;
}
if (left>mid) return query(left,right,*k+);
else if (right<=mid) return query(left,right,*k);
else
return query(left,mid,*k)+query(mid+,right,*k+); } int main()
{
int Q;
int all_p,a,b;
double c;
char comend;
while (scanf("%d%d",&all_p,&Q)!=EOF)
{
Init(,all_p,);
while (Q--)
{
getchar();
scanf("%c",&comend);
scanf("%d%d",&a,&b); if (comend=='Q') printf("%.0lf\n",query(a,b,));
if (comend=='C')
{
scanf("%lf",&c);
insert(a,b,c,);
}
}
}
return ;
}

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. Memory Barriers

    这回该进入主题了.         上一文最后提到了 Memory Barriers ,即内存屏障.由于对一个 CPU 而言,a = 1; b = 1. 由于在中间加了内存屏障,在 X86 架构下,就 ...

  2. 如何注册ocx文件

    32位系统: 将文件放到c:\windows\system目录注册 运行:Regsvr32 c:\windows\system\xxx.ocx取消注册运行:Regsvr32.exe /u c:\win ...

  3. 利用velocity.js将svg动起来

    关于velocity.js Velocity.js是一款jquery动画引擎插件,它拥有与jquery中的$.animate()相同的API,还打包了颜色动画,转换,循环,easing效果,类动画.滚 ...

  4. Linux——.bash_profile和.bashrc的区别(如何设置生效)

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置./etc/bashrc:为每一个运 ...

  5. C/C++获取当前系统时间

    个人觉得第二种还是比较实用的,而且也是最常用的~ 不过当计算算法耗时的时候,不要忘记second,不能只要用Milliseconds来减,不然后出现负值,若是算法耗时太长就得用minutes啦.再不然 ...

  6. jsp+servlet实现文件上传

    上传(上传不能使用BaseServlet) 1. 上传对表单限制 * method="post" * enctype="multipart/form-data" ...

  7. A.0 B.1 C.2 D.3

    17. 以下哪个不是广告平台? A.Admob B.Domob C.InMobi D.TalkingData 错误 应该选择:D.TalkingData 10. 哪个不是免费的工具? A.Xcode  ...

  8. Java之旅(2)—反射

    1.     概念 反射就是将java类中的各种成分映射成对应的java类.之前我们已经讲过了Class类,也明确了一个java类中用一个Class类的对象来表示,一个类中的组成部分有:成员变量,方法 ...

  9. linux如何手动释放linux内存

    当在Linux下频繁存取文件后,物理内存会很快被用光,当程序结束后,内存不会被正常释放,而是一直作为caching.这个问题,貌似有不少人在问,不过都没有看到有什么很好解决的办法.那么我来谈谈这个问题 ...

  10. usb转串口模块下载时遇到的问题

    ch340g usb转TTL模块,烧写wifi模块ESP8266固件时,为图省事,我直接用的该模块的3.3v电为wifi模块供的电,结果刚一上电就出现串口模块消失(听到噔的一声),电脑设备管理器里就看 ...