#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int N = 1e6 + 3;
int n, q, h[N], y[N];
namespace Sub12348{
bool valid_input() { return n * q <= 1e7; }
void solve(){
for(int t = 1; t <= q; ++t){
ll cur = 0, res = 0;
for(int i = 1; i <= n; ++i){
if(h[i] <= y[t])
++cur;
else{
res += cur * (cur + 1) / 2;
cur = 0;
}
}
res += cur * (cur + 1) / 2;
cout << res << '\n';
}
}
}
namespace Sub5{
bool valid_input(){ return q == 1 and y[1] == 1e6; }
void solve() { cout << 1ll * n * (n + 1) / 2; }
}
namespace Sub67{
bool valid_input(){
bool ok = 1;
for(int i = 2; i <= n; ++i)
ok &= (h[i] > h[i - 1]);
return ok;
}
void solve(){
for(int t = 1; t <= q; ++t){
ll x = upper_bound(h + 1, h + n + 1, y[t]) - h;
cout << x * (x + 1) / 2 << '\n';
}
}
}
struct DSU{
vector<ll> p;
ll cur;
void init(int n){
p.resize(n + 2, 0);
cur = 0;
}
ll calc(ll x) { return x * (x + 1) / 2; }
void make_set(int u){
++cur;
p[u] = -1;
}
int find_set(int u) { return p[u] < 0 ? u : p[u] = find_set(p[u]); }
void join_sets(int u, int v){
u = find_set(u);
v = find_set(v);
if(u == v)
return;
cur -= calc(-p[u]);
cur -= calc(-p[v]);
if(p[u] > p[v])
swap(u, v);
p[u] += p[v];
p[v] = u;
cur += calc(-p[u]);
}
} dsu;
bool ok[N];
vector<int> to_add[N];
ll res[N];
namespace SubFull{
void solve(){
dsu.init(N);
for(int i = 1; i <= n; ++i)
to_add[h[i]].push_back(i);
for(int i = 1; i < N; ++i){
for(int j = 0; j < (int)to_add[i].size(); ++j){
int x = to_add[i][j];
dsu.make_set(x);
if(ok[x - 1])
dsu.join_sets(x - 1, x);
if(ok[x + 1])
dsu.join_sets(x, x + 1);
ok[x] = 1;
}
res[i] = dsu.cur;
}
for(int i = 1; i <= q; ++i)
cout << res[y[i]] << '\n';
}
}
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> q;
for(int i = 1; i <= n; ++i)
cin >> h[i];
for(int i = 1; i <= q; ++i)
cin >> y[i];
if(Sub12348::valid_input())
Sub12348::solve();
else if(Sub5::valid_input())
Sub5::solve();
else if(Sub67::valid_input())
Sub67::solve();
else
SubFull::solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29020 KB |
Output is correct |
2 |
Correct |
5 ms |
29020 KB |
Output is correct |
3 |
Correct |
6 ms |
29020 KB |
Output is correct |
4 |
Correct |
7 ms |
29020 KB |
Output is correct |
5 |
Correct |
6 ms |
29020 KB |
Output is correct |
6 |
Correct |
6 ms |
29084 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
6 ms |
29020 KB |
Output is correct |
9 |
Correct |
6 ms |
29016 KB |
Output is correct |
10 |
Correct |
7 ms |
29016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29020 KB |
Output is correct |
2 |
Correct |
5 ms |
29020 KB |
Output is correct |
3 |
Correct |
6 ms |
29020 KB |
Output is correct |
4 |
Correct |
7 ms |
29020 KB |
Output is correct |
5 |
Correct |
6 ms |
29020 KB |
Output is correct |
6 |
Correct |
6 ms |
29084 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
6 ms |
29020 KB |
Output is correct |
9 |
Correct |
6 ms |
29016 KB |
Output is correct |
10 |
Correct |
7 ms |
29016 KB |
Output is correct |
11 |
Correct |
7 ms |
29020 KB |
Output is correct |
12 |
Correct |
6 ms |
29020 KB |
Output is correct |
13 |
Correct |
6 ms |
29016 KB |
Output is correct |
14 |
Correct |
6 ms |
29020 KB |
Output is correct |
15 |
Correct |
7 ms |
29020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29020 KB |
Output is correct |
2 |
Correct |
5 ms |
29020 KB |
Output is correct |
3 |
Correct |
6 ms |
29020 KB |
Output is correct |
4 |
Correct |
7 ms |
29020 KB |
Output is correct |
5 |
Correct |
6 ms |
29020 KB |
Output is correct |
6 |
Correct |
6 ms |
29084 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
6 ms |
29020 KB |
Output is correct |
9 |
Correct |
6 ms |
29016 KB |
Output is correct |
10 |
Correct |
7 ms |
29016 KB |
Output is correct |
11 |
Correct |
7 ms |
29020 KB |
Output is correct |
12 |
Correct |
6 ms |
29020 KB |
Output is correct |
13 |
Correct |
6 ms |
29016 KB |
Output is correct |
14 |
Correct |
6 ms |
29020 KB |
Output is correct |
15 |
Correct |
7 ms |
29020 KB |
Output is correct |
16 |
Correct |
6 ms |
29020 KB |
Output is correct |
17 |
Correct |
6 ms |
29020 KB |
Output is correct |
18 |
Correct |
6 ms |
29020 KB |
Output is correct |
19 |
Correct |
6 ms |
29020 KB |
Output is correct |
20 |
Correct |
6 ms |
29016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29020 KB |
Output is correct |
2 |
Correct |
5 ms |
29020 KB |
Output is correct |
3 |
Correct |
6 ms |
29020 KB |
Output is correct |
4 |
Correct |
7 ms |
29020 KB |
Output is correct |
5 |
Correct |
6 ms |
29020 KB |
Output is correct |
6 |
Correct |
6 ms |
29084 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
6 ms |
29020 KB |
Output is correct |
9 |
Correct |
6 ms |
29016 KB |
Output is correct |
10 |
Correct |
7 ms |
29016 KB |
Output is correct |
11 |
Correct |
7 ms |
29020 KB |
Output is correct |
12 |
Correct |
6 ms |
29020 KB |
Output is correct |
13 |
Correct |
6 ms |
29016 KB |
Output is correct |
14 |
Correct |
6 ms |
29020 KB |
Output is correct |
15 |
Correct |
7 ms |
29020 KB |
Output is correct |
16 |
Correct |
6 ms |
29020 KB |
Output is correct |
17 |
Correct |
6 ms |
29020 KB |
Output is correct |
18 |
Correct |
6 ms |
29020 KB |
Output is correct |
19 |
Correct |
6 ms |
29020 KB |
Output is correct |
20 |
Correct |
6 ms |
29016 KB |
Output is correct |
21 |
Correct |
7 ms |
29020 KB |
Output is correct |
22 |
Correct |
8 ms |
29020 KB |
Output is correct |
23 |
Correct |
8 ms |
29020 KB |
Output is correct |
24 |
Correct |
8 ms |
29020 KB |
Output is correct |
25 |
Correct |
7 ms |
29020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
29532 KB |
Output is correct |
2 |
Correct |
13 ms |
29788 KB |
Output is correct |
3 |
Correct |
11 ms |
29532 KB |
Output is correct |
4 |
Correct |
13 ms |
29684 KB |
Output is correct |
5 |
Correct |
11 ms |
29532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1046 ms |
30696 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
34 ms |
31324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29020 KB |
Output is correct |
2 |
Correct |
5 ms |
29020 KB |
Output is correct |
3 |
Correct |
6 ms |
29020 KB |
Output is correct |
4 |
Correct |
7 ms |
29020 KB |
Output is correct |
5 |
Correct |
6 ms |
29020 KB |
Output is correct |
6 |
Correct |
6 ms |
29084 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
6 ms |
29020 KB |
Output is correct |
9 |
Correct |
6 ms |
29016 KB |
Output is correct |
10 |
Correct |
7 ms |
29016 KB |
Output is correct |
11 |
Correct |
13 ms |
29532 KB |
Output is correct |
12 |
Correct |
13 ms |
29788 KB |
Output is correct |
13 |
Correct |
11 ms |
29532 KB |
Output is correct |
14 |
Correct |
13 ms |
29684 KB |
Output is correct |
15 |
Correct |
11 ms |
29532 KB |
Output is correct |
16 |
Correct |
12 ms |
29700 KB |
Output is correct |
17 |
Correct |
12 ms |
29784 KB |
Output is correct |
18 |
Correct |
12 ms |
29788 KB |
Output is correct |
19 |
Correct |
11 ms |
29716 KB |
Output is correct |
20 |
Correct |
12 ms |
29788 KB |
Output is correct |
21 |
Correct |
11 ms |
29528 KB |
Output is correct |
22 |
Correct |
14 ms |
29944 KB |
Output is correct |
23 |
Correct |
14 ms |
29672 KB |
Output is correct |
24 |
Correct |
12 ms |
29788 KB |
Output is correct |
25 |
Correct |
12 ms |
29648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29020 KB |
Output is correct |
2 |
Correct |
5 ms |
29020 KB |
Output is correct |
3 |
Correct |
6 ms |
29020 KB |
Output is correct |
4 |
Correct |
7 ms |
29020 KB |
Output is correct |
5 |
Correct |
6 ms |
29020 KB |
Output is correct |
6 |
Correct |
6 ms |
29084 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
6 ms |
29020 KB |
Output is correct |
9 |
Correct |
6 ms |
29016 KB |
Output is correct |
10 |
Correct |
7 ms |
29016 KB |
Output is correct |
11 |
Correct |
7 ms |
29020 KB |
Output is correct |
12 |
Correct |
6 ms |
29020 KB |
Output is correct |
13 |
Correct |
6 ms |
29016 KB |
Output is correct |
14 |
Correct |
6 ms |
29020 KB |
Output is correct |
15 |
Correct |
7 ms |
29020 KB |
Output is correct |
16 |
Correct |
6 ms |
29020 KB |
Output is correct |
17 |
Correct |
6 ms |
29020 KB |
Output is correct |
18 |
Correct |
6 ms |
29020 KB |
Output is correct |
19 |
Correct |
6 ms |
29020 KB |
Output is correct |
20 |
Correct |
6 ms |
29016 KB |
Output is correct |
21 |
Correct |
7 ms |
29020 KB |
Output is correct |
22 |
Correct |
8 ms |
29020 KB |
Output is correct |
23 |
Correct |
8 ms |
29020 KB |
Output is correct |
24 |
Correct |
8 ms |
29020 KB |
Output is correct |
25 |
Correct |
7 ms |
29020 KB |
Output is correct |
26 |
Correct |
13 ms |
29532 KB |
Output is correct |
27 |
Correct |
13 ms |
29788 KB |
Output is correct |
28 |
Correct |
11 ms |
29532 KB |
Output is correct |
29 |
Correct |
13 ms |
29684 KB |
Output is correct |
30 |
Correct |
11 ms |
29532 KB |
Output is correct |
31 |
Execution timed out |
1046 ms |
30696 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29020 KB |
Output is correct |
2 |
Correct |
5 ms |
29020 KB |
Output is correct |
3 |
Correct |
6 ms |
29020 KB |
Output is correct |
4 |
Correct |
7 ms |
29020 KB |
Output is correct |
5 |
Correct |
6 ms |
29020 KB |
Output is correct |
6 |
Correct |
6 ms |
29084 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
6 ms |
29020 KB |
Output is correct |
9 |
Correct |
6 ms |
29016 KB |
Output is correct |
10 |
Correct |
7 ms |
29016 KB |
Output is correct |
11 |
Correct |
7 ms |
29020 KB |
Output is correct |
12 |
Correct |
6 ms |
29020 KB |
Output is correct |
13 |
Correct |
6 ms |
29016 KB |
Output is correct |
14 |
Correct |
6 ms |
29020 KB |
Output is correct |
15 |
Correct |
7 ms |
29020 KB |
Output is correct |
16 |
Correct |
6 ms |
29020 KB |
Output is correct |
17 |
Correct |
6 ms |
29020 KB |
Output is correct |
18 |
Correct |
6 ms |
29020 KB |
Output is correct |
19 |
Correct |
6 ms |
29020 KB |
Output is correct |
20 |
Correct |
6 ms |
29016 KB |
Output is correct |
21 |
Correct |
7 ms |
29020 KB |
Output is correct |
22 |
Correct |
8 ms |
29020 KB |
Output is correct |
23 |
Correct |
8 ms |
29020 KB |
Output is correct |
24 |
Correct |
8 ms |
29020 KB |
Output is correct |
25 |
Correct |
7 ms |
29020 KB |
Output is correct |
26 |
Correct |
13 ms |
29532 KB |
Output is correct |
27 |
Correct |
13 ms |
29788 KB |
Output is correct |
28 |
Correct |
11 ms |
29532 KB |
Output is correct |
29 |
Correct |
13 ms |
29684 KB |
Output is correct |
30 |
Correct |
11 ms |
29532 KB |
Output is correct |
31 |
Execution timed out |
1046 ms |
30696 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |