题意:n个点。m个操作。两种操作类型。C X Y K 表示区间[x,y]上每一个点值加k。Q X Y 求区间[x,y]的和



分析:线段树区间求和,裸模板



注意:结果会超int,要用long long 表示,假设是在hust上交结果要用%I64d。poj的话则用%lld



代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include<vector>
#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int maxn = 1e5+5;
long long sum[maxn*4];
long long lazy[maxn*4];
int num[maxn];
void up(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void down(int l,int r,int rt){
if(!lazy[rt]) return;
int mid=(l+r)/2;
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
sum[rt<<1]+=lazy[rt]*(mid-l+1);
sum[rt<<1|1]+=lazy[rt]*(r-mid);
lazy[rt]=0;
}
void build(int l,int r,int rt){
lazy[rt]=sum[rt]=0;
if(l==r){
sum[rt]=num[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
up(rt);
}
void update(int L,int R,int k,int l,int r,int rt){
if(L<=l&&r<=R){
lazy[rt]+=k;
sum[rt]+=(r-l+1)*k;
return;
}
down(l,r,rt);
int mid=(l+r)>>1;
if(L<=mid) update(L,R,k,l,mid,rt<<1);
if(R>mid) update(L,R,k,mid+1,r,rt<<1|1);
up(rt);
}
long long query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R) return sum[rt];
down(l,r,rt);
int mid=(l+r)>>1;
long long ret = 0;
if(L<=mid) ret+=query(L,R,l,mid,rt<<1);
if(R>mid) ret+=query(L,R,mid+1,r,rt<<1|1);
up(rt);
return ret;
}
int main()
{
int n,q;
while(scanf("%d%d",&n,&q)!=EOF){
for(int i=1;i<=n;i++) scanf("%d",&num[i]);
build(1,n,1);
while(q--){
char c;
int x,y,k;
scanf(" %c",&c);
if(c=='C'){
scanf("%d%d%d",&x,&y,&k);
update(x,y,k,1,n,1);
}
else{
scanf("%d%d",&x,&y);
printf("%lld\n",query(x,y,1,n,1));
}
}
}
}

线段树专题 POJ3468 A Simple Problem with Integers的更多相关文章

  1. 【成端更新线段树模板】POJ3468-A Simple Problem with Integers

    http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...

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

    这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和 ...

  3. (线段树模板)A Simple Problem with Integers --POJ--3468

    链接: http://poj.org/problem?id=3468 代码: #include<stdio.h> #include<algorithm> #include< ...

  4. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

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

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

  6. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  7. POJ3468 A Simple Problem with Integers 【段树】+【成段更新】

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

  8. poj------(3468)A Simple Problem with Integers(区间更新)

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

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

    https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...

随机推荐

  1. vscode使用教程(web开发)

    1.安装 进入官网下载https://code.visualstudio.com/ 一直下一步就好了,中间可以选择把软件安装在哪个目录. 2.常用插件安装 a. 进入扩展视图界面安装/卸载 a1.快捷 ...

  2. Tomcat无法clean,无法remove部署的项目

    错误: 对部署在Tomcat下的项目进行clean操作,总是提示Could not load the Tomcat server configuration,错误信息如图: 解决: 原来是将Serve ...

  3. C/c++中 数组全局变量的定义声明 与 引用声明

    数组声明一次,所有的  定义声明  有切仅有一次! 别人遇到的问题如下: 在声明定义时,定义数组如下: int G_glob[100]; 在其他文件引用时声明如下: int *G_glob; 这样的操 ...

  4. parsley.js验证的基本引用

    前段时间看到博客有些parsley.js验证,只是对parsley.js验证框架基本的应用,对parsley.js更深层理解没有介绍和demo 比如:异步请求,扩展验证的写法,我把我学到的parsle ...

  5. HTML <section> 标签

    实例 文档中的区段,解释了 PRC: <section>   <h1>PRC</h1>   <p>The People's Republic of Ch ...

  6. Codeforces_765_D. Artsem and Saunders_(数学)

    D. Artsem and Saunders time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  7. Nginx 通过 certbot 为网站自动配置 SSL 证书并续期

    一.背景知识 1.1.http 和 https 是什么? 简单来说,http 是一个传输网页内容的协议,比如你看到的 http 开头的网站 http://www.163.com ,其网页上的文字.图片 ...

  8. @ControllerAdvice全局异常拦截

    @ControllerAdvice 拦截异常并统一处理 在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder ...

  9. WebService接口数据传输加密

    1.加密流程 客服端--->加密文本------>服务端接收到加密文本,通过固定加密密文进行解密,然后做相应处理------------>返回结果 2.固定密文创建 密文创建有很多种 ...

  10. ZOJ - 3983 - Crusaders Quest(思维 + 暴力)

    题意: 给出一个字符串,长度为9,包含三种各三个字母"a","g","o",如果一次消除连续三个一样的分数+1,消完自动向左补齐 其中可以消 ...