結果

問題 No.1332 Range Nearest Query
ユーザー ゆにぽけゆにぽけ
提出日時 2024-01-27 14:33:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,450 bytes
コンパイル時間 1,696 ms
コンパイル使用メモリ 142,520 KB
実行使用メモリ 25,192 KB
最終ジャッジ日時 2024-01-27 14:33:41
合計ジャッジ時間 11,163 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,480 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <utility>
#include <functional>
using namespace std;
struct Mo{
    int sq;
    vector<int> left,right,order;

    Mo(int N,int Q)
    {
        order.resize(Q);
        sq = max<int>(1,1.0*N/ max<double>(1.0,sqrt(2.0*Q/3.0)));
        iota(order.begin(),order.end(),0);
    }

    void insert(int l,int r)
    {
        left.push_back(l);
        right.push_back(r);
    }

    template<class AL,class AR,class DL,class DR,class REM>
    void run(const AL &addleft,const AR &addright,const DL &delleft,const DR &delright,const REM &rem)
    {
        assert(left.size() == order.size());
        sort(order.begin(),order.end(),[&](int u,int v)
        {
            int ublock = left[u]/sq,vblock = left[v]/sq;
            if(ublock != vblock) return ublock < vblock;

            if(ublock & 1) return right[u] < right[v];
            return right[u] > right[v];
        });

        int nl = 0,nr = 0;
        for(int i:order)
        {
            while(nl > left[i]) addleft(--nl);
            while(nr < right[i]) addright(nr++);
            while(nl < left[i]) delleft(nl++);
            while(nr > right[i]) delright(--nr);

            rem(i);
        }
    }
};
void Main()
{
	int N;
	cin >> N;
	vector<int> X(N);
	for(int i = 0;i < N;i++)
	{
		cin >> X[i];
	}
	int Q;
	cin >> Q;
	Mo mo(N,Q);
	vector<int> x(Q);
	for(int i = 0;i < Q;i++)
	{
		int l,r;
		cin >> l >> r >> x[i];
		mo.insert(--l,r);
	}

	set<int> S;
	vector<int> ans(Q,(int)2e9);
	auto add = [&](int i) -> void
	{
		S.insert(X[i]);
	};
	auto del = [&](int i) -> void
	{
		auto it = S.find(X[i]);
		assert(it != S.end());
		S.erase(it);
	};
	auto rem = [&](int i) -> void
	{
		auto it = S.lower_bound(x[i]);
		if(it != S.end())
		{
			ans[i] = min(ans[i],*it - x[i]);
		}
		if(it != S.begin())
		{
			it--;
			ans[i] = min(ans[i],x[i] - *it);
		}
	};
	mo.run(add,add,del,del,rem);
	for(int i = 0;i < Q;i++)
	{
		cout << ans[i] << "\n";
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) Main();
}
0