天上一颗蛋 @ 2018-01-31 17:51:20
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define lid (id << 1)
#define rid (id << 1) | 1
int RD(){
int out = 0,flag = 1;char c = getchar();
while(c < '0' || c > '9'){if(c == '-')flag = -1;c = getchar();}
while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
return out * flag;
}
const int maxn = 1000100;
int num,k;
int a[maxn];
struct sag_tree{
int l,r;
int max,min;
}tree[maxn << 2];
void build(int id,int l,int r){
tree[id].l = l;
tree[id].r = r;
if(l == r){
tree[id].max = a[l];
tree[id].min = a[l];
return;
}
int mid = (l + r) >> 1;
build(lid,l,mid);
build(rid,mid + 1,r);
tree[id].max = max(tree[lid].max,tree[rid].max);
tree[id].min = min(tree[lid].min,tree[rid].min);
}
int querymax(int id,int l,int r){
if(tree[id].l == l && tree[id].r == r){
return tree[id].max;
}
int mid = (tree[id].l + tree[id].r) >> 1;
if(mid < l){
return querymax(rid,l,r);
}
else if(mid >= r){
return querymax(lid,l,r);
}
else{
return max(querymax(lid,l,mid),querymax(rid,mid + 1,r));
}
}
int querymin(int id,int l,int r){
if(tree[id].l == l && tree[id].r == r){
return tree[id].min;
}
int mid = (tree[id].l + tree[id].r) >> 1;
if(mid < l){
return querymin(rid,l,r);
}
else if(mid >= r){
return querymin(lid,l,r);
}
else{
return min(querymin(lid,l,mid),querymin(rid,mid + 1,r));
}
}
int main(){
num = RD();k = RD();
for(int i = 1;i <= num;i++){a[i] = RD();}
build(1,1,num);
for(int i = 1;i <= num - k + 1;i++){
printf("%d ",querymin(1,i,i + k - 1));
//cout<<querymin(1,i,i + k - 1)<<" ";
}
printf("\n");
for(int i = 1;i <= num - k + 1;i++){
printf("%d ",querymax(1,i,i + k - 1));
//cout<<querymax(1,i,i + k - 1)<<" ";
}
return 0;
}
by 览遍千秋 @ 2018-01-31 17:58:58
@天上一颗蛋 应当充分考虑CCF机器的速度
by 微雨燕双飞 @ 2018-02-01 08:14:09
为什么不用单调队列啊?我的几乎全站最慢,也有560ms了。毕竟线段树也不是万能的(仅仅是蒟蒻的个人意见)
by 天上一颗蛋 @ 2018-02-01 09:55:54
蒟蒻单调队列没学过
咳咳,诶呀就是分享一种方法嘛
by nitrobenzene @ 2018-02-01 11:19:51
线段树+scanf/printf,第十个点420ms... 线段树+cin/cout,超时 priority_queue+cin/cout,超时 iostream真慢。。。
by nitrobenzene @ 2018-02-01 11:24:20
@yananfqx priority_queue超时 线段树不超时
by nitrobenzene @ 2018-02-01 11:33:33
@nitrobenzene 。。。我菜鸡看题解发现单调队列不是priority_q...
by 天上一颗蛋 @ 2018-02-01 17:38:12
@nitrobenzene 为什么我快读比你scan还要慢QAQ