Garden

Time Limit: 1000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 4047
64-bit integer IO format: %lld      Java class name: Main

There are n flowerpots in Daniel's garden.  These flowerpots are placed in n positions, and these n positions are numbered from 1 to n. Each flower is assigned an aesthetic value. The aesthetic values vary during different time of a day and different seasons of a year. Naughty Daniel is a happy and hardworking gardener who takes pleasure in exchanging the position of the flowerpots.
Friends of Daniel are great fans of his miniature gardens. Before they visit Daniel's home, they will take their old-fashioned cameras which are unable to adjust the focus so that it can give a shot of exactly k consecutive flowerpots. Daniel hopes his friends enjoy themselves, but he doesn't want his friend to see all of his flowers due to some secret reasons, so he guides his friends to the best place to catch the most beautiful view in the interval [x, y], that is to say, to maximize the sum of the aesthetics values of the k flowerpots taken in one camera shot when they are only allow to see the flowerpots between position x to position y.
There are m operations or queries are given in form of (p, x, y), here p = 0, 1 or 2. The meanings of different value of p are shown below.
1. p = 0  set the aesthetic value of the pot in position x as y. (1 <= x <= n; -100 <= y <= 100)
2. p = 1  exchange the pot in position x and the pot in position y. (1 <= x, y <= n; x might equal to y)
3. p = 2  print the maximum  sum of  aesthetics values of one camera shot in interval [x, y].  (1 <= x <= y <= n; We guarantee that y-x+1>=k)
 

Input

There are multiple test cases.
The first line of the input file contains only one integer indicates the number of test cases.
For each test case, the first line contains three integers: n, m, k (1 <= k <= n <= 200,000; 0 <= m <= 200,000).
The second line contains n integers indicates the initial aesthetic values of flowers from  position  1 to  position  n. Some flowers are sick, so their aesthetic values are negative integers. The aesthetic values range from -100 to 100.  (Notice: The number of position is assigned 1 to n from left to right.)
In the next m lines, each line contains a triple (p, x, y). The meaning of triples is mentioned above.
 

Output

For each query with p = 2, print the maximum sum of the aesthetics values in one shot in interval [x, y].

 

Sample Input

1
5 7 3
-1 2 -4 6 1
2 1 5
2 1 3
1 2 1
2 1 5
2 1 4
0 2 4
2 1 5

Sample Output

4
-3
3
1
6

Source

 
解题:线段树,由于区间长度为k,即k是固定的,我们只要使得线段树的每个叶节点代表一个长度为k的区间的和即可。
 
1..k 2..k+1 3..k+2 ...
 
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = ;
struct node {
int lt,rt,maxv,lazy;
} tree[maxn<<];
int d[maxn],a[maxn],n,m,k;
void pushup(int v) {
tree[v].maxv = max(tree[v<<].maxv + tree[v<<].lazy,tree[v<<|].maxv+tree[v<<|].lazy);
}
void pushdown(int v) {
if(tree[v].lazy) {
tree[v<<].lazy += tree[v].lazy;
tree[v<<|].lazy += tree[v].lazy;
tree[v].lazy = ;
}
}
void build(int lt,int rt,int v) {
tree[v].lt = lt;
tree[v].rt = rt;
tree[v].lazy = ;
if(lt == rt) {
tree[v].maxv = d[tree[v].lt + k - ] - d[tree[v].lt-];
return;
}
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
pushup(v);
}
void update(int lt,int rt,int val,int v){
if(lt <= tree[v].lt && rt >= tree[v].rt){
tree[v].lazy += val;
return;
}
pushdown(v);
if(lt <= tree[v<<].rt) update(lt,rt,val,v<<);
if(rt >= tree[v<<|].lt) update(lt,rt,val,v<<|);
pushup(v);
}
int query(int lt,int rt,int v){
if(lt <= tree[v].lt && rt >= tree[v].rt) return tree[v].maxv + tree[v].lazy;
pushdown(v);
int ans = -0x3f3f3f3f;
if(lt <= tree[v<<].rt) ans = max(ans,query(lt,rt,v<<));
if(rt >= tree[v<<|].lt) ans = max(ans,query(lt,rt,v<<|));
pushup(v);
return ans;
}
int main() {
int T,p,x,y;
scanf("%d",&T);
while(T--) {
scanf("%d %d %d",&n,&m,&k);
for(int i = ; i <= n; ++i) {
scanf("%d",d+i);
a[i] = d[i];
d[i] += d[i-];
}
build(,n-k+,);
while(m--){
scanf("%d %d %d",&p,&x,&y);
if(!p){
update(max(,x - k + ),min(n-k+,x),y - a[x],);
a[x] = y;
}else if(p == ){
update(max(,x - k + ),min(n-k+,x),a[y] - a[x],);
update(max(,y - k + ),min(n-k+,y),a[x] - a[y],);
swap(a[x],a[y]);
}else if(p == ) printf("%d\n",query(x,y-k+,));
}
}
return ;
}

