#line 1 "main.cpp"
#include <bits/stdc++.h>
#line 1 "ProCon/cp_library/atcoder/segtree.hpp"
#line 7 "ProCon/cp_library/atcoder/segtree.hpp"
#line 1 "ProCon/cp_library/atcoder/internal_bit.hpp"
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
int x = 0;
while ((1U << x) < (unsigned int)(n)) x++;
return x;
}
// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
constexpr int bsf_constexpr(unsigned int n) {
int x = 0;
while (!(n & (1 << x))) x++;
return x;
}
// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
} // namespace internal
} // namespace atcoder
#line 9 "ProCon/cp_library/atcoder/segtree.hpp"
namespace atcoder {
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
public:
segtree() : segtree(0) {}
explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {
log = internal::ceil_pow2(_n);
size = 1 << log;
d = std::vector<S>(2 * size, e());
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = size - 1; i >= 1; i--) {
update(i);
}
}
void set(int p, S x) {
assert(0 <= p && p < _n);
p += size;
d[p] = x;
for (int i = 1; i <= log; i++) update(p >> i);
}
S get(int p) const {
assert(0 <= p && p < _n);
return d[p + size];
}
S prod(int l, int r) const {
assert(0 <= l && l <= r && r <= _n);
S sml = e(), smr = e();
l += size;
r += size;
while (l < r) {
if (l & 1) sml = op(sml, d[l++]);
if (r & 1) smr = op(d[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
S all_prod() const { return d[1]; }
template <bool (*f)(S)> int max_right(int l) const {
return max_right(l, [](S x) { return f(x); });
}
template <class F> int max_right(int l, F f) const {
assert(0 <= l && l <= _n);
assert(f(e()));
if (l == _n) return _n;
l += size;
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!f(op(sm, d[l]))) {
while (l < size) {
l = (2 * l);
if (f(op(sm, d[l]))) {
sm = op(sm, d[l]);
l++;
}
}
return l - size;
}
sm = op(sm, d[l]);
l++;
} while ((l & -l) != l);
return _n;
}
template <bool (*f)(S)> int min_left(int r) const {
return min_left(r, [](S x) { return f(x); });
}
template <class F> int min_left(int r, F f) const {
assert(0 <= r && r <= _n);
assert(f(e()));
if (r == 0) return 0;
r += size;
S sm = e();
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!f(op(d[r], sm))) {
while (r < size) {
r = (2 * r + 1);
if (f(op(d[r], sm))) {
sm = op(d[r], sm);
r--;
}
}
return r + 1 - size;
}
sm = op(d[r], sm);
} while ((r & -r) != r);
return 0;
}
private:
int _n, size, log;
std::vector<S> d;
void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};
} // namespace atcoder
#line 4 "main.cpp"
int op(int a, int b) {
return a + b;
}
int e() {
return 0;
}
using i64 = long long;
i64 c2(i64 x) {
return x * (x + 1) / 2;
}
void solve() {
int N, Q;
std::cin >> N >> Q;
std::vector<int> H(N), Y(Q);
for (auto &e : H) std::cin >> e;
for (auto &e : Y) std::cin >> e;
std::vector<std::pair<int, int>> queries(Q);
for (int i = 0; i < Q; ++i) queries[i] = {Y[i], i};
std::sort(queries.begin(), queries.end());
std::vector<std::pair<int, int>> mountains(N);
for (int i = 0; i < N; ++i) mountains[i] = {H[i], i};
std::sort(mountains.begin(), mountains.end());
atcoder::segtree<int, op, e> seg(N + 2);
seg.set(0, 1);
seg.set(N + 1, 1);
int j = 0;
for (int i = 0; i < N; ++i) seg.set(i + 1, 1);
std::vector<i64> answer(Q);
i64 countFlights = 0;
for (int i = 0; i < N; ++i) {
while (j != Q and queries[j].first < mountains[i].first) {
answer[queries[j].second] = countFlights;
++j;
}
const int m = mountains[i].second + 1;
const int r = seg.max_right(m + 1, [](int x) {
return x == 0;
});
const int l = seg.min_left(m, [](int x) {
return x == 0;
}) - 1;
seg.set(m, 0);
countFlights -= c2(r - m - 1);
countFlights -= c2(m - l - 1);
countFlights += c2(r - l - 1);
}
while (j != Q) {
answer[queries[j].second] = countFlights;
++j;
}
for (const auto e : answer) std::cout << e << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2880 KB |
Output is correct |
2 |
Correct |
26 ms |
2900 KB |
Output is correct |
3 |
Correct |
30 ms |
2772 KB |
Output is correct |
4 |
Correct |
25 ms |
2644 KB |
Output is correct |
5 |
Correct |
28 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
5212 KB |
Output is correct |
2 |
Correct |
49 ms |
5324 KB |
Output is correct |
3 |
Correct |
49 ms |
5208 KB |
Output is correct |
4 |
Correct |
51 ms |
5224 KB |
Output is correct |
5 |
Correct |
49 ms |
5148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
5096 KB |
Output is correct |
2 |
Correct |
51 ms |
5172 KB |
Output is correct |
3 |
Correct |
50 ms |
5224 KB |
Output is correct |
4 |
Correct |
54 ms |
5192 KB |
Output is correct |
5 |
Correct |
59 ms |
5444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
29 ms |
2880 KB |
Output is correct |
12 |
Correct |
26 ms |
2900 KB |
Output is correct |
13 |
Correct |
30 ms |
2772 KB |
Output is correct |
14 |
Correct |
25 ms |
2644 KB |
Output is correct |
15 |
Correct |
28 ms |
2772 KB |
Output is correct |
16 |
Correct |
28 ms |
2700 KB |
Output is correct |
17 |
Correct |
28 ms |
2856 KB |
Output is correct |
18 |
Correct |
30 ms |
2908 KB |
Output is correct |
19 |
Correct |
32 ms |
2744 KB |
Output is correct |
20 |
Correct |
28 ms |
2748 KB |
Output is correct |
21 |
Correct |
25 ms |
2772 KB |
Output is correct |
22 |
Correct |
26 ms |
2700 KB |
Output is correct |
23 |
Correct |
26 ms |
2904 KB |
Output is correct |
24 |
Correct |
26 ms |
2688 KB |
Output is correct |
25 |
Correct |
27 ms |
2740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
29 ms |
2880 KB |
Output is correct |
27 |
Correct |
26 ms |
2900 KB |
Output is correct |
28 |
Correct |
30 ms |
2772 KB |
Output is correct |
29 |
Correct |
25 ms |
2644 KB |
Output is correct |
30 |
Correct |
28 ms |
2772 KB |
Output is correct |
31 |
Correct |
49 ms |
5212 KB |
Output is correct |
32 |
Correct |
49 ms |
5324 KB |
Output is correct |
33 |
Correct |
49 ms |
5208 KB |
Output is correct |
34 |
Correct |
51 ms |
5224 KB |
Output is correct |
35 |
Correct |
49 ms |
5148 KB |
Output is correct |
36 |
Correct |
53 ms |
5096 KB |
Output is correct |
37 |
Correct |
51 ms |
5172 KB |
Output is correct |
38 |
Correct |
50 ms |
5224 KB |
Output is correct |
39 |
Correct |
54 ms |
5192 KB |
Output is correct |
40 |
Correct |
59 ms |
5444 KB |
Output is correct |
41 |
Correct |
28 ms |
2700 KB |
Output is correct |
42 |
Correct |
28 ms |
2856 KB |
Output is correct |
43 |
Correct |
30 ms |
2908 KB |
Output is correct |
44 |
Correct |
32 ms |
2744 KB |
Output is correct |
45 |
Correct |
28 ms |
2748 KB |
Output is correct |
46 |
Correct |
25 ms |
2772 KB |
Output is correct |
47 |
Correct |
26 ms |
2700 KB |
Output is correct |
48 |
Correct |
26 ms |
2904 KB |
Output is correct |
49 |
Correct |
26 ms |
2688 KB |
Output is correct |
50 |
Correct |
27 ms |
2740 KB |
Output is correct |
51 |
Correct |
51 ms |
5204 KB |
Output is correct |
52 |
Correct |
57 ms |
4968 KB |
Output is correct |
53 |
Correct |
62 ms |
5120 KB |
Output is correct |
54 |
Correct |
48 ms |
4952 KB |
Output is correct |
55 |
Correct |
56 ms |
5140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
29 ms |
2880 KB |
Output is correct |
27 |
Correct |
26 ms |
2900 KB |
Output is correct |
28 |
Correct |
30 ms |
2772 KB |
Output is correct |
29 |
Correct |
25 ms |
2644 KB |
Output is correct |
30 |
Correct |
28 ms |
2772 KB |
Output is correct |
31 |
Correct |
49 ms |
5212 KB |
Output is correct |
32 |
Correct |
49 ms |
5324 KB |
Output is correct |
33 |
Correct |
49 ms |
5208 KB |
Output is correct |
34 |
Correct |
51 ms |
5224 KB |
Output is correct |
35 |
Correct |
49 ms |
5148 KB |
Output is correct |
36 |
Correct |
53 ms |
5096 KB |
Output is correct |
37 |
Correct |
51 ms |
5172 KB |
Output is correct |
38 |
Correct |
50 ms |
5224 KB |
Output is correct |
39 |
Correct |
54 ms |
5192 KB |
Output is correct |
40 |
Correct |
59 ms |
5444 KB |
Output is correct |
41 |
Correct |
28 ms |
2700 KB |
Output is correct |
42 |
Correct |
28 ms |
2856 KB |
Output is correct |
43 |
Correct |
30 ms |
2908 KB |
Output is correct |
44 |
Correct |
32 ms |
2744 KB |
Output is correct |
45 |
Correct |
28 ms |
2748 KB |
Output is correct |
46 |
Correct |
25 ms |
2772 KB |
Output is correct |
47 |
Correct |
26 ms |
2700 KB |
Output is correct |
48 |
Correct |
26 ms |
2904 KB |
Output is correct |
49 |
Correct |
26 ms |
2688 KB |
Output is correct |
50 |
Correct |
27 ms |
2740 KB |
Output is correct |
51 |
Correct |
51 ms |
5204 KB |
Output is correct |
52 |
Correct |
57 ms |
4968 KB |
Output is correct |
53 |
Correct |
62 ms |
5120 KB |
Output is correct |
54 |
Correct |
48 ms |
4952 KB |
Output is correct |
55 |
Correct |
56 ms |
5140 KB |
Output is correct |
56 |
Correct |
669 ms |
46492 KB |
Output is correct |
57 |
Correct |
652 ms |
58524 KB |
Output is correct |
58 |
Correct |
612 ms |
56088 KB |
Output is correct |
59 |
Correct |
616 ms |
57060 KB |
Output is correct |
60 |
Correct |
677 ms |
60448 KB |
Output is correct |