結果
問題 |
No.3078 Difference Sum Query
|
ユーザー |
![]() |
提出日時 | 2025-04-14 16:52:23 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 170 ms / 2,000 ms |
コード長 | 1,998 bytes |
コンパイル時間 | 868 ms |
コンパイル使用メモリ | 63,480 KB |
実行使用メモリ | 17,128 KB |
最終ジャッジ日時 | 2025-04-14 16:52:32 |
合計ジャッジ時間 | 8,457 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 26 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:68:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 68 | scanf("%d%d", &n, &qn); | ~~~~~^~~~~~~~~~~~~~~~~ main.cpp:69:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 69 | for (int i = 0; i < n; i++) scanf("%lld", as + i); | ~~~~~^~~~~~~~~~~~~~~~ main.cpp:71:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 71 | scanf("%d%d%lld", ls + i, rs + i, xs + i), ls[i]--, rs[i]--; | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*- * * 3078.cc: No.3078 Difference Sum Query - yukicoder */ #include<cstdio> #include<vector> #include<algorithm> using namespace std; /* constant */ const int MAX_N = 100000; const int MAX_QN = 100000; /* typedef */ using ll = long long; using vi = vector<int>; template <typename T> struct BIT { int n; vector<T> bits; BIT() {} BIT(int _n) { init(_n); } void init(int _n) { n = _n; bits.assign(n + 1, 0); } T sum(int x) { x = min(x, n); T s = 0; while (x > 0) { s += bits[x]; x -= (x & -x); } return s; } void add(int x, T v) { if (x <= 0) return; while (x <= n) { bits[x] += v; x += (x & -x); } } }; /* global variables */ ll as[MAX_N], bs[MAX_N], xs[MAX_QN], res[MAX_QN]; int ls[MAX_QN], rs[MAX_QN], xis[MAX_QN]; vi lvs[MAX_QN], rvs[MAX_QN]; BIT<int> bit0; BIT<ll> bit1; /* subroutines */ /* main */ int main() { int n, qn; scanf("%d%d", &n, &qn); for (int i = 0; i < n; i++) scanf("%lld", as + i); for (int i = 0; i < qn; i++) scanf("%d%d%lld", ls + i, rs + i, xs + i), ls[i]--, rs[i]--; copy(as, as + n, bs); sort(bs, bs + n); for (int i = 0; i < qn; i++) { xis[i] = lower_bound(bs, bs + n, xs[i]) - bs; lvs[ls[i]].push_back(i); rvs[rs[i]].push_back(i); } bit0.init(n); bit1.init(n); for (int i = 0; i < n; i++) { for (auto j: lvs[i]) { int lc = bit0.sum(xis[j]), rc = bit0.sum(n) - lc; ll lsum = bit1.sum(xis[j]), rsum = bit1.sum(n) - lsum; res[j] = (xs[j] * lc - lsum) + (rsum - xs[j] * rc); } int ai = lower_bound(bs, bs + n, as[i]) - bs; bit0.add(ai + 1, 1); bit1.add(ai + 1, as[i]); for (auto j: rvs[i]) { int lc = bit0.sum(xis[j]), rc = bit0.sum(n) - lc; ll lsum = bit1.sum(xis[j]), rsum = bit1.sum(n) - lsum; res[j] = (xs[j] * lc - lsum) + (rsum - xs[j] * rc) - res[j]; } } for (int i = 0; i < qn; i++) printf("%lld\n", res[i]); return 0; }