题目大意:

  一个数列,有两个操作:1.修改操作,将一段区间内的数加上c;2.查询操作,查询一段区间内的数的和。

思路:

  线段树裸题,区间修改、区间查询,维护和以及加上的数,由于无序,不需要向下推标记,只需在子树更新完之后更新根节点即可。

代码:

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; long long sum[],mor[]; void up(int cur,int t)
{
sum[cur]=sum[cur<<]+sum[cur<<|]+t*mor[cur];
} void add(int L,int R,int l,int r,int x,int cur)
{
if (L>=l && R<=r)
{
mor[cur]+=x;
sum[cur]+=(R-L+)*x;
return;
}
int mid=L+R>>;
if (l<=mid) add(L,mid,l,r,x,cur<<);
if (r>mid) add(mid+,R,l,r,x,cur<<|);
up(cur,R-L+);
} long long ask(int L,int R,int l,int r,int cur)
{
if (L>=l && R<=r) return sum[cur];
int mid=L+R>>;
long long ans=mor[cur]*(r-l+);
if (l<=mid) ans+=ask(L,mid,l,min(mid,r),cur<<);
if (r>mid) ans+=ask(mid+,R,max(l,mid+),r,cur<<|);
return ans;
} int main()
{
int n,m,i,a,b,c;
scanf("%d%d",&n,&m);
for (i=;i<=n;i++) scanf("%d",&a),add(,n,i,i,a,);
for (i=;i<=m;i++)
{
char ch=getchar();
while (ch<'A' || ch>'Z') ch=getchar();
if (ch=='Q') scanf("%d%d",&a,&b),printf("%lld\n",ask(,n,a,b,));
else scanf("%d%d%d",&a,&b,&c),add(,n,a,b,c,);
}
return ;
}

BZOJ3212 Pku3468 A Simple Problem with Integers 题解的更多相关文章

  1. BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...

  2. bzoj3212 Pku3468 A Simple Problem with Integers 线段树

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2046  So ...

  3. BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2530  So ...

  4. bzoj3212 pku3468 A Simple Problem with Integers

    一个有初值的数列.区间加.区间查 用线段树直接水过 然而并没有1A,主要是做题太快没看规模结果没注意线段树要用longlong建 卧槽怎么可以这么坑爹,害得我看见wa心慌了,还以为连线段树都要跪 一开 ...

  5. 【分块】【线段树】bzoj3212 Pku3468 A Simple Problem with Integers

    线段树入门题…… 因为poj原来的代码莫名RE,所以丧病地写了区间修改的分块…… 其实就是块上打标记,没有上传下传之类. #include<cstdio> #include<cmat ...

  6. 3212: Pku3468 A Simple Problem with Integers

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1053  So ...

  7. bzoj 3212 Pku3468 A Simple Problem with Integers

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MB Description You ...

  8. bzoj 3212 Pku3468 A Simple Problem with Integers 线段树基本操作

    Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2173  Solved:  ...

  9. POJ3468:A Simple Problem with Integers——题解

    http://poj.org/problem?id=3468 实现一个线段树,能够做到区间修改和区间查询和. 明显板子题. #include<cstdio> #include<cma ...

随机推荐

  1. Xcode 7如何免费真机调试iOS应用

    Xcode 7如何免费真机调试iOS应用的简单方式: 运行Xcode后,点击菜单中的Preferences…进入Accounts标签,这里选择添加Apple ID:在弹出的对话框中登入你的Apple ...

  2. php 分页

    分页类 <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private ...

  3. jquery学习笔记-----插件开发的编写总结

    一.对jQuery对象的扩展 ;(function($){ $.fn.extend(  { fun1:abc,fun2:1bc … } ) })(jQuery) 这里采用立即执行模式,即不用调用也能执 ...

  4. 从 Eclipse 迁移至 Android Studio

    从 Eclipse 迁移至 Android Studio 本文内容 Android Studio 基础知识 迁移先决条件 将项目导入 Android Studio 后续步骤 将项目迁移至 Androi ...

  5. poj 1182:食物链(种类并查集,食物链问题)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44168   Accepted: 12878 Description ...

  6. AOJ673 聪明的输入法(字典树)

    #include<cstdio> #include <cstdlib> #include <cstring> #include <iostream> # ...

  7. memcache(使用php操作memcache)

    .概念 memcache 是一个高效的分布式的内存对象缓存系统,他可以支持把php的各种数据(数组,对象,基本数据类型)放在它管理的内存中 . 安装步骤 1.下载php_memcache.dll文件并 ...

  8. 使用windows的远程桌面连接连接Ubuntu

    想起来用笔记本连接一个windows server时只需要在远程桌面连接里面输入一下ip地址然后账号密码就可以了,十分简单.于是乎既然装了个Ubuntu当服务器使那么我就业来远程连接一下,由于wind ...

  9. C# 使用 NPOI 库读写 Excel 文件

    NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件.在处理Excel文件上,NPOI 可以同时兼容 xls 和 xlsx.官网提供了一份 Examples,给出 ...

  10. Hibernate 延迟加载

    一.什么是延迟加载? 延迟加载是指当应用程序想要从数据库获取对象时(在没有设置lazy属性值为false),Hibernate只是从数据库获取符合条件的对象的OId从而生成代理对象,并没有加载出对象访 ...