結果

問題 No.1332 Range Nearest Query
ユーザー mtsd
提出日時 2021-01-08 22:05:45
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 420 ms / 2,500 ms
コード長 4,467 bytes
コンパイル時間 1,619 ms
コンパイル使用メモリ 134,096 KB
実行使用メモリ 34,232 KB
最終ジャッジ日時 2024-11-16 12:15:33
合計ジャッジ時間 18,322 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
std::fill( (T*)array, (T*)(array+N), val );
}
template<class T> inline bool chmax(T &a, T b){
if(a<b){
a = b;
return true;
}
return false;
}
template<class T> inline bool chmin(T &a, T b){
if(a>b){
a = b;
return true;
}
return false;
}
template<typename T> class segtree{
private:
int n,sz;
vector<T> node;
public:
segtree(const vector<T>& v) : n(1), sz((int)v.size()){
while(n < sz) n *= 2;
node.resize(2*n-1, numeric_limits<T>::max());
for(int i = 0; i < sz; i++){
node[i+n-1] = v[i];
}
for(int i=n-2; i>=0; i--){
node[i] = min(node[i*2+1], node[i*2+2]);
}
}
void update(int k,T a){
k += n-1;
node[k] = a;
while(k>0){
k = (k-1)/2;
node[k] = min(node[2*k+1],node[2*k+2]);
}
}
T query(int a,int b,int k=0,int l=0,int r=-1){
if(r < 0) r = n;
if(r <= a || b <= l){
return numeric_limits<T>::max();
}
if(a <= l && r <= b){
return node[k];
}else{
T vl = query(a,b,2*k+1,l,(l+r)/2);
T vr = query(a,b,2*k+2,(l+r)/2,r);
return min(vl,vr);
}
}
void print(){
for(int i = 0; i < sz; i++){
cout<<query(i,i+1)<< " ";
}
cout<<endl;
}
};
template<typename T> class segtree2{
private:
int n,sz;
vector<T> node;
public:
segtree2(const vector<T>& v) : n(1), sz((int)v.size()){
while(n < sz) n *= 2;
node.resize(2*n-1, numeric_limits<T>::min());
for(int i = 0; i < sz; i++){
node[i+n-1] = v[i];
}
for(int i=n-2; i>=0; i--){
node[i] = max(node[i*2+1], node[i*2+2]);
}
}
void update(int k,T a){
k += n-1;
node[k] = a;
while(k>0){
k = (k-1)/2;
node[k] = max(node[2*k+1],node[2*k+2]);
}
}
T query(int a,int b,int k=0,int l=0,int r=-1){
if(r < 0) r = n;
if(r <= a || b <= l){
return numeric_limits<T>::min();
}
if(a <= l && r <= b){
return node[k];
}else{
T vl = query(a,b,2*k+1,l,(l+r)/2);
T vr = query(a,b,2*k+2,(l+r)/2,r);
return max(vl,vr);
}
}
void print(){
for(int i = 0; i < sz; i++){
cout<<query(i,i+1)<< " ";
}
cout<<endl;
}
};
int main(){
int n;
cin >> n;
vector<int>a(n);
rep(i,n)cin >> a[i];
int q;
cin >> q;
vector<pair<pair<int,int>,pair<int,int> > > query;
rep(zz,q){
int l,r,x;
cin >> l >> r >> x;
query.push_back(MP(MP(x,zz),MP(l-1,r)));
}
vector<pair<pair<int,int>,pair<int,int>> > v;
rep(i,n){
v.push_back(MP(MP(a[i],-1),MP(i,0)));
}
rep(i,q){
v.push_back(query[i]);
}
sort(all(v));
vector<ll>p(n,-(1LL<<60));
segtree2<ll> seg(p);
vector<ll> res(q,1LL<<60);
rep(i,n+q){
if(v[i].first.second==-1){
seg.update(v[i].second.first,v[i].first.first);
}else{
chmin(res[v[i].first.second],v[i].first.first -seg.query(v[i].second.first,v[i].second.second));
}
}
vector<ll>qq(n,(1LL<<60));
segtree<ll> segg(qq);
reverse(all(v));
rep(i,n+q){
if(v[i].first.second==-1){
segg.update(v[i].second.first,v[i].first.first);
}else{
chmin(res[v[i].first.second],segg.query(v[i].second.first,v[i].second.second)-v[i].first.first);
}
}
rep(i,q){
cout << res[i] << "\n";
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0