QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#446144#8301. Hold the LinelymTL 0ms0kbC++202.0kb2024-06-16 22:38:212024-06-16 22:38:21

Judging History

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

  • [2024-06-16 22:38:21]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-06-16 22:38:21]
  • 提交

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::set<int> > v;
	std::vector<std::set<int> > st;
	void init(int n) {
		this -> n = n;
		this -> b = (int) std::pow(n, 0.46);
		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;
		st[p].insert(x);
	}
	int query(int l, int r, int c) {
		int res = inf, p = blo[l], q = blo[r];
		auto L = st[p].lower_bound(l);
		while (L != st[p].end() && *L <= r) {
			res = std::min(res, std::abs(a[*L] - c));
			L = next(L);
		}
		if (p == q) return res;
		auto R = st[q].begin();
		while (R != st[q].end() && *R <= r) {
			res = std::min(res, std::abs(a[*R] - c));
			R = next(R);
		}

		for (int i = p + 1; i < q; i ++) {
			auto it = v[i].lower_bound(c);
			if (it != v[i].end()) {
				res = std::min(res, std::abs(*it - c));
			}
			if (it != v[i].begin()) {
				it = prev(it);
				res = std::min(res, std::abs(*it - c));
			}
		}
		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;
	std::cin >> t;
	while (t --) {
		solve();
	}
	return 0;
}

詳細信息

Test #1:

score: 0
Time Limit Exceeded

input:

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

output:


result: