結果

問題 No.404 部分門松列
ユーザー IseriIseri
提出日時 2022-07-13 09:13:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,712 ms / 2,000 ms
コード長 16,933 bytes
コンパイル時間 3,687 ms
コンパイル使用メモリ 226,772 KB
実行使用メモリ 45,840 KB
最終ジャッジ日時 2024-06-24 08:13:29
合計ジャッジ時間 24,871 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 559 ms
17,524 KB
testcase_14 AC 333 ms
6,944 KB
testcase_15 AC 375 ms
6,944 KB
testcase_16 AC 1,139 ms
26,580 KB
testcase_17 AC 1,080 ms
31,148 KB
testcase_18 AC 170 ms
8,064 KB
testcase_19 AC 374 ms
14,780 KB
testcase_20 AC 552 ms
9,216 KB
testcase_21 AC 1,092 ms
24,636 KB
testcase_22 AC 191 ms
8,192 KB
testcase_23 AC 591 ms
9,728 KB
testcase_24 AC 898 ms
9,856 KB
testcase_25 AC 1,712 ms
45,840 KB
testcase_26 AC 1,534 ms
28,816 KB
testcase_27 AC 77 ms
6,944 KB
testcase_28 AC 481 ms
16,728 KB
testcase_29 AC 1,317 ms
27,556 KB
testcase_30 AC 425 ms
6,940 KB
testcase_31 AC 31 ms
6,940 KB
testcase_32 AC 1,106 ms
31,840 KB
testcase_33 AC 755 ms
24,724 KB
testcase_34 AC 555 ms
7,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
// #pragma GCC target("arch=skylake-avx512")
// #include <atcoder/all>
// using namespace atcoder;
// #define NDEBUG
// #define _GLIBCXX_DEBUG

#pragma region template
// Define
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <class T> using pvector = vector<pair<T, T>>;
template <class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
constexpr const ll dx[4] = {1, 0, -1, 0};
constexpr const ll dy[4] = {0, 1, 0, -1};
constexpr const ll MOD = 1e9 + 7;
constexpr const ll mod = 998244353;
constexpr const ll INF = 1LL << 60;
constexpr const ll inf = 1 << 30;
constexpr const char rt = '\n';
constexpr const char sp = ' ';
#define rt(i, n) (i == (ll) (n) -1 ? rt : sp)
#define len(x) ((ll) (x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define ifn(x) if (not(x))
#define elif else if
#define elifn else ifn
#define fi first
#define se second
#define uniq(x) (sort(all(x)), (x).erase(unique(all(x)), (x).end()))
#define bis(x, y) ((ll) (lower_bound(all(x), y) - (x).begin()))

using graph = vector<vector<ll>>;
template <class T> using wgraph = vector<vector<pair<ll, T>>>;
bool __DIRECTED__ = true;
bool __ZERO_INDEXED__ = false;
istream &operator>>(istream &is, graph &g) {
    ll a, b;
    is >> a >> b;
    if (__ZERO_INDEXED__ == false) a--, b--;
    g[a].pb(b);
    if (__DIRECTED__ == false) g[b].pb(a);
    return is;
}
template <class T> istream &operator>>(istream &is, wgraph<T> &g) {
    ll a, b;
    T c;
    is >> a >> b >> c;
    if (__ZERO_INDEXED__ == false) a--, b--;
    g[a].pb({b, c});
    if (__DIRECTED__ == false) g[b].pb({a, c});
    return is;
}

template <class T> bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

