社区讨论

S组t3全re,为什么

学术版参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@lo2qxmf3
此快照首次捕获于
2023/10/23 18:19
2 年前
此快照最后确认于
2023/11/02 11:42
2 年前
查看原帖
CPP
#include <bits/stdc++.h>
#define ll long long
#define inf 2000000000
using namespace std;

ll read()
{
	ll num = 0, w = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if(ch == '-')
			w = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		num = (num << 3) + (num << 1) + (ch - '0');
		ch = getchar();
	}
	return w * num;
}

int n;

struct Str
{
	string name;
	string ndtp[101], ndnm[101];
	ll k;
	map<string, string> nodtp;
	map<string, ll> nmid;
	ll stseg[101];
} str[110];

struct Node
{
	string nodetp, nodename;
	ll st;
} node[110];

map<string, ll> strid, strmaxseg, strleng, nodeid;

ll nowstr, nownode, nowseg;

void add_str()
{
	ll k, id = ++nowstr;
	string s;
	
	cin >> s >> k;
	str[id].name = s;
	str[id].k = k;
	strid[str[id].name] = id;
	for(int i = 1; i <= k; i++)
	{
		string u, v;
		cin >> u >> v;
		str[id].ndtp[i] = u;
		str[id].ndnm[i] = v;
		str[id].nodtp[v] = u;
		str[id].nmid[v] = i;
		strmaxseg[s] = max(strmaxseg[s], strmaxseg[u]);
//		cout << 1;
	}
	ll tmpseg = 0;
	for(int i = 1; i <= k; i++)
	{
		string tp = str[id].ndtp[i];
		int seg = strmaxseg[tp];
		if(tmpseg % seg != 0)
			tmpseg = seg * (tmpseg / seg + 1);
		str[id].stseg[i] = tmpseg;
		tmpseg += strleng[tp];
	}
	strleng[s] = ((tmpseg - 1) / strmaxseg[s] + 1) * strmaxseg[s];
	
	printf("%lld %lld\n", strleng[s], strmaxseg[s]);
	return;
}

void add_node()
{
	printf("%lld\n", nowseg);
	string tp, nm;
	cin >> tp >> nm;
	ll id = ++nownode, seg = strmaxseg[tp];
	node[id].nodename = nm;
	node[id].nodetp = tp;
	nodeid[nm] = id;
	if(nowseg % seg != 0)
		nowseg = seg * (nowseg / seg + 1);
	node[id].st = nowseg;
	nowseg += strleng[tp];
	
}

void ask_node()
{
	string s;
	cin >> s;
	string now = "";
	ll tmpseg = 0, tmpstrid;
	string tmpstr = "";
	for(int i = 0, sz = s.size(); i < sz; i++)
	{
		if(s[i] == '.')
		{
			if(tmpstr == "")
			{
				ll id = nodeid[now];
				tmpstr = node[id].nodetp;
				tmpseg = node[id].st;
				tmpstrid = strid[tmpstr];
			}
			else 
			{
				ll id = str[tmpstrid].nmid[now];
				tmpstr = str[tmpstrid].ndtp[id];
				tmpseg += str[tmpstrid].stseg[id];
				tmpstrid = strid[tmpstr];
			}
			now = "";
		}
		else now += s[i];
	}
	
	ll id = str[tmpstrid].nmid[now];
	tmpstr = str[tmpstrid].ndtp[id];
	tmpseg += str[tmpstrid].stseg[id];
	tmpstrid = strid[tmpstr];
	
	
	printf("%lld\n", tmpseg);
	return;
}

void pre()
{
	strleng["byte"] = 1;
	strleng["short"] = 2;
	strleng["int"] = 4;
	strleng["long"] = 8;
	strmaxseg["byte"] = 1;
	strmaxseg["short"] = 2;
	strmaxseg["int"] = 4;
	strmaxseg["long"] = 8;
	strid["byte"] = 101;
	strid["short"] = 102;
	strid["int"] = 103;
	strid["long"] = 104;
	
}

void ask_pos()
{
	ll addr;
	cin >> addr;
	
	if(addr >= nowseg)
	{
		printf("ERR\n");
		return;
	}
	
	string ans[101];
	int tmpansid = 0;
	
	ll l = 1, r = nownode;
	ll tmpndid = 0;
	while(l <= r)
	{
		ll mid = (l + r) >> 1;
		if(node[mid].st <= addr)
		{
			tmpndid = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	ans[++tmpansid] = node[tmpndid].nodename;
	string tmpstr = node[tmpndid].nodetp, tmpnm;
	ll tmpstrid = strid[tmpstr], tmppos = node[tmpndid].st;
	//cout << tmppos;
	while(tmpstrid <= 100)
	{
		int l = 1, r = str[tmpstrid].k, anss;
		while(l <= r)
		{
			int mid = (l + r) >> 1;
			if(tmppos + str[tmpstrid].stseg[mid] < addr)
			{
				anss = mid;
				l = mid + 1;
			}
			else r = mid - 1;
		}
		tmppos += str[tmpstrid].stseg[anss];
		tmpnm = str[tmpstrid].ndnm[anss];
		tmpstr = str[tmpstrid].ndtp[anss];
		tmpstrid = strid[tmpstr];
		ans[++tmpansid] = tmpnm;
		//cout << strid[tmpstr];
		//return;
	}
	if(tmppos + strmaxseg[tmpstr] - 1 < addr)
	{
		printf("ERR\n");
		return;
	}
	for(int i = 1; i <= tmpansid; i++)
	{
		cout << ans[i];
		if(i != tmpansid) cout << '.';
	}
	cout << "\n";
}

int main()
{
	n = read();
	pre();
	while(n--)
	{
		int opt = read();
		if(opt == 1) add_str();
		else if(opt == 2) add_node();
		else if(opt == 3) ask_node();
		else if(opt == 4) ask_pos();
	}
	
	return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...