QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#356838#8301. Hold the LineFOYWA 407ms3928kbC++141.8kb2024-03-18 13:19:452024-03-18 13:19:45

Judging History

你现在查看的是最新测评结果

  • [2024-03-18 13:19:45]
  • 评测
  • 测评结果:WA
  • 用时:407ms
  • 内存:3928kb
  • [2024-03-18 13:19:45]
  • 提交

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(1+((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 (int sz=0, l=(l+n),r=(r+n); l < r; l /= 2, r /= 2, sz++) {
			if ((l&1)) {
				if (sz >= bs) out = closest(targ, out, closest(targ, tree[l]));
				l++;
			}
			if (r&1) {
				r--;
				if (sz >= bs) out = closest(targ, out, closest(targ, tree[r]));
			}
		}
		l = sl; r = sr;
		for (int i = l; i < min(r, l+4*(1<<bs)); i++) {
			out = closest(targ, out, vals[i]);
		}
		for (int i = r-1; i >= max(l, r-4*(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();

}

詳細信息

Test #1:

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

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: 0
Accepted
time: 301ms
memory: 3532kb

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:

18886079
12321047
-1
3572752
47089340
9277398
39894370
19950691
4855252
2221206
1596905
-1
3120922
34260194
3353597
-1
2499997
-1
15114024
1193064
49448136
734969
3981124
4159424
5836824
61155540
5163746
-1
283130
3982548
-1
-1
-1
9647216
2356693
8711627
379947
70230536
2637615
7856589
153976
148089...

result:

ok 700000 lines

Test #3:

score: -100
Wrong Answer
time: 407ms
memory: 3928kb

input:

300
1000 2000
0 421 19938
1 153 254 35195
0 567 74665
1 88 371 61709
0 689 57559
1 39 744 67718
0 770 25816
1 576 955 75098
0 215 17619
1 133 133 29547
0 207 25038
1 929 965 45024
0 820 40726
1 245 505 82535
0 52 99669
1 631 819 77027
0 436 69966
1 102 243 65413
0 878 73531
1 331 759 23736
0 698 594...

output:

-1
-1
10159
-1
-1
-1
-1
19468
40375
-1
45299
-1
-1
11815
-1
-1
-1
40333
-1
-1
-1
9628
43993
-1
17794
-1
972
4091
818
-1
14740
-1
53092
-1
712
-1
16697
-1
-1
1592
38454
-1
24068
12896
5677
964
2836
29744
501
-1
-1
6597
1859
-1
6311
10290
4562
1589
838
17240
18220
719
11147
2781
-1
59401
2809
77412
11...

result:

wrong answer 3rd lines differ - expected: '6947', found: '10159'