結果

問題 No.1441 MErGe
ユーザー SSRSSSRS
提出日時 2021-03-26 21:50:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 929 ms / 1,000 ms
コード長 3,107 bytes
コンパイル時間 1,784 ms
コンパイル使用メモリ 177,156 KB
実行使用メモリ 23,280 KB
最終ジャッジ日時 2023-08-19 08:03:25
合計ジャッジ時間 18,770 ms
ジャッジサーバーID
(参考情報)
judge10 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 6 ms
4,376 KB
testcase_04 AC 7 ms
4,380 KB
testcase_05 AC 19 ms
4,376 KB
testcase_06 AC 19 ms
4,380 KB
testcase_07 AC 20 ms
4,380 KB
testcase_08 AC 197 ms
7,748 KB
testcase_09 AC 196 ms
7,804 KB
testcase_10 AC 329 ms
7,580 KB
testcase_11 AC 306 ms
6,036 KB
testcase_12 AC 330 ms
7,876 KB
testcase_13 AC 753 ms
22,760 KB
testcase_14 AC 771 ms
22,708 KB
testcase_15 AC 802 ms
22,176 KB
testcase_16 AC 751 ms
22,692 KB
testcase_17 AC 723 ms
22,444 KB
testcase_18 AC 594 ms
20,120 KB
testcase_19 AC 676 ms
21,376 KB
testcase_20 AC 539 ms
19,592 KB
testcase_21 AC 482 ms
13,916 KB
testcase_22 AC 699 ms
13,236 KB
testcase_23 AC 915 ms
23,220 KB
testcase_24 AC 911 ms
23,236 KB
testcase_25 AC 919 ms
23,116 KB
testcase_26 AC 929 ms
23,280 KB
testcase_27 AC 922 ms
23,112 KB
testcase_28 AC 600 ms
23,232 KB
testcase_29 AC 607 ms
23,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000;
template <typename T>
struct lazy_segment_tree{
	int N;
	vector<T> ST;
	vector<T> lazy;
	lazy_segment_tree(vector<T> &A){
		int n = A.size();
		N = 1;
		while (N < n){
			N *= 2;
		}
		ST = vector<T>(N * 2 - 1, 0);
		lazy = vector<T>(N * 2 - 1, INF);
		for (int i = 0; i < n; i++){
			ST[N - 1 + i] = A[i];
		}
		for (int i = N - 2; i >= 0; i--){
			ST[i] = ST[i * 2 + 1] + ST[i * 2 + 2];
		}
	}
	void eval(int i, int l, int r){
		if (lazy[i] != INF){
			if (i < N - 1){
				lazy[i * 2 + 1] = lazy[i];
				lazy[i * 2 + 2] = lazy[i];
			}
			ST[i] = lazy[i] * (r - l);
			lazy[i] = INF;
		}
	}
	void range_update(int L, int R, T x, int i, int l, int r){
		eval(i, l, r);
		if (R <= l || r <= L){
			return;
		} else if (L <= l && r <= R){
			lazy[i] = x;
			eval(i, l, r);
		} else {
			int m = (l + r) / 2;
			range_update(L, R, x, i * 2 + 1, l, m);
			range_update(L, R, x, i * 2 + 2, m, r);
			ST[i] = ST[i * 2 + 1] + ST[i * 2 + 2];
		}
	}
	void range_update(int L, int R, T x){
		range_update(L, R, x, 0, 0, N);
	}
	T range_sum(int L, int R, int i, int l, int r){
		eval(i, l, r);
		if (R <= l || r <= L){
			return 0;
		} else if (L <= l && r <= R){
			return ST[i];
		} else {
			int m = (l + r) / 2;
			return range_sum(L, R, i * 2 + 1, l, m) + range_sum(L, R, i * 2 + 2, m, r);
		}
	}
	T range_sum(int L, int R){
		return range_sum(L, R, 0, 0, N);
	}
};
template <typename T>
struct binary_indexed_tree{
	int N;
	vector<T> BIT;
	binary_indexed_tree(int n){
		N = 1;
		while (N < n){
			N *= 2;
		}
		BIT = vector<T>(N + 1, 0);
	}
	void add(int i, T x){
		i++;
		while (i <= N){
			BIT[i] += x;
			i += i & -i;
		}
	}
	T sum(int i){
		T ans = 0;
		while (i > 0){
			ans += BIT[i];
			i -= i & -i;
		}
		return ans;
	}
};
int main(){
  int N, Q;
  cin >> N >> Q;
  vector<long long> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  lazy_segment_tree<long long> ST(A);
  binary_indexed_tree<int> BIT(N);
  for (int i = 0; i < N; i++){
    BIT.add(i, 1);
  }
  set<int> st;
  for (int i = 0; i < N; i++){
    st.insert(i);
  }
  for (int i = 0; i < Q; i++){
    int T, l, r;
    cin >> T >> l >> r;
    l--;
    r--;
    int l2 = l, r2 = r;
    int tv1 = N, fv1 = -1;
    while (tv1 - fv1 > 1){
      int mid = (tv1 + fv1) / 2;
      if (BIT.sum(mid + 1) > l){
        tv1 = mid;
      } else {
        fv1 = mid;
      }
    }
    l = tv1;
    int tv2 = N, fv2 = l - 1;
    while (tv2 - fv2 > 1){
      int mid = (tv2 + fv2) / 2;
      if (BIT.sum(mid + 1) > r){
        tv2 = mid;
      } else {
        fv2 = mid;
      }
    }
    r = tv2;
    if (T == 1){
      long long S = ST.range_sum(l, r + 1);
      ST.range_update(l, l + 1, S);
      ST.range_update(l + 1, r + 1, 0);
      while (true){
        auto itr = st.upper_bound(l);
        if (itr == st.end()){
          break;
        }
        if (*itr > r){
          break;
        }
        BIT.add(*itr, -1);
        st.erase(itr);
      }
    }
    if (T == 2){
      long long S = ST.range_sum(l, r + 1);
      cout << S << endl;
    }
  }
}
0