P16697 星际网络 II 题解
WangBoAn20150103 · · 题解
P16697 星际网络 II
题目大意
这道题的地址特别长,最长可以到 512 位,普通变量存不下。地址写法类似 IPv6,用冒号分隔十六进制。
一共三种操作:
1 id l r:用户id申请[l, r]这段地址- 区间里面有别人占用的地址 → 输出
NO - 整段全部已经是他自己的地址 → 输出
NO - 只有空闲地址,或者混着他自己的地址 → 输出
YES,整块改成他的
- 区间里面有别人占用的地址 → 输出
2 s:查询某个地址归属,没人用就输出 03 l r:判断[l, r]是不是全部属于同一个用户,是就输出编号,否则输出 0
解题思路
地址的值域大到爆炸,数组、普通线段树都没法开。我们用珂朵莉树(ODT)。
珂朵莉树的想法很简单:把连续、归属相同的地址合并成一块一块保存,不用单独存每一个地址,适合这种超大值域的区间问题。
要注意几个坑:
- 地址太长,自定义地址结构体,用字节数组存储,依靠 vector 的比较功能来判断地址大小。
- 珂朵莉树用左闭右开区间
[L, R),题目给的是闭区间,所以右端点要做地址加一,写了inc函数处理进位。 - 输入的字符串不能直接比较大小,要解析转换成我们定义的地址格式,不然大小关系会出错。
- 判断申请是否合法时,一旦发现别人的地址就直接 break,不再继续遍历,这个剪枝是防止超时、拿到满分的关键。
具体做法
- 地址解析:把冒号隔开的十六进制字符串,转成程序可以识别的字节数组。
split分裂:珂朵莉树所有操作的基础,在指定位置切开区间,保证边界对齐。- 申请地址:切开目标区间,遍历里面的块。标记有没有别人的地址、是不是全部都是自己的。符合条件就删掉原来的块,插入一块新的。
- 单点查询:分裂之后直接读取这个地址所在块的归属编号。
- 区间同色查询:拿区间第一个块的编号作为标准,依次对比后面所有块,不一样就返回 0。
- 初始化:一开始全部地址都是空闲,编号为 0。
小提示:珂朵莉树怕专门卡它的数据,这道题靠着提前 break 的剪枝,就可以顺利通过全部测试点。
完整 AC 代码
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <cstdint>
using namespace std;
struct Addr {
vector<uint8_t> v;
bool operator < (const Addr &o) const {
return v < o.v;
}
bool operator == (const Addr &o) const {
return v == o.v;
}
};
bool inc(Addr &a) {
int i = a.v.size() - 1;
while (i >= 0) {
a.v[i]++;
if (a.v[i]) return true;
i--;
}
return false;
}
Addr parse_addr(const string &s, int n) {
Addr res;
int bytes = n / 8;
res.v.resize(bytes, 0);
size_t pos = 0;
int ptr = 0;
while (pos < s.size()) {
size_t colon = s.find(':', pos);
string part;
if (colon == string::npos) {
part = s.substr(pos);
pos = s.size();
} else {
part = s.substr(pos, colon - pos);
pos = colon + 1;
}
uint16_t val = stoi(part, nullptr, 16);
res.v[ptr++] = val >> 8;
res.v[ptr++] = val & 0xff;
}
return res;
}
struct Node {
Addr l, r;
int id;
Node(Addr L, Addr R, int v) : l(L), r(R), id(v) {}
bool operator < (const Node &o) const {
return l < o.l;
}
};
set<Node> st;
auto split(Addr pos) {
auto it = st.lower_bound(Node(pos, Addr(), 0));
if (it != st.end() && it->l == pos) return it;
--it;
Addr L = it->l, R = it->r;
int v = it->id;
st.erase(it);
st.insert(Node(L, pos, v));
return st.insert(Node(pos, R, v)).first;
}
bool apply(Addr L, Addr R, int uid) {
Addr Rplus = R;
if (!inc(Rplus)) return false;
auto itr = split(Rplus);
auto itl = split(L);
bool has_other = false;
bool all_self = true;
for (auto it = itl; it != itr; ++it) {
if (it->id != 0 && it->id != uid) {
has_other = true;
break;
}
if (it->id != uid) all_self = false;
}
if (has_other || all_self) return false;
st.erase(itl, itr);
st.insert(Node(L, Rplus, uid));
return true;
}
int query_point(Addr s) {
auto it = split(s);
return it->id;
}
int query_range(Addr L, Addr R) {
Addr Rplus = R;
if (!inc(Rplus)) return 0;
auto itr = split(Rplus);
auto itl = split(L);
int res = itl->id;
for (auto it = next(itl); it != itr; ++it) {
if (it->id != res) return 0;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
int bytes = n / 8;
Addr start, end;
start.v.resize(bytes, 0);
end.v.resize(bytes, 0xff);
inc(end);
st.insert(Node(start, end, 0));
while (q--) {
int op;
cin >> op;
if (op == 1) {
int id;
string ls, rs;
cin >> id >> ls >> rs;
Addr L = parse_addr(ls, n);
Addr R = parse_addr(rs, n);
cout << (apply(L, R, id) ? "YES" : "NO") << '\n';
} else if (op == 2) {
string s;
cin >> s;
Addr a = parse_addr(s, n);
cout << query_point(a) << '\n';
} else if (op == 3) {
string ls, rs;
cin >> ls >> rs;
Addr L = parse_addr(ls, n);
Addr R = parse_addr(rs, n);
cout << query_range(L, R) << '\n';
}
}
return 0;
}