結果

問題 No.878 Range High-Element Query
ユーザー sigma425sigma425
提出日時 2019-09-09 20:21:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 260 ms / 2,000 ms
コード長 1,999 bytes
コンパイル時間 2,056 ms
コンパイル使用メモリ 157,280 KB
実行使用メモリ 9,868 KB
最終ジャッジ日時 2023-09-10 23:47:19
合計ジャッジ時間 6,300 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 258 ms
9,400 KB
testcase_12 AC 165 ms
8,920 KB
testcase_13 AC 206 ms
7,492 KB
testcase_14 AC 154 ms
6,636 KB
testcase_15 AC 173 ms
8,776 KB
testcase_16 AC 246 ms
9,668 KB
testcase_17 AC 260 ms
9,868 KB
testcase_18 AC 258 ms
9,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
using ll = long long;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif
struct segtree{
	using D = long long;

	int N;
	vector<D> val;

	segtree(){}
	segtree(int n){
		N=1;
		while(N<n) N*=2;
		val.assign(N*2,0);
	}
	segtree(const vector<D>& ds){
		int n = ds.size();
		N=1;
		while(N<n) N*=2;
		val.assign(N*2,0);
		rep(i,n) val[i+N] = ds[i];
		for(int i=N-1;i>0;i--) val[i] = val[i*2] + val[i*2+1];
	}
	void assign(int k,D d){
		k+=N;
		val[k]=d;
		k/=2;
		while(k){
			val[k] = val[k*2] + val[k*2+1];
			k/=2;
		}
	}
	D sum(int a,int b){		//non-commutative & unrecursive
		D res = 0;
		a+=N,b+=N;
		while(a<b){
			if(a&1) res += val[a++];
			if(b&1) res += val[--b];
			a/=2,b/=2;
		}
		return res;
	}
};
int main(){
	int N,Q;
	cin >> N >> Q;
	V<int> a(N);
	rep(i,N) cin >> a[i], a[i]--;
	using P = pair<int,int>;
	VV<P> l2qr(N);
	rep(i,Q){
		int t,l,r;
		cin >> t >> l >> r;
		l--;
		l2qr[l].pb(P(i,r));
	}

	segtree seg(N);
	stack<int> st;
	V<int> ans(Q);

	for(int l=N-1;l>=0;l--){
		while(!st.empty() && a[st.top()] < a[l]){
			seg.assign(st.top(),0);
			st.pop();
		}
		st.push(l);
		seg.assign(l,1);

		for(P qr: l2qr[l]){
			int q = qr.fs, r = qr.sc;
			ans[q] = seg.sum(l,r);
		}
	}
	rep(i,Q) cout << ans[i] << endl;
}
0