結果

問題 No.3078 Difference Sum Query
ユーザー 👑 hos.lyric
提出日時 2025-03-28 23:11:40
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 654 ms / 2,000 ms
コード長 2,444 bytes
コンパイル時間 3,140 ms
コンパイル使用メモリ 239,056 KB
実行使用メモリ 7,324 KB
最終ジャッジ日時 2025-03-28 23:11:59
合計ジャッジ時間 16,473 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:64:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   64 |       scanf("%lld", &A[i]);
      |       ~~~~~^~~~~~~~~~~~~~~
main.cpp:79:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   79 |       scanf("%d%d%lld", &L, &R, &X);
      |       ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#pragma GCC target("avx2")

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


int N, Q;
Int A[100'010];

vector<Int> as;
vector<Int> bs;

int indexOf(Int a) {
  return lower_bound(as.begin(), as.end(), a) - as.begin();
}
Int total(Int x) {
  const int pos = indexOf(x);
  Int ret = 0;
  ret += (pos * x - bs[pos]);
  ret += ((bs[N] - bs[pos]) - (N - pos) * x);
  return ret;
}

int main() {
  for (; ~scanf("%d%d", &N, &Q); ) {
    for (int i = 0; i < N; ++i) {
      scanf("%lld", &A[i]);
    }
    
    as = vector<Int>(A, A + N);
    sort(as.begin(), as.end());
    bs.assign(N + 1, 0);
    for (int i = 0; i < N; ++i) {
      const int pos = indexOf(A[i]);
      bs[pos + 1] += A[i];
    }
    for (int j = 0; j < N; ++j) bs[j + 1] += bs[j];
    
    for (; Q--; ) {
      int L, R;
      Int X;
      scanf("%d%d%lld", &L, &R, &X);
      --L;
      
      Int ans = 0;
      if (2 * (R-L) <= N) {
        for (int i = L; i < R; ++i) ans += abs(A[i] - X);
      } else {
        ans += total(X);
        for (int i = 0; i < L; ++i) ans -= abs(A[i] - X);
        for (int i = R; i < N; ++i) ans -= abs(A[i] - X);
      }
      printf("%lld\n", ans);
    }
  }
  return 0;
}
0