POJ 4047 Garden 线段树 区间更新
给出一个n个元素的序列,序列有正数也有负数
支持3个操作:
p x y
0.p=0时,把第x个的值改为y
1.p=1时,交换第x个和第y个的值
2.p=2时,问区间[x,y]里面连续k个的子序列的最大和(保证y-x+1>=k)
我们只要定义数组v
v[i]表示原序列中,从第i个开始,连续k个元素的值的和
然后我们只需要维护一棵线段树,树的叶子节点表示数组v
树的节点维护:
区间[l,r]中,连续k个的子序列的最大和,即数组v的最大值
这样的话,3个操作就变为:
0.把区间[max(x-k+1,0),x]的值加y-init_v[x]
1.区间[max(x-k+1,0),x]加上init_v[y]-init_v[x]
区间[max(y-k+1,0),y]加上init_v[x]-init_v[y]
交换init_v[x]和init_v[y]的值
2.求max[x,y-k+1]
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; #define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 const int maxn=+;
const int inf=0x3f3f3f3f; int init_v[maxn];
int init_sum[maxn];
int v[maxn<<];
int lazy[maxn<<];
int n,m,k; void solve(); int main()
{
int test;
scanf("%d",&test);
while(test--){
scanf("%d %d %d",&n,&m,&k);
for(int i=;i<=n;i++){
scanf("%d",&init_v[i]);
}
solve();
}
return ;
} void pushup(int rt)
{
v[rt]=max(v[rt<<],v[rt<<|]);
} void pushdown(int rt)
{
if(lazy[rt]){
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
v[rt<<]+=lazy[rt];
v[rt<<|]+=lazy[rt];
lazy[rt]=;
}
} void build(int l,int r,int rt)
{
if(l==r){
v[rt]=init_sum[l+k-]-init_sum[l-];
return ;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
} void update(int L,int R,int add,int l,int r,int rt)
{
if(L<=l&&R>=r){
lazy[rt]+=add;
v[rt]+=add;
return ;
}
int m=(l+r)>>;
pushdown(rt);
if(L<=m)
update(L,R,add,lson);
if(R>m)
update(L,R,add,rson);
pushup(rt);
} int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r){
return v[rt];
}
int m=(l+r)>>;
pushdown(rt);
int ret=-inf;
if(L<=m)
ret=max(ret,query(L,R,lson));
if(R>m)
ret=max(ret,query(L,R,rson)); return ret;
} void solve()
{
init_sum[]=;
for(int i=;i<=n;i++){
init_sum[i]=init_sum[i-]+init_v[i];
} build(,n,);
memset(lazy,,sizeof lazy);
for(int i=;i<=m;i++){
int p,x,y;
scanf("%d %d %d",&p,&x,&y);
if(p==){
update(max(x-k+,),x,y-init_v[x],,n,);
init_v[x]=y;
}
else if(p==){
update(max(x-k+,),x,init_v[y]-init_v[x],,n,);
update(max(y-k+,),y,init_v[x]-init_v[y],,n,);
swap(init_v[x],init_v[y]);
}
else{
printf("%d\n",query(x,y-k+,,n,));
}
}
return ;
}
POJ 4047 Garden 线段树 区间更新的更多相关文章
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- (简单) POJ 3468 A 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(线段树区间更新模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 141093 ...
随机推荐
- caffe: fuck compile error again : error: a value of type "const float *" cannot be used to initialize an entity of type "float *"
wangxiao@wangxiao-GTX980:~/Downloads/caffe-master$ make -j8find: `wangxiao/bvlc_alexnet/spl': No suc ...
- myeclipse10安装egit和使用
一.下载egit插件并安装到eclipse 下载egit插件包,然后解压放到Eclipse的dropins文件夹内或者直接放到对应的文件夹下 二.安装成功(window->preferences ...
- CentOS怎样强制卸载PHP以及自定义安装PHP
很无语,CentOS居然php版本才5.1.6,很多开源的CMS无法安装. 查看php版本命令: #php -v 这个命令是删除不干净的 #yum remove php 因为使用这个命令以后再用 #p ...
- DDD, MVC & Entity Framework
https://digitalpolis.co.uk/software-thoughts/ddd-mvc-entity-framework/ Data Points - Coding for Doma ...
- linux mount (挂载命令)详解
挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式:mount [-t vfstype] [-o option ...
- Castle
Castle AOP 系列(一):对类方法调用的拦截(有源码) 标签: aopAOPCastle对类方法调用的拦截 2012-11-09 16:51 4207人阅读 评论(1) 收藏 举报 分类: ...
- EDIUS分别输出视频和音频的教程
使用EDIUS剪辑好视频之后渲染输出文件时,为什么视频和音频分别出,而不是一起呢?这个问题很可能会让一些新手困惑不已,到底是什么原因呢?又要如何设置才能让输出的文件视音频一体呢?下面,小编将来和大家探 ...
- Open vSwitch
https://github.com/openvswitch/ovs/blob/master/INSTALL.RHEL.md
- unity, SerializedObject.FindProperty不要写在Editor的OnEnable里,要写在OnInspectorGUI里
如果像下面这样写: using UnityEngine;using System.Collections;using UnityEditor;using System.Collections.Gene ...
- Cobertura 代码覆盖率测试
Cobertura 是一种开源工具,它通过检测基本的代码,并观察在测试包运行时执行了哪些代码和没有执行哪些代码,来测量测试覆盖率.除了找出未测试到的代码并发现 bug 外,Cobertura 还可以通 ...