大样例#3 WA,推测是操作 2 和 4 有问题。
#include <cstring>
#include <map>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
using namespace std;
using ll = long long;
const int MAXN = 105;
char buf[105];
struct Member
{
int type;
ll shift;
// string name;
};
struct Type
{
Type(int _s, int _a) : size(_s), align(_a) {}
// string name;
ll size;
int align;
map<string, Member> memb;
bool isBasic() { return memb.size() == 0; }
};
vector<Type> tps;
map<string, int> tpIdx;
map<string, Member> vars;
ll totMem = 0;
inline void opDefType(string &name, int k)
{
Type t(0, 0);
ll tot = 0;
for (int i = 1; i <= k; i++)
{
Member m;
scanf("%s", buf);
m.type = tpIdx[string(buf)];
scanf("%s", buf);
string mn(buf);
Type &mt = tps[m.type];
tot = (tot + mt.align - 1) / mt.align * mt.align;
m.shift = tot;
t.memb[mn] = m;
tot += mt.size;
t.align = max(t.align, mt.align);
}
t.size = (tot + t.align - 1) / t.align * t.align;
tps.push_back(t);
tpIdx[name] = tps.size() - 1;
printf("%lld %d\n", t.size, t.align);
}
inline void opDefVar(int type, string &name)
{
Member m;
m.type = type;
// m.name = name;
int al = tps[m.type].align;
int sz = tps[m.type].size;
totMem = (totMem + al - 1) / al * al;
m.shift = totMem;
vars[name] = m;
printf("%lld\n", totMem);
totMem += sz;
}
inline void opQueryVar(vector<string> &path)
{
map<string, Member> *vl = &vars;
ll tot = 0;
for (string seg : path)
{
tot += (*vl)[seg].shift;
Type &t = tps[(*vl)[seg].type];
vl = &t.memb;
}
printf("%lld\n", tot);
}
pair<bool, string> dfs(map<string, Member> &vl, ll addr)
{
string path;
bool flag = false;
for (auto p : vl)
{
if (addr == p.second.shift)
{
if (tps[p.second.type].isBasic())
{
flag = true;
path = p.first;
}
break;
}
else if (addr > p.second.shift && addr - p.second.shift < tps[p.second.type].size)
{
if (tps[p.second.type].isBasic())
{
flag = true;
path = p.first;
}
else
{
auto res = dfs(tps[p.second.type].memb, addr - p.second.shift);
if (res.first)
{
flag = true;
path = p.first + "." + res.second;
}
}
break;
}
}
return {flag, path};
}
int n;
int main()
{
tps.push_back(Type(1, 1));
tps.push_back(Type(2, 2));
tps.push_back(Type(4, 4));
tps.push_back(Type(8, 8));
tpIdx["byte"] = 0;
tpIdx["short"] = 1;
tpIdx["int"] = 2;
tpIdx["long"] = 3;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int op;
scanf("%d", &op);
if (op == 1)
{
int k;
scanf("%s %d", buf, &k);
string name(buf);
opDefType(name, k);
}
else if (op == 2)
{
scanf("%s", buf);
int ti = tpIdx[string(buf)];
scanf("%s", buf);
string name(buf);
opDefVar(ti, name);
}
else if (op == 3)
{
scanf("%s", buf);
vector<string> path;
int len = strlen(buf);
string s;
for (int c = 0; c < len; c++)
{
if (buf[c] == '.')
{
path.push_back(s);
s.clear();
}
else
{
s.push_back(buf[c]);
}
}
path.push_back(s);
opQueryVar(path);
}
else if (op == 4)
{
ll addr;
scanf("%lld", &addr);
auto res = dfs(vars, addr);
if (!res.first)
printf("ERR\n");
else
printf("%s\n", res.second.c_str());
}
}
return 0;
}