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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; ++i)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;
const int maxn = 140010;
template<typename type>
type read() {
type s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * f;
}
int n, m, blkLen;
int p[maxn], blk[maxn], dep[maxn];
bool vis[maxn];
struct QUERY {
int l, r, id;
friend bool operator< (QUERY a, QUERY b) {
return a.r < b.r;
}
};
struct STK {
int id ,dep;
} stk[maxn * 2];
vector<QUERY> que[maxn];
int solveSameBlock(int l, int r) {
if (l == r) return 1;
int tmp[maxn] = {0};
For(i, l, r) {
tmp[++tmp[0]] = p[i];
}
sort(tmp + 1, tmp + 1 + tmp[0]);
int nowLen = 1, maxLen = 1;
int now = tmp[1];
For(i, 2, tmp[0]) {
if (tmp[i] == now + 1) {
nowLen++;
now = tmp[i];
} else {
maxLen = max(maxLen, nowLen);
nowLen = 1;
now = tmp[i];
}
}
maxLen = max(maxLen, nowLen);
return maxLen;
}
int top, maxx; // (dep)
class DJU {
public:
int fa[maxn];
void init(int n) {
For(i, 1, n) {
fa[i] = i;
}
}
int findFa(int a) {
return (fa[a] != a) ? fa[a] = findFa(fa[a]) : a;
}
void merge(int a, int b) {
int faa = findFa(a), fab = findFa(b);
if (faa == fab) return;
if (dep[faa] < dep[fab]) swap(faa, fab);
maxx = max(maxx, dep[faa] + dep[fab]);
fa[fab] = faa;
stk[++top] = (STK){faa, dep[faa]};
stk[++top] = (STK){fab, dep[fab]};
dep[faa] += dep[fab];
}
} dj;
void mge(int a) {
vis[a] = 1;
if (vis[a - 1]) dj.merge(a - 1, a);
if (vis[a + 1]) dj.merge(a, a + 1);
}
int ans[maxn];
void solveDiffBlock(int now, vector<QUERY> &v) {
maxx = 1, top = 0;
int pos = min(maxn, now * blkLen) + 1;
int lstPos, lstMaxx, l = pos, r = pos - 1;
For(i, 0, n) {
dj.fa[i] = i;
dep[i] = 1;
vis[i] = 0;
}
for (auto i : v) {
while (r < i.r) mge(p[++r]);
lstMaxx = maxx;
lstPos = top;
while (l > i.l) mge(p[--l]);
ans[i.id] = maxx;
maxx = lstMaxx;
while (top > lstPos) {
dj.fa[stk[top].id] = stk[top].id;
dep[stk[top].id] = stk[top].dep;
top--;
}
while (l < pos) vis[p[l++]] = 0;
}
}
int main() {
n = read<int>();
m = read<int>();
dj.init(n);
blkLen = n / sqrt(m);
For(i, 1, n) {
p[i] = read<int>();
blk[i] = (i - 1) / blkLen + 1;
}
For(i, 1, m) {
int l, r;
l = read<int>();
r = read<int>();
if (blk[l] == blk[r]) {
ans[i] = solveSameBlock(l, r);
} else {
que[blk[l]].push_back((QUERY){l, r, i});
}
}
For(i, 1, blk[n]) {
sort(que[i].begin(), que[i].end());
solveDiffBlock(i, que[i]);
}
For(i, 1, m) {
cout << ans[i] << endl;
}
return 0;
}
|