玄学样例RE,提交AC+WA+RE
求求了,也就二百多行
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pr;
inline pr mp(ll x, ll y) {
return make_pair(x, y);
}
const ll MAXN = 256;
ll n, op, k, addr_in;
string s, ti, ni, in1[MAXN], in2[MAXN];
struct type {
string name;
vector<type*> member_type;
vector<string> member_name;
ll memory_size;
ll memory_align;
inline void clear() {
name.clear();
member_type.clear();
member_name.clear();
memory_size = memory_align = 0;
}
};
type def_type[MAXN];
ll type_num;
map<string, type*> name_to_type;
struct element {
type ele_type;
string name;
ll address;
inline void clear() {
ele_type.clear();
name.clear();
address = 0;
}
};
element def_ele[MAXN];
ll ele_num;
map<string, element*> name_to_ele;
ll addr_pos;
map<pr, element*> addr_to_ele;
inline void init() {
def_type[1].name = "byte";
def_type[2].name = "short";
def_type[3].name = "int";
def_type[4].name = "long";
def_type[1].memory_size = def_type[1].memory_align = 1;
def_type[2].memory_size = def_type[2].memory_align = 2;
def_type[3].memory_size = def_type[3].memory_align = 4;
def_type[4].memory_size = def_type[4].memory_align = 8;
name_to_type["byte"] = &def_type[1];
name_to_type["short"] = &def_type[2];
name_to_type["int"] = &def_type[3];
name_to_type["long"] = &def_type[4];
type_num = 4;
}
inline void op1(string type_name, ll num, string *ar1, string *ar2) {
type new_type;
new_type.clear();
new_type.name = type_name;
for (int i = 1; i <= num; i++) {
new_type.member_type.push_back(name_to_type[ar1[i]]);
new_type.member_name.push_back(ar2[i]);
}
ll pos = 0;
vector<type*>::iterator it = new_type.member_type.begin();
for (; it != new_type.member_type.end(); it++) {
ll ailgn = (*it) -> memory_align;
new_type.memory_align = max(new_type.memory_align, ailgn);
if (pos % (*it) -> memory_align)
pos = (pos / (*it) -> memory_align + 1) * (*it) -> memory_align;
pos += (*it) -> memory_size;
}
if (pos % new_type.memory_align)
pos = (pos / new_type.memory_align + 1) * new_type.memory_align;
new_type.memory_size = pos;
def_type[++type_num] = new_type;
name_to_type[type_name] = &def_type[type_num];
}
inline void op2(string type_name, string ele_name) {
element new_ele;
new_ele.clear();
ll size = name_to_type[type_name] -> memory_size;
if (addr_pos % name_to_type[type_name] -> memory_align)
addr_pos = (addr_pos / name_to_type[type_name] -> memory_align + 1) * addr_pos;
new_ele.address = addr_pos;
new_ele.ele_type = *name_to_type[type_name];
new_ele.name = ele_name;
def_ele[++ele_num] = new_ele;
name_to_ele[ele_name] = &def_ele[ele_num];
addr_to_ele[mp(addr_pos, addr_pos + size - 1)] = &def_ele[ele_num];
addr_pos += size;
}
inline ll op3(string ele_name) {
queue<string> name;
string tool;
tool.clear();
for (int i = 0; i < (ll)ele_name.length(); i++) {
if (ele_name[i] == '.') {
name.push(tool);
tool.clear();
} else
tool += ele_name[i];
}
name.push(tool);
ll pos = name_to_ele[name.front()] -> address;
type type = name_to_ele[name.front()] -> ele_type;
name.pop();
while (!name.empty()) {
string Name = name.front();
name.pop();
auto it1 = type.member_name.begin();
auto it2 = type.member_type.begin();
for (; it1 != type.member_name.end(); it1++, it2++) {
if (pos % (*it2) -> memory_align)
pos = (pos / (*it2) -> memory_align + 1) * (*it2) -> memory_align;
if (*it1 == Name) {
type = **it2;
break;
} else
pos += (*it2) -> memory_size;
}
}
return pos;
}
inline string op4(ll addr) {
if (addr > addr_pos)
return "ERR";
element ele;
for (auto it : addr_to_ele) {
if (addr < it.first.first || addr > it.first.second)
continue;
ele = *(it.second);
break;
}
ll pos_goal = addr - ele.address, pos_s = 0, pos_t;
type typ = ele.ele_type;
string ele_name;
ele_name.clear();
ele_name = ele.name;
while (!typ.member_name.empty()) {
auto it1 = typ.member_type.begin();
auto it2 = typ.member_name.begin();
for (; it1 != typ.member_type.end(); it1++, it2++) {
pos_t = pos_s + (*it1) -> memory_size;
if (pos_goal < pos_t && pos_goal >= pos_s) {
ele_name += '.' + *it2;
typ = **it1;
break;
} else {
if (it1 + 1 == typ.member_type.end())
return "ERR";
ll align = (*(it1 + 1)) -> memory_align;
if (pos_t % align)
pos_t = (pos_t / align + 1) * align;
if (pos_goal < pos_t)
return "ERR";
pos_s = pos_t;
}
}
}
if (ele_name == "")
return "ERR";
return ele_name;
}
inline void work() {
cin >> n;
while (n--) {
cin >> op;
if (op == 1) {
cin >> s >> k;
for (int i = 1; i <= k; i++)
cin >> in1[k] >> in2[k];
op1(s, k, in1, in2);
cout << def_type[type_num].memory_size << ' ' << def_type[type_num].memory_align << endl;
} else if (op == 2) {
cin >> ti >> ni;
op2(ti, ni);
cout << def_ele[ele_num].address << endl;
} else if (op == 3) {
cin >> s;
cout << op3(s) << endl;
} else {
cin >> addr_in;
cout << op4(addr_in) << endl;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
init();
work();
return 0;
}