// Programmer: Shadow1
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using str = string; // yay python!
#define i64 int64_t
#define show(x) cerr << (#x) << " = " << (x) << '\n';
#define output_vector(v) for(auto &x : v){cout << x << " ";}cout << '\n';
#define vt vector
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define umap unordered_map
#define uset unordered_set
#define fir first
#define sec second
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
// T: O(n^3)
// M : O(n + k log n)
const int N = 1e6;
vector<int> p, ranks, set_size;
void build() {
p.assign(N+2, 0);
ranks.assign(N+2, 0);
set_size.assign(N+2, 1);
for(int i=0; i<=N; ++i)
p[i] = i;
}
int find_set(int i) {
if(p[i] == i)
return i;
return p[i] = find_set(p[i]);
}
bool isSameSet(int i, int j) {
return find_set(i) == find_set(j);
}
void unite(int i, int j) {
if(isSameSet(i,j)) return;
int x = find_set(i), y = find_set(j);
if(ranks[x] > y)
swap(x,y);
p[x] = y;
if(ranks[x] == ranks[y])
++ranks[y];
set_size[y] += set_size[x];
}
ll find_size(int x) {
return set_size[find_set(x)];
}
ll tri(ll x) {
return x * (x + 1) / 2ll;
}
void solve() {
int n, q;
cin >> n >> q;
vector<pair<ll,ll>> h(n);
vector<bool> vis(n+1);
for(int i=0; i<n; ++i) {
cin >> h[i].fir;
h[i].sec = i+1;
}
sort(all(h));
build();
vector<ll> query(1e6 + 7);
ll cnt = 0;
for(int i=0; i<n; ++i) {
ll ind = h[i].sec;
ll height = h[i].fir;
query[height] = cnt;
if(!vis[ind - 1] && !vis[ind + 1]) {
query[height]++;
}else if(vis[ind-1] && vis[ind+1]) {
ll s = find_size(ind-1);
query[height] -= tri(s);
s = find_size(ind + 1);
query[height] -= tri(s);
unite(ind, ind-1);
unite(ind, ind+1);
s = find_size(ind);
query[height] += tri(s);
} else if(vis[ind-1]) {
unite(ind - 1, ind);
query[height] += find_size(ind);
} else if(vis[ind+1]) {
unite(ind, ind+1);
query[height] += find_size(ind);
}
vis[ind] = true;
cnt = query[height];
}
for(int i=1; i<=1e6; ++i) {
if(query[i] == 0) query[i] = query[i-1];
}
while(q--) {
int y; cin >> y;
cout << query[y] << '\n';
}
}
int main() {
// freopen("output.txt", "w", stdout);
// freopen("input.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
// cin >> T;
while(T--)
solve();
return 0;
}
/* CHECK :
1. COMPARATOR FUNCTION MUST RETURN FALSE WHEN ARGUMENTS ARE EQUAL!!!
2. Overflow! Typecase int64_t on operations if varaibles are int
3. Check array bounds!!!
4. Check array indexing!!!
5. Edge cases. (N==1)!!!
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19804 KB |
Output is correct |
2 |
Correct |
8 ms |
19996 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
8 ms |
19804 KB |
Output is correct |
5 |
Correct |
8 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
20016 KB |
Output is correct |
9 |
Correct |
9 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19804 KB |
Output is correct |
2 |
Correct |
8 ms |
19996 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
8 ms |
19804 KB |
Output is correct |
5 |
Correct |
8 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
20016 KB |
Output is correct |
9 |
Correct |
9 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
9 ms |
19800 KB |
Output is correct |
12 |
Correct |
8 ms |
19804 KB |
Output is correct |
13 |
Correct |
8 ms |
19952 KB |
Output is correct |
14 |
Correct |
9 ms |
19804 KB |
Output is correct |
15 |
Correct |
10 ms |
20040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19804 KB |
Output is correct |
2 |
Correct |
8 ms |
19996 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
8 ms |
19804 KB |
Output is correct |
5 |
Correct |
8 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
20016 KB |
Output is correct |
9 |
Correct |
9 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
9 ms |
19800 KB |
Output is correct |
12 |
Correct |
8 ms |
19804 KB |
Output is correct |
13 |
Correct |
8 ms |
19952 KB |
Output is correct |
14 |
Correct |
9 ms |
19804 KB |
Output is correct |
15 |
Correct |
10 ms |
20040 KB |
Output is correct |
16 |
Correct |
9 ms |
20048 KB |
Output is correct |
17 |
Correct |
9 ms |
19912 KB |
Output is correct |
18 |
Correct |
8 ms |
19804 KB |
Output is correct |
19 |
Correct |
9 ms |
19800 KB |
Output is correct |
20 |
Correct |
9 ms |
19804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19804 KB |
Output is correct |
2 |
Correct |
8 ms |
19996 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
8 ms |
19804 KB |
Output is correct |
5 |
Correct |
8 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
20016 KB |
Output is correct |
9 |
Correct |
9 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
9 ms |
19800 KB |
Output is correct |
12 |
Correct |
8 ms |
19804 KB |
Output is correct |
13 |
Correct |
8 ms |
19952 KB |
Output is correct |
14 |
Correct |
9 ms |
19804 KB |
Output is correct |
15 |
Correct |
10 ms |
20040 KB |
Output is correct |
16 |
Correct |
9 ms |
20048 KB |
Output is correct |
17 |
Correct |
9 ms |
19912 KB |
Output is correct |
18 |
Correct |
8 ms |
19804 KB |
Output is correct |
19 |
Correct |
9 ms |
19800 KB |
Output is correct |
20 |
Correct |
9 ms |
19804 KB |
Output is correct |
21 |
Correct |
9 ms |
20060 KB |
Output is correct |
22 |
Correct |
9 ms |
20060 KB |
Output is correct |
23 |
Correct |
13 ms |
20060 KB |
Output is correct |
24 |
Correct |
9 ms |
20060 KB |
Output is correct |
25 |
Correct |
9 ms |
20060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
22104 KB |
Output is correct |
2 |
Correct |
26 ms |
22108 KB |
Output is correct |
3 |
Correct |
26 ms |
21848 KB |
Output is correct |
4 |
Correct |
29 ms |
21848 KB |
Output is correct |
5 |
Correct |
24 ms |
21852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
23380 KB |
Output is correct |
2 |
Correct |
28 ms |
23636 KB |
Output is correct |
3 |
Correct |
29 ms |
23424 KB |
Output is correct |
4 |
Correct |
30 ms |
23580 KB |
Output is correct |
5 |
Correct |
27 ms |
23388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
23724 KB |
Output is correct |
2 |
Correct |
35 ms |
23660 KB |
Output is correct |
3 |
Correct |
29 ms |
23632 KB |
Output is correct |
4 |
Correct |
30 ms |
23632 KB |
Output is correct |
5 |
Correct |
40 ms |
23632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19804 KB |
Output is correct |
2 |
Correct |
8 ms |
19996 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
8 ms |
19804 KB |
Output is correct |
5 |
Correct |
8 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
20016 KB |
Output is correct |
9 |
Correct |
9 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
24 ms |
22104 KB |
Output is correct |
12 |
Correct |
26 ms |
22108 KB |
Output is correct |
13 |
Correct |
26 ms |
21848 KB |
Output is correct |
14 |
Correct |
29 ms |
21848 KB |
Output is correct |
15 |
Correct |
24 ms |
21852 KB |
Output is correct |
16 |
Correct |
25 ms |
21776 KB |
Output is correct |
17 |
Correct |
24 ms |
22108 KB |
Output is correct |
18 |
Correct |
27 ms |
22108 KB |
Output is correct |
19 |
Correct |
23 ms |
21852 KB |
Output is correct |
20 |
Correct |
24 ms |
22176 KB |
Output is correct |
21 |
Correct |
23 ms |
21868 KB |
Output is correct |
22 |
Correct |
25 ms |
22104 KB |
Output is correct |
23 |
Correct |
25 ms |
22060 KB |
Output is correct |
24 |
Correct |
24 ms |
22104 KB |
Output is correct |
25 |
Correct |
25 ms |
22064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19804 KB |
Output is correct |
2 |
Correct |
8 ms |
19996 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
8 ms |
19804 KB |
Output is correct |
5 |
Correct |
8 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
20016 KB |
Output is correct |
9 |
Correct |
9 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
9 ms |
19800 KB |
Output is correct |
12 |
Correct |
8 ms |
19804 KB |
Output is correct |
13 |
Correct |
8 ms |
19952 KB |
Output is correct |
14 |
Correct |
9 ms |
19804 KB |
Output is correct |
15 |
Correct |
10 ms |
20040 KB |
Output is correct |
16 |
Correct |
9 ms |
20048 KB |
Output is correct |
17 |
Correct |
9 ms |
19912 KB |
Output is correct |
18 |
Correct |
8 ms |
19804 KB |
Output is correct |
19 |
Correct |
9 ms |
19800 KB |
Output is correct |
20 |
Correct |
9 ms |
19804 KB |
Output is correct |
21 |
Correct |
9 ms |
20060 KB |
Output is correct |
22 |
Correct |
9 ms |
20060 KB |
Output is correct |
23 |
Correct |
13 ms |
20060 KB |
Output is correct |
24 |
Correct |
9 ms |
20060 KB |
Output is correct |
25 |
Correct |
9 ms |
20060 KB |
Output is correct |
26 |
Correct |
24 ms |
22104 KB |
Output is correct |
27 |
Correct |
26 ms |
22108 KB |
Output is correct |
28 |
Correct |
26 ms |
21848 KB |
Output is correct |
29 |
Correct |
29 ms |
21848 KB |
Output is correct |
30 |
Correct |
24 ms |
21852 KB |
Output is correct |
31 |
Correct |
27 ms |
23380 KB |
Output is correct |
32 |
Correct |
28 ms |
23636 KB |
Output is correct |
33 |
Correct |
29 ms |
23424 KB |
Output is correct |
34 |
Correct |
30 ms |
23580 KB |
Output is correct |
35 |
Correct |
27 ms |
23388 KB |
Output is correct |
36 |
Correct |
35 ms |
23724 KB |
Output is correct |
37 |
Correct |
35 ms |
23660 KB |
Output is correct |
38 |
Correct |
29 ms |
23632 KB |
Output is correct |
39 |
Correct |
30 ms |
23632 KB |
Output is correct |
40 |
Correct |
40 ms |
23632 KB |
Output is correct |
41 |
Correct |
25 ms |
21776 KB |
Output is correct |
42 |
Correct |
24 ms |
22108 KB |
Output is correct |
43 |
Correct |
27 ms |
22108 KB |
Output is correct |
44 |
Correct |
23 ms |
21852 KB |
Output is correct |
45 |
Correct |
24 ms |
22176 KB |
Output is correct |
46 |
Correct |
23 ms |
21868 KB |
Output is correct |
47 |
Correct |
25 ms |
22104 KB |
Output is correct |
48 |
Correct |
25 ms |
22060 KB |
Output is correct |
49 |
Correct |
24 ms |
22104 KB |
Output is correct |
50 |
Correct |
25 ms |
22064 KB |
Output is correct |
51 |
Correct |
39 ms |
23372 KB |
Output is correct |
52 |
Correct |
38 ms |
23236 KB |
Output is correct |
53 |
Correct |
40 ms |
23380 KB |
Output is correct |
54 |
Correct |
38 ms |
23376 KB |
Output is correct |
55 |
Correct |
41 ms |
23376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19804 KB |
Output is correct |
2 |
Correct |
8 ms |
19996 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
8 ms |
19804 KB |
Output is correct |
5 |
Correct |
8 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
20016 KB |
Output is correct |
9 |
Correct |
9 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
9 ms |
19800 KB |
Output is correct |
12 |
Correct |
8 ms |
19804 KB |
Output is correct |
13 |
Correct |
8 ms |
19952 KB |
Output is correct |
14 |
Correct |
9 ms |
19804 KB |
Output is correct |
15 |
Correct |
10 ms |
20040 KB |
Output is correct |
16 |
Correct |
9 ms |
20048 KB |
Output is correct |
17 |
Correct |
9 ms |
19912 KB |
Output is correct |
18 |
Correct |
8 ms |
19804 KB |
Output is correct |
19 |
Correct |
9 ms |
19800 KB |
Output is correct |
20 |
Correct |
9 ms |
19804 KB |
Output is correct |
21 |
Correct |
9 ms |
20060 KB |
Output is correct |
22 |
Correct |
9 ms |
20060 KB |
Output is correct |
23 |
Correct |
13 ms |
20060 KB |
Output is correct |
24 |
Correct |
9 ms |
20060 KB |
Output is correct |
25 |
Correct |
9 ms |
20060 KB |
Output is correct |
26 |
Correct |
24 ms |
22104 KB |
Output is correct |
27 |
Correct |
26 ms |
22108 KB |
Output is correct |
28 |
Correct |
26 ms |
21848 KB |
Output is correct |
29 |
Correct |
29 ms |
21848 KB |
Output is correct |
30 |
Correct |
24 ms |
21852 KB |
Output is correct |
31 |
Correct |
27 ms |
23380 KB |
Output is correct |
32 |
Correct |
28 ms |
23636 KB |
Output is correct |
33 |
Correct |
29 ms |
23424 KB |
Output is correct |
34 |
Correct |
30 ms |
23580 KB |
Output is correct |
35 |
Correct |
27 ms |
23388 KB |
Output is correct |
36 |
Correct |
35 ms |
23724 KB |
Output is correct |
37 |
Correct |
35 ms |
23660 KB |
Output is correct |
38 |
Correct |
29 ms |
23632 KB |
Output is correct |
39 |
Correct |
30 ms |
23632 KB |
Output is correct |
40 |
Correct |
40 ms |
23632 KB |
Output is correct |
41 |
Correct |
25 ms |
21776 KB |
Output is correct |
42 |
Correct |
24 ms |
22108 KB |
Output is correct |
43 |
Correct |
27 ms |
22108 KB |
Output is correct |
44 |
Correct |
23 ms |
21852 KB |
Output is correct |
45 |
Correct |
24 ms |
22176 KB |
Output is correct |
46 |
Correct |
23 ms |
21868 KB |
Output is correct |
47 |
Correct |
25 ms |
22104 KB |
Output is correct |
48 |
Correct |
25 ms |
22060 KB |
Output is correct |
49 |
Correct |
24 ms |
22104 KB |
Output is correct |
50 |
Correct |
25 ms |
22064 KB |
Output is correct |
51 |
Correct |
39 ms |
23372 KB |
Output is correct |
52 |
Correct |
38 ms |
23236 KB |
Output is correct |
53 |
Correct |
40 ms |
23380 KB |
Output is correct |
54 |
Correct |
38 ms |
23376 KB |
Output is correct |
55 |
Correct |
41 ms |
23376 KB |
Output is correct |
56 |
Correct |
322 ms |
55308 KB |
Output is correct |
57 |
Correct |
308 ms |
55380 KB |
Output is correct |
58 |
Correct |
300 ms |
53488 KB |
Output is correct |
59 |
Correct |
313 ms |
54184 KB |
Output is correct |
60 |
Correct |
313 ms |
56400 KB |
Output is correct |