結果

問題 No.1332 Range Nearest Query
ユーザー snrnsidysnrnsidy
提出日時 2021-11-10 03:31:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 214 ms / 2,500 ms
コード長 5,106 bytes
コンパイル時間 2,588 ms
コンパイル使用メモリ 219,700 KB
実行使用メモリ 21,748 KB
最終ジャッジ日時 2024-04-30 02:35:16
合計ジャッジ時間 13,515 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 AC 196 ms
19,912 KB
testcase_04 AC 196 ms
20,040 KB
testcase_05 AC 194 ms
19,956 KB
testcase_06 AC 162 ms
21,644 KB
testcase_07 AC 162 ms
21,748 KB
testcase_08 AC 163 ms
21,620 KB
testcase_09 AC 163 ms
21,740 KB
testcase_10 AC 163 ms
21,692 KB
testcase_11 AC 162 ms
21,620 KB
testcase_12 AC 163 ms
21,616 KB
testcase_13 AC 165 ms
21,616 KB
testcase_14 AC 163 ms
21,616 KB
testcase_15 AC 162 ms
21,616 KB
testcase_16 AC 214 ms
21,616 KB
testcase_17 AC 210 ms
21,612 KB
testcase_18 AC 212 ms
21,612 KB
testcase_19 AC 211 ms
21,744 KB
testcase_20 AC 211 ms
21,612 KB
testcase_21 AC 208 ms
21,744 KB
testcase_22 AC 208 ms
21,616 KB
testcase_23 AC 210 ms
21,616 KB
testcase_24 AC 206 ms
21,612 KB
testcase_25 AC 207 ms
21,616 KB
testcase_26 AC 181 ms
21,620 KB
testcase_27 AC 144 ms
21,612 KB
testcase_28 AC 47 ms
5,740 KB
testcase_29 AC 48 ms
5,868 KB
testcase_30 AC 48 ms
5,872 KB
testcase_31 AC 45 ms
5,744 KB
testcase_32 AC 49 ms
5,748 KB
testcase_33 AC 49 ms
5,872 KB
testcase_34 AC 45 ms
5,872 KB
testcase_35 AC 47 ms
5,744 KB
testcase_36 AC 47 ms
5,872 KB
testcase_37 AC 48 ms
5,840 KB
testcase_38 AC 140 ms
12,396 KB
testcase_39 AC 73 ms
6,008 KB
testcase_40 AC 204 ms
21,744 KB
testcase_41 AC 97 ms
8,820 KB
testcase_42 AC 136 ms
11,628 KB
testcase_43 AC 113 ms
9,836 KB
testcase_44 AC 172 ms
16,496 KB
testcase_45 AC 163 ms
16,492 KB
testcase_46 AC 128 ms
9,832 KB
testcase_47 AC 153 ms
16,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
 
using namespace std;

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
constexpr int bsf_constexpr(unsigned int n) {
    int x = 0;
    while (!(n & (1 << x))) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder

namespace atcoder {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
    explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) const {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) const {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() const { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) const {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) const {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) const {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) const {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder

int op1(int a, int b) {
    return min(a, b);
}

int e1() {
    return (int)(2e9);
}

int op2(int a, int b) {
    return max(a, b);
}

int e2() {
    return (int)(-2e9);
}

using namespace atcoder;

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

	int N,Q,L,R,X;

	cin >> N;

	segtree<int, op1, e1> minseg(N+1);
	segtree<int, op2, e2> maxseg(N+1);

	vector <pair<int,pair<pair<int,int>,int>>> v;

	for(int i=1;i<=N;i++)
	{
		cin >> X;
		v.push_back(make_pair(X,make_pair(make_pair(i,i),-1)));
	}

	cin >> Q;

	vector <int> res(Q,2e9);

	for(int i=0;i<Q;i++)
	{
		cin >> L >> R >> X;
		v.push_back(make_pair(X,make_pair(make_pair(L,R),i)));
	}

	sort(v.begin(),v.end());

	for(int i=0;i<v.size();i++)
	{
		if(v[i].second.second==-1)
		{
			maxseg.set(v[i].second.first.first,v[i].first);
		}	
		else
		{
			int L = v[i].second.first.first;
			int R = v[i].second.first.second;
			int idx = v[i].second.second;
			int val = maxseg.prod(L,R+1);
			if(val<=-2e9) continue;
			res[idx] = min(res[idx],v[i].first - val);
		}
	}

	sort(v.rbegin(),v.rend());

	for(int i=0;i<v.size();i++)
	{
		if(v[i].second.second==-1)
		{
			minseg.set(v[i].second.first.first,v[i].first);
		}	
		else
		{
			int L = v[i].second.first.first;
			int R = v[i].second.first.second;
			int idx = v[i].second.second;
			int val = minseg.prod(L,R+1);
			if(val>=2e9) continue;
			res[idx] = min(res[idx],val - v[i].first);
		}
	}	

	for(int i=0;i<Q;i++)
	{
		cout << res[i] << '\n';
	}
    return 0;

}
0