Steven_MengのBlog

传送门

线段树模板题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
#define MAXN 1000005
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9'){
if (x=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+(ch^'0');
ch=getchar();
}
return x*f;
}
int a[MAXN];
namespace SegmentTree{
struct node{
int l,r;
int maxn,mino;
}tree[MAXN<<2];
#define lc i<<1
#define rc i<<1|1
inline void pushup(int i){
tree[i].maxn=max(tree[lc].maxn,tree[rc].maxn);
tree[i].mino=min(tree[lc].mino,tree[rc].mino);
}
void Build(int i,int l,int r){
tree[i].l=l,tree[i].r=r;
if (l==r){
tree[i].maxn=tree[i].mino=a[l];
return ;
}
int mid=(l+r)>>1;
Build(lc,l,mid);
Build(rc,mid+1,r);
pushup(i);
}
int QueryMax(int i,int L,int R){
if (L<=tree[i].l&&tree[i].r<=R){
return tree[i].maxn;
}
int mid=(tree[i].l+tree[i].r)>>1;
int ans=-0x7fffffff;
if (L<=mid) ans=max(ans,QueryMax(lc,L,R));
if (mid<R) ans=max(ans,QueryMax(rc,L,R));
return ans;
}
int QueryMin(int i,int L,int R){
if (L<=tree[i].l&&tree[i].r<=R){
return tree[i].mino;
}
int mid=(tree[i].l+tree[i].r)>>1;
int ans=0x7fffffff;
if (L<=mid) ans=min(ans,QueryMin(lc,L,R));
if (mid<R) ans=min(ans,QueryMin(rc,L,R));
return ans;
}
}
using namespace SegmentTree;
int main(){
int n=read(),m=read(),c=read();
for (register int i=1;i<=n;++i) a[i]=read();
Build(1,1,n);
bool flag=false;
for (register int i=1;i<=n-m+1;++i){
int Max=QueryMax(1,i,i+m-1),Min=QueryMin(1,i,i+m-1);
if (Max-Min<=c){
printf("%d\n",i);
flag=true;
}
}
if (!flag) printf("NONE\n");
}

 评论