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 ...
随机推荐
- GCC编译命令常用选项
GCC是GUN Compiler Collection的简称,除编译程序外,还包含其他相关工具.GCC可将高级语言编写的源代码构建成计算机直接执行的二进制代码.GCC是Linux平台下最常用的编译程序 ...
- Win8应用开发 入门篇(三) UX交互导航模式
导航模式(Windows 应用商店应用) 在本文中 分层模式 画布上导航 顶部应用栏 语义式缩放 相关主题 组织 Windows 应用商店应用中的内容,以便用户可以轻松而直观地进行导航.使用正确的 ...
- C#TreeView节点选中后失去焦点时改变节点背景色
C#TreeView节点选中后失去焦点时改变节点背景色 在使用TreeView控件时候,单击一个节点,当鼠标聚焦到别的地方的时候,之前点击的这个节点就看不清楚了 举例截图 单击后 ...
- Linux 下安装JDK1.8
本文主要介绍的是如何是Linux环境下安装JDK的,因为Linux环境下,很多时候也离不开Java的,下面笔者就和大家一起分享如何jdk1.8的过程吧. 一.安装环境 操作系统:Red Hat Ent ...
- sencha touch 模仿tabpanel导航栏TabBar(2013-11-7)
基于sencha touch 2.2所写 代码: /* *模仿tabpanel导航栏 */ Ext.define('ux.TabBar', { alternateClassName: 'tabBar' ...
- nginx(二)----ubuntu14.04下启动或重启和关闭nginx
/** * lihaibo * 文章内容都是根据自己工作情况实践得出. *如有错误,请指正 *转载请注明出处 */ 一.启动 /usr/local/nginx/sbin/nginx或者cd /usr/ ...
- Apache Server Status详解
Apache的日志如果靠分析日志或者查看服务器进程来监视Apache运行状态的话,比较繁冗.不过在Apache 1.3.2及以后的版本中就自带一个查看Apache状态的功能模块server-statu ...
- 23种设计模式之中介者模式(Mediator)
中介者模式是一种对象的行为型模式,通过一个中介对象来封装一系列的对象交互.中介者使得各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互.中介者对象的存在保证了对象结构上的稳 ...
- VC消息传递(对话框间传递参数)
以下用一个自创的对话框类(MyMessageDlg)向视图类(MessageTestView)发送自定义消息为例,说明这两种不同方法的自定义消息的 消息传递的方法一:使用ON_MESSAGE使用ON_ ...
- vue--自定义指令进行验证(1)
实例代码: <template> <div id="app" class="app"> <h3>{{msg}}</h3 ...