[CSP-S 2023] 结构体

题目大意

模拟定义结构体、元素,并查询某个元素的地址或某个地址的元素。

题解

直接模拟,挺难调的。

代码

考场写了个屎山。

inline ll Read() {
	ll x = 0, f = 1;
	char c = getchar();
	while (c != '-' && (c < '0' || c > '9')) c = getchar();
	if (c == '-') f = -f, c = getchar();
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
	return x * f;
}

namespace Main {
	int n, m;
	ll fcur;
	vector<string> empS, tmpS, varn, vart;
	vector<ll> empL, tmpL, varst, varsz;
	struct node {
		map<string, int> id;
		vector<string> name, type;
		vector<ll> sta, siz;
		ll fsiz, mx;
		bool basic;
	};
	map<string, int> empID, varid;
	map <string, node> type;
	int main () {
		n = Read();
		type["long"] = (node) {empID, empS, empS, empL, empL, 8, 8, 1};
		type["int"] = (node) {empID, empS, empS, empL, empL, 4, 4, 1};
		type["short"] = (node) {empID, empS, empS, empL, empL, 2, 2, 1};
		type["byte"] = (node) {empID, empS, empS, empL, empL, 1, 1, 1};
		for (int i = 1; i <= n; i++) {
			int op = Read();
			if (op == 1) {
				node newone = (node) {empID, empS, empS, empL, empL, 0, 0, 0};
				string s; int k;
				cin >> s >> k;
				ll cur = 0;
				for (int j = 0; j < k; j++) {
					string t, st;
					cin >> t >> st;
					newone.name.push_back(st);
					newone.type.push_back(t);
					ll Siz = type[t].fsiz, Mx = type[t].mx;
					if (cur % Mx) cur = cur - cur % Mx + Mx;
					newone.sta.push_back(cur);
					newone.siz.push_back(Siz);
					newone.mx = max(newone.mx, Mx);
					newone.id[st] = j;
					cur += Siz;
				}
				if (cur % newone.mx) cur = cur - cur % newone.mx + newone.mx;
				newone.fsiz = cur;
				type[s] = newone;
				printf ("%lld %lld\n", newone.fsiz, newone.mx);
				continue;
			} 
			if (op == 2) {
				string t, st;
				cin >> t >> st;
				varn.push_back(st);
				vart.push_back(t);
				ll Siz = type[t].fsiz, Mx = type[t].mx;
				if (fcur % Mx) fcur = fcur - fcur % Mx + Mx;
				printf("%lld\n", fcur);
				varst.push_back(fcur);
				varsz.push_back(Siz);
				fcur += Siz;
				varid[st] = m++;
				continue;
			}
			if (op == 3) {
				string s, st = "", nowtype;
				cin >> s; s = s + '.';
				ll ans = 0;
				bool fir = 1;
				for (int i = 0; i < s.size(); i++) {
					if (s[i] == '.') {
						if (fir) {
							int id = varid[st];
							ans += varst[id];
							nowtype = vart[id];
							fir = 0;
						} else {
							node nownode = type[nowtype];
							int id = nownode.id[st];
							ans += nownode.sta[id];
							nowtype = nownode.type[id];
						}
						st = "";
					} else st = st + s[i];
				}
				printf ("%lld\n", ans);
				continue;
			}
			if (op == 4) {
				ll k = Read();
				int id = lower_bound(varst.begin(), varst.end(), k) - 1 - varst.begin();
				if (id > m - 1) {puts("ERR");continue;}
				if (id + 1 <= m - 1 && varst[id + 1] == k) id++;
				if (id == -1) {puts("ERR");continue;}
				if (varst[id] + varsz[id] - 1 < k) {puts("ERR");continue;}
				string nowtype = vart[id], ans = varn[id];
				k -= varst[id];
				bool flag = 1;
				for (; !type[nowtype].basic; ) {
					node nownode = type[nowtype];
					id = lower_bound(nownode.sta.begin(), nownode.sta.end(), k) - 1 - nownode.sta.begin();
					if (id + 1 <= nownode.sta.size() - 1 && nownode.sta[id + 1] == k) id++;
					if (nownode.sta[id] + nownode.siz[id] - 1 < k) {
						puts("ERR");
						flag = 0;
						break;
					}
					nowtype = nownode.type[id]; ans = ans + "." + nownode.name[id];
					k -= nownode.sta[id];
				}
				if (flag) cout << ans << endl;
			}
		}
		return 0;
	}
}

int main () {
	freopen ("struct.in", "r", stdin);
	freopen ("struct.out", "w", stdout);
	Main::main();
	return 0;
}
EOF

评论

暂无评论

发表评论

可以用@mike来提到mike这个用户,mike会被高亮显示。如果你真的想打“@”这个字符,请用“@@”。

博客信息

作者
Jayun
时间
2023-10-23 07:34:57
博客类型
标签