#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
const int N = 1000002;
ll ans[N];
vector<int> v[N];
ll cur;
struct UFDS {
vector<int> p;
vector<ll> siz;
void init(int n){
p.resize(n + 2);
siz.resize(n + 2);
for(int i = 0; i <= n; i++){
p[i] = i;
siz[i] = 0;
}
}
void set(int x){
siz[x] = 1;
cur++;
}
int get(int x){
return p[x]=(x==p[x] ? x : get(p[x]));
}
void unio(int u , int vt){
int a = get(u);
int b=get(vt);
if(a==b)return;
if(siz[a]>siz[b])swap(a,b);
p[b]=a;
cur -= (siz[a] * (siz[a]+1))/2;
cur -= (siz[b] * (siz[b]+1))/2;
siz[a]+=siz[b];
cur += (siz[a] * (siz[a]+1))/2;
}
};
void solve(){
int n , q;
cin >> n >> q;
int arr[n];
for(int i = 0; i < n; i++){cin >> arr[i];v[arr[i]].pb(i);}
UFDS d;
d.init(n + 2);
cur = 0;
for(int i = 1; i <= 1000000; i++){
for(int j : v[i]){
d.set(j);
}
for(int j : v[i]){
if(j - 1 >= 0 && arr[j-1] <= i)d.unio(j,j-1);
if(j + 1 < n && arr[j + 1] <= i)d.unio(j,j+1);
}
ans[i] = cur;
}
while(q--){
int x;
cin >> x;
cout << ans[x] << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31564 KB |
Output is correct |
2 |
Correct |
21 ms |
31528 KB |
Output is correct |
3 |
Correct |
17 ms |
31608 KB |
Output is correct |
4 |
Correct |
21 ms |
31564 KB |
Output is correct |
5 |
Correct |
18 ms |
31624 KB |
Output is correct |
6 |
Correct |
16 ms |
31508 KB |
Output is correct |
7 |
Correct |
16 ms |
31572 KB |
Output is correct |
8 |
Correct |
19 ms |
31608 KB |
Output is correct |
9 |
Correct |
16 ms |
31564 KB |
Output is correct |
10 |
Correct |
17 ms |
31624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31564 KB |
Output is correct |
2 |
Correct |
21 ms |
31528 KB |
Output is correct |
3 |
Correct |
17 ms |
31608 KB |
Output is correct |
4 |
Correct |
21 ms |
31564 KB |
Output is correct |
5 |
Correct |
18 ms |
31624 KB |
Output is correct |
6 |
Correct |
16 ms |
31508 KB |
Output is correct |
7 |
Correct |
16 ms |
31572 KB |
Output is correct |
8 |
Correct |
19 ms |
31608 KB |
Output is correct |
9 |
Correct |
16 ms |
31564 KB |
Output is correct |
10 |
Correct |
17 ms |
31624 KB |
Output is correct |
11 |
Correct |
17 ms |
31628 KB |
Output is correct |
12 |
Correct |
17 ms |
31620 KB |
Output is correct |
13 |
Correct |
17 ms |
31564 KB |
Output is correct |
14 |
Correct |
17 ms |
31564 KB |
Output is correct |
15 |
Correct |
17 ms |
31596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31564 KB |
Output is correct |
2 |
Correct |
21 ms |
31528 KB |
Output is correct |
3 |
Correct |
17 ms |
31608 KB |
Output is correct |
4 |
Correct |
21 ms |
31564 KB |
Output is correct |
5 |
Correct |
18 ms |
31624 KB |
Output is correct |
6 |
Correct |
16 ms |
31508 KB |
Output is correct |
7 |
Correct |
16 ms |
31572 KB |
Output is correct |
8 |
Correct |
19 ms |
31608 KB |
Output is correct |
9 |
Correct |
16 ms |
31564 KB |
Output is correct |
10 |
Correct |
17 ms |
31624 KB |
Output is correct |
11 |
Correct |
17 ms |
31628 KB |
Output is correct |
12 |
Correct |
17 ms |
31620 KB |
Output is correct |
13 |
Correct |
17 ms |
31564 KB |
Output is correct |
14 |
Correct |
17 ms |
31564 KB |
Output is correct |
15 |
Correct |
17 ms |
31596 KB |
Output is correct |
16 |
Correct |
20 ms |
31584 KB |
Output is correct |
17 |
Correct |
16 ms |
31588 KB |
Output is correct |
18 |
Correct |
17 ms |
31564 KB |
Output is correct |
19 |
Correct |
16 ms |
31608 KB |
Output is correct |
20 |
Correct |
16 ms |
31564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31564 KB |
Output is correct |
2 |
Correct |
21 ms |
31528 KB |
Output is correct |
3 |
Correct |
17 ms |
31608 KB |
Output is correct |
4 |
Correct |
21 ms |
31564 KB |
Output is correct |
5 |
Correct |
18 ms |
31624 KB |
Output is correct |
6 |
Correct |
16 ms |
31508 KB |
Output is correct |
7 |
Correct |
16 ms |
31572 KB |
Output is correct |
8 |
Correct |
19 ms |
31608 KB |
Output is correct |
9 |
Correct |
16 ms |
31564 KB |
Output is correct |
10 |
Correct |
17 ms |
31624 KB |
Output is correct |
11 |
Correct |
17 ms |
31628 KB |
Output is correct |
12 |
Correct |
17 ms |
31620 KB |
Output is correct |
13 |
Correct |
17 ms |
31564 KB |
Output is correct |
14 |
Correct |
17 ms |
31564 KB |
Output is correct |
15 |
Correct |
17 ms |
31596 KB |
Output is correct |
16 |
Correct |
20 ms |
31584 KB |
Output is correct |
17 |
Correct |
16 ms |
31588 KB |
Output is correct |
18 |
Correct |
17 ms |
31564 KB |
Output is correct |
19 |
Correct |
16 ms |
31608 KB |
Output is correct |
20 |
Correct |
16 ms |
31564 KB |
Output is correct |
21 |
Correct |
20 ms |
31556 KB |
Output is correct |
22 |
Correct |
17 ms |
31624 KB |
Output is correct |
23 |
Correct |
17 ms |
31588 KB |
Output is correct |
24 |
Correct |
22 ms |
31608 KB |
Output is correct |
25 |
Correct |
17 ms |
31624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
35652 KB |
Output is correct |
2 |
Correct |
56 ms |
36620 KB |
Output is correct |
3 |
Correct |
37 ms |
35396 KB |
Output is correct |
4 |
Correct |
54 ms |
36300 KB |
Output is correct |
5 |
Correct |
36 ms |
35396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
38008 KB |
Output is correct |
2 |
Correct |
51 ms |
38140 KB |
Output is correct |
3 |
Correct |
47 ms |
37976 KB |
Output is correct |
4 |
Correct |
56 ms |
38256 KB |
Output is correct |
5 |
Correct |
50 ms |
37908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
38276 KB |
Output is correct |
2 |
Correct |
54 ms |
38156 KB |
Output is correct |
3 |
Correct |
65 ms |
38028 KB |
Output is correct |
4 |
Correct |
60 ms |
38468 KB |
Output is correct |
5 |
Correct |
54 ms |
38340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31564 KB |
Output is correct |
2 |
Correct |
21 ms |
31528 KB |
Output is correct |
3 |
Correct |
17 ms |
31608 KB |
Output is correct |
4 |
Correct |
21 ms |
31564 KB |
Output is correct |
5 |
Correct |
18 ms |
31624 KB |
Output is correct |
6 |
Correct |
16 ms |
31508 KB |
Output is correct |
7 |
Correct |
16 ms |
31572 KB |
Output is correct |
8 |
Correct |
19 ms |
31608 KB |
Output is correct |
9 |
Correct |
16 ms |
31564 KB |
Output is correct |
10 |
Correct |
17 ms |
31624 KB |
Output is correct |
11 |
Correct |
37 ms |
35652 KB |
Output is correct |
12 |
Correct |
56 ms |
36620 KB |
Output is correct |
13 |
Correct |
37 ms |
35396 KB |
Output is correct |
14 |
Correct |
54 ms |
36300 KB |
Output is correct |
15 |
Correct |
36 ms |
35396 KB |
Output is correct |
16 |
Correct |
38 ms |
35336 KB |
Output is correct |
17 |
Correct |
55 ms |
36732 KB |
Output is correct |
18 |
Correct |
53 ms |
36676 KB |
Output is correct |
19 |
Correct |
40 ms |
35280 KB |
Output is correct |
20 |
Correct |
62 ms |
36616 KB |
Output is correct |
21 |
Correct |
37 ms |
35312 KB |
Output is correct |
22 |
Correct |
50 ms |
36460 KB |
Output is correct |
23 |
Correct |
39 ms |
35660 KB |
Output is correct |
24 |
Correct |
49 ms |
36276 KB |
Output is correct |
25 |
Correct |
37 ms |
35524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31564 KB |
Output is correct |
2 |
Correct |
21 ms |
31528 KB |
Output is correct |
3 |
Correct |
17 ms |
31608 KB |
Output is correct |
4 |
Correct |
21 ms |
31564 KB |
Output is correct |
5 |
Correct |
18 ms |
31624 KB |
Output is correct |
6 |
Correct |
16 ms |
31508 KB |
Output is correct |
7 |
Correct |
16 ms |
31572 KB |
Output is correct |
8 |
Correct |
19 ms |
31608 KB |
Output is correct |
9 |
Correct |
16 ms |
31564 KB |
Output is correct |
10 |
Correct |
17 ms |
31624 KB |
Output is correct |
11 |
Correct |
17 ms |
31628 KB |
Output is correct |
12 |
Correct |
17 ms |
31620 KB |
Output is correct |
13 |
Correct |
17 ms |
31564 KB |
Output is correct |
14 |
Correct |
17 ms |
31564 KB |
Output is correct |
15 |
Correct |
17 ms |
31596 KB |
Output is correct |
16 |
Correct |
20 ms |
31584 KB |
Output is correct |
17 |
Correct |
16 ms |
31588 KB |
Output is correct |
18 |
Correct |
17 ms |
31564 KB |
Output is correct |
19 |
Correct |
16 ms |
31608 KB |
Output is correct |
20 |
Correct |
16 ms |
31564 KB |
Output is correct |
21 |
Correct |
20 ms |
31556 KB |
Output is correct |
22 |
Correct |
17 ms |
31624 KB |
Output is correct |
23 |
Correct |
17 ms |
31588 KB |
Output is correct |
24 |
Correct |
22 ms |
31608 KB |
Output is correct |
25 |
Correct |
17 ms |
31624 KB |
Output is correct |
26 |
Correct |
37 ms |
35652 KB |
Output is correct |
27 |
Correct |
56 ms |
36620 KB |
Output is correct |
28 |
Correct |
37 ms |
35396 KB |
Output is correct |
29 |
Correct |
54 ms |
36300 KB |
Output is correct |
30 |
Correct |
36 ms |
35396 KB |
Output is correct |
31 |
Correct |
49 ms |
38008 KB |
Output is correct |
32 |
Correct |
51 ms |
38140 KB |
Output is correct |
33 |
Correct |
47 ms |
37976 KB |
Output is correct |
34 |
Correct |
56 ms |
38256 KB |
Output is correct |
35 |
Correct |
50 ms |
37908 KB |
Output is correct |
36 |
Correct |
52 ms |
38276 KB |
Output is correct |
37 |
Correct |
54 ms |
38156 KB |
Output is correct |
38 |
Correct |
65 ms |
38028 KB |
Output is correct |
39 |
Correct |
60 ms |
38468 KB |
Output is correct |
40 |
Correct |
54 ms |
38340 KB |
Output is correct |
41 |
Correct |
38 ms |
35336 KB |
Output is correct |
42 |
Correct |
55 ms |
36732 KB |
Output is correct |
43 |
Correct |
53 ms |
36676 KB |
Output is correct |
44 |
Correct |
40 ms |
35280 KB |
Output is correct |
45 |
Correct |
62 ms |
36616 KB |
Output is correct |
46 |
Correct |
37 ms |
35312 KB |
Output is correct |
47 |
Correct |
50 ms |
36460 KB |
Output is correct |
48 |
Correct |
39 ms |
35660 KB |
Output is correct |
49 |
Correct |
49 ms |
36276 KB |
Output is correct |
50 |
Correct |
37 ms |
35524 KB |
Output is correct |
51 |
Correct |
79 ms |
36968 KB |
Output is correct |
52 |
Correct |
73 ms |
37700 KB |
Output is correct |
53 |
Correct |
62 ms |
37032 KB |
Output is correct |
54 |
Correct |
69 ms |
37764 KB |
Output is correct |
55 |
Correct |
56 ms |
36996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31564 KB |
Output is correct |
2 |
Correct |
21 ms |
31528 KB |
Output is correct |
3 |
Correct |
17 ms |
31608 KB |
Output is correct |
4 |
Correct |
21 ms |
31564 KB |
Output is correct |
5 |
Correct |
18 ms |
31624 KB |
Output is correct |
6 |
Correct |
16 ms |
31508 KB |
Output is correct |
7 |
Correct |
16 ms |
31572 KB |
Output is correct |
8 |
Correct |
19 ms |
31608 KB |
Output is correct |
9 |
Correct |
16 ms |
31564 KB |
Output is correct |
10 |
Correct |
17 ms |
31624 KB |
Output is correct |
11 |
Correct |
17 ms |
31628 KB |
Output is correct |
12 |
Correct |
17 ms |
31620 KB |
Output is correct |
13 |
Correct |
17 ms |
31564 KB |
Output is correct |
14 |
Correct |
17 ms |
31564 KB |
Output is correct |
15 |
Correct |
17 ms |
31596 KB |
Output is correct |
16 |
Correct |
20 ms |
31584 KB |
Output is correct |
17 |
Correct |
16 ms |
31588 KB |
Output is correct |
18 |
Correct |
17 ms |
31564 KB |
Output is correct |
19 |
Correct |
16 ms |
31608 KB |
Output is correct |
20 |
Correct |
16 ms |
31564 KB |
Output is correct |
21 |
Correct |
20 ms |
31556 KB |
Output is correct |
22 |
Correct |
17 ms |
31624 KB |
Output is correct |
23 |
Correct |
17 ms |
31588 KB |
Output is correct |
24 |
Correct |
22 ms |
31608 KB |
Output is correct |
25 |
Correct |
17 ms |
31624 KB |
Output is correct |
26 |
Correct |
37 ms |
35652 KB |
Output is correct |
27 |
Correct |
56 ms |
36620 KB |
Output is correct |
28 |
Correct |
37 ms |
35396 KB |
Output is correct |
29 |
Correct |
54 ms |
36300 KB |
Output is correct |
30 |
Correct |
36 ms |
35396 KB |
Output is correct |
31 |
Correct |
49 ms |
38008 KB |
Output is correct |
32 |
Correct |
51 ms |
38140 KB |
Output is correct |
33 |
Correct |
47 ms |
37976 KB |
Output is correct |
34 |
Correct |
56 ms |
38256 KB |
Output is correct |
35 |
Correct |
50 ms |
37908 KB |
Output is correct |
36 |
Correct |
52 ms |
38276 KB |
Output is correct |
37 |
Correct |
54 ms |
38156 KB |
Output is correct |
38 |
Correct |
65 ms |
38028 KB |
Output is correct |
39 |
Correct |
60 ms |
38468 KB |
Output is correct |
40 |
Correct |
54 ms |
38340 KB |
Output is correct |
41 |
Correct |
38 ms |
35336 KB |
Output is correct |
42 |
Correct |
55 ms |
36732 KB |
Output is correct |
43 |
Correct |
53 ms |
36676 KB |
Output is correct |
44 |
Correct |
40 ms |
35280 KB |
Output is correct |
45 |
Correct |
62 ms |
36616 KB |
Output is correct |
46 |
Correct |
37 ms |
35312 KB |
Output is correct |
47 |
Correct |
50 ms |
36460 KB |
Output is correct |
48 |
Correct |
39 ms |
35660 KB |
Output is correct |
49 |
Correct |
49 ms |
36276 KB |
Output is correct |
50 |
Correct |
37 ms |
35524 KB |
Output is correct |
51 |
Correct |
79 ms |
36968 KB |
Output is correct |
52 |
Correct |
73 ms |
37700 KB |
Output is correct |
53 |
Correct |
62 ms |
37032 KB |
Output is correct |
54 |
Correct |
69 ms |
37764 KB |
Output is correct |
55 |
Correct |
56 ms |
36996 KB |
Output is correct |
56 |
Correct |
703 ms |
85400 KB |
Output is correct |
57 |
Correct |
664 ms |
86644 KB |
Output is correct |
58 |
Correct |
668 ms |
82776 KB |
Output is correct |
59 |
Correct |
650 ms |
84700 KB |
Output is correct |
60 |
Correct |
681 ms |
87416 KB |
Output is correct |