Subtask1
因为题目保证豌豆射手和僵尸的坐标互不相同,所以可以用一个二维数组 来存第 行第 列的植物或僵尸状态,当 时是豌豆射手,当 时表示场上存在一个血量为 的僵尸,每次放置后,遍历整个地图,每一列当遇到植物时就增加一个豌豆,遇到僵尸后就拿剩余的豌豆攻击僵尸,时间复杂度 。
参考代码1
#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
这个子任务的地图是 ,所以遍历整张地图肯定是不行的,考虑仅遍历有植物或有僵尸的格子,用 set 来存每一行的状态,方法和 Subtask1 类似,时间复杂度 。
参考代码2
#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
这个子任务的地图是 ,但是植物和僵尸的总数只有 ,所以考虑对输入的 y 轴离散化,时间复杂度还是 。
参考代码3
#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
这个子任务保证豌豆射手不超过 个,考虑每次遍历所有豌豆射手,然后查询每个豌豆射手的下一个僵尸并将这个僵尸的血量减 ,维护血量异或和可以在僵尸加进去时答案异或血量,把僵尸血量减 前再异或一次,这样就可以把这个贡献抵消,再异或减 后的血量,时间复杂度 。
参考代码4
#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
现在植物和僵尸的数量都不少,怎么办呢?
观察到 ,也就是僵尸的血量是小于 ,考虑只有在僵尸受到攻击时才遍历到僵尸。
具体的,先对所有物体的 x 坐标和 y 坐标离散化,然后用树状数组维护每一行豌豆射手数量的前缀和,树状数组可以用 vector 来节省空间。
由于最大有 行,所以我们不能每次都遍历行,只能遍历能被豌豆攻击的僵尸所在的行,所以我们要维护这个集合。
对于每一个时间段,放置物品后查询物品这一列是否有能被豌豆攻击的僵尸,具体的,可以判断第一个豌豆射手是否在最后一个僵尸的后面,是就说明有能被豌豆攻击的僵尸,把它加进待遍历的集合中。
遍历地图时,只遍历在集合中的一行,先遍历第一个豌豆射手,再遍历这个豌豆射手后面的第一个僵尸,这样是为了防止前面未被豌豆射手攻击的僵尸被遍历到,浪费时间,然后如果这个僵尸能接住所有的攻击,就遍历这个僵尸后面的第一个豌豆射手,否则就遍历下一个僵尸,这样,每个遍历到的僵尸都是受到攻击的,一个僵尸最多遍历 次,所以时间复杂度是 。
参考代码5
#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;
}