結果

問題 No.877 Range ReLU Query
ユーザー minatominato
提出日時 2020-05-26 20:06:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,470 bytes
コンパイル時間 5,015 ms
コンパイル使用メモリ 250,484 KB
最終ジャッジ日時 2025-01-10 15:45:50
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 9 WA * 6 TLE * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long; 
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////

const int B = 700;

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N,Q; cin >> N >> Q;
    vector<int> A(N);
    rep(i,N) cin >> A[i];
    int sz = (N-1)/B+1;
    vector<map<int, int>> bucket(sz);
    vector<vector<ll>> S(sz);
    rep(i,N) {
        bucket[i/B][A[i]]++;
        S[i/B].emplace_back(A[i]);
    }

    rep(i,sz) {
        int cur = 0;
        for (auto &j : bucket[i]) {
            j.second += cur;
            cur = j.second;
        }
        int M = S[i].size();
        S[i].emplace_back(0);
        sort(all(S[i]));
        rep(j,M) S[i][j+1] += S[i][j];
    }

    while (Q--) {
        int t,l,r,x; cin >> t >> l >> r >> x;
        --l; --r;
        ll ans = 0;
        if (l/B==r/B) {
            for (int i = l; i <= r; i++) ans += max(A[i]-x,0);
        } else {
            for (int i = l; i/B == l/B; i++) ans += max(A[i]-x,0);
            for (int i = l/B+1; i < r/B; i++) {
                auto it = bucket[i].upper_bound(x);
                if (it==bucket[i].begin()) {
                    ans += S[i][B] - ll(x)*B;
                } else {
                    --it;
                    ans += S[i][B] - S[i][it->second] - ll(x)*(B - it->second);
                }
            }
            for (int i = r/B*B; i/B == r/B and i < N; i++) ans += max(A[i]-x,0);
        }
        cout << ans << ln;
    }
}
0