結果

問題 No.1441 MErGe
ユーザー NOSSNOSS
提出日時 2021-03-26 22:46:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 529 ms / 1,000 ms
コード長 2,570 bytes
コンパイル時間 2,205 ms
コンパイル使用メモリ 207,336 KB
実行使用メモリ 15,508 KB
最終ジャッジ日時 2023-08-19 09:25:51
合計ジャッジ時間 13,197 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 14 ms
4,376 KB
testcase_06 AC 13 ms
4,376 KB
testcase_07 AC 13 ms
4,376 KB
testcase_08 AC 129 ms
5,848 KB
testcase_09 AC 120 ms
5,980 KB
testcase_10 AC 197 ms
5,736 KB
testcase_11 AC 191 ms
5,056 KB
testcase_12 AC 203 ms
5,844 KB
testcase_13 AC 422 ms
14,976 KB
testcase_14 AC 423 ms
14,972 KB
testcase_15 AC 436 ms
14,452 KB
testcase_16 AC 409 ms
15,008 KB
testcase_17 AC 390 ms
14,648 KB
testcase_18 AC 376 ms
12,044 KB
testcase_19 AC 416 ms
13,652 KB
testcase_20 AC 340 ms
11,440 KB
testcase_21 AC 317 ms
10,124 KB
testcase_22 AC 449 ms
9,788 KB
testcase_23 AC 529 ms
15,504 KB
testcase_24 AC 517 ms
15,396 KB
testcase_25 AC 517 ms
15,508 KB
testcase_26 AC 516 ms
15,444 KB
testcase_27 AC 508 ms
15,404 KB
testcase_28 AC 252 ms
15,396 KB
testcase_29 AC 246 ms
15,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using P = pair<int, int>;
using P3 = pair<int,P>;
using PP = pair<P, P>;
constexpr int INF32 = 1 << 30;
constexpr ll INF64 = 1LL << 62;
// constexpr ll MOD = 1000000007;
constexpr ll MOD = 998244353;
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr double EPS = 1e-10;
const double PI = acos(-1);

#define ALL(v) (v).begin(),(v).end()
#define REP(i,n) for(int i=0,i_len=n; i<i_len; ++i)

template<typename T1,typename T2> bool chmax(T1 &a, T2 b) { if (a<b) { a=b; return 1; } return 0; }
template<typename T1,typename T2> bool chmin(T1 &a, T2 b) { if (b<a) { a=b; return 1; } return 0; }

template <typename T>
struct BIT {
    int N;
    std::vector<T> bit;

    BIT() = default;
    BIT(int n) {init(n);}
    void init(int n){
        N = n;
        bit.assign(n+1, 0);
    }
    T getSum(int i) {  // i番目までの要素の和を求める(1-index)
        T sum = 0;
        while (i > 0) {
            sum += bit[i];
            i -= i & -i;
        }
        return sum;
    }
    void add(int i, T x) {  // i番目の要素にxを加算
        while (i <= N) {
            bit[i] += x;
            i += i & -i;
        }
    }
};

int n, q;
BIT<ll> bit, cnt;

int getIndex(int x){
    int ok = 0, ng = n+1;
    while(abs(ok-ng)>1){
        int mid = (ok+ng)/2;
        if(cnt.getSum(mid)<=x){
            ok = mid;
        }else{
            ng = mid;
        }
    }
    return ok;
}

int solve(){
    cin >> n >> q;
    bit.init(n);
    cnt.init(n);
    REP(i,n){
        ll a;
        cin >> a;
        bit.add(i+1, a);
        cnt.add(i+1, 1);
    }
    set<int> st;
    REP(i,n) st.insert(i);
    REP(i,q){
        int t, l, r;
        cin >> t >> l >> r;
        l = getIndex(l-1);
        r = getIndex(r);
        // cout << l << " " << r << endl;
        if(t==1){
            while(1){
                auto itr = st.lower_bound(l+1);
                if(itr == st.end() || *itr >= r) break;
                cnt.add(*itr+1,-1);
                // cout << *itr << endl;
                st.erase(itr);
            }
        }else{
            cout << bit.getSum(r)-bit.getSum(l) << endl;
        }
    }
    return 0;
}

int main(){
    cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);
    // int t; cin >> t; for(int i=0;i<t;i++) solve();
    // while(!solve());
    solve();
    return 0;
}
0