結果
問題 |
No.1332 Range Nearest Query
|
ユーザー |
![]() |
提出日時 | 2020-11-23 04:13:40 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,790 bytes |
コンパイル時間 | 2,153 ms |
コンパイル使用メモリ | 180,248 KB |
実行使用メモリ | 11,828 KB |
最終ジャッジ日時 | 2024-07-23 16:56:58 |
合計ジャッジ時間 | 17,655 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 38 WA * 10 |
ソースコード
#include<bits/stdc++.h> using std::cin; using std::cout; using std::vector; using ll=long long; #define endl "\n"; const int inf=1<<30; //セグ木 struct Segtree{ int N; vector<int> node; Segtree(int n){ N=1; while(N<n) N*=2; node.resize(N*2-1,-inf); } void update(int ind,int val){ ind+=N-1; node[ind]=val; while(ind){ ind=(ind-1)/2; node[ind]=std::max(node[ind*2+1],node[ind*2+2]); } } int Query(int left,int right,int now=0,int l=0,int r=-1){ if(r<0) r=N; if(left>=r||right<=l) return -inf; if(left<=l&&r<=right) return node[now]; return std::max(Query(left,right,now*2+1,l,(l+r)/2),Query(left,right,now*2+2,(l+r)/2,r)); } }; int main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); //入力、宣言 int N; cin>>N; vector<std::pair<int,int>> data; //data[i].second が 0 以下なら座標を、1 以上ならクエリを表す for(int i=0;i<N;i++){ int X; cin>>X; data.push_back(std::make_pair(X,-i)); } int Q; cin>>Q; vector<int> l(Q),r(Q); for(int i=0;i<Q;i++){ int x; cin>>l[i]>>r[i]>>x; data.push_back(std::make_pair(x,i+1)); } sort(data.begin(),data.end()); vector<int> ans(Q,inf); //本題 for(int _=0;_<2;_++){ Segtree seg(N); for(auto p:data){ if(p.second<=0) seg.update(-p.second,p.first); else{ int index=p.second-1; ans[index]=std::min(ans[index],p.first-seg.Query(l[index]-1,r[index])); } } for(auto &p:data) p.first*=-1; reverse(data.begin(),data.end()); } for(int i=0;i<Q;i++) cout<<ans[i]<<endl; }