結果

問題 No.878 Range High-Element Query
ユーザー 👑 kmjpkmjp
提出日時 2019-09-06 22:07:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 422 ms / 2,000 ms
コード長 2,136 bytes
コンパイル時間 1,995 ms
コンパイル使用メモリ 184,552 KB
実行使用メモリ 33,844 KB
最終ジャッジ日時 2023-09-07 00:03:10
合計ジャッジ時間 6,209 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
15,372 KB
testcase_01 AC 9 ms
15,708 KB
testcase_02 AC 9 ms
15,620 KB
testcase_03 AC 8 ms
15,652 KB
testcase_04 AC 8 ms
15,108 KB
testcase_05 AC 9 ms
15,352 KB
testcase_06 AC 8 ms
15,380 KB
testcase_07 AC 8 ms
15,104 KB
testcase_08 AC 10 ms
15,240 KB
testcase_09 AC 9 ms
15,204 KB
testcase_10 AC 9 ms
15,112 KB
testcase_11 AC 393 ms
30,144 KB
testcase_12 AC 291 ms
31,724 KB
testcase_13 AC 313 ms
27,080 KB
testcase_14 AC 227 ms
24,900 KB
testcase_15 AC 284 ms
30,548 KB
testcase_16 AC 388 ms
33,152 KB
testcase_17 AC 422 ms
33,844 KB
testcase_18 AC 416 ms
33,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

template<class V,int NV> class SegTree_1 {
public:
	vector<vector<V>> val;
	static V const def=0;
	V comp(V l,V r){ return max(l,r);};
	
	SegTree_1(){val.resize(NV*2);};
	V getval(int x,int y,int v,int l=0,int r=NV,int k=1) { // x<=i<y
		if(r<=x || y<=l) return 0;
		if(x<=l && r<=y) {
			return lower_bound(ALL(val[k]),v+1)-val[k].begin();
		}
		return getval(x,y,v,l,(l+r)/2,k*2)+getval(x,y,v,(l+r)/2,r,k*2+1);
	}
	void set(int entry,V v) {
		val[entry+NV].clear();
		val[entry+NV].push_back(v);
	}
	void build() {
		for(int i=NV-1;i>=1;i--) {
			val[i].clear();
			int a=0,b=0;
			int x=i*2,y=i*2+1;
			while(a<val[x].size() || b<val[y].size()) {
				if(a==val[x].size()) {
					val[i].push_back(val[y][b++]);
				}
				else if(b==val[y].size()) {
					val[i].push_back(val[x][a++]);
				}
				else if(val[x][a]<val[y][b]) {
					val[i].push_back(val[x][a++]);
				}
				else {
					val[i].push_back(val[y][b++]);
				}
			}
		}
	}
};
SegTree_1<int,1<<18> st;

int N,Q;
int A[202020];
int lef[202020];
void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>Q;
	vector<pair<int,int>> V;
	FOR(i,N) {
		cin>>A[i+1];
		V.push_back({A[i+1],i+1});
	}
	set<int> did;
	sort(ALL(V));
	reverse(ALL(V));
	FORR(v,V) {
		auto it=did.lower_bound(v.second);
		if(it!=did.begin()) {
			it--;
			lef[v.second]=*it;
		}
		st.set(v.second,lef[v.second]);
		did.insert(v.second);
	}
	st.build();
	FOR(i,Q) {
		int T,L,R;
		cin>>T>>L>>R;
		cout<<st.getval(L,R+1,L-1)<<endl;
	}
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0