結果

問題 No.877 Range ReLU Query
ユーザー ゆきのんゆきのん
提出日時 2020-03-12 04:39:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 2,099 bytes
コンパイル時間 2,331 ms
コンパイル使用メモリ 180,800 KB
実行使用メモリ 10,300 KB
最終ジャッジ日時 2024-04-25 22:20:13
合計ジャッジ時間 6,645 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 319 ms
9,456 KB
testcase_12 AC 252 ms
8,960 KB
testcase_13 AC 200 ms
7,936 KB
testcase_14 AC 220 ms
7,936 KB
testcase_15 AC 291 ms
10,112 KB
testcase_16 AC 277 ms
9,728 KB
testcase_17 AC 293 ms
9,984 KB
testcase_18 AC 281 ms
9,868 KB
testcase_19 AC 289 ms
10,300 KB
testcase_20 AC 284 ms
10,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=1000000007;
const LL LINF=1LL<<60;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

template< typename T >
struct BinaryIndexedTree{

    //0-indexed
    vector< T > bit;

    BinaryIndexedTree(int sz){
        bit.assign(sz, 0);
    }

    //[0,k)までの総和を返す
    T sum(int k){
        T ret = 0;
        for (--k; k >= 0; k = (k&(k+1))-1) ret += bit[k];
        return ret;
    }
    
    //[l,r)の総和を返す
    T sum(int l, int r){
        return sum(r) - sum(l);
    }

    //kにx加算する
    void add(int k, T x){
        for (; k < bit.size(); k |= k+1) bit[k] += x;
    }
};

int main(){
    int n,q;cin >> n >> q;
    vector<P> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i].fs;
        a[i].sc = i;
    }
    BinaryIndexedTree<LL> S(n);
    for (int i = 0; i < n; i++) {
        S.add(i,a[i].fs);
    }
    sort(ALL(a));
    vector<pair<P,P>> Q(q);
    for (int i = 0; i < q; i++) {
        int t,l,r,x;cin >> t >> l >> r >> x;
        l--;
        Q[i] = mp(mp(x,i), mp(l,r));
    }
    sort(ALL(Q));
    BinaryIndexedTree<LL> bit(n);
    int idx = 0;
    vector<LL> ans(q);
    for (int i = 0; i < q; i++) {
        LL x = Q[i].fs.fs, id = Q[i].fs.sc;
        int l = Q[i].sc.fs, r = Q[i].sc.sc;
        while(idx < n && a[idx].fs <= x){
            bit.add(a[idx].sc, 1);
            S.add(a[idx].sc, -a[idx].fs);
            idx++;
        }
        ans[id] = S.sum(l,r) - x * (r - l - bit.sum(l,r));
    }
    for (int i = 0; i < q; i++) {
        cout << ans[i] << endl;
    }
    return 0;
}
0