結果
| 問題 | No.1332 Range Nearest Query |
| コンテスト | |
| ユーザー |
mtsd
|
| 提出日時 | 2021-01-08 21:51:53 |
| 言語 | C++14 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,885 bytes |
| 記録 | |
| コンパイル時間 | 1,796 ms |
| コンパイル使用メモリ | 126,924 KB |
| 実行使用メモリ | 31,460 KB |
| 最終ジャッジ日時 | 2024-11-16 11:16:48 |
| 合計ジャッジ時間 | 112,527 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 TLE * 32 |
ソースコード
#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;
}
int a[300000];
int res; //区間内の種類の数
multiset<int>st;
class Mo{
private:
vector<int> left, right,x;
const int width;
void add(const int id);
void del(const int id);
public:
vector<int> ans;
Mo(const int n) : width((int)sqrt(n)){}
// クエリ[l,r)
void insert(const int l, const int r,const int xx){
left.push_back(l), right.push_back(r);
x.push_back(xx);
}
void solve(){
const int sz = (int)left.size();
int nl = 0, nr = 0;
vector<int> ord(sz);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](const int a, const int b){
const int c = left[a] / width, d = left[b] / width;
return (c == d) ? ((c & 1) ? (right[b] < right[a]) : (right[a] < right[b])) : (c < d);
});
ans.resize(sz);
for(const int id : ord){
while(nl > left[id]) add(--nl);
while(nr < right[id]) add(nr++); //add
while(nl < left[id]) del(nl++);
while(nr > right[id]) del(--nr); //del
auto k = st.lower_bound(x[id]);
int tmp = inf;
if(k!=st.end()){
tmp = (*k)-x[id];
}
if(k!=st.begin()){
k--;
chmin(tmp,x[id]-(*k));
}
ans[id] = tmp;
}
}
};
//idは元の配列のインデックス
void Mo::add(const int id)
{
st.insert(a[id]);
}
void Mo::del(const int id)
{
st.erase(st.find(a[id]));
}
int main(){
int n;
cin >> n;
rep(i,n)cin >> a[i];
int q;
cin >> q;
Mo mo(n);
rep(zz,q){
int l,r,x;
cin >> l >> r >> x;
mo.insert(l-1,r,x);
}
mo.solve();
rep(i,q){
cout << mo.ans[i] << "\n";
}
return 0;
}
mtsd