#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e6 + 69;
int sz[maxn];
int root[maxn];
int ans[maxn];
int last = 0;
int findroot(int x)
{
while (x!=root[x])
x= root[x];
return x;
}
void unite(int x, int y, int w)
{
x = findroot(x);
y = findroot(y);
if (sz[x]<sz[y])swap(x, y);
root[y] = x;
ans[w] += sz[x]*sz[y];
sz[x] += sz[y];
}
void Solve()
{
int n,q;
cin>>n>>q;
vector <pair<int,int>> v;
for (int i=0; i<n; i++)
{
int x;
cin>>x;
v.push_back(make_pair(x, i));
}
//ans[0] = n;
sort(v.begin(), v.end());
for (int i=0; i<maxn; i++)
{
ans[i] = 0;
}
for (int i=0; i<n+1; i++)
{
sz[i] = 0;
root[i] = i;
}
//int last = 0;
//ans[0] = 0;
//for (int i=0; i<n; i++)
//cout<<v[i].first<<" "<<v[i].second<<"\n";
for (int i=0; i<n; i++)
{
int w = v[i].first;
int x = v[i].second;
//cout<<x<<"\n";
sz[x] = 1;
ans[w] += 1;
if (x!=0 && sz[x-1]!=0)
unite(x, x-1, w);
if (x!=n-1 && sz[x+1]!=0)
unite(x, x+1, w);
// for (int j=0; j<n; j++)
// {
// cout<<findroot(j)<<" ";
//}
//cout<<"\n";
// for (int j=0; j<n; j++)
//{
//cout<<sz[j]<<" ";
// }
//cout<<"\n";
//cout<<w<<" "<<ans[w]<<"\n";
}
int s = 0;
for (int i=0; i<maxn; i++)
{
s+= ans[i];
ans[i] = s;
}
for (int i=0; i<q; i++)
{
int h;
cin>>h;
cout<<ans[h]<<"\n";
}
}
int32_t main()
{
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8128 KB |
Output is correct |
2 |
Correct |
6 ms |
8140 KB |
Output is correct |
3 |
Correct |
7 ms |
8136 KB |
Output is correct |
4 |
Correct |
6 ms |
8148 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8128 KB |
Output is correct |
2 |
Correct |
6 ms |
8140 KB |
Output is correct |
3 |
Correct |
7 ms |
8136 KB |
Output is correct |
4 |
Correct |
6 ms |
8148 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8140 KB |
Output is correct |
12 |
Correct |
6 ms |
8104 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
5 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8128 KB |
Output is correct |
2 |
Correct |
6 ms |
8140 KB |
Output is correct |
3 |
Correct |
7 ms |
8136 KB |
Output is correct |
4 |
Correct |
6 ms |
8148 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8140 KB |
Output is correct |
12 |
Correct |
6 ms |
8104 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
5 ms |
8148 KB |
Output is correct |
16 |
Correct |
5 ms |
8148 KB |
Output is correct |
17 |
Correct |
5 ms |
8148 KB |
Output is correct |
18 |
Correct |
5 ms |
8148 KB |
Output is correct |
19 |
Correct |
5 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8128 KB |
Output is correct |
2 |
Correct |
6 ms |
8140 KB |
Output is correct |
3 |
Correct |
7 ms |
8136 KB |
Output is correct |
4 |
Correct |
6 ms |
8148 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8140 KB |
Output is correct |
12 |
Correct |
6 ms |
8104 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
5 ms |
8148 KB |
Output is correct |
16 |
Correct |
5 ms |
8148 KB |
Output is correct |
17 |
Correct |
5 ms |
8148 KB |
Output is correct |
18 |
Correct |
5 ms |
8148 KB |
Output is correct |
19 |
Correct |
5 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8148 KB |
Output is correct |
21 |
Correct |
6 ms |
8228 KB |
Output is correct |
22 |
Correct |
5 ms |
8148 KB |
Output is correct |
23 |
Correct |
5 ms |
8144 KB |
Output is correct |
24 |
Correct |
5 ms |
8148 KB |
Output is correct |
25 |
Correct |
5 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
11876 KB |
Output is correct |
2 |
Correct |
26 ms |
12024 KB |
Output is correct |
3 |
Correct |
24 ms |
11716 KB |
Output is correct |
4 |
Correct |
24 ms |
11612 KB |
Output is correct |
5 |
Correct |
23 ms |
11716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
13252 KB |
Output is correct |
2 |
Correct |
44 ms |
13352 KB |
Output is correct |
3 |
Correct |
38 ms |
13248 KB |
Output is correct |
4 |
Correct |
38 ms |
13360 KB |
Output is correct |
5 |
Correct |
36 ms |
13264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
13460 KB |
Output is correct |
2 |
Correct |
38 ms |
13448 KB |
Output is correct |
3 |
Correct |
40 ms |
13328 KB |
Output is correct |
4 |
Correct |
37 ms |
13644 KB |
Output is correct |
5 |
Correct |
38 ms |
13528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8128 KB |
Output is correct |
2 |
Correct |
6 ms |
8140 KB |
Output is correct |
3 |
Correct |
7 ms |
8136 KB |
Output is correct |
4 |
Correct |
6 ms |
8148 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
27 ms |
11876 KB |
Output is correct |
12 |
Correct |
26 ms |
12024 KB |
Output is correct |
13 |
Correct |
24 ms |
11716 KB |
Output is correct |
14 |
Correct |
24 ms |
11612 KB |
Output is correct |
15 |
Correct |
23 ms |
11716 KB |
Output is correct |
16 |
Correct |
25 ms |
11616 KB |
Output is correct |
17 |
Correct |
25 ms |
11996 KB |
Output is correct |
18 |
Correct |
26 ms |
11976 KB |
Output is correct |
19 |
Correct |
25 ms |
11640 KB |
Output is correct |
20 |
Correct |
25 ms |
11948 KB |
Output is correct |
21 |
Correct |
23 ms |
11588 KB |
Output is correct |
22 |
Correct |
23 ms |
11824 KB |
Output is correct |
23 |
Correct |
25 ms |
11948 KB |
Output is correct |
24 |
Correct |
23 ms |
11716 KB |
Output is correct |
25 |
Correct |
25 ms |
11852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8128 KB |
Output is correct |
2 |
Correct |
6 ms |
8140 KB |
Output is correct |
3 |
Correct |
7 ms |
8136 KB |
Output is correct |
4 |
Correct |
6 ms |
8148 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8140 KB |
Output is correct |
12 |
Correct |
6 ms |
8104 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
5 ms |
8148 KB |
Output is correct |
16 |
Correct |
5 ms |
8148 KB |
Output is correct |
17 |
Correct |
5 ms |
8148 KB |
Output is correct |
18 |
Correct |
5 ms |
8148 KB |
Output is correct |
19 |
Correct |
5 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8148 KB |
Output is correct |
21 |
Correct |
6 ms |
8228 KB |
Output is correct |
22 |
Correct |
5 ms |
8148 KB |
Output is correct |
23 |
Correct |
5 ms |
8144 KB |
Output is correct |
24 |
Correct |
5 ms |
8148 KB |
Output is correct |
25 |
Correct |
5 ms |
8148 KB |
Output is correct |
26 |
Correct |
27 ms |
11876 KB |
Output is correct |
27 |
Correct |
26 ms |
12024 KB |
Output is correct |
28 |
Correct |
24 ms |
11716 KB |
Output is correct |
29 |
Correct |
24 ms |
11612 KB |
Output is correct |
30 |
Correct |
23 ms |
11716 KB |
Output is correct |
31 |
Correct |
33 ms |
13252 KB |
Output is correct |
32 |
Correct |
44 ms |
13352 KB |
Output is correct |
33 |
Correct |
38 ms |
13248 KB |
Output is correct |
34 |
Correct |
38 ms |
13360 KB |
Output is correct |
35 |
Correct |
36 ms |
13264 KB |
Output is correct |
36 |
Correct |
35 ms |
13460 KB |
Output is correct |
37 |
Correct |
38 ms |
13448 KB |
Output is correct |
38 |
Correct |
40 ms |
13328 KB |
Output is correct |
39 |
Correct |
37 ms |
13644 KB |
Output is correct |
40 |
Correct |
38 ms |
13528 KB |
Output is correct |
41 |
Correct |
25 ms |
11616 KB |
Output is correct |
42 |
Correct |
25 ms |
11996 KB |
Output is correct |
43 |
Correct |
26 ms |
11976 KB |
Output is correct |
44 |
Correct |
25 ms |
11640 KB |
Output is correct |
45 |
Correct |
25 ms |
11948 KB |
Output is correct |
46 |
Correct |
23 ms |
11588 KB |
Output is correct |
47 |
Correct |
23 ms |
11824 KB |
Output is correct |
48 |
Correct |
25 ms |
11948 KB |
Output is correct |
49 |
Correct |
23 ms |
11716 KB |
Output is correct |
50 |
Correct |
25 ms |
11852 KB |
Output is correct |
51 |
Correct |
44 ms |
13232 KB |
Output is correct |
52 |
Correct |
46 ms |
13140 KB |
Output is correct |
53 |
Correct |
45 ms |
13252 KB |
Output is correct |
54 |
Correct |
45 ms |
13056 KB |
Output is correct |
55 |
Correct |
42 ms |
13260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8128 KB |
Output is correct |
2 |
Correct |
6 ms |
8140 KB |
Output is correct |
3 |
Correct |
7 ms |
8136 KB |
Output is correct |
4 |
Correct |
6 ms |
8148 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8140 KB |
Output is correct |
12 |
Correct |
6 ms |
8104 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
5 ms |
8148 KB |
Output is correct |
16 |
Correct |
5 ms |
8148 KB |
Output is correct |
17 |
Correct |
5 ms |
8148 KB |
Output is correct |
18 |
Correct |
5 ms |
8148 KB |
Output is correct |
19 |
Correct |
5 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8148 KB |
Output is correct |
21 |
Correct |
6 ms |
8228 KB |
Output is correct |
22 |
Correct |
5 ms |
8148 KB |
Output is correct |
23 |
Correct |
5 ms |
8144 KB |
Output is correct |
24 |
Correct |
5 ms |
8148 KB |
Output is correct |
25 |
Correct |
5 ms |
8148 KB |
Output is correct |
26 |
Correct |
27 ms |
11876 KB |
Output is correct |
27 |
Correct |
26 ms |
12024 KB |
Output is correct |
28 |
Correct |
24 ms |
11716 KB |
Output is correct |
29 |
Correct |
24 ms |
11612 KB |
Output is correct |
30 |
Correct |
23 ms |
11716 KB |
Output is correct |
31 |
Correct |
33 ms |
13252 KB |
Output is correct |
32 |
Correct |
44 ms |
13352 KB |
Output is correct |
33 |
Correct |
38 ms |
13248 KB |
Output is correct |
34 |
Correct |
38 ms |
13360 KB |
Output is correct |
35 |
Correct |
36 ms |
13264 KB |
Output is correct |
36 |
Correct |
35 ms |
13460 KB |
Output is correct |
37 |
Correct |
38 ms |
13448 KB |
Output is correct |
38 |
Correct |
40 ms |
13328 KB |
Output is correct |
39 |
Correct |
37 ms |
13644 KB |
Output is correct |
40 |
Correct |
38 ms |
13528 KB |
Output is correct |
41 |
Correct |
25 ms |
11616 KB |
Output is correct |
42 |
Correct |
25 ms |
11996 KB |
Output is correct |
43 |
Correct |
26 ms |
11976 KB |
Output is correct |
44 |
Correct |
25 ms |
11640 KB |
Output is correct |
45 |
Correct |
25 ms |
11948 KB |
Output is correct |
46 |
Correct |
23 ms |
11588 KB |
Output is correct |
47 |
Correct |
23 ms |
11824 KB |
Output is correct |
48 |
Correct |
25 ms |
11948 KB |
Output is correct |
49 |
Correct |
23 ms |
11716 KB |
Output is correct |
50 |
Correct |
25 ms |
11852 KB |
Output is correct |
51 |
Correct |
44 ms |
13232 KB |
Output is correct |
52 |
Correct |
46 ms |
13140 KB |
Output is correct |
53 |
Correct |
45 ms |
13252 KB |
Output is correct |
54 |
Correct |
45 ms |
13056 KB |
Output is correct |
55 |
Correct |
42 ms |
13260 KB |
Output is correct |
56 |
Correct |
459 ms |
58224 KB |
Output is correct |
57 |
Correct |
475 ms |
59080 KB |
Output is correct |
58 |
Correct |
447 ms |
55756 KB |
Output is correct |
59 |
Correct |
480 ms |
56944 KB |
Output is correct |
60 |
Correct |
510 ms |
59980 KB |
Output is correct |