跳到正文

个人记录

HT-122-Div.3 题解

shuqiang约 25 分钟
原文

这是 2026年8月2日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年8月2日
收录
2026年8月2日
更新
2026年11月14日
篇幅
约 25 分钟

版本

  1. v2当前
  2. v1

A

观察到 ∑q≤106\sum q \le 10^6,那么这题就非常简单了,你可以枚举在 xx 坐标操作了 kxk_x 次,则 yy 坐标就操作了 k−kxk-k_x 次。

接下来,就转化成了在一个一维数轴上移动,AA 在前就执行操作 1,21,2,BB 在前就执行操作 3,43,4。

注意,操作是必须恰好用完,如果操作不够用,那当然是能用多少用多少,否则假设如果已经让两点重复,以 xx 为例,剩余的可以这么循环用:

  • x1→x1+1x_1 \rightarrow x_1+1
  • x2→x2+1x_2 \rightarrow x_2+1

此时若 kk 是 22 的倍数,答案为 00,否则为 11。

注意,设全局变量 y1 并导入 cmath 会报错!

时间复杂度 O(∑k)\mathcal{O}(\sum k)。

参考代码
C++
#include<iostream>
#include<cmath>

using namespace std;

int t, ax, ay, bx, by, k, s; long long ans;

int solve(int ft, int ed, int k){
	if(ed < ft){
		int c = min(k, ft - ed);
		ed += c;
		k -= c;
	}
	if(ft < ed){
		int c = min(k, ed - ft);
		ft += c;
		k -= c;
	}
	return abs(ft - ed) + (k % 2);
}

int main(){
	cin >> t;
	while(t--){
		cin >> ax >> ay >> bx >> by >> k; ans = 2e18;
		for(int i = 0; i <= k; i++){
			int dx = solve(ax, bx, i);
			int dy = solve(ay, by, k - i);
			ans = min(ans, (long long)dx * dx + (long long)dy * dy);
		}
        s += k;
		cout << ans << '\n';
	}
	return 0;
}

B

设最终 ci=ai+bi=xc_i=a_i+b_i=x,因为 bi=x−ai∈[−k,k]b_i=x-a_i \in[-k,k],所以 x∈[ai−k,ai+k]x \in [a_i-k,a_i+k],因此可选的 xx 就是所有 [ai−k,ai+k][a_i-k,a_i+k] 的交集,那么它的大小就是 max⁡(0,min⁡(ai+k)−max⁡(ai−k)+1)\max(0,\min(a_i+k)-\max(a_i-k)+1),化简得答案为:

max⁡(0,2k+min⁡(ai)−max⁡(ai)+1)\max(0,2k+\min(a_i)-\max(a_i)+1)

于是求 aa 的 min⁡,max⁡\min,\max,再计算出答案即可。

时间复杂度 O(n)\mathcal{O}(n)。

参考代码
C++
#include<iostream>

using namespace std;

const int N = 1e6 + 10;
int n, k, mx, mn = 1e9, a[N];

int main(){
	cin >> n >> k;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		mx = max(a[i], mx);
		mn = min(a[i], mn);
	}
	cout << max(0, 2*k - (mx - mn) + 1);
	return 0;
}

C

首先要解决一个问题,什么时候一个分数是有限小数?答案是:一个分数,约分到最简以后,如果分母只含有质因子 22 和 55,那么它就是有限小数。

为什么呢,一个很明显的结论:如果是有限小数,一定存在非负整数 kk,使这个小数和 10k10^k 的乘积是一个正整数,而 10k10^k 又有质因子 22 和 55,所以结论成立。

那么 aa 中如果有质因子 2,52,5 就可以直接消掉了。那么假设 pp 是 aa 的一个质因子,那么一定有其中一个 x=p×2i×5jx=p \times 2^i \times 5^j,这里 i,ji,j 是非负整数。

然后这题就简单了,枚举质因子,再枚举 rr 以内的 i,ji,j,检验是否大于等于 ll 即可,时间复杂度 O(r+ω(r)log⁡2a)\mathcal{O}(\sqrt{r}+\omega(r)\log^2 a)。其中 ω(r)\omega(r) 表示 rr 的质因子个数。

参考代码
C++
#include<iostream>

using namespace std;
typedef long long ll;

