結果
| 問題 |
No.1332 Range Nearest Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-01-09 10:34:08 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,808 bytes |
| コンパイル時間 | 2,494 ms |
| コンパイル使用メモリ | 210,068 KB |
| 最終ジャッジ日時 | 2025-01-17 15:14:58 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 TLE * 20 |
ソースコード
#line 2 "/home/defineprogram/Desktop/Library/template/template.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<ll, ll>
#define len(s) (ll) s.size()
template <class T, class U>
inline bool chmin(T &a, U b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T, class U>
inline bool chmax(T &a, U b) {
if (a < b) {
a = b;
return true;
}
return false;
}
constexpr ll inf = 3e18;
#line 3 "/home/defineprogram/Desktop/Library/structure/Mo.cpp"
struct Mo {
vector<int> left, right, order;
vector<bool> v;
int width, l = 0, r = 0, cur = 0;
Mo(int n) : width(sqrt(n)), v(n) {}
void insert(int l, int r) {
left.push_back(l);
right.push_back(r);
}
void init() {
order.resize(len(left));
iota(all(order), 0);
sort(all(order), [&](int a, int b) {
if (left[a] / width != left[b] / width) return left[a] < left[b];
return right[a] < right[b];
});
}
int process() {
int id = order[cur];
while (l > left[id]) dis(--l);
while (r < right[id]) dis(r++);
while (l < left[id]) dis(l++);
while (r > right[id]) dis(--r);
return order[cur++];
}
inline void dis(int idx) {
v[idx].flip();
if (v[idx])
add(idx);
else
del(idx);
}
void add(int);
void del(int);
};
#line 3 "/home/defineprogram/Desktop/Library/structure/BIT.cpp"
template <class T>
class BIT {
ll N;
vector<T> bit;
void add_(ll x, T y) {
while (x <= N) {
bit[x] += y;
x += x & -x;
}
}
T sum_(ll x) {
T res = 0;
while (x > 0) {
res += bit[x];
x -= x & -x;
}
return res;
}
public:
ll lower_bound(T w) {
if (w <= 0) return -1;
ll x = 0;
ll k = 1;
while (k * 2 <= N) k *= 2;
for (; k > 0; k /= 2) {
if (x + k <= N && bit[x + k] < w) {
w -= bit[x + k];
x += k;
}
}
return x;
}
void add(ll x, T y) { add_(x + 1, y); }
T sum(ll l, ll r) { return sum_(r) - sum_(l); }
BIT(ll x) : N(x), bit(x + 1) {}
};
/*
@brief Binary Indexed Tree
@docs docs/BIT.md
*/
#line 4 "main.cpp"
int N,X[1<<19];
BIT<int> bit(1),bit2(1);
multiset<int>st;
void Mo::add(int idx){
bit.add(X[idx],1);
if(bit.sum(X[idx],X[idx]+1)==1){
bit2.add(X[idx],1);
}
}
void Mo::del(int idx){
bit.add(X[idx],-1);
if(bit.sum(X[idx],X[idx]+1)==0){
bit2.add(X[idx],-1);
}
}
int Q,L[1<<17],R[1<<17],x[1<<17];
int ans[1<<17];
int main() {
cin.tie(0); ios::sync_with_stdio(false);
cin>>N;
vector<int>xx;
rep(i,N){
cin>>X[i];
xx.push_back(X[i]);
}
xx.push_back(-1e9);xx.push_back(2e9);
sort(all(xx));xx.erase(unique(all(xx)),xx.end());
rep(i,N)X[i]=lower_bound(all(xx),X[i])-xx.begin();
bit=BIT<int>(len(xx));
bit2=BIT<int>(len(xx));
bit.add(0,1);bit2.add(0,1);
bit.add(len(xx)-1,1);bit2.add(len(xx)-1,1);
Mo mo(len(xx));
cin>>Q;
rep(i,Q){
cin>>L[i]>>R[i]>>x[i];
L[i]--;
mo.insert(L[i],R[i]);
}
mo.init();
rep(i,Q){
int id=mo.process();
int idx=lower_bound(all(xx),x[id])-xx.begin();
int memo=bit2.sum(0,idx);
int l=bit2.lower_bound(memo),r=bit2.lower_bound(memo+1);
ans[id]=min(x[id]-xx[l],xx[r]-x[id]);
}
rep(i,Q){
cout<<ans[i]<<"\n";
}
}