// Debug
#ifdef NDEBUG
#define debug(...)
#define dumpi(a, h, w)
#define vdumpi(a, n)
#define dump(a, h, w)
#define vdump(a, n)
#else
#define debug(...)                                                                                 \
    {                                                                                              \
        cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ";                                         \
        for (auto &&__i : {__VA_ARGS__}) cerr << "[" << __i << "] ";                               \
        cerr << rt;                                                                                \
    }

#define dumpi(a, h, w)                                                                             \
    {                                                                                              \
        cerr << __LINE__ << ": " << #a << " = [" << rt;                                            \
        rep(__i, h) {                                                                              \
            if (__i) cerr << ",\n";                                                                \
            cerr << "[";                                                                           \
            rep(__j, w) {                                                                          \
                if (__j) cerr << ", ";                                                             \
                if (abs(a[__i][__j]) >= INF / 2 and a[__i][__j] <= -INF / 2) cerr << '-';          \
                if (abs(a[__i][__j]) >= INF / 2) cerr << "∞";                                      \
                else                                                                               \
                    cerr << a[__i][__j];                                                           \
            }                                                                                      \
            cerr << "]";                                                                           \
        }                                                                                          \
        cerr << "\n]" << rt;                                                                       \
    }

#define vdumpi(a, n)                                                                               \
    {                                                                                              \
        cerr << __LINE__ << ": " << #a << " = [";                                                  \
        rep(__i, n) {                                                                              \
            if (__i) cerr << ", ";                                                                 \
            if (abs(a[__i]) >= INF / 2 and a[__i] <= -INF / 2) cerr << '-';                        \
            if (abs(a[__i]) >= INF / 2) cerr << "∞";                                               \
            else                                                                                   \
                cerr << a[__i];                                                                    \
        }                                                                                          \
        cerr << "]" << rt;                                                                         \
    }

#define dump(a, h, w)                                                                              \
    {                                                                                              \
        cerr << __LINE__ << ": " << #a << " = [" << rt;                                            \
        rep(__i, h) {                                                                              \
            if (__i) cerr << ",\n";                                                                \
            cerr << "[";                                                                           \
            rep(__j, w) {                                                                          \
                if (__j) cerr << ", ";                                                             \
                cerr << a[__i][__j];                                                               \
            }                                                                                      \
            cerr << "]";                                                                           \
        }                                                                                          \
        cerr << "\n]" << rt;                                                                       \
    }

#define vdump(a, n)                                                                                \
    {                                                                                              \
        cerr << __LINE__ << ": " << #a << " = [";                                                  \
        rep(__i, n) {                                                                              \
            if (__i) cerr << ", ";                                                                 \
            cerr << a[__i];                                                                        \
        }                                                                                          \
        cerr << "]" << rt;                                                                         \
    }
#endif

template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
    is >> p.first >> p.second;
    return is;
}
template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &p) {
    os << p.first << ' ' << p.second;
    return os;
}

// Loop
#define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i)
#define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i)
#define rep(i, n) for (ll i = 0, _##i = (n); i < _##i; ++i)
#define each(i, a) for (auto &&i : a)

// Stream
#define fout(n) cout << fixed << setprecision(n)
struct io {
    io() { cin.tie(nullptr), ios::sync_with_stdio(false); }
} io;

// Speed
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("Ofast,unroll-loops")

// Math
inline constexpr ll gcd(const ll a, const ll b) { return b ? gcd(b, a % b) : a; }
inline constexpr ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; }

inline constexpr ll modulo(const ll n, const ll m = MOD) {
    ll k = n % m;
    return k + m * (k < 0);
}
inline constexpr ll chmod(ll &n, const ll m = MOD) {
    n %= m;
    return n += m * (n < 0);
}
inline constexpr ll mpow(ll a, ll n, const ll m = MOD) {
    ll r = 1;
    rep(i, 64) {
        if (n & (1LL << i)) r *= a;
        chmod(r, m);
        a *= a;
        chmod(a, m);
    }
    return r;
}
inline ll inv(const ll n, const ll m = MOD) {
    ll a = n, b = m, x = 1, y = 0;
    while (b) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        x -= t * y;
        swap(x, y);
    }
    return modulo(x, m);
}
unsigned long long binary_gcd(unsigned long long x, unsigned long long y) {
    if (!x | !y) return x | y;
    unsigned long long cx = __builtin_ctzll(x), cy = __builtin_ctzll(y);
    x >>= cx, y >>= cy;
    while (x ^ y) {
        if (x > y) {
            x = (x - y) >> __builtin_ctzll(x ^ y);
        } else {
            y = (y - x) >> __builtin_ctzll(x ^ y);
        }
    }
    return x << min(cx, cy);
}

inline long long binary_gcd(long long x, long long y) {
    return binary_gcd((unsigned long long) (abs(x)), (unsigned long long) (abs(y)));
}

#define codeforces                                                                                 \
    ll testcases;                                                                                  \
    cin >> testcases;                                                                              \
    rep(testcase, testcases)
#define gcj(s) cout << s << testcase + 1 << ": "

#pragma endregion

/**
 * @brief Succinct Indexable Dictionary(完備辞書)
 */
struct SuccinctIndexableDictionary {
    size_t length;
    size_t blocks;
    vector<unsigned> bit, sum;

    SuccinctIndexableDictionary() = default;

    SuccinctIndexableDictionary(size_t length) : length(length), blocks((length + 31) >> 5) {
        bit.assign(blocks, 0U);
        sum.assign(blocks, 0U);
    }

    void set(int k) { bit[k >> 5] |= 1U << (k & 31); }

    void build() {
        sum[0] = 0U;
        for (int i = 1; i < blocks; i++) {
            sum[i] = sum[i - 1] + __builtin_popcount(bit[i - 1]);
        }
    }

    bool operator[](int k) { return (bool((bit[k >> 5] >> (k & 31)) & 1)); }

    int rank(int k) {
        return (sum[k >> 5] + __builtin_popcount(bit[k >> 5] & ((1U << (k & 31)) - 1)));
    }

    int rank(bool val, int k) { return (val ? rank(k) : k - rank(k)); }
};

template <typename T, int MAXLOG> struct WaveletTree {

    struct Node {
        SuccinctIndexableDictionary sid;
        Node *ch[2];

        Node() = default;

        Node(size_t length) : sid(length + 1), ch{nullptr} {}
    };

    Node *root;

    Node *build(vector<T> &v, vector<T> &rbuff, int bit, int l, int r) {
        if (l >= r || bit == -1) return nullptr;
        Node *node = new Node(r - l);
        int left = 0, right = 0;
        for (int k = l; k < r; k++) {
            if (((v[k] >> bit) & 1)) {
                rbuff[right++] = v[k];
                node->sid.set(k - l);
            } else {
                v[l + left++] = v[k];
            }
        }
        for (int k = 0; k < right; k++) {
            v[l + left + k] = rbuff[k];
        }
        node->sid.build();
        node->ch[0] = build(v, rbuff, bit - 1, l, l + left);
        node->ch[1] = build(v, rbuff, bit - 1, l + left, r);
        return node;
    }

    WaveletTree() = default;

    WaveletTree(vector<T> v) {
        vector<T> rbuff(v.size());
        root = build(v, rbuff, MAXLOG - 1, 0, v.size());
    }

    int rank(Node *t, int l, int r, const T &x, int level) {
        if (l >= r || t == nullptr) return 0;
        if (level == -1) return r - l;
        bool f = (x >> level) & 1;
        l = t->sid.rank(f, l), r = t->sid.rank(f, r);
        return rank(t->ch[f], l, r, x, level - 1);
    }

    int rank(const T &x, int r) { return rank(root, 0, r, x, MAXLOG - 1); }

    T kth_smallest(Node *t, int l, int r, int k, int level) {
        if (l >= r || t == nullptr) return 0;
        int cnt = t->sid.rank(false, r) - t->sid.rank(false, l);
        bool f = cnt <= k;
        l = t->sid.rank(f, l), r = t->sid.rank(f, r);
        if (f) return kth_smallest(t->ch[f], l, r, k - cnt, level - 1) | ((T(1) << level));
        return kth_smallest(t->ch[f], l, r, k, level - 1);
    }

    // k-th(0-indexed) smallest number in v[l,r)
    T kth_smallest(int l, int r, int k) {
        assert(0 <= k && k < r - l);
        return kth_smallest(root, l, r, k, MAXLOG - 1);
    }

    // k-th(0-indexed) largest number in v[l,r)
    T kth_largest(int l, int r, int k) { return kth_smallest(l, r, r - l - k - 1); }

    int range_freq(Node *t, int l, int r, T upper, int level) {
        if (t == nullptr || l >= r) return 0;
        bool f = ((upper >> level) & 1);
        int ret = 0;
        if (f) ret += t->sid.rank(false, r) - t->sid.rank(false, l);
        l = t->sid.rank(f, l), r = t->sid.rank(f, r);
        return range_freq(t->ch[f], l, r, upper, level - 1) + ret;
    }

    // count i s.t. (l <= i < r) && (v[i] < upper)
    int range_freq(int l, int r, T upper) { return range_freq(root, l, r, upper, MAXLOG - 1); }

    // count i s.t. (l <= i < r) && (lower <= v[i] < upper)
    int range_freq(int l, int r, T lower, T upper) {
        return range_freq(l, r, upper) - range_freq(l, r, lower);
    }

    // max v[i] s.t. (l <= i < r) && (v[i] < upper)
    T prev_value(int l, int r, T upper) {
        int cnt = range_freq(l, r, upper);
        return cnt == 0 ? T(-1) : kth_smallest(l, r, cnt - 1);
    }

    // min v[i] s.t. (l <= i < r) && (lower <= v[i])
    T next_value(int l, int r, T lower) {
        int cnt = range_freq(l, r, lower);
        return cnt == r - l ? T(-1) : kth_smallest(l, r, cnt);
    }
};

template <typename T, int MAXLOG> struct CompressedWaveletTree {
    WaveletTree<int, MAXLOG> mat;
    vector<T> ys;

    CompressedWaveletTree(const vector<T> &v) : ys(v) {
        sort(begin(ys), end(ys));
        ys.erase(unique(begin(ys), end(ys)), end(ys));
        vector<int> t(v.size());
        for (int i = 0; i < v.size(); i++) t[i] = get(v[i]);
        mat = WaveletTree<int, MAXLOG>(t);
    }

    inline int get(const T &x) { return lower_bound(begin(ys), end(ys), x) - begin(ys); }

    int rank(const T &x, int r) {
        auto pos = get(x);
        if (pos == ys.size() || ys[pos] != x) return 0;
        return mat.rank(pos, r);
    }

    T kth_smallest(int l, int r, int k) { return ys[mat.kth_smallest(l, r, k)]; }

    T kth_largest(int l, int r, int k) { return ys[mat.kth_largest(l, r, k)]; }

    int range_freq(int l, int r, T upper) { return mat.range_freq(l, r, get(upper)); }

    int range_freq(int l, int r, T lower, T upper) {
        return mat.range_freq(l, r, get(lower), get(upper));
    }

    T prev_value(int l, int r, T upper) {
        auto ret = mat.prev_value(l, r, get(upper));
        return ret == -1 ? T(-1) : ys[ret];
    }

    T next_value(int l, int r, T lower) {
        auto ret = mat.next_value(l, r, get(lower));
        return ret == -1 ? T(-1) : ys[ret];
    }
};

signed main() {
    ll n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    CompressedWaveletTree<ll, 20> cwt(a);
    map<ll, ll> res;
    rep(i, n) {
        ll c1 = cwt.range_freq(0, i, 0, a[i]);
        ll c2 = cwt.range_freq(i + 1, n, 0, a[i]);
        ll c3 = cwt.range_freq(0, i, a[i] + 1, INF);
        ll c4 = cwt.range_freq(i + 1, n, a[i] + 1, INF);
        ll c5 = cwt.range_freq(0, i, a[i], a[i] + 1);
        ll c6 = cwt.range_freq(i + 1, n, a[i], a[i] + 1);
        res[a[i]] += c1 * c2 + c3 * c4 + c5 * c6;
    }
    ll sum = 0;
    rep(i, n) {
        sum -= cwt.range_freq(0, i - 1, a[i], a[i] + 1);
        if (i != 0) sum += cwt.range_freq(i + 1, n, a[i - 1], a[i - 1] + 1);
        res[a[i]] -= sum;
    }

    vector<pair<ll, ll>> v = {{0, 0}};
    each(it, res) v.push_back(it);
    sort(begin(v), end(v));
    rep(i, v.size() - 1) v[i + 1].second += v[i].second;
    ll q;
    cin >> q;
    rep(i, q) {
        ll l, h;
        cin >> l >> h;
        ll hi = upper_bound(begin(v), end(v), make_pair(h, INF)) - begin(v) - 1;
        if (hi == -1) hi = 0;
        ll lo = upper_bound(begin(v), end(v), make_pair(l - 1, INF)) - begin(v) - 1;
        if (lo == -1) lo = 0;
        cout << v[hi].second - v[lo].second << rt;
    }
}
0