poj3468A Simple Problem with Integers(线段树的区域更新)
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(线段树的区域更新)的更多相关文章
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- A Simple Problem with Integers(线段树,区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 83822 ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- poj3468A Simple Problem with Integers(线段树,在段更新时要注意)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- POJ 3468 A Simple Problem with Integers (线段树成段更新)
题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- 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 ...
随机推荐
- 原生js--兼容获取窗口滚动条位置和窗口大小的方法
各个浏览器对获取获取窗口滚动条位置和窗口大小没有提供统一的API,以下是对其封装,解决兼容性问题 /** * 获取浏览器视口的大小(显示文档的部分) * */function getViewPort ...
- Python汉字转换成拼音
最近在使用Python做项目时,需要将汉字转化成对应的拼音. 网上的一些包大多是python2.x的,使用下面这个包,支持python3.6 xpinyin 0.5.5 >>> fr ...
- win10下网狐荣耀手机端android app编译
基于荣耀版(2017.5.21)12 款游戏..7z这款游戏,网上有下载的 1.解压后进入 cd shoujiduan 2.将client/base复制到client/ciphercode/下,也就是 ...
- javaweb基础 02--javaweb基础概念
1.WEB资源 * 静态web资源:指web页面中供人们浏览的数据始终是不变(如 html 页面). * 动态web资源:指web页面中供人们浏览的数据是由程序产生的,不同时间点访问web页面看到的内 ...
- Unity3D Animator控制参数和添加事件
Animator控制参数和添加事件 using UnityEngine; using System.Collections; public class AniControl : MonoBehavio ...
- oracle如何删除表空间
drop tablespace 表空间名 including contents and datafiles cascade constraint; ............. 以system用户登录, ...
- swiper监听左滑还是右滑动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Linux下pip使用国内源
pip国内的一些镜像 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/si ...
- NEFU 84 - 五指山 - [exgcd求解一元线性同余方程]
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=84 Time Limit:1000ms Memory Limit ...
- 0004python中的map,reduce,lambda,filter
编程实现:a[0]*b[0] + a[1]*b[1] +...+a[i]*b[j] >>> a=[1,2,3,4,5]>>> b=[6,7,8,9,0] >& ...