結果

問題 No.878 Range High-Element Query
ユーザー ransewhaleransewhale
提出日時 2019-09-07 06:59:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 347 ms / 2,000 ms
コード長 2,312 bytes
コンパイル時間 1,561 ms
コンパイル使用メモリ 170,228 KB
実行使用メモリ 11,412 KB
最終ジャッジ日時 2023-09-08 04:51:16
合計ジャッジ時間 5,667 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,532 KB
testcase_01 AC 5 ms
7,452 KB
testcase_02 AC 5 ms
7,444 KB
testcase_03 AC 4 ms
7,432 KB
testcase_04 AC 5 ms
7,420 KB
testcase_05 AC 6 ms
7,608 KB
testcase_06 AC 5 ms
7,436 KB
testcase_07 AC 4 ms
7,696 KB
testcase_08 AC 6 ms
7,472 KB
testcase_09 AC 6 ms
7,588 KB
testcase_10 AC 5 ms
7,540 KB
testcase_11 AC 303 ms
11,308 KB
testcase_12 AC 224 ms
10,760 KB
testcase_13 AC 246 ms
11,224 KB
testcase_14 AC 186 ms
10,644 KB
testcase_15 AC 221 ms
10,792 KB
testcase_16 AC 317 ms
11,216 KB
testcase_17 AC 347 ms
11,412 KB
testcase_18 AC 339 ms
11,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);

#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a = (a<MOD)?a:(a-MOD);
	b = (b<MOD)?b:(b-MOD);
	a += b;
	a = (a<MOD)?a:(a-MOD);
}

int n,segtree[410410][2],a[100100],p[100100],ans[100100];
bool delayed[410410];
vector<P> v[100100];

void eval(int l, int r, int i){
	if(delayed[i]){
		segtree[i][0] = 0;
		if(r-l>1){
			delayed[i*2+1] = true; 
			delayed[i*2+2] = true;
		}
		delayed[i] = false;
	}
}

void init(int l=0, int r=n, int i=0){
	segtree[i][0] = r-l;
	segtree[i][1] = n;
	if(r-l>1){
		init(l,(l+r)>>1,i*2+1);
		init((l+r)>>1,r,i*2+2);
	}
}

void update(int p, int q, int l=0, int r=n, int i=0){
	int vl,vr;
	eval(l,r,i);
	if(q<=l || r<=p){
		return;
	}else if(p<=l && r<=q){
		delayed[i] = true;
		eval(l,r,i);
	}else if(r-l>1){
		update(p,q,l,(l+r)>>1,i*2+1);
		update(p,q,(l+r)>>1,r,i*2+2);
		segtree[i][0] = segtree[i*2+1][0]+segtree[i*2+2][0];
	}
}

int query(int p, int q, int l=0, int r=n, int i=0){
	int vl,vr;
	eval(l,r,i);
	if(q<=l || r<=p){
		return 0;
	}else if(p<=l && r<=q){
		return segtree[i][0];
	}else{
		vl = query(p,q,l,(l+r)>>1,i*2+1);
		vr = query(p,q,(l+r)>>1,r,i*2+2);
		return (vl+vr);
	}
}

void update2(int p, int v, int l=0, int r=n, int i=0){
	if(!(l<=p && p<r)){
		return;
	}else if(r-l>1){
		update2(p,v,l,(l+r)>>1,i*2+1);
		update2(p,v,(l+r)>>1,r,i*2+2);
		segtree[i][1] = min(segtree[i*2+1][1],segtree[i*2+2][1]);
	}else{
		segtree[i][1] = v;
	}
}

int query2(int p, int q=n, int l=0, int r=n, int i=0){
	int vl,vr;
	if(q<=l || r<=p){
		return n;
	}else if(p<=l && r<=q){
		return segtree[i][1];
	}else{
		vl = query2(p,q,l,(l+r)>>1,i*2+1);
		vr = query2(p,q,(l+r)>>1,r,i*2+2);
		return min(vl,vr);
	}
}

int main(void){
	int q,i,j,t,l,r;
	cin >> n >> q;
	init();
	for(i=0; i<n; ++i){
		cin >> a[i]; --a[i];
	}
	for(i=0; i<q; ++i){
		cin >> t >> l >> r;
		--l;
		v[l].push_back(P(r,i));
	}
	for(i=(n-1); i>=0; --i){
		l = i+1;
		r = query2(a[i]+1);
		update2(a[i],i);
		update(l,r);
		for(j=(v[i].l_ength()-1); j>=0; --j){
			ans[v[i][j].second] = query(i,v[i][j].first);
		}
	}
	for(i=0; i<q; ++i){
		cout << ans[i] << endl;
	}
	return 0;
}
0