結果
問題 |
No.878 Range High-Element Query
|
ユーザー |
![]() |
提出日時 | 2019-09-06 22:07:39 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 406 ms / 2,000 ms |
コード長 | 2,136 bytes |
コンパイル時間 | 2,811 ms |
コンパイル使用メモリ | 186,556 KB |
実行使用メモリ | 33,904 KB |
最終ジャッジ日時 | 2024-06-24 18:01:56 |
合計ジャッジ時間 | 5,794 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 18 |
ソースコード
#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; }