#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) int32_t(s.length())
#define MASK(k)(1LL<<(k))
#define TASK "test"
#define int long long
using namespace std;
typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;
const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MX = 1000005;
struct DSU{
int n;
vector<int> e;
DSU(int n=0){
this->n = n;
this->e.assign(n+5, 0);
}
int fp(int u){
return e[u] < 0? u : e[u] = fp(e[u]);
}
void unon(int u, int v){
u = fp(u);
v = fp(v);
if(u == v) return;
if(e[u] > e[v]) swap(u, v);
e[u] += e[v];
e[v] = u;
}
int cost(int u){
u = fp(u);
return e[u]*(e[u]-1)/2;
}
};
int n, q;
int res[MX];
pii a[MX];
pii b[MX];
void nhap(){
cin >> n >> q;
for(int i=1; i<=n; i++) cin >> a[i].fi, a[i].se = i;
for(int i=1; i<=q; i++) cin >> b[i].fi, b[i].se = i;
}
void solve(){
sort(a+1, a+1+n);
sort(b+1, b+1+q);
DSU dsu(n);
int ans = 0;
for(int i=1, j=1; j<=q; j++){
while(i<=n && a[i].fi <= b[j].fi){
dsu.e[a[i].se] = -1;
if(dsu.e[a[i].se-1]) ans -= dsu.cost(a[i].se - 1), dsu.unon(a[i].se, a[i].se - 1);
if(dsu.e[a[i].se+1]) ans -= dsu.cost(a[i].se + 1), dsu.unon(a[i].se, a[i].se + 1);
ans += dsu.cost(a[i].se);
i++;
}
res[b[j].se] = ans;
}
for(int i=1; i<=q; i++) cout << res[i] << endl;
}
int32_t main(){
fastIO;
//file(TASK);
nhap();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
7248 KB |
Output is correct |
2 |
Correct |
16 ms |
8028 KB |
Output is correct |
3 |
Correct |
16 ms |
7664 KB |
Output is correct |
4 |
Correct |
15 ms |
7772 KB |
Output is correct |
5 |
Correct |
15 ms |
7772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
10332 KB |
Output is correct |
2 |
Correct |
28 ms |
11600 KB |
Output is correct |
3 |
Correct |
28 ms |
11608 KB |
Output is correct |
4 |
Correct |
32 ms |
11600 KB |
Output is correct |
5 |
Correct |
29 ms |
11508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
10320 KB |
Output is correct |
2 |
Correct |
32 ms |
11600 KB |
Output is correct |
3 |
Correct |
28 ms |
11856 KB |
Output is correct |
4 |
Correct |
28 ms |
11652 KB |
Output is correct |
5 |
Correct |
29 ms |
11860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
18 ms |
7248 KB |
Output is correct |
12 |
Correct |
16 ms |
8028 KB |
Output is correct |
13 |
Correct |
16 ms |
7664 KB |
Output is correct |
14 |
Correct |
15 ms |
7772 KB |
Output is correct |
15 |
Correct |
15 ms |
7772 KB |
Output is correct |
16 |
Correct |
14 ms |
7772 KB |
Output is correct |
17 |
Correct |
16 ms |
8052 KB |
Output is correct |
18 |
Correct |
15 ms |
8028 KB |
Output is correct |
19 |
Correct |
13 ms |
7772 KB |
Output is correct |
20 |
Correct |
19 ms |
8028 KB |
Output is correct |
21 |
Correct |
15 ms |
7656 KB |
Output is correct |
22 |
Correct |
14 ms |
7772 KB |
Output is correct |
23 |
Correct |
14 ms |
7772 KB |
Output is correct |
24 |
Correct |
13 ms |
7768 KB |
Output is correct |
25 |
Correct |
14 ms |
7772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
18 ms |
7248 KB |
Output is correct |
27 |
Correct |
16 ms |
8028 KB |
Output is correct |
28 |
Correct |
16 ms |
7664 KB |
Output is correct |
29 |
Correct |
15 ms |
7772 KB |
Output is correct |
30 |
Correct |
15 ms |
7772 KB |
Output is correct |
31 |
Correct |
27 ms |
10332 KB |
Output is correct |
32 |
Correct |
28 ms |
11600 KB |
Output is correct |
33 |
Correct |
28 ms |
11608 KB |
Output is correct |
34 |
Correct |
32 ms |
11600 KB |
Output is correct |
35 |
Correct |
29 ms |
11508 KB |
Output is correct |
36 |
Correct |
30 ms |
10320 KB |
Output is correct |
37 |
Correct |
32 ms |
11600 KB |
Output is correct |
38 |
Correct |
28 ms |
11856 KB |
Output is correct |
39 |
Correct |
28 ms |
11652 KB |
Output is correct |
40 |
Correct |
29 ms |
11860 KB |
Output is correct |
41 |
Correct |
14 ms |
7772 KB |
Output is correct |
42 |
Correct |
16 ms |
8052 KB |
Output is correct |
43 |
Correct |
15 ms |
8028 KB |
Output is correct |
44 |
Correct |
13 ms |
7772 KB |
Output is correct |
45 |
Correct |
19 ms |
8028 KB |
Output is correct |
46 |
Correct |
15 ms |
7656 KB |
Output is correct |
47 |
Correct |
14 ms |
7772 KB |
Output is correct |
48 |
Correct |
14 ms |
7772 KB |
Output is correct |
49 |
Correct |
13 ms |
7768 KB |
Output is correct |
50 |
Correct |
14 ms |
7772 KB |
Output is correct |
51 |
Correct |
35 ms |
11540 KB |
Output is correct |
52 |
Correct |
35 ms |
11452 KB |
Output is correct |
53 |
Correct |
36 ms |
11348 KB |
Output is correct |
54 |
Correct |
38 ms |
11344 KB |
Output is correct |
55 |
Correct |
36 ms |
11508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
18 ms |
7248 KB |
Output is correct |
27 |
Correct |
16 ms |
8028 KB |
Output is correct |
28 |
Correct |
16 ms |
7664 KB |
Output is correct |
29 |
Correct |
15 ms |
7772 KB |
Output is correct |
30 |
Correct |
15 ms |
7772 KB |
Output is correct |
31 |
Correct |
27 ms |
10332 KB |
Output is correct |
32 |
Correct |
28 ms |
11600 KB |
Output is correct |
33 |
Correct |
28 ms |
11608 KB |
Output is correct |
34 |
Correct |
32 ms |
11600 KB |
Output is correct |
35 |
Correct |
29 ms |
11508 KB |
Output is correct |
36 |
Correct |
30 ms |
10320 KB |
Output is correct |
37 |
Correct |
32 ms |
11600 KB |
Output is correct |
38 |
Correct |
28 ms |
11856 KB |
Output is correct |
39 |
Correct |
28 ms |
11652 KB |
Output is correct |
40 |
Correct |
29 ms |
11860 KB |
Output is correct |
41 |
Correct |
14 ms |
7772 KB |
Output is correct |
42 |
Correct |
16 ms |
8052 KB |
Output is correct |
43 |
Correct |
15 ms |
8028 KB |
Output is correct |
44 |
Correct |
13 ms |
7772 KB |
Output is correct |
45 |
Correct |
19 ms |
8028 KB |
Output is correct |
46 |
Correct |
15 ms |
7656 KB |
Output is correct |
47 |
Correct |
14 ms |
7772 KB |
Output is correct |
48 |
Correct |
14 ms |
7772 KB |
Output is correct |
49 |
Correct |
13 ms |
7768 KB |
Output is correct |
50 |
Correct |
14 ms |
7772 KB |
Output is correct |
51 |
Correct |
35 ms |
11540 KB |
Output is correct |
52 |
Correct |
35 ms |
11452 KB |
Output is correct |
53 |
Correct |
36 ms |
11348 KB |
Output is correct |
54 |
Correct |
38 ms |
11344 KB |
Output is correct |
55 |
Correct |
36 ms |
11508 KB |
Output is correct |
56 |
Correct |
400 ms |
67400 KB |
Output is correct |
57 |
Correct |
422 ms |
66572 KB |
Output is correct |
58 |
Correct |
373 ms |
65064 KB |
Output is correct |
59 |
Correct |
391 ms |
65836 KB |
Output is correct |
60 |
Correct |
407 ms |
67984 KB |
Output is correct |