#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
const int N=2e5+69;
int ans=0;
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
class DisjointSets {
private:
vector<int> parents;
vector<int> sizes;
public:
DisjointSets(int size) : parents(size), sizes(size, 1) {
for (int i = 0; i < size; i++) { parents[i] = i; }
}
/** @return the "representative" node in x's component */
int find(int x) {
return parents[x] == x ? x : (parents[x] = find(parents[x]));
}
/** @return whether the merge changed connectivity */
bool unite(int x, int y) {
int x_root = find(x);
int y_root = find(y);
if (x_root == y_root)
{ return false;
}
ans+=sizes[x_root]*sizes[y_root];
if (sizes[x_root] < sizes[y_root]) { swap(x_root, y_root); }
sizes[x_root] += sizes[y_root];
parents[y_root] = x_root;
return true;
}
/** @return whether x and y are in the same connected component */
bool connected(int x, int y) { return find(x) == find(y); }
};
void Solve(){
int n,q;
cin>>n>>q;
int H[n];
int A[q];
const int N = 1e6 + 1;
vector <int> mp(N, 0);
for(int i=0;i<n;i++)
cin>>H[i];
for(int i=0;i<q;i++)
cin>>A[i];
vector <pair<int,int>> vec;
for(int i=0;i<n;i++)
vec.push_back({H[i],i});
DisjointSets ds(n);
sort(vec.begin(),vec.end());
int cnt = 0;
for(auto [x,i]: vec){
if(i!=0&&H[i-1]<=H[i]){
ds.unite(i-1,i);
}
if(i!=n-1 && H[i+1]<=H[i])
ds.unite(i,i+1);
cnt++;
mp[x] = ans + cnt;
}
int last = 0;
for (int i = 1; i < N; i++){
if (mp[i]) last = mp[i];
mp[i] = last;
}
for(int i=0;i<q;i++){
cout << mp[A[i]] << "\n";
}
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
//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 |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8284 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8184 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8284 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8184 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
2 ms |
8280 KB |
Output is correct |
12 |
Correct |
2 ms |
8284 KB |
Output is correct |
13 |
Correct |
2 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8284 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8184 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
2 ms |
8280 KB |
Output is correct |
12 |
Correct |
2 ms |
8284 KB |
Output is correct |
13 |
Correct |
2 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
2 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8284 KB |
Output is correct |
20 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8284 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8184 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
2 ms |
8280 KB |
Output is correct |
12 |
Correct |
2 ms |
8284 KB |
Output is correct |
13 |
Correct |
2 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
2 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8284 KB |
Output is correct |
20 |
Correct |
2 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8284 KB |
Output is correct |
22 |
Correct |
2 ms |
8284 KB |
Output is correct |
23 |
Correct |
2 ms |
8284 KB |
Output is correct |
24 |
Correct |
2 ms |
8284 KB |
Output is correct |
25 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
11984 KB |
Output is correct |
2 |
Correct |
22 ms |
11988 KB |
Output is correct |
3 |
Correct |
20 ms |
11732 KB |
Output is correct |
4 |
Correct |
19 ms |
11732 KB |
Output is correct |
5 |
Correct |
19 ms |
11732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
13524 KB |
Output is correct |
2 |
Correct |
25 ms |
13520 KB |
Output is correct |
3 |
Correct |
28 ms |
13520 KB |
Output is correct |
4 |
Correct |
30 ms |
13764 KB |
Output is correct |
5 |
Correct |
24 ms |
13448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
13512 KB |
Output is correct |
2 |
Correct |
26 ms |
13532 KB |
Output is correct |
3 |
Correct |
25 ms |
13520 KB |
Output is correct |
4 |
Correct |
29 ms |
13776 KB |
Output is correct |
5 |
Correct |
26 ms |
13628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8284 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8184 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
20 ms |
11984 KB |
Output is correct |
12 |
Correct |
22 ms |
11988 KB |
Output is correct |
13 |
Correct |
20 ms |
11732 KB |
Output is correct |
14 |
Correct |
19 ms |
11732 KB |
Output is correct |
15 |
Correct |
19 ms |
11732 KB |
Output is correct |
16 |
Correct |
19 ms |
11732 KB |
Output is correct |
17 |
Correct |
21 ms |
11988 KB |
Output is correct |
18 |
Correct |
20 ms |
11988 KB |
Output is correct |
19 |
Correct |
19 ms |
11732 KB |
Output is correct |
20 |
Correct |
20 ms |
11988 KB |
Output is correct |
21 |
Correct |
22 ms |
11768 KB |
Output is correct |
22 |
Correct |
19 ms |
11732 KB |
Output is correct |
23 |
Correct |
21 ms |
11988 KB |
Output is correct |
24 |
Correct |
20 ms |
11692 KB |
Output is correct |
25 |
Correct |
20 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8284 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8184 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
2 ms |
8280 KB |
Output is correct |
12 |
Correct |
2 ms |
8284 KB |
Output is correct |
13 |
Correct |
2 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
2 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8284 KB |
Output is correct |
20 |
Correct |
2 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8284 KB |
Output is correct |
22 |
Correct |
2 ms |
8284 KB |
Output is correct |
23 |
Correct |
2 ms |
8284 KB |
Output is correct |
24 |
Correct |
2 ms |
8284 KB |
Output is correct |
25 |
Correct |
2 ms |
8284 KB |
Output is correct |
26 |
Correct |
20 ms |
11984 KB |
Output is correct |
27 |
Correct |
22 ms |
11988 KB |
Output is correct |
28 |
Correct |
20 ms |
11732 KB |
Output is correct |
29 |
Correct |
19 ms |
11732 KB |
Output is correct |
30 |
Correct |
19 ms |
11732 KB |
Output is correct |
31 |
Correct |
25 ms |
13524 KB |
Output is correct |
32 |
Correct |
25 ms |
13520 KB |
Output is correct |
33 |
Correct |
28 ms |
13520 KB |
Output is correct |
34 |
Correct |
30 ms |
13764 KB |
Output is correct |
35 |
Correct |
24 ms |
13448 KB |
Output is correct |
36 |
Correct |
26 ms |
13512 KB |
Output is correct |
37 |
Correct |
26 ms |
13532 KB |
Output is correct |
38 |
Correct |
25 ms |
13520 KB |
Output is correct |
39 |
Correct |
29 ms |
13776 KB |
Output is correct |
40 |
Correct |
26 ms |
13628 KB |
Output is correct |
41 |
Correct |
19 ms |
11732 KB |
Output is correct |
42 |
Correct |
21 ms |
11988 KB |
Output is correct |
43 |
Correct |
20 ms |
11988 KB |
Output is correct |
44 |
Correct |
19 ms |
11732 KB |
Output is correct |
45 |
Correct |
20 ms |
11988 KB |
Output is correct |
46 |
Correct |
22 ms |
11768 KB |
Output is correct |
47 |
Correct |
19 ms |
11732 KB |
Output is correct |
48 |
Correct |
21 ms |
11988 KB |
Output is correct |
49 |
Correct |
20 ms |
11692 KB |
Output is correct |
50 |
Correct |
20 ms |
11988 KB |
Output is correct |
51 |
Correct |
33 ms |
13512 KB |
Output is correct |
52 |
Correct |
41 ms |
13276 KB |
Output is correct |
53 |
Correct |
35 ms |
13516 KB |
Output is correct |
54 |
Correct |
31 ms |
13264 KB |
Output is correct |
55 |
Correct |
33 ms |
13512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8284 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8184 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
2 ms |
8280 KB |
Output is correct |
12 |
Correct |
2 ms |
8284 KB |
Output is correct |
13 |
Correct |
2 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
2 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8284 KB |
Output is correct |
20 |
Correct |
2 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8284 KB |
Output is correct |
22 |
Correct |
2 ms |
8284 KB |
Output is correct |
23 |
Correct |
2 ms |
8284 KB |
Output is correct |
24 |
Correct |
2 ms |
8284 KB |
Output is correct |
25 |
Correct |
2 ms |
8284 KB |
Output is correct |
26 |
Correct |
20 ms |
11984 KB |
Output is correct |
27 |
Correct |
22 ms |
11988 KB |
Output is correct |
28 |
Correct |
20 ms |
11732 KB |
Output is correct |
29 |
Correct |
19 ms |
11732 KB |
Output is correct |
30 |
Correct |
19 ms |
11732 KB |
Output is correct |
31 |
Correct |
25 ms |
13524 KB |
Output is correct |
32 |
Correct |
25 ms |
13520 KB |
Output is correct |
33 |
Correct |
28 ms |
13520 KB |
Output is correct |
34 |
Correct |
30 ms |
13764 KB |
Output is correct |
35 |
Correct |
24 ms |
13448 KB |
Output is correct |
36 |
Correct |
26 ms |
13512 KB |
Output is correct |
37 |
Correct |
26 ms |
13532 KB |
Output is correct |
38 |
Correct |
25 ms |
13520 KB |
Output is correct |
39 |
Correct |
29 ms |
13776 KB |
Output is correct |
40 |
Correct |
26 ms |
13628 KB |
Output is correct |
41 |
Correct |
19 ms |
11732 KB |
Output is correct |
42 |
Correct |
21 ms |
11988 KB |
Output is correct |
43 |
Correct |
20 ms |
11988 KB |
Output is correct |
44 |
Correct |
19 ms |
11732 KB |
Output is correct |
45 |
Correct |
20 ms |
11988 KB |
Output is correct |
46 |
Correct |
22 ms |
11768 KB |
Output is correct |
47 |
Correct |
19 ms |
11732 KB |
Output is correct |
48 |
Correct |
21 ms |
11988 KB |
Output is correct |
49 |
Correct |
20 ms |
11692 KB |
Output is correct |
50 |
Correct |
20 ms |
11988 KB |
Output is correct |
51 |
Correct |
33 ms |
13512 KB |
Output is correct |
52 |
Correct |
41 ms |
13276 KB |
Output is correct |
53 |
Correct |
35 ms |
13516 KB |
Output is correct |
54 |
Correct |
31 ms |
13264 KB |
Output is correct |
55 |
Correct |
33 ms |
13512 KB |
Output is correct |
56 |
Correct |
357 ms |
61868 KB |
Output is correct |
57 |
Correct |
364 ms |
75016 KB |
Output is correct |
58 |
Correct |
334 ms |
71284 KB |
Output is correct |
59 |
Correct |
356 ms |
71612 KB |
Output is correct |
60 |
Correct |
373 ms |
75444 KB |
Output is correct |