QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#356806#8301. Hold the LineFOYRE 1ms3776kbC++231.8kb2024-03-18 12:30:252024-03-18 12:30:26

Judging History

This is the latest submission verdict.

  • [2024-03-18 12:30:26]
  • Judged
  • Verdict: RE
  • Time: 1ms
  • Memory: 3776kb
  • [2024-03-18 12:30:25]
  • Submitted

answer

#pragma GCC optimize "Ofast"
#include <vector>
#include <set>
#include <iostream>

using namespace std;
const int inf = 1e9-5;

int closest(int x, set<int> &a) {
	auto u = a.upper_bound(x);
	auto l = a.lower_bound(x);
	int ans = -inf;
	if (u != a.begin()) 
		ans = *prev(u);
	if (l != a.end()) 
		if (abs(ans-x) > abs(*l-x)) ans = *l;
	return ans;
}

int closest(int x, int y, int z) {
	if (abs(x-y) < abs(x-z)) return y;
	else return z;
}

int bs = 4;
struct Segtree {
	int n;
	vector<set<int>> tree;
	vector<int> vals;
	Segtree(int n): n(n), tree((2*n)>>bs), vals(n, -inf) { }

	void insert(int idx, int val) {
		vals[idx] = val;
		idx += n;
		idx >>= bs;
		while (idx > 0) {
			tree[idx].insert(val);
			idx /= 2;
		}
	}

	int find(int l, int r, int targ) {
		int sl = l, sr = r;
		int out = -inf;
		for (l=(l+n)>>(bs),r=(r+n)>>(bs); l < r; l /= 2, r /= 2) {
			if ((l&1)) {
				out = closest(targ, out, closest(targ, tree[l]));
				l++;
			}
			if (r&1) {
				r--;
				out = closest(targ, out, closest(targ, tree[r]));
			}
		}
		l = sl; r = sr;
		for (int i = l; i < min(r, l+(1<<bs)); i++) {
			out = closest(targ, out, vals[i]);
		}
		for (int i = r-1; i >= max(l+(1<<bs), r-(1<<bs)); i--) {
			out = closest(targ, out, vals[i]);
		}
		return out;
	}
};

void solve() {
	int n, m; cin >> n >> m;
	Segtree s(n);
	for (int i = 0; i < m; i++) {
		int type; cin >> type;
		if (type == 0) {
			int x, h; cin >> x>> h;
			s.insert(x-1, h);
		}
		else {
			int l, r, h; cin >> l >> r >> h;
			l--;
			int ans = s.find(l, r, h);
			if (ans == -inf) cout << -1 << '\n';
			else cout << abs(ans-h) << '\n';
		}
	}

}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t; cin >> t;
	while (t--) solve();

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3776kb

input:

1
3 5
1 1 3 2
0 1 1
0 3 3
1 1 2 2
1 2 3 2

output:

-1
1
1

result:

ok 3 lines

Test #2:

score: -100
Runtime Error

input:

3000
100 200
0 59 64091111
1 10 94 45205032
0 41 67249140
1 15 93 79570187
0 51 83215051
1 3 22 20062363
0 21 5188814
1 43 94 79642299
0 73 39313603
1 43 67 17001771
0 65 10784990
1 51 69 73368509
0 42 57377517
1 36 49 17483147
0 40 67280095
1 3 41 25139505
0 56 22833553
1 26 65 15640242
0 22 189761...

output:


result: