結果

問題 No.877 Range ReLU Query
ユーザー gasingasin
提出日時 2019-09-07 16:45:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 827 ms / 2,000 ms
コード長 2,889 bytes
コンパイル時間 865 ms
コンパイル使用メモリ 80,668 KB
実行使用メモリ 182,480 KB
最終ジャッジ日時 2024-04-25 22:11:48
合計ジャッジ時間 9,070 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 3 ms
6,948 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 752 ms
144,588 KB
testcase_12 AC 675 ms
140,880 KB
testcase_13 AC 513 ms
108,496 KB
testcase_14 AC 517 ms
98,124 KB
testcase_15 AC 827 ms
178,000 KB
testcase_16 AC 792 ms
173,396 KB
testcase_17 AC 809 ms
177,236 KB
testcase_18 AC 813 ms
179,020 KB
testcase_19 AC 451 ms
182,480 KB
testcase_20 AC 586 ms
182,156 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:100:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  100 |     scanf("%d%d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~
main.cpp:103:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  103 |         scanf("%lld", &a[i].first);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
main.cpp:112:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  112 |         scanf("%lld%lld%lld%lld", &f, &l, &r, &x);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string.h>
#define rep(i,n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.1415926535897932;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

pl operator+(const pl &l, const pl &r) {
    return pl(l.first+r.first, l.second+r.second);
}

// 区間和、要素更新のsegtree

template<typename T>
struct permanent_segtree {
    struct NODE {
        T val;
        struct NODE *l, *r;
    };

    int n;
    T unit;
    vector<NODE*> roots;

    void init(int n_, T unit_){
        n = 1;
        unit = unit_;
        while(n < n_) n *= 2;
        vector<NODE*> nodes;
        rep(i,2*n-1) {
            NODE* nn = new NODE();
            nn->val = unit;
            nodes.push_back(nn);
        }
        rep(i,2*n-1) {
            if (i < n-1) {
                nodes[i]->l = nodes[2*i+1];
                nodes[i]->r = nodes[2*i+2];
            }
        }
        roots.push_back(nodes[0]);
    }

    void app(int a, T x, NODE* root, int l, int r) {
        int b = a+1;
        if (r <= a || b <= l) return;
        if (a <= l && r <= b) {
            root->val = x;
            return;
        }
        NODE* nl = new NODE(*(root->l));
        NODE* nr = new NODE(*(root->r));
        root->r = nr;
        root->l = nl;
        app(a, x, nl, l, (l+r)/2);
        app(a, x, nr, (l+r)/2, r);
        root->val = nr->val + nl->val;
    }

    void update(int k, T a){
        NODE* nr = new NODE(*(roots.back()));
        roots.push_back(nr);

        app(k, a, nr, 0, n);
    }

    T sub_query(int a, int b, NODE* root, int l, int r){
        if(r <= a || b <= l) return unit;
        if(a <= l && r <= b) return root->val;

        T vl = sub_query(a,b,root->l,l,(l+r)/2);
        T vr = sub_query(a,b,root->r,(l+r)/2,r);
        return vl + vr;
    }

    T query (int a, int b, int t) {
        return sub_query(a, b, roots[t], 0, n);
    }
};

permanent_segtree<pl> seg;
int n, q;
pl a[100000];

int main() {
    scanf("%d%d", &n, &q);
    seg.init(n, pl(0,0));
    rep(i,n) {
        scanf("%lld", &a[i].first);
        a[i].second = i;
    }
    sort(a, a+n, greater<pl>());
    rep(i,n) {
        seg.update(a[i].second, pl(a[i].first, 1));
    }
    rep(i,q) {
        ll f, l, r, x;
        scanf("%lld%lld%lld%lld", &f, &l, &r, &x);
        l--; r--;
        int st = -1, en = n; 
        while (en-st > 1) {
            int mid = (st + en) / 2;
            if (a[mid].first < x) en = mid;
            else st = mid;
        }
        pl val = seg.query(l, r+1, st+1);
        printf("%lld\n", val.first-val.second*x);
    }
}
0