(线段树模板)A Simple Problem with Integers --POJ--3468
链接:
http://poj.org/problem?id=3468
代码:
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
using namespace std; #define Lson r<<1
#define Rson r<<1|1 const int N = 1e5+; struct SegmentTree
{
int L, R;
long long sum, e;
int Mid()
{
return (L+R)>>;
}
int len()
{
return R-L+;
}
}a[N<<]; void BuildSegTree(int r, int L, int R)
{
a[r].L=L, a[r].R=R;
a[r].e=; if(L==R)
{
scanf("%lld", &a[r].sum);
return ;
} BuildSegTree(Lson, L, a[r].Mid());
BuildSegTree(Rson, a[r].Mid()+, R); a[r].sum = a[Lson].sum + a[Rson].sum;
}
void PushDown(int r)
{
a[Lson].sum += a[r].e*a[Lson].len();
a[Lson].e += a[r].e;
a[Rson].sum += a[r].e*a[Rson].len();
a[Rson].e += a[r].e; a[r].e=;
}
void Update(int r, int L, int R, int e)
{
a[r].sum += (R-L+)*e; if(a[r].L==L && a[r].R==R)
{
a[r].e += e;
return ;
} PushDown(r); if(R<=a[r].Mid())
Update(Lson, L, R, e);
else if(L>a[r].Mid())
Update(Rson, L, R, e);
else
{
Update(Lson, L, a[r].Mid(), e);
Update(Rson, a[r].Mid()+, R, e);
}
}
long long Query(int r, int L, int R)
{
if(a[r].L==L && a[r].R==R)
return a[r].sum; PushDown(r); if(R<=a[r].Mid())
return Query(Lson, L, R);
else if(L > a[r].Mid())
return Query(Rson, L, R);
else
{
long long Lsum = Query(Lson, L, a[r].Mid());
long long Rsum = Query(Rson, a[r].Mid()+, R); return Lsum+Rsum;
}
} int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
BuildSegTree(, , n); while(m--)
{
char s[];
int L, R, e; scanf("%s", s); if(s[]=='Q')
{
scanf("%d%d", &L, &R);
printf("%lld\n", Query(, L, R));
}
else
{
scanf("%d%d%d", &L, &R, &e);
Update(, L, R, e);
}
}
}
return ;
}
(线段树模板)A Simple Problem with Integers --POJ--3468的更多相关文章
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)
A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...
- A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 69589 ...
- 【POJ3468】【zkw线段树】A Simple Problem with Integers
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)
参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...
- A Simple Problem with Integers POJ - 3468 (线段树)
思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...
- A Simple Problem with Integers~POJ - 3468
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- A Simple Problem with Integers POJ - 3468 (分块)
题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...
- 【算法系列学习】线段树 区间修改,区间求和 [kuangbin带你飞]专题七 线段树 C - A Simple Problem with Integers
https://vjudge.net/contest/66989#problem/C #include<iostream> #include<cstdio> #include& ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
随机推荐
- 双机\RAC\Dataguard的区别
Oracle 双机/RAC/Dataguard的区别 Data Guard 是Oracle的远程复制技术,它有物理和逻辑之分,但是总的来说,它需要在异地有一套独立的系统,这是两套硬件配置可以不同的系统 ...
- oc NSLog输出格式大全
本文的内容是总结了一下iOS开发中NSLog输出格式大全,虽然比较基础,但有总结毕竟会各位正在学习iOS开发的朋友们一些小小的帮助. %@ 对象 %d, %i ...
- JDK和Tomcat部署时,版本不同的问题解决
问题: 在以Tomcat作为Web容器,启动java Web工程时,遇到下面问题:org.eclipse.jdt.internal.compiler.classfmt.ClassFormatExcep ...
- 趣味编程:CPS风格代码(C#,F#版)
CPS风格代码(C#版) using System; namespace fp { class CPS { static int add(int x, int y) => x + y; stat ...
- (1)shiro简介和第一个demo
之前一直在用shiro开发,不过只是会使用,并没有深入了解,最近有时间学习了一下,把最近学习所得分享一下. shiro简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授 ...
- SQL获取分组后取某字段最大一条记录(求每个类别中最大的值的列表)
获取分组后取某字段最大一条记录 方法一:(效率最高) select * from test as a where typeindex = (select max(b.typeindex) from t ...
- Eclipse中Git插件使用技巧:还原文件
如果修改了某个文件并未提交至本地库(add index),那么怎么还原呢?Git插件中并不像Svn插件直接提供有还原方式.其实无论是否提交至本地库或者远程库,还原操作的本质都是将文件的当前版本还原至之 ...
- mysql datetime与timestamp精确到毫秒的问题
CREATE TABLE `tab1` (`tab1_id` VARCHAR(11) DEFAULT NULL,`create` TIMESTAMP(3) NULL DEFAULT NULL,`cre ...
- 慕课网access_token的获取
access_token的接口是微信公众号一个基础接口,access_token接口微信公众号一个非常重要的接口 access_token是微信公众号的全局唯一票据,公众号的所有接口的调用都需要使用到 ...
- 源码安装php时出现Sorry, I cannot run apxs. Possible reasons follow:
1.可能的原因是你没有安装perl > yum install perl > yum install httpd-devel 2.在你apache安装目录下的bin下找到apxs,并用vi ...