POJ 4047 Garden的更多相关文章

  1. POJ 4047 Garden 线段树 区间更新

    给出一个n个元素的序列,序列有正数也有负数 支持3个操作: p x y 0.p=0时,把第x个的值改为y 1.p=1时,交换第x个和第y个的值 2.p=2时,问区间[x,y]里面连续k个的子序列的最大 ...

  2. poj 3262 Protecting the Flowers

    http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Tota ...

  3. POJ 1518 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】

    链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  4. POJ 1584 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】

    链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  5. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  6. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  7. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  8. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  9. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

随机推荐

  1. perl JSON模块使用

    简介: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 它基于JavaScript(Standard ECMA-262 3rd Edition - De ...

  2. python -迭代器与生成器 以及 iterable(可迭代对象)、yield语句

    我刚开始学习编程没多久,对于很多知识还完全不知道,而有些知道的也是一知半解,我想把学习到的知识记录下来,一是弥补记忆力差的毛病,二也是为了待以后知识能进一步理解透彻时再回来做一个补充. 参考链接: 完 ...

  3. CSS浏览器兼容问题集(一)

    CSS对浏览器的兼容性有时让人非常头疼,也许当你了解其中的技巧跟原理,就会认为也不是难事,从网上收集了IE7,6与Fireofx的兼容性处理方法并整理了一下.对于web2.0的过度,请尽量用xhtml ...

  4. C++windows内核编程笔记day11 win32静态库和动态库的使用

    windows库程序: 静态库: 源码被链接到调用的程序或动态库,被调用时,代码最少有1份,文件后缀.LIB 动态库: 函数被程序或其它动态库调用,被调用时,代码仅仅有1份,文件后缀.DLL 静态库( ...

  5. linux log日志解析

    linux log日志解析   其实,可以说成是监控系统的记录,系统一举一动基本会记录下来.这样由于信息非常全面很重要,通常只有 root 可以进行视察!通过登录文件(日志文件)可以根据屏幕上面的错误 ...

  6. Linux下图解minicom安装

    Linux下图解minicom安装 minicom是一个串口通信工具,就像Windows下的HyperTerminal.可用来与串口设备通信,如调试交换机和Modem等.它的Ubuntu软件包的名称就 ...

  7. springmvc使用jQuery.datatable时遇到的错误

    springmvc使用jQuery.datatable时遇到的错误 1正确时返回 josn结果: {,,,,,,,,,,,,,,,,"} 页面显示处理中(processing),列表空白,其 ...

  8. Firefox 浏览器有用的插件

    1.Undo Closed Tabs Button或Undo Closed Tabs Button (revived) 恢复关闭的标签页 2.NetVideohunter Video Download ...

  9. java关于File.separator

    写好代码在模拟环境下测试,完全没问 题:但linux+tomcat下用的时候,却老是报告“No such file or diretory ”的异常,上传不了.后来发现是文件路径的问题.我的模拟测试环 ...

  10. COWRUN

    USACO COWRUN 随机化搜索+双重递归调用 题面描述:给出8*N(<=14)组牌,每次按顺序选择8张,FJ可以选择前4张或者后4张,COW从FJ选出的牌中选择前两张或者后两张,然后COW ...