結果
| 問題 |
No.789 範囲の合計
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-02-08 22:17:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 324 ms / 1,000 ms |
| コード長 | 3,157 bytes |
| コンパイル時間 | 1,950 ms |
| コンパイル使用メモリ | 210,900 KB |
| 最終ジャッジ日時 | 2025-01-06 20:59:38 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:79:21: warning: narrowing conversion of ‘t’ from ‘int’ to ‘bool’ [-Wnarrowing]
79 | Q[i] = {t, {x, y}};
| ^
main.cpp:84:21: warning: narrowing conversion of ‘t’ from ‘int’ to ‘bool’ [-Wnarrowing]
84 | Q[i] = {t, {l, r}};
| ^
ソースコード
#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;
}