30分
处理n=16
的情况
2^16=65536
,所以n=16的情况是可以暴力求解的。- 开一个数组
st[]
存储每个地址分配给了哪个用户。 - 函数
trans()
将16进制转换为10进制,在10进制下进行处理。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 65536 + 10; // 2 ^16
int n, m;
int st[N];
int trans(string str) // 16 to 10 进制
{
int res = 0;
for (int i = 0; i <= 3; i ++ )
{
char c = str[i];
int num;
if (c >= '0' && c <= '9') num = c - '0';
else num = c - '0' - 39;
res += num * pow(16, 3 - i);
}
return res;
}
// 检查地址是否可用
bool check(int l, int r, int id)
{
int len = 0;
for (int i = l; i <= r; i ++ )
{
if (st[i] && st[i] != id) return false;
if (st[i] == id) len ++ ;
}
if (len == r - l + 1) return false;
return true;
}
// 为用户分配地址
void alloc(int a, int b, int id)
{
for (int i = a; i <= b; i ++ )
st[i] = id;
}
// 检查 a~b 范围内的所有地址是否完整地分配给了某个用户
int check2(int a, int b)
{
bool flag = true;
for (int i = a; i < b; i ++ )
{
if (st[i] != st[i + 1]) flag = false;
}
if (flag) return st[a];
else return 0;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < m; i ++ )
{
int op;
cin >> op;
if (op == 1)
{
int id;
string l, r;
cin >> id >> l >> r;
int a = trans(l), b = trans(r);
if (check(a, b, id))
{
puts("YES");
alloc(a, b, id);
}
else puts("NO");
}
else if (op == 2)
{
string str;
cin >> str;
int a = trans(str);
cout << st[a] << endl;
}
else
{
string l, r;
cin >> l >> r;
int a = trans(l), b = trans(r);
cout << check2(a, b) << endl;
}
}
return 0;
}
/*
16 5
1 1 0001 ffff
2 0001
3 0002 ffff
1 2 0002 ffff
1 1 0000 ffff
*/