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

真心觉得这题坑死我了,一直错,怎么改也没戏,最后tjj把q[rt].lz改成了long long 就对了,真心坑啊。

线段树的区域更新。

线段树功能:update:成段增减 query:区间求和

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define N 100002
struct node
{
int l,r;
long long lz,w;
}q[*N];
void pushup(int rt)
{
q[rt].w=q[rt*].w+q[rt*+].w;
}
void pushdown(int rt,int m)
{
if(q[rt].lz)
{
q[rt*].lz+=q[rt].lz;
q[rt*+].lz+=q[rt].lz;
q[rt*].w+=(m-m/)*q[rt].lz;
q[rt*+].w+=(m/)*q[rt].lz;
q[rt].lz=;
}
}
void build(int l,int r,int rt)
{
q[rt].l=l;
q[rt].r=r;
q[rt].lz=;
q[rt].w=;
if(l==r)
{
scanf("%lld",&q[rt].w);
return ;
}
build(l,(r+l)/,rt*);
build((r+l)/+,r,rt*+);
pushup(rt);
}
void update(int key,int ll,int rr,int l,int r,int rt)
{
if(ll<=l&&rr>=r)
{
q[rt].w+=(long long)key*(r-l+);
q[rt].lz+=key;
return ;
}
pushdown(rt,r-l+);
int m=(r+l)/;
if(ll<=m)
update(key,ll,rr,l,m,rt*);
if(rr>m)
{
update(key,ll,rr,m+,r,rt*+);
}
pushup(rt);
}
long long query(int ll,int rr,int l,int r,int rt)
{
if(ll<=l&&rr>=r)
{
return q[rt].w;
}
pushdown(rt,r-l+);
int m=(r+l)/;
long long L=;
if(ll<=m) L+=query(ll,rr,l,m,rt*);
if(rr>m) L+=query(ll,rr,m+,r,rt*+);
return L;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
build(,n,);
while(m--)
{
char op[];
int a , b , c;
scanf("%s",op);
if (op[] == 'Q')
{
scanf("%d%d",&a,&b);
long long tt=query(a,b,,n,);
printf("%lld\n",tt);
}
else if(op[]=='C')
{
scanf("%d%d%d",&a,&b,&c);
update(c,a,b,,n,);
}
}
}
return ; }

感觉这个博客不错http://www.douban.com/note/273509745/

poj3468A Simple Problem with Integers(线段树的区域更新)的更多相关文章

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

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

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

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

  3. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

  4. poj3468A Simple Problem with Integers(线段树,在段更新时要注意)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  5. POJ 3468 A Simple Problem with Integers (线段树成段更新)

    题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...

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

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

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

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

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

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

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

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

  10. 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 ...

随机推荐

  1. Qt下libusb-win32的使用(二)批量读写操作

    一.概述 学习libusb-win32的使用.使用批量传输方式与USB开发板进行数据读.写操作.上位机使用Qt做界面, 使用USB开发板的端点2作为批量传输端点. 二.实现 代码比较简单,直接给出,如 ...

  2. 浅谈CSS盒子模型

    [摘要]盒子模型是CSS中的一个重要概念,虽然CSS中没有盒子这个单独的属性对象,但它却是CSS中无处不在的一个重要组成部分.掌握盒子模型的原理和使用方法可以极大地丰富HTML元素的表现效果,同时对于 ...

  3. Eclipse安装php插件phpeclipse(转)

    1.PHPEclipse是Eclipse的一个用于开发PHP的插件.官网下载地址:http://sourceforge.net/projects/phpeclipse/下载解压后,将features和 ...

  4. EXPLAIN 具体含义 ( type possible_key key key_len ref )

  5. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十七:IIC储存模块 - FIFO读写

    . int main() . { . int A: . A = : . } 代码17.1 话题为进入之前,首先让我们来聊聊一些题外话.那些学过软核NIOS的朋友可曾记得,软核NIOS可利用片上内存作为 ...

  6. C语言位操作--判断整数是否为2的幂

    unsigned int v; // 判断v是否为2的幂 bool f; // f为判断的结果 f = (v & (v - 1)) == 0; // 结果为0表示不是2 的幂 // 改变表示方 ...

  7. Angular打开页面隐藏显示表达式

    1.使用 ng-cloak, 同时要在css加入一行 [ng-cloak] {display: none;} 样式 [ng:cloak], [ng-cloak], [data-ng-cloak], [ ...

  8. python pandas 豆瓣电影 top250 数据分析

    豆瓣电影top250数据分析 数据来源(豆瓣电影top250) 爬虫代码比较简单 数据较为真实,可以进行初步的数据分析 可以将前面的几篇文章中的介绍的数据预处理的方法进行实践 最后用matplotli ...

  9. HOJ 2156 &POJ 2978 Colored stones(线性动规)

    Colored stones Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1759 Accepted: 829 Descrip ...

  10. python2和python3的不同

    1.性能 Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可 以取得很好的优化结果. Py3.1性能比Py2 ...