結果

問題 No.1332 Range Nearest Query
ユーザー snrnsidysnrnsidy
提出日時 2021-11-10 02:40:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,874 bytes
コンパイル時間 2,554 ms
コンパイル使用メモリ 216,668 KB
実行使用メモリ 26,096 KB
最終ジャッジ日時 2024-04-30 01:41:06
合計ジャッジ時間 11,335 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 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 <bits/stdc++.h>
 
using namespace std;

int sq;

struct Query {
    int idx, l, r, val;
    int V;
    long long ans;
    Query(int ii, int ll, int rr,int vv) :idx(ii), l(ll), r(rr), val(vv), V(l / sq) {}
};

int N, M;
int arr[300010];
set <int> S;
int res;

int main(void) 
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> N;
    vector <int> v(N + 1);

    sq = (int)sqrt(N);

    for (int n = 0; n < N; n++)
    {
        cin >> arr[n];
    }

    vector<Query> save;
    cin >> M;
    for (int m = 0; m < M; m++) 
    {
        int u, v, x;

        cin >> u >> v >> x;
        u--;
        v--;
        if (u > v)
        {
            swap(u, v);
        }
        save.emplace_back(Query(m, u, v, x));

    }

    sort(save.begin(), save.end(), [&](Query const& a, Query const& b)->bool {
        if (a.V == b.V) return a.r < b.r;
        return a.V < b.V;
        });


    int L = save[0].l, R = save[0].l - 1;

    for (int m = 0; m < M; m++) {

        while (L < save[m].l) 
        {
            S.erase(arr[L]);
            L++;
        }

        while (L > save[m].l) 
        {
            L--;
            S.insert(arr[L]);
        }

        while (R < save[m].r) 
        {
            R++;
            S.insert(arr[R]);
        }

        while (R > save[m].r) 
        {
            S.erase(arr[R]);
            R--;
        }

		int X = save[m].val;
		auto it = S.lower_bound(X);
		long long int res = 1e9;
		if(it!=S.end())
		{
			long long int d = *it - X;
			res = min(res,d);
		}
		if(it!=S.begin())
		{
			it--;
			long long int d = X - *it;
			res = min(res,d);
		}
        save[m].ans = res;
    }

    sort(save.begin(), save.end(), [&](Query const& a, Query const& b)->bool 
    {
        return a.idx < b.idx;
    });

    for (int m = 0; m < M; m++)
    {
        cout << save[m].ans << '\n';
    }

    return 0;

}
0