結果
問題 |
No.789 範囲の合計
|
ユーザー |
|
提出日時 | 2019-11-03 17:19:17 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 298 ms / 1,000 ms |
コード長 | 1,546 bytes |
コンパイル時間 | 1,052 ms |
コンパイル使用メモリ | 78,552 KB |
最終ジャッジ日時 | 2025-01-08 02:19:11 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 15 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:39:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 39 | size_t Q; scanf("%lu", &Q); | ~~~~~^~~~~~~~~~~ main.cpp:43:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 43 | 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, int> compress(const vector<T> &v) { size_t n = v.size(); vector<T> mp = v; sort(mp.begin(), mp.end()); mp.erase(unique(mp.begin(), mp.end()), mp.end()); map<T, int> w; for(size_t i=0; i<n; ++i) { w[v[i]] = static_cast<int>(distance(mp.begin(), lower_bound(mp.begin(), mp.end(), v[i]))); } return w; } template <class T> class BIT { int N; vector<T> dat; public: explicit BIT(int n) : N(n+2), dat(N) {} void add(int k, T x) { for(int i=k+1; i<N; i+=i&-i) { dat[i] += x; } } T sum(int k) { T res = 0; for(int 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, int> w = compress(v); BIT<i64> tree(static_cast<int>(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; }