ll a, l, r; int ans;

void calc(ll x){
	for(ll i2 = 1; i2*x <= r; i2 *= 2){
		for(ll i5 = 1; i2*i5*x <= r; i5 *= 5){
			if(i2*i5*x >= l) ans++;
		}
	}
}

int main(){
	cin >> a >> l >> r;
	while(a % 2 == 0) a /= 2;
	while(a % 5 == 0) a /= 5;
	for(ll p = 1; p*p <= a; p++){
		if(a % p == 0){
			calc(p);
			if(a/p != p) calc(a/p);		
		}
	}
	cout << ans;
	return 0;
}

D

Subtask1

因为题目保证豌豆射手和僵尸的坐标互不相同,所以可以用一个二维数组 ai,ja_{i,j} 来存第 ii 行第 jj 列的植物或僵尸状态,当 ai,j=−1a_{i,j}=-1 时是豌豆射手,当 ai,j>0a_{i,j}>0 时表示场上存在一个血量为 ai,ja_{i,j} 的僵尸,每次放置后,遍历整个地图,每一列当遇到植物时就增加一个豌豆,遇到僵尸后就拿剩余的豌豆攻击僵尸,时间复杂度 O(qnm)\mathcal{O}(qnm)。

参考代码1
C++
#include<iostream>

using namespace std;

const int N = 20;
int q, op, x, y, z, a[N][N];

int main(){
	cin >> q;
	while(q--){
		cin >> op >> x >> y;
		if(op == 1) a[x][y] = -1;
		else{
			cin >> z;
			a[x][y] = z;
		}
		int cnt = 0, ans = 0;
		for(int i = 1; i <= 8; i++){
			cnt = 0;
			for(int j = 1; j <= 8; j++){
				if(a[j][i] == -1) cnt++;
				if(a[j][i] > 0){
					if(cnt > a[j][i]){
						cnt -= a[j][i];
						a[j][i] = 0;
					}
					else{
						a[j][i] -= cnt;
						cnt = 0;
					}
					ans ^= a[j][i];
				}
			}
		}
		cout << ans << '\n';
	}
	for(int i = 1; i <= 8; i++){
		for(int j = 1; j <= 8; j++){
			if(a[i][j] == -1) cout << 1 << ' ' << i << ' ' << j << '\n';
		}
	}
	for(int i = 1; i <= 8; i++){
		for(int j = 1; j <= 8; j++){
			if(a[i][j] > 0) cout << 2 << ' ' << i << ' ' << j << ' ' << a[i][j] << '\n'; 
		}
	}
	return 0;
}

Subtask2

这个子任务的地图是 1000×10001000 \times 1000,所以遍历整张地图肯定是不行的,考虑仅遍历有植物或有僵尸的格子,用 set 来存每一行的状态,方法和 Subtask1 类似,时间复杂度 O(q2log⁡q)\mathcal{O}(q^2 \log q)。

参考代码2
C++
#include<iostream>
#include<algorithm>
#include<set>

using namespace std;

const int N = 1010;

int q, op, x, y, z, cnt, ans, idx, idx1, idx2;

struct pii{
	int x, z;
	
	bool operator < (const pii & o) const{
		return x < o.x;
	}
};
set<pii> st[N];

struct thing{
	int x, y, z;
	
	bool operator < (const thing & o) const{
		if(x == o.x) return y < o.y;
		return x < o.x;
	}
} a[N], b[N];

int main(){
	cin >> q;
	while(q--){
		cin >> op >> x >> y;
		if(op == 1){
			st[y].insert({x, -1});
		}
		else{
			cin >> z;
			st[y].insert({x, z});
		}
		ans = 0;
		for(int i = 1; i <= 1000; i++){
			idx = cnt = 0;
			while(1){
				auto it = st[i].upper_bound({idx, 0});
				if(it == st[i].end()) break;
				idx = (*it).x, z = (*it).z;
				if(z == -1) cnt++;
				else{
					st[i].erase(it);
					if(cnt >= z){
						cnt -= z;
					}
					else{
						z -= cnt; cnt = 0;
						st[i].insert({idx, z});
						ans ^= z;
					} 
				}
				
			}
		}
		cout << ans << '\n';
	}
	for(int i = 1; i <= 1000; i++){
		for(auto j: st[i]){
			if(j.z == -1){
				a[idx1].x = j.x; a[idx1++].y = i;
			}
			else{
				b[idx2].x = j.x; b[idx2].y = i; b[idx2++].z = j.z;
			}
		}
	}
	sort(a, a + idx1);
	sort(b, b + idx2);
	for(int i = 0; i < idx1; i++){
		cout << 1 << ' ' << a[i].x << ' ' << a[i].y << '\n';
	}
	for(int i = 0; i < idx2; i++){
		cout << 2 << ' ' << b[i].x << ' ' << b[i].y << ' ' << b[i].z << '\n';
	}
	return 0;
}

Subtask3

这个子任务的地图是 109×10910^9 \times 10^9,但是植物和僵尸的总数只有 10001000,所以考虑对输入的 y 轴离散化,时间复杂度还是 O(q2log⁡q)\mathcal{O}(q^2 \log q)。

参考代码3
C++
#include<iostream>
#include<algorithm>
#include<set>
#include<map>

using namespace std;

const int N = 1010;

int q, op, x, y, z, cnt, ans, idx, idx1, idx2, idx3 = 1;

struct pii{
	int x, z;
	
	bool operator < (const pii & o) const{
		return x < o.x;
	}
};
set<pii> st[N];

struct thing{
	int x, y, z;
	
	bool operator < (const thing & o) const{
		if(x == o.x) return y < o.y;
		return x < o.x;
	}
} a[N], b[N], c[N];

set<int> x1; map<int, int> x2, x3;

int main(){
	cin >> q;
	for(int i = 0; i < q; i++){
		cin >> op >> x >> y;
		if(op == 1) c[i].x = x, c[i].y = y, c[i].z = -1;
		else{
			cin >> z;
			c[i].x = x, c[i].y = y, c[i].z = z;
		}
		x1.insert(y);
	}
	for(int i: x1){
		x3[idx3] = i; x2[i] = idx3++;
	}
	for(int i = 0; i < q; i++){
		c[i].y = x2[c[i].y];
	}
	for(int i = 0; i < q; i++){
		if(c[i].z == -1){
			st[c[i].y].insert({c[i].x, -1});
		}
		else{
			st[c[i].y].insert({c[i].x, c[i].z});
		}
		ans = 0;
		for(int j = 1; j <= 1000; j++){
			idx = cnt = 0;
			while(1){
				auto it = st[j].upper_bound({idx, 0});
				if(it == st[j].end()) break;
				idx = (*it).x, z = (*it).z;
				if(z == -1) cnt++;
				else{
					st[j].erase(it);
					if(cnt >= z){
						cnt -= z;
					}
					else{
						z -= cnt; cnt = 0;
						st[j].insert({idx, z});
						ans ^= z;
					} 
				}
			}
		}
		cout << ans << '\n';
	}
	for(int i = 1; i <= 1000; i++){
		for(auto j: st[i]){
			if(j.z == -1){
				a[idx1].x = j.x; a[idx1++].y = i;
			}
			else{
				b[idx2].x = j.x; b[idx2].y = i; b[idx2++].z = j.z;
			}
		}
	}
	sort(a, a + idx1);
	sort(b, b + idx2);
	for(int i = 0; i < idx1; i++){
		cout << 1 << ' ' << a[i].x << ' ' << x3[a[i].y] << '\n';
	}
	for(int i = 0; i < idx2; i++){
		cout << 2 << ' ' << b[i].x << ' ' << x3[b[i].y] << ' ' << b[i].z << '\n';
	}
	return 0;
}

Subtask4

留给是正解但没有离散化的代码,正解见 Subtask7。

Subtask5

这个子任务保证豌豆射手不超过 1010 个,考虑每次遍历所有豌豆射手,然后查询每个豌豆射手的下一个僵尸并将这个僵尸的血量减 11,维护血量异或和可以在僵尸加进去时答案异或血量,把僵尸血量减 11 前再异或一次,这样就可以把这个贡献抵消,再异或减 11 后的血量,时间复杂度 O(qlog⁡q)\mathcal{O}(q \log q)。

参考代码4
C++
#include<iostream>
#include<algorithm>
#include<set>
#include<map>

using namespace std;

const int N = 1e5 + 10;

int q, op, x, y, z, cnt, ans, idx, idx1, idx2, idx3 = 1;

struct pii{
	int x, z;
	
	bool operator < (const pii & o) const{
		if(x == o.x) return z < o.z;
		return x < o.x;
	}
};
set<pii> st1, st2[N];

struct thing{
	int x, y, z;
	
	bool operator < (const thing & o) const{
		if(x == o.x) return y < o.y;
		return x < o.x;
	}
} a[N], b[N], c[N];

set<int> x1; map<int, int> x2, x3;

int main(){
	cin >> q;
	for(int i = 0; i < q; i++){
		cin >> op >> x >> y;
		if(op == 1) c[i].x = x, c[i].y = y, c[i].z = -1;
		else{
			cin >> z;
			c[i].x = x, c[i].y = y, c[i].z = z;
		}
		x1.insert(y);
	}
	for(int i: x1){
		x3[idx3] = i; x2[i] = idx3++;
	}
	for(int i = 0; i < q; i++){
		c[i].y = x2[c[i].y];
	}
	for(int i = 0; i < q; i++){
		if(c[i].z == -1){
			st1.insert({c[i].x, c[i].y});
		}
		else{
			st2[c[i].y].insert({c[i].x, c[i].z});
			ans ^= c[i].z;
		}
		for(auto j: st1){
			auto it = st2[j.z].upper_bound({j.x, 0});
			if(it == st2[j.z].end()) continue;
			st2[j.z].erase(it);
			x = (*it).x, z = (*it).z;
			ans ^= z;
			z--;
			ans ^= z;
			if(z > 0) st2[j.z].insert({x, z});
		}
		cout << ans << '\n';
	}
	for(auto i: st1){
		a[idx1].x = i.x; a[idx1++].y = i.z;
	}
	for(int i = 1; i <= 100000; i++){
		for(auto j: st2[i]){
			b[idx2].x = j.x; b[idx2].y = i; b[idx2++].z = j.z;
		}
	}
	sort(a, a + idx1);
	sort(b, b + idx2);
	for(int i = 0; i < idx1; i++){
		cout << 1 << ' ' << a[i].x << ' ' << x3[a[i].y] << '\n';
	}
	for(int i = 0; i < idx2; i++){
		cout << 2 << ' ' << b[i].x << ' ' << x3[b[i].y] << ' ' << b[i].z << '\n';
	}
	return 0;
}

Subtask7

现在植物和僵尸的数量都不少,怎么办呢?

观察到 z≤50z \le 50,也就是僵尸的血量是小于 5050,考虑只有在僵尸受到攻击时才遍历到僵尸。

具体的,先对所有物体的 x 坐标和 y 坐标离散化,然后用树状数组维护每一行豌豆射手数量的前缀和,树状数组可以用 vector 来节省空间。

由于最大有 10510^5 行,所以我们不能每次都遍历行,只能遍历能被豌豆攻击的僵尸所在的行,所以我们要维护这个集合。

对于每一个时间段,放置物品后查询物品这一列是否有能被豌豆攻击的僵尸,具体的,可以判断第一个豌豆射手是否在最后一个僵尸的后面,是就说明有能被豌豆攻击的僵尸,把它加进待遍历的集合中。

遍历地图时,只遍历在集合中的一行,先遍历第一个豌豆射手,再遍历这个豌豆射手后面的第一个僵尸,这样是为了防止前面未被豌豆射手攻击的僵尸被遍历到,浪费时间,然后如果这个僵尸能接住所有的攻击,就遍历这个僵尸后面的第一个豌豆射手,否则就遍历下一个僵尸,这样,每个遍历到的僵尸都是受到攻击的,一个僵尸最多遍历 5050 次,所以时间复杂度是 O(qzlog⁡q)\mathcal{O}(qz \log q)。

参考代码5
C++
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>

using namespace std;

const int N = 1e5 + 10;
int q, mp_y2[N], hpans = 0;
set<int> st_y;
map<int, int> mp_y, mp_fy;

struct stu{
	int opt, x, y, z;
	
	bool operator < (const stu & o) const{
		if(opt == o.opt){
			if(x == o.x) return y < o.y;
			return x < o.x; 
		}
		return opt < o.opt;
	}
} a[N]; vector<stu> ans;

struct line{
	set<int> st_x; int mx_x;
	map<int, int> mp_x, mp_fx;
};
vector<line> vec;

struct zombie{
	int y, z;
	
	bool operator < (const zombie & o) const{
		return y < o.y;
	}
};

struct group{
	set<int> p;
	set<zombie> z;
	vector<int> pn;
	
	int get(int x){
		int res = 0; x++;
		while(x){
			res += pn[x];
			x -= x & -x;
		}
		return res;
	}
	
	void add(int x){
		x++;
		while(x < pn.size()){
			pn[x]++;
			x += x & -x;
		}
	}
};
vector<group> gp;
set<int> hvzb; vector<int> nozb;

bool is_hvzb(int y){
	if(gp[y].z.empty() || gp[y].p.empty()) return 0;
	auto it = gp[y].z.end();
	it--;
	return (*it).y > (*gp[y].p.begin());
}

int main(){
	cin >> q;
	for(int i = 0; i < q; i++){
		cin >> a[i].opt >> a[i].x >> a[i].y;
		st_y.insert(a[i].y);
		if(a[i].opt == 2) cin >> a[i].z;
	}
	int idx = 0;
	for(int i: st_y) {mp_fy[idx] = i; mp_y[i] = idx++;}
	for(int i = 0; i < q; i++) a[i].y = mp_y[a[i].y];
	for(int i = 0; i < idx; i++) vec.push_back({{}, 0, {}});
	for(int i = 0; i < q; i++) vec[a[i].y].st_x.insert(a[i].x);
	for(int i = 0; i < idx; i++){
		int idx2 = 0;
		for(int j: vec[i].st_x) {vec[i].mp_fx[idx2] = j; vec[i].mp_x[j] = idx2++;}
		vec[i].mx_x = idx2;
	}
	for(int i = 0; i < q; i++) a[i].x = vec[a[i].y].mp_x[a[i].x];
	for(int i = 0; i < idx; i++){
		gp.push_back({{}, {}, {}});
		for(int j = 0; j <= vec[i].mx_x; j++) gp[i].pn.push_back(0);
	}
	for(int i = 0; i < q; i++){
		if(a[i].opt == 1){
			gp[a[i].y].p.insert(a[i].x);
			gp[a[i].y].add(a[i].x);
			if(is_hvzb(a[i].y)) hvzb.insert(a[i].y);
		}
		else{
			gp[a[i].y].z.insert({a[i].x, a[i].z});
			if(is_hvzb(a[i].y)) hvzb.insert(a[i].y);
			hpans ^= a[i].z;
		}
		for(int gety: hvzb){
			int ft = -1, x = 0, nz = 0, num; bool fg = 0;
			while(1){
				auto it = gp[gety].p.upper_bound(ft);
				if(it == gp[gety].p.end()) break;
				ft = *it;
				while(1){
					auto it2 = gp[gety].z.upper_bound({ft, 0});
					if(it2 == gp[gety].z.end()) {fg = 1; break;}
					ft = (*it2).y; nz = (*it2).z;
					num = gp[gety].get(ft);
					hpans ^= nz;
					gp[gety].z.erase(it2);
					if(nz > num - x){
						hpans ^= nz - (num - x);
						gp[gety].z.insert({ft, nz - (num - x)});
						x = num;
						break;
					}
					else x += nz;
				}
				if(fg) break;
			}
			if(!is_hvzb(gety)) nozb.push_back(gety);
		}
		for(int gety: nozb) hvzb.erase(gety);
		nozb.clear();
		cout << hpans << '\n';
	}
	for(int i = 0; i < idx; i++){
		for(int j: gp[i].p){
			ans.push_back({1, vec[i].mp_fx[j], mp_fy[i], 0});
		}
	}
	for(int i = 0; i < idx; i++){
		for(zombie j: gp[i].z){
			ans.push_back({2, vec[i].mp_fx[j.y], mp_fy[i], j.z});
		}
	}
	sort(ans.begin(), ans.end());
	for(stu i: ans){
		cout << i.opt << ' ' << i.x << ' ' << i.y;
		if(i.opt == 2) cout << ' ' << i.z;
		cout << '\n';
	}
	return 0;
}