結果

問題 No.789 範囲の合計
ユーザー PachicobuePachicobue
提出日時 2019-02-08 22:17:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 445 ms / 1,000 ms
コード長 3,157 bytes
コンパイル時間 2,349 ms
コンパイル使用メモリ 216,012 KB
実行使用メモリ 28,000 KB
最終ジャッジ日時 2023-09-14 03:45:52
合計ジャッジ時間 6,278 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 362 ms
24,464 KB
testcase_03 AC 70 ms
4,404 KB
testcase_04 AC 339 ms
22,984 KB
testcase_05 AC 246 ms
24,908 KB
testcase_06 AC 270 ms
24,464 KB
testcase_07 AC 62 ms
4,376 KB
testcase_08 AC 163 ms
11,368 KB
testcase_09 AC 149 ms
10,540 KB
testcase_10 AC 445 ms
28,000 KB
testcase_11 AC 298 ms
23,308 KB
testcase_12 AC 295 ms
23,412 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:79:21: 警告: narrowing conversion of ‘t’ from ‘int’ to ‘bool’ [-Wnarrowing]
   79 |             Q[i] = {t, {x, y}};
      |                     ^
main.cpp:84:21: 警告: narrowing conversion of ‘t’ from ‘int’ to ‘bool’ [-Wnarrowing]
   84 |             Q[i] = {t, {l, r}};
      |                     ^

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
template <typename T>
constexpr T MOD() { return 1000000007; }
template <typename T>
constexpr T INF() { return std::numeric_limits<T>::max() / 16; }
class Fenwick
{
    static std::size_t SZ(const std::size_t n)
    {
        std::size_t ans = 1;
        for (; ans < n; ans <<= 1) {}
        return ans;
    }

public:
    using T = ll;
    Fenwick(const std::size_t n) : size(n), cap(SZ(n)), value(cap + 1, 0) {}
    template <typename InIt>
    Fenwick(const InIt first, const InIt last) : size(std::distance(first, last)), cap(SZ(size)), value(cap + 1, 0)
    {
        std::copy(first, last, value.begin() + 1);
        for (std::size_t x = 1; x < cap; x++) { value[x + (x & -x)] += value[x]; }
    }
    void add(const std::size_t a, const T& val)
    {
        for (std::size_t ind = a + 1; ind <= cap; ind += ind & (-ind)) { value[ind] += val; }
    }
    T accumulate(const std::size_t a) const
    {
        T sum = 0;
        for (std::size_t ind = a + 1; ind != 0; ind &= ind - 1) { sum += value[ind]; }
        return sum;
    }
    T accumulate(const std::size_t l, const std::size_t r) const { return accumulate(r - 1) - (l == 0 ? 0 : accumulate(l - 1)); }
    std::size_t partitionPoint(const T val) const
    {
        if (val < 0) { return 0; }
        std::size_t x = 0;
        T offset = 0;
        for (std::size_t k = ((cap == size) ? cap : cap / 2); k != 0; k >>= 1) {
            if (x + k <= cap and value[x + k] + offset <= val) { offset += value[x + k], x += k; }
        }
        return std::min(x, size);
    }
    std::vector<T> data() const
    {
        std::vector<T> ans(size);
        for (std::size_t i = 0; i < size; i++) { ans[i] = accumulate(i, i + 1); }
        return ans;
    }

private:
    const std::size_t size, cap;
    std::vector<T> value;
};
std::ostream& operator<<(std::ostream& os, const Fenwick& bit)
{
    const auto v = bit.data();
    os << "[";
    for (const auto e : v) { os << e << ","; }
    return (os << "]\n");
}
int main()
{
    int N;
    std::cin >> N;
    using P = std::pair<int, int>;
    using PP = std::pair<bool, P>;
    std::vector<PP> Q(N);
    std::set<int> st;
    for (int i = 0; i < N; i++) {
        int t;
        std::cin >> t;
        if (t == 0) {
            int x, y;
            std::cin >> x >> y, x--;
            Q[i] = {t, {x, y}};
            st.insert(x);
        } else {
            int l, r;
            std::cin >> l >> r, l--;
            Q[i] = {t, {l, r}};
            st.insert(l), st.insert(r - 1), st.insert(r);
        }
    }
    std::map<int, int> mp;
    int ind = 0;
    for (const int p : st) { mp[p] = ind, ind++; }
    const int L = st.size();
    Fenwick bit(L);
    ll ans = 0;
    for (int i = 0; i < N; i++) {
        const int t = Q[i].first;
        if (t == 0) {
            const int x = mp[Q[i].second.first], y = Q[i].second.second;
            bit.add(x, y);
        } else {
            const int l = mp[Q[i].second.first], r = mp[Q[i].second.second];
            ans += bit.accumulate(l, r);
        }
    }
    std::cout << ans << std::endl;
    return 0;
}
0