// #define _GLIBCXX_DEBUG
// #pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <chrono>
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
using namespace __gnu_pbds;
using namespace chrono;
#define pb push_back
#define f first
#define s second
#define min3(a, b, c) min(min(a, b), c)
#define max3(a, b, c) max(max(a, b), c)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef double ld;
typedef long double lld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
template <class T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
// const int INF = 1e9;
const ll INF = 1e18;
const ll mod = 1000000007;
// const ll mod = 998244353;
struct DSU{
vl e;
vector<bool> active;
ll ans;
void init(int n){
e = vl(n, -1);
ans = 0;
active = vector<bool>(n, 0);
}
int get(int x){
return (e[x] < 0 ? x : e[x] = get(e[x]));
}
bool same_set(int x, int y){
return get(x) == get(y);
}
ll sz(int x){
return -e[get(x)];
}
ll fx(int x){
return 1LL*sz(x)*(sz(x) + 1)/2;
}
bool unite(int x, int y){
x = get(x), y = get(y);
if(!active[x]){
active[x] = 1;
ans += fx(x);
}
if(!active[y] || x == y) return false;
if(e[x] > e[y])
swap(x, y);
ans -= fx(x) + fx(y);
e[x] += e[y];
e[y] = x;
ans += fx(x);
return true;
}
};
void solve(int tc){
int n, q;
cin >> n >> q;
vpi a(n);
for(int i = 0; i < n; i++){
cin >> a[i].f;
a[i].s = i;
}
sort(all(a));
vpi query(q);
for(int i = 0; i < q; i++){
cin >> query[i].f;
query[i].s = i;
}
sort(all(query));
vl ans(q);
DSU s;
s.init(n);
int j = 0;
for(const auto&[plane, idx] : query){
while(j < n && a[j].first <= plane){
int cur = a[j].second;
int prev = cur - 1;
int next = cur + 1;
if(prev >= 0) s.unite(cur, prev);
if(next < n) s.unite(cur, next);
j++;
}
ans[idx] = s.ans;
}
for(auto& x : ans)
cout << x << ' ';
}
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
#ifdef LOCAL
auto begin = high_resolution_clock::now();
#endif
int tc = 1;
// cin >> tc;
for (int t = 0; t < tc; t++) solve(t);
#ifdef LOCAL
auto end = high_resolution_clock::now();
cout << fixed << setprecision(4) << "Execution Time: " << duration_cast<duration<double>>(end - begin).count() << "seconds" << endl;
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
1900 KB |
Output is correct |
2 |
Correct |
23 ms |
1900 KB |
Output is correct |
3 |
Correct |
22 ms |
1772 KB |
Output is correct |
4 |
Correct |
21 ms |
1772 KB |
Output is correct |
5 |
Correct |
28 ms |
1772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
4204 KB |
Output is correct |
2 |
Correct |
44 ms |
5484 KB |
Output is correct |
3 |
Correct |
54 ms |
5508 KB |
Output is correct |
4 |
Correct |
51 ms |
5484 KB |
Output is correct |
5 |
Correct |
43 ms |
5376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
4332 KB |
Output is correct |
2 |
Correct |
44 ms |
5484 KB |
Output is correct |
3 |
Correct |
47 ms |
5612 KB |
Output is correct |
4 |
Correct |
45 ms |
5740 KB |
Output is correct |
5 |
Correct |
50 ms |
5868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
23 ms |
1900 KB |
Output is correct |
12 |
Correct |
23 ms |
1900 KB |
Output is correct |
13 |
Correct |
22 ms |
1772 KB |
Output is correct |
14 |
Correct |
21 ms |
1772 KB |
Output is correct |
15 |
Correct |
28 ms |
1772 KB |
Output is correct |
16 |
Correct |
21 ms |
2412 KB |
Output is correct |
17 |
Correct |
20 ms |
2668 KB |
Output is correct |
18 |
Correct |
20 ms |
2540 KB |
Output is correct |
19 |
Correct |
40 ms |
2432 KB |
Output is correct |
20 |
Correct |
20 ms |
2540 KB |
Output is correct |
21 |
Correct |
25 ms |
2284 KB |
Output is correct |
22 |
Correct |
22 ms |
2412 KB |
Output is correct |
23 |
Correct |
24 ms |
2540 KB |
Output is correct |
24 |
Correct |
18 ms |
2412 KB |
Output is correct |
25 |
Correct |
20 ms |
2412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
23 ms |
1900 KB |
Output is correct |
27 |
Correct |
23 ms |
1900 KB |
Output is correct |
28 |
Correct |
22 ms |
1772 KB |
Output is correct |
29 |
Correct |
21 ms |
1772 KB |
Output is correct |
30 |
Correct |
28 ms |
1772 KB |
Output is correct |
31 |
Correct |
43 ms |
4204 KB |
Output is correct |
32 |
Correct |
44 ms |
5484 KB |
Output is correct |
33 |
Correct |
54 ms |
5508 KB |
Output is correct |
34 |
Correct |
51 ms |
5484 KB |
Output is correct |
35 |
Correct |
43 ms |
5376 KB |
Output is correct |
36 |
Correct |
42 ms |
4332 KB |
Output is correct |
37 |
Correct |
44 ms |
5484 KB |
Output is correct |
38 |
Correct |
47 ms |
5612 KB |
Output is correct |
39 |
Correct |
45 ms |
5740 KB |
Output is correct |
40 |
Correct |
50 ms |
5868 KB |
Output is correct |
41 |
Correct |
21 ms |
2412 KB |
Output is correct |
42 |
Correct |
20 ms |
2668 KB |
Output is correct |
43 |
Correct |
20 ms |
2540 KB |
Output is correct |
44 |
Correct |
40 ms |
2432 KB |
Output is correct |
45 |
Correct |
20 ms |
2540 KB |
Output is correct |
46 |
Correct |
25 ms |
2284 KB |
Output is correct |
47 |
Correct |
22 ms |
2412 KB |
Output is correct |
48 |
Correct |
24 ms |
2540 KB |
Output is correct |
49 |
Correct |
18 ms |
2412 KB |
Output is correct |
50 |
Correct |
20 ms |
2412 KB |
Output is correct |
51 |
Correct |
52 ms |
5356 KB |
Output is correct |
52 |
Correct |
71 ms |
5356 KB |
Output is correct |
53 |
Correct |
60 ms |
5356 KB |
Output is correct |
54 |
Correct |
64 ms |
5228 KB |
Output is correct |
55 |
Correct |
74 ms |
5356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
23 ms |
1900 KB |
Output is correct |
27 |
Correct |
23 ms |
1900 KB |
Output is correct |
28 |
Correct |
22 ms |
1772 KB |
Output is correct |
29 |
Correct |
21 ms |
1772 KB |
Output is correct |
30 |
Correct |
28 ms |
1772 KB |
Output is correct |
31 |
Correct |
43 ms |
4204 KB |
Output is correct |
32 |
Correct |
44 ms |
5484 KB |
Output is correct |
33 |
Correct |
54 ms |
5508 KB |
Output is correct |
34 |
Correct |
51 ms |
5484 KB |
Output is correct |
35 |
Correct |
43 ms |
5376 KB |
Output is correct |
36 |
Correct |
42 ms |
4332 KB |
Output is correct |
37 |
Correct |
44 ms |
5484 KB |
Output is correct |
38 |
Correct |
47 ms |
5612 KB |
Output is correct |
39 |
Correct |
45 ms |
5740 KB |
Output is correct |
40 |
Correct |
50 ms |
5868 KB |
Output is correct |
41 |
Correct |
21 ms |
2412 KB |
Output is correct |
42 |
Correct |
20 ms |
2668 KB |
Output is correct |
43 |
Correct |
20 ms |
2540 KB |
Output is correct |
44 |
Correct |
40 ms |
2432 KB |
Output is correct |
45 |
Correct |
20 ms |
2540 KB |
Output is correct |
46 |
Correct |
25 ms |
2284 KB |
Output is correct |
47 |
Correct |
22 ms |
2412 KB |
Output is correct |
48 |
Correct |
24 ms |
2540 KB |
Output is correct |
49 |
Correct |
18 ms |
2412 KB |
Output is correct |
50 |
Correct |
20 ms |
2412 KB |
Output is correct |
51 |
Correct |
52 ms |
5356 KB |
Output is correct |
52 |
Correct |
71 ms |
5356 KB |
Output is correct |
53 |
Correct |
60 ms |
5356 KB |
Output is correct |
54 |
Correct |
64 ms |
5228 KB |
Output is correct |
55 |
Correct |
74 ms |
5356 KB |
Output is correct |
56 |
Correct |
706 ms |
51360 KB |
Output is correct |
57 |
Correct |
641 ms |
50656 KB |
Output is correct |
58 |
Correct |
611 ms |
48108 KB |
Output is correct |
59 |
Correct |
616 ms |
49124 KB |
Output is correct |
60 |
Correct |
658 ms |
52396 KB |
Output is correct |