結果
問題 |
No.789 範囲の合計
|
ユーザー |
|
提出日時 | 2019-11-04 09:27:34 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 213 ms / 1,000 ms |
コード長 | 1,468 bytes |
コンパイル時間 | 1,162 ms |
コンパイル使用メモリ | 78,560 KB |
最終ジャッジ日時 | 2025-01-08 02:19:19 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 15 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:38:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 38 | size_t Q; scanf("%lu", &Q); | ~~~~~^~~~~~~~~~~ main.cpp:42:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 42 | int a, b, c; scanf("%d%d%d", &a, &b, &c); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <algorithm> #include <cstdint> #include <cstdio> #include <map> #include <tuple> #include <vector> using namespace std; using i64 = int64_t; template <class T> map<T, size_t> compress(const vector<T> &v) { vector<T> mp = v; sort(mp.begin(), mp.end()); mp.erase(unique(mp.begin(), mp.end()), mp.end()); map<T, size_t> w; for(size_t i=0, m=mp.size(); i<m; ++i) { w[mp[i]] = i; } return w; } template <class T> class BIT { size_t N; vector<T> dat; public: explicit BIT(size_t n) : N(n+2), dat(N) {} void add(size_t k, T x) { for(size_t i=k+1; i<N; i+=i&-i) { dat[i] += x; } } T sum(size_t k) { T res = 0; for(size_t i=k; i; i-=i&-i) { res += dat[i]; } return res; } }; int main(void) { size_t Q; scanf("%lu", &Q); vector<tuple<int, int, int>> logs; vector<int> v; for(size_t q=0; q<Q; ++q) { int a, b, c; scanf("%d%d%d", &a, &b, &c); switch(a) { case 0: logs.emplace_back(a, b, c); v.push_back(b); break; case 1: logs.emplace_back(a, b, c+1); v.push_back(b); v.push_back(c+1); break; } } map<int, size_t> w = compress(v); BIT<i64> tree(w.size()); i64 res = 0; for(size_t i=0; i<Q; ++i) { auto[a, b, c] = logs[i]; switch(a) { case 0: tree.add(w[b], c); break; case 1: res += tree.sum(w[c]) - tree.sum(w[b]); break; } } printf("%ld\n", res); return 0; }