結果
| 問題 |
No.3305 Shift Sort
|
| コンテスト | |
| ユーザー |
nonon
|
| 提出日時 | 2025-10-08 19:12:35 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 312 ms / 2,000 ms |
| コード長 | 7,803 bytes |
| コンパイル時間 | 8,463 ms |
| コンパイル使用メモリ | 300,728 KB |
| 実行使用メモリ | 15,724 KB |
| 最終ジャッジ日時 | 2025-10-08 19:12:53 |
| 合計ジャッジ時間 | 14,538 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; }
bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; }
template <typename Monoid>
struct segtree {
using M = Monoid;
using S = typename M::S;
segtree() : segtree(0) {}
segtree(int _n) : segtree(vector<S>(_n, M::e())) {}
segtree(const vector<S> &v) : n(v.size()) {
log = 1;
while ((1 << log) < n) log++;
sz = 1 << log;
d = vector<S>(2 * sz, M::e());
for (int i = 0; i < n; i++) d[i + sz] = v[i];
for (int i = sz - 1; i >= 1; i--) update(i);
}
void set(int p, const S &x) {
assert(0 <= p && p < n);
p += sz;
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 + sz];
}
S prod(int l, int r) const {
assert(0 <= l && l <= r && r <= n);
l += sz;
r += sz;
S pl = M::e(), pr = M::e();
while (l < r) {
if (l & 1) pl = M::op(pl, d[l++]);
if (r & 1) pr = M::op(d[--r], pr);
l >>= 1;
r >>= 1;
}
return M::op(pl, pr);
}
S all_prod() const { return d[1]; }
template<typename C>
int max_right(int l, const C &check) const {
assert(0 <= l && l <= n);
assert(check(M::e()));
if (l == n) return l;
l += sz;
S p = M::e();
do {
while (!(l & 1)) l >>= 1;
S np = M::op(p, d[l]);
if (!check(np)) {
while (l < sz) {
l <<= 1;
np = M::op(p, d[l]);
if (check(np)) {
p = np;
l++;
}
}
return l - sz;
}
p = np;
l++;
} while ((l & -l) != l);
return n;
}
template<typename C>
int max_right(const C &check) const {
return max_right(0, check);
}
template<typename C>
int min_left(int r, const C &check) const {
assert(0 <= r && r <= n);
assert(check(M::e()));
if (r == 0) return r;
r += sz;
S p = M::e();
do {
r--;
while (r > 1 && r & 1) r >>= 1;
S np = M::op(d[r], p);
if (!check(np)) {
while (r < sz) {
(r <<= 1)++;
np = M::op(d[r], p);
if (check(np)) {
p = np;
r--;
}
}
return r + 1 - sz;
}
p = np;
} while ((r & -r) != r);
return 0;
}
template<typename C>
int min_left(const C &check) const {
return min_left(n, check);
}
private:
int n, log, sz;
vector<S> d;
void update(int p) {
d[p] = M::op(d[2 * p], d[2 * p + 1]);
}
};
#include <ranges>
template<typename T, typename... Ts>
vector<T> merge_and_unique(const vector<T> &a, const Ts &...z) {
vector<T> res = a;
(res.insert(res.end(), z.begin(), z.end()), ...);
sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());
return res;
}
template<typename Abelian_Group>
struct fenwick_tree {
using M = Abelian_Group;
using S = typename M::S;
fenwick_tree() = default;
fenwick_tree(int _n) : fenwick_tree(vector<S>(_n, M::e())) {}
fenwick_tree(const vector<S> &_v) : n(_v.size()) {
d = vector<S>(n, M::e());
v = vector<S>(n, M::e());
for (int i = 1; i <= n; i++) {
v[i - 1] = _v[i - 1];
d[i - 1] += v[i - 1];
int j = i + (i & -i);
if (j <= n) d[j - 1] += d[i - 1];
}
}
void add(int p, S x) {
assert(0 <= p && p < n);
v[p] = M::op(v[p], x);
for (p++; p <= n; p += p & -p) {
d[p - 1] = M::op(d[p - 1], x);
}
}
void set(int p, S x) {
assert(0 <= p && p < n);
add(p, M::op(x, M::inv(v[p])));
}
S prod(int l, int r) const {
assert(0 <= l && l <= r && r <= n);
return prod(r) - prod(l);
}
private:
int n;
vector<S> d, v;
S prod(int p) const {
S res = M::e();
for (; p > 0; p -= p & -p) {
res = M::op(res, d[p - 1]);
}
return res;
}
};
template<typename Abelian_Group, typename T = int>
struct static_point_add_rectangle_sum {
using M = Abelian_Group;
using S = typename M::S;
static_point_add_rectangle_sum() = default;
static_point_add_rectangle_sum(int n, int q) {
xs.reserve(n);
ys.reserve(n);
ws.reserve(n);
xls.reserve(q);
yls.reserve(q);
xrs.reserve(q);
yrs.reserve(q);
}
void add_point(T x, T y, S w) {
xs.emplace_back(x);
ys.emplace_back(y);
ws.emplace_back(w);
}
void add_query(T xl, T yl, T xr, T yr) {
xls.emplace_back(xl);
yls.emplace_back(yl);
xrs.emplace_back(xr);
yrs.emplace_back(yr);
}
vector<S> solve() {
int n = xs.size(), q = xls.size();
if (n == 0 || q == 0) return vector<S>(q, M::e());
auto yb = merge_and_unique(ys);
for (T &y : ys) {
y = lower_bound(yb.begin(), yb.end(), y) - yb.begin();
}
vector<int> ord_pts(n), ord_query(2 * q);
iota(ord_pts.begin(), ord_pts.end(), 0);
iota(ord_query.begin(), ord_query.end(), 0);
ranges::sort(ord_pts, {}, [&](int i) { return xs[i]; });
ranges::sort(ord_query, {}, [&](int i) { return i < q ? xls[i] : xrs[i - q]; });
fenwick_tree<M> fw(yb.size());
vector<S> res(q);
int j = 0;
for (int i : ord_query) {
T x = (i < q ? xls[i] : xrs[i - q]);
while (j < n && xs[ord_pts[j]] < x) {
fw.add(ys[ord_pts[j]], ws[ord_pts[j]]);
j++;
}
if (i < q) {
int yl = lower_bound(yb.begin(), yb.end(), yls[i]) - yb.begin();
int yr = lower_bound(yb.begin(), yb.end(), yrs[i]) - yb.begin();
res[i] = M::op(res[i], M::inv(fw.prod(yl, yr)));
} else {
i -= q;
int yl = lower_bound(yb.begin(), yb.end(), yls[i]) - yb.begin();
int yr = lower_bound(yb.begin(), yb.end(), yrs[i]) - yb.begin();
res[i] = M::op(res[i], fw.prod(yl, yr));
}
}
return res;
}
private:
vector<T> xs, ys, xls, yls, xrs, yrs;
vector<S> ws;
};
template <typename T, T E = -numeric_limits<T>::max()>
struct max_monoid {
using S = T;
static S op(const S &a, const S &b) {
return max(a, b);
}
static S e() { return E; }
};
template <typename T>
struct add_monoid {
using S = T;
static S op(const S &a, const S &b) {
return a + b;
}
static S e() { return 0; }
static S inv(const S &x) {
return -x;
}
static S pow(const S &x, long long k) {
return x * k;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
segtree<max_monoid<int, 0>> seg(N + 1);
static_point_add_rectangle_sum<add_monoid<int>> RS(N, 2 * N);
for (int i = 1, P; i <= N; i++) {
cin >> P;
RS.add_point(i, seg.prod(P, N + 1), 1);
seg.set(P, i);
}
for (int i = 0, l, r; i < Q; i++) {
cin >> l >> r;
r++;
RS.add_query(l, l, r, N + 2);
}
for (auto v : RS.solve()) {
cout << v << '\n';
}
}
nonon