-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1997E.cpp
95 lines (82 loc) · 1.48 KB
/
1997E.cpp
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
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
void solve()
{
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
int root = sqrt(n);
if (root * root < n)
root++;
const int criteria = min(n, root * 5 / 2);
vector<vector<int> > lvlup(criteria);
for (int k = 1; k <= criteria; k++)
{
for (int i = 0, l = 1, x = 0; i < n; i++)
{
if (a[i] >= l)
{
x++;
if (x == k)
{
x = 0;
l++;
lvlup[k - 1].push_back(i + 1);
}
}
}
}
const int lmt = (n + criteria - 1) / criteria + 1;
vector<vector<int> > frq(lmt, vector<int>(n + 1));
vector<vector<int> > occ(lmt);
for (int lvl = 0; lvl < lmt; lvl++)
{
frq[lvl][0] = 0;
for (int i = 0; i < n; i++)
{
frq[lvl][i + 1] = frq[lvl][i] + (a[i] >= lvl + 1);
if (a[i] >= lvl + 1)
occ[lvl].push_back(i);
}
}
for (int qn = 0; qn < q; qn++)
{
int idx, x;
cin >> idx >> x;
idx--;
bool ans = false;
if (x <= criteria)
{
int l = upper_bound(lvlup[x - 1].begin(), lvlup[x - 1].end(), idx) - lvlup[x - 1].begin();
ans = l + 1 <= a[idx];
}
else
{
int l = 0;
for (int i = 0; i < idx; )
{
int z = frq[l][i] + x;
if (z <= occ[l].size() && occ[l][z - 1] < idx)
{
i = occ[l][z - 1] + 1;
l++;
}
else
break;
}
ans = l + 1 <= a[idx];
}
cout << (ans ? "YES" : "NO") << endl;
}
}
int main()
{
ios::sync_with_stdio(false);
solve();
return 0;
}