这道题吧 没计算时间 因为给了那么多 一算还可以

就直接写了线段树,刘汝佳那本模板

然后!poj的g++比C++慢大约500ms。。。。。。。g++tle,C++就过了

Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 67576   Accepted: 19163
Case Time Limit: 5000MS

Description

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and k is 3.

Window position Minimum value Maximum value
[1  3  -1] -3  5  3  6  7  -1 3
 1 [3  -1  -3] 5  3  6  7  -3 3
 1  3 [-1  -3  5] 3  6  7  -3 5
 1  3  -1 [-3  5  3] 6  7  -3 5
 1  3  -1  -3 [5  3  6] 7  3 6
 1  3  -1  -3  5 [3  6  7] 3 7

Your task is to determine the maximum and minimum values in the sliding window at each position.

Input

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Source

POJ Monthly--2006.04.28, Ikki

上代码 通俗易懂哦~~~~~~看我的就行了

#include<iostream>
#include<cstdio>
#include<cstring>using namespace std;int Max[5000100];
int Min[5000100];
int pinf = 2000000000;
int ninf = -2000000000;
int n,k;void build(int l,int r,int cur){if(l == r){scanf("%d",&Max[cur]);Min[cur] = Max[cur];return;}int mid = (l + r)/2;build(l,mid,cur*2);build(mid + 1,r,cur*2 + 1);Min[cur] = min(Min[cur*2],Min[cur*2 + 1]);Max[cur] = max(Max[cur*2],Max[cur*2 + 1]);return;
}int ql,qr;
int queryMax(int l,int r,int cur){if(ql <= l&&r <= qr){return Max[cur];}int mid = (l + r)/2;int ans = ninf;if(ql <= mid){ans = max(ans,queryMax(l,mid,cur*2));}if(qr > mid){ans = max(ans,queryMax(mid + 1,r,cur*2 +1));}return ans;
}
int queryMin(int l,int r,int cur){if(ql <= l && r <= qr){return Min[cur];}int mid = (l + r)/2;int ans = pinf;if(ql <= mid){ans = min(ans,queryMin(l,mid,cur*2));}if(qr > mid){ans = min(ans,queryMin(mid + 1,r,cur*2 + 1));}return ans;
}int main(){while(scanf("%d%d",&n,&k) != EOF){build(1,n,1);ql = 1;qr = k;printf("%d",queryMin(1,n,1));ql++;qr++; while(qr <= n){printf(" %d",queryMin(1,n,1));ql++;qr++;}printf("\n");ql = 1;qr = k;printf("%d",queryMax(1,n,1));ql++;qr++; while(qr <= n){printf(" %d",queryMax(1,n,1));ql++;qr++;}printf("\n");}return 0;
}

转载于:https://www.cnblogs.com/xuyanqd/p/9073920.html

poj2823 线段树模板题 点修改(也可以用单调队列)相关推荐

  1. 线段树模板题3:区间染色问题

    1.3线段树模板题3:区间染色问题 在DotA游戏中,帕吉的肉钩实际上是大多数英雄中最恐怖的东西.挂钩由长度相同的几个连续的金属棍组成. 现在,帕吉(Pudge)希望对挂接进行一些操作. 让我们将钩子 ...

  2. J.哭泣的阿木木(线段树模板题)

    哭泣的阿木木 Description 没啥用的背景故事: 在远古的恕瑞玛,有一个孤独而又忧郁的灵魂,阿木木.他在世间游荡,只为找到一个朋友.他遭受了一种远古的巫术诅咒,注定忍受永世的孤单,因为被他触碰 ...

  3. 【线段树】[LUOGU 守墓人] [LUOGU 维护序列] 线段树模板题

    题目: 题目链接:[LUOGU 守墓人] 题解: 线段树单点修改,区间修改,单点查询,区间查询,一系列线段树基本操作,模板打就好. (回头再补一个分块和树状数组的这种板子题,就是用分块和树状数组再写一 ...

  4. 试题 算法训练 操作格子(线段树模板题)

    资源限制 内存限制:256.0MB C/C++时间限制:1.0s Java时间限制:3.0s Python时间限制:5.0s 问题描述 有n个格子,从左到右放成一排,编号为1-n. 共有m次操作,有3 ...

  5. hdu1156(简单线段树 模板题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  6. UESTC - 1057 秋实大哥与花 线段树模板题

    http://acm.uestc.edu.cn/#/problem/show/1057 题意:给你n个数,q次操作,每次在l,r上加上x并输出此区间的sum 题解:线段树模板, #define _CR ...

  7. 树套树 ---- 树状数组套权值线段树模板题 P2617 Dynamic Rankings 动态第K大

    题目链接 题目大意: 给你一个数组aaa,aaa有两个操作 询问aaa中[l,r][l,r][l,r]区间里面第kkk小的数是哪个? 修改axa_xax​为yyy 解题思路: 首先我们知道权值线段树是 ...

  8. 洛谷 P3373 线段树模板题

    链接:https://www.luogu.com.cn/problem/P3373 题意:一个区间 三种操作 1 给lr范围内乘一个数 2 给lr范围内加一个数 3 询问lr范围内的和 啊这题真·做了 ...

  9. 洛谷OJ U552 守墓人 线段树模板题

    题目描述 Description 在一个荒凉的墓地上 有一个令人尊敬的守墓人, 他看守的墓地从来 没有被盗过, 所以人们很放心的把自己的先人的墓 安顿在他那 守墓人能看好这片墓地是必然而不是偶然... ...

最新文章

  1. 石锤!谷歌排名第一的编程语言,死磕这点,程序员都收益
  2. git解决pre-commit hook failed的问题
  3. 有没有办法检查`null`和`undefined`?
  4. 程序员为什么要单身?
  5. nvidia驱动程序与windows版本不兼容
  6. war项目主页跳转找不到_找不到优秀的开源项目?快来看看 11 月入选的码云 GVP...
  7. FireWork 制作android 应用程序 icon
  8. linux内核_查看Linux内核版本
  9. 【POJ 2503】Babelfish(水题)stl map存取即可
  10. android调用系统相机图片不旋转,Android 处理调用系统相机生成的被旋转图片
  11. js笔记(五)文档对象模型DOM
  12. const char *p、char const *p、char *const p
  13. mysql current_timestamp 不自动更新_MySQL ON UPDATE CURRENT_TIMESTAMP不更新
  14. 移动端拖拽排序 html,移动端拖拽排序
  15. django的视图与模板
  16. 代码能跑就不要动,为什么我们都会有这样的想法?
  17. pxe网络克隆工具_Carbon Copy Cloner for Mac(全盘系统备份克隆)
  18. 【GDB调试学习笔记】调试逻辑错误
  19. c语言 五个学生学号 姓名 三门,有五个学生,每个学生的数据包括学号、姓名、三门课的成绩,从键盘输入五个学生的数据,要求打印三门课总平均...
  20. “ORA-01017(:用户名/口令无效; 登录被拒绝)” ORA-28000: the accout is locked(用户账号被锁)解决方法

热门文章

  1. JSunpack-n的安装与简单使用
  2. 自定义注解实现业务分发
  3. c语言蓝桥十进制转十六进制,蓝桥杯 基础练习 十进制转十六进制
  4. 错误 未找到引用源_你不理解的EXCEL函数中常见的错误值,都在这里
  5. android udp和tcp区别,UDP模式与TCP模式的区别
  6. python threading模块的方法_Python THREADING模块中的JOIN()方法深入理解
  7. hive 解密_hive 中自定义 base64 加密 解密 UDF 函数
  8. mysql 主键 uniqo_优衣库某处SQL注入可导致移动平台被劫持
  9. 如何用 Nacos 构建服务网格生态
  10. mysql查询索引数组_mysql-索引