QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#446179#8301. Hold the LinelymWA 114ms3864kbC++202.1kb2024-06-16 23:18:112024-06-16 23:18:11

Judging History

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

  • [2024-06-16 23:18:11]
  • 评测
  • 测评结果:WA
  • 用时:114ms
  • 内存:3864kb
  • [2024-06-16 23:18:11]
  • 提交

answer

#include<bits/stdc++.h>
using i64 = long long;
const int inf = 1e9 + 10;
struct Block {
	int n, b;
	std::vector<int> blo, a;
	std::vector<std::vector<int> > v;
	void init(int n) {
		this -> n = n;
		this -> b = (int) std::pow(n, 0.5);
		blo.assign(n + 1, 0);
		v.assign(n + 1, {});
		a.assign(n + 1, 0);
		//st.assign(n + 1, {});
		for (int i = 1; i <= n; i ++) {
			blo[i] = (i - 1) / b + 1;
		}
	}
	void add(int x, int c) {
		int p = blo[x];
		//v[p].insert(c);
		a[x] = c;
		int l = v[p].size();
		v[p].push_back(c);

		while (l > 0 && v[l - 1] > v[l]) {
			std::swap(v[l - 1], v[l]);
			l --;
		}
		//st[p].insert(x);
	}
	int query(int l, int r, int c) {
		int res = inf, p = blo[l], q = blo[r];
		for (int i = l; i <= std::min(r, p * b); i ++) {
			if (a[i]) {
				res = std::min(res, std::abs(a[i] - c));
			}
		}
		if (p == q) return res;
		for (int i = (q - 1) * b + 1; i <= r; i ++) {
			if (a[i]) {
				res = std::min(res, std::abs(a[i] - c));
			}
		}

		for (int i = p + 1; i < q; i ++) {
			int l = v[i].size();
			if (! l) continue;
			auto it = std::lower_bound(v[i].begin(), v[i].end(), c) - v[i].begin();
			if (it != l) {
				res = std::min(res, v[i][it] - c);
			}
			if (it != 0) {
				res = std::min(res, c - v[i][it - 1]);
			}
		}
		return res;
	}
};

int read(){
    int red=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){ if(ch=='-') f=-f;ch=getchar();}
    while(ch>='0'&&ch<='9') red=red*10+ch-48,ch=getchar();
    return red*f;
}
void solve() {
	int n, m;
	n = read();
	m = read();
	Block t;
	t.init(n);
	while (m --) {
		int op;
		//std::cin >> op;
		op = read();
		if (op & 1) {
			int l, r, v;
			//std::cin >> l >> r >> v;
			l = read();
			r = read();
			v = read();
			int res = t.query(l, r, v);
			if (res == inf) {
				res = -1;
			}
			std::cout << res << '\n';
		} else {
			int x, v;
			//std::cin >> x >> v;
			x = read();
			v = read();
			t.add(x, v);
		}
	}
}
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int t = 1;
	t = read();
	while (t --) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3864kb

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
Wrong Answer
time: 114ms
memory: 3812kb

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
4000027
3981124
4159424
5836824
61155540
5163746
-1
283130
3982548
-1
-1
-1
9647216
2356693
8711627
6338066
70230536
2637615
17759167
153976
148...

result:

wrong answer 22nd lines differ - expected: '734969', found: '4000027'