先有请甲级战犯登场
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
int n;//0 bt,1 sho,2 int,3 long
struct OP1{
string nm;
int num;
vector<pair<string,string> > meb;
int len;
};
map<string,OP1> stru;
map<string,int> ll;
map<string,int> dx;
map<string,int> qd;
int yd;
vector<pair<string,string> > all;
pair<string,string> brk(string jx){
string head,res;
for(auto v:jx){
if(v=='.')break;
head+=v;
}
res=jx.erase(0,head.size());
if(res.size())res.erase(0,1);
return {head,res};
}
bool ckjb(string tp){
if(tp=="byte"||tp=="short"||tp=="int"||tp=="long")return 1;
return 0;
}
int place(string tp,string qz,int st){
int lll=ll[tp];
if(st%lll!=0){
st=st-st%lll;
st+=lll;
}
qd[qz]=st;
st+=dx[tp];
return st;
}
int depin(string tp,string jx,string qz,int st){
string head,res;
pair<string,string> as;
as=brk(jx);
head=as.first,res=as.second;
int lsyd=st,lst=0;
if(head.size()){
OP1 now=stru[tp];
for(int i=0;i<now.meb.size();i++){
lsyd=place(now.meb[i].first,qz+'.'+now.meb[i].second,lsyd);
lst=dx[now.meb[i].first];
if(now.meb[i].second==head){
int zhi=0;
if(res.size())return depin(now.meb[i].first,res,qz+'.'+now.meb[i].second,qd[qz+'.'+now.meb[i].second]);
else{
int lll=ll[tp];
zhi=lsyd-dx[now.meb[i].first];
}
return zhi;
}
}
}
int lll=ll[tp];
lsyd=ceil(lsyd * 1.0 / lll) * lll;
return lsyd;
}
void find(int pos,string chuan,string tp){
if(ckjb(tp)){
int st=qd[chuan],len=dx[tp];
if(st<=pos&&pos<=st+len){
cout<<chuan<<'\n';
return;
}
cout<<"ERR"<<'\n';
return;
}
OP1 now=stru[tp];
int lsyd=qd[chuan];
for(int i=0;i<now.meb.size();i++){
lsyd=place(now.meb[i].first,chuan+'.'+now.meb[i].second,lsyd);
if(pos<lsyd&&pos>=qd[chuan+'.'+now.meb[i].second]){
find(pos,chuan+'.'+now.meb[i].second,now.meb[i].first);
return;
}
}
cout<<"ERR"<<'\n';
}
signed main(){
cin>>n;
ll["byte"]=1,ll["short"]=2,ll["int"]=4,ll["long"]=8;
dx["byte"]=1,dx["short"]=2,dx["int"]=4,dx["long"]=8;
for(int i=1;i<=n;i++){
int op;
cin>>op;
if(op==1){
string nm;
int k;
cin>>nm;
cin>>k;
OP1 op1;
op1.nm=nm;
op1.num=k;
int maxx=0,lsyd=0;
int pre=0;
for(int j=1;j<=k;j++){
string tp;
string nn;
cin>>tp>>nn;
int lll=ll[tp];
lsyd=ceil((lsyd+pre) * 1.0 / lll) * lll;
pre=dx[tp];
maxx=max(maxx,lll);
op1.meb.emplace_back(tp,nn);
}
ll[nm]=maxx;
lsyd=ceil((lsyd+pre) * 1.0 / maxx) * maxx;
dx[nm]=lsyd;
cout<<lsyd<<' '<<maxx<<'\n';
op1.len=maxx;
stru[nm]=op1;
}else if(op==2){
string tp;
string nn;
cin>>tp>>nn;
int lll=ll[tp];
yd=ceil(yd * 1.0 / lll) * lll;
cout<<yd<<'\n';
qd[nn]=yd;
yd+=dx[tp];
all.push_back({tp,nn});
}else if(op==3){
string jx;
cin>>jx;
string head,res;
pair<string,string> as;
as=brk(jx);
head=as.first,res=as.second;
for(auto v:all){
if(v.second==head){
if(res.size())cout<<depin(v.first,res,head,qd[head])<<'\n';
else cout<<qd[head]<<'\n';
}
}
}else{
int pos;
cin>>pos;
bool flag=0;
for(auto v:all){
if(qd[v.second]+dx[v.first]>pos&&pos>=qd[v.second]){
find(pos,v.second,v.first);
flag=1;
break;
}
}
if(!flag)cout<<"ERR"<<'\n';
}
}
return 0;
}代码简单,建议降红
回归正题
初遇此题是在CSP-2023考场上,当时看到了,感觉是水题,就直接开始写65pts无深度嵌套的暴力分,调了两个多小时,下考前20min终于调完。只可惜对于操作三的理解出现偏差,以为只会查最内层元素,痛失65pts(就加了8个字符就有了啊。。。)。
考完再看,开始写正解,思路很简单,就是查一个元素时递归,找到就深入,加入元素时也不必加到底,感觉像线段树懒标记?就是只有查询时才”pushdown”唛。
只花了四十分钟写完,不过此时我还没有意识到事情的严重性,一个5 char的错误正在酝酿,将折磨我两天。。。
代码解析
part.1 准备工作
struct OP1{
string nm;//名称
int num;//成员数量
vector<pair<string,string> > meb;//成员
int len; //大小
};//保存结构体信息
map<string,OP1> stru;//存下来
map<string,int> ll;//某个类型的对齐标准(按类型名称查找,下同)
map<string,int> dx;//某个类型的大小
map<string,int> qd;//某个元素的起点(按层叠名称查找)
int yd;//全局的内存分配右界
vector<pair<string,string> > all;//浅层的元素(全局元素)
part.2功能模块
pair<string,string> brk(string jx){//将一个层叠形式的字符串拆分(a.b.c -> a b.c)
string head,res;//拆出来的东西和剩下的东西
for(auto v:jx){
if(v=='.')break;
head+=v;
}
res=jx.erase(0,head.size());
if(res.size())res.erase(0,1);
return {head,res};
}
bool ckjb(string tp){//检查是不是基本元素
if(tp=="byte"||tp=="short"||tp=="int"||tp=="long")return 1;
return 0;
}
int place(string tp,string qz,int st){//pushdown,放置一个元素到内存
int lll=ll[tp];//对齐要求
st=ceil(st*1.0/lll)*lll;//手模即可理解
qd[qz]=st;//记录起点
st+=dx[tp];//占据了一段空间(大小)
return st;//返回这个元素被放置以后内存的右界
}part.3 功能实现
op1
if(op==1){
string nm;//名称
int k;//成员数量
cin>>nm;
cin>>k;
OP1 op1;
op1.nm=nm;
op1.num=k;
int maxx=0,lsyd=0;//分别计算的是对齐要求和大小
for(int j=1;j<=k;j++){
string tp;
string nn;
cin>>tp>>nn;
int lll=ll[tp];//查找这个成员的对齐要求
lsyd=ceil(lsyd*1.0/lll)*lll;//对齐成员
lsyd+=dx[tp];//放置
maxx=max(maxx,lll);
op1.meb.emplace_back(tp,nn);
}
ll[nm]=maxx;//记录对齐要求
lsyd=ceil(lsyd*1.0/maxx)*maxx;//尾对齐,见题面的样例解释
dx[nm]=lsyd;//记录大小
cout<<lsyd<<' '<<maxx<<'\n';
op1.len=maxx;
stru[nm]=op1;//保存
}op2
else if(op==2){//在全局存下来即可
string tp;
string nn;
cin>>tp>>nn;
int lll=ll[tp];
yd=ceil(yd*1.0/lll)*lll;
cout<<yd<<'\n';
qd[nn]=yd;
yd+=dx[tp];
all.push_back({tp,nn});
}op3
else if(op==3){
string jx;
cin>>jx;
string head,res;
pair<string,string> as;
as=brk(jx);//解析
head=as.first,res=as.second;
for(auto v:all){//先在全局查找
if(v.second==head){
if(res.size())cout<<depin(v.first,res,head,qd[head])<<'\n';//深入查找
else cout<<qd[head]<<'\n';
}
}
}
//depin函数
int depin(string tp,string jx,string qz,int st){
string head,res;
pair<string,string> as;
as=brk(jx);
head=as.first,res=as.second;//解析出当前要找的东西
int lsyd=st,lst=0;//这个结构体的起点
if(head.size()){
OP1 now=stru[tp];
for(int i=0;i<now.meb.size();i++){//逐个pushdown直到找到所求的东西
lsyd=place(now.meb[i].first,qz+'.'+now.meb[i].second,lsyd);//放置,并更新地址
lst=dx[now.meb[i].first];
if(now.meb[i].second==head){
int zhi=0;
if(res.size())return depin(now.meb[i].first,res,qz+'.'+now.meb[i].second,qd[qz+'.'+now.meb[i].second]);//还有就继续深入
else{
int lll=ll[tp];
zhi=lsyd-dx[now.meb[i].first];//由于这个是加了一个大小的终点地址,所以查起点要从终点减去大小
}
return zhi;
}
}
}
int lll=ll[tp];
lsyd=ceil(lsyd * 1.0 / lll) * lll;
return lsyd;
}op4
else{//实现思路和op3基本一致,只讲一些细节
int pos;
cin>>pos;
bool flag=0;
for(auto v:all){
if(qd[v.second]+dx[v.first]>pos&&pos>=qd[v.second]){//注意一定要两头判断,否则有可能此处是虚空
find(pos,v.second,v.first);
flag=1;
break;
}
}
if(!flag)cout<<"ERR"<<'\n';
}
void find(int pos,string chuan,string tp){
if(ckjb(tp)){
int st=qd[chuan],len=dx[tp];
if(st<=pos&&pos<=st+len){//同上
cout<<chuan<<'\n';
return;
}
cout<<"ERR"<<'\n';
return;
}
OP1 now=stru[tp];
int lsyd=qd[chuan];
for(int i=0;i<now.meb.size();i++){
lsyd=place(now.meb[i].first,chuan+'.'+now.meb[i].second,lsyd);
if(pos<lsyd&&pos>=qd[chuan+'.'+now.meb[i].second]){
find(pos,chuan+'.'+now.meb[i].second,now.meb[i].first);
return;
}
}
cout<<"ERR"<<'\n';
}完结撒花!
wait,还有一件事。到底是什么折磨了我两天?原因是我使用了一个从名称映射到类型的map,只在全局使用,按理来说不会出现问题,但可能一些奇奇怪怪的原因重名了,这个bug找了好久啊。。。
另:附上 @zymool 大佬写的对拍数据生成器
// Author:zymooll
#include<bits/stdc++.h>
#if defined(__linux__)
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#elif defined(_WIN32)
#define getchar _getchar_nolock
#define putchar _putchar_nolock
#endif
#define int long long
using namespace std;
const int n = 100;
const int k = 100;
const int slen = 10;
const bool A = 0;
const bool B = 0;
const bool C = 0;
const bool D = 0;
int tot1 = 0;//记录 1 操作的总次数
map<string, int>mp;//结构体名称映射
map<string, bool>vis;//标记成员的出现情况
struct S{
string nam;
vector<pair<int, string> >mem;
};
vector<S>stru;//记录随机出的
random_device rd;
mt19937_64 mt;
void init(){
mt.seed(rd());
stru.push_back((S){ "", vector<pair<int, string> >() });
stru.push_back((S){ "byte", vector<pair<int, string> >() }); mp["byte"] = 1;
stru.push_back((S){ "short", vector<pair<int, string> >() }); mp["short"] = 2;
stru.push_back((S){ "int", vector<pair<int, string> >() }); mp["int"] = 3;
stru.push_back((S){ "long", vector<pair<int, string> >() }); mp["long"] = 4;
}
int getOperation(){//获取随机的操作
if(A || (B && tot1)){
if(stru[0].mem.empty()){//2,4
int ret = mt() % 2;
if(ret == 0)ret = 2;
else ret = 4;
return ret;//define element
}
else{//2,3,4
return 2 + mt() % 3;
}
}
else if(stru[0].mem.empty()){//1,2,4
int ret = 1 + mt() % 2;
if(ret == 3)ret = 4;
if(ret == 1)tot1++;
return ret;
}
else{//1,2,3,4
int ret = 1 + mt() % 4;
if(ret == 1)tot1++;
return ret;
}
}
int getType(bool _instru){//获取随机的元素,其中返回值代表结构体 id (_instru 代表是否获取的是结构体内元素)
if(D)return 4 + mt() % (stru.size() - 4);
if(C && _instru)return 1 + mt() % 4;
return 1 + mt() % (stru.size() - 1);
}
string getRamdomString(){//获得一个未出现过的字符串
string ret;
for(int i = 0; i < slen; i++){
ret += 'a' + mt() % 26;
}
while(mp[ret] || vis[ret]){
int pos = mt() % slen;
ret[pos] = 'a' + mt() % 26;
}
return ret;
}
signed main(){
// freopen(".in", "r", stdin);
freopen("data.out", "w", stdout);
init();
cout << n << endl;
for(int kk = 1; kk <= n; kk++){
int opt = getOperation();
cout << opt << " ";
// cerr << kk << " " << opt << endl;
if(opt == 1){
vis.clear();
S ls;//临时
string name = getRamdomString();
int num = 1 + mt() % k;
cout << name << " " << num << endl;
ls.nam = name;
for(int i = 1; i <= num; i++){
int type = getType(true);
string elename = getRamdomString();
vis[elename] = 1;
cout << stru[type].nam << " " << elename << endl;
ls.mem.push_back(make_pair(type, elename));
}
stru.push_back(ls);
}
else if(opt == 2){
int type = getType(false);
string name = getRamdomString();
stru[0].mem.push_back(make_pair(type, name));
cout << stru[type].nam << " " << name << endl;
}
else if(opt == 3){
int u = 0;
string outp = "";
while(1){
int pos = mt() % stru[u].mem.size();
if(u)outp += ".";
outp += stru[u].mem[pos].second;
// cerr << " - " << pos << " " << outp << endl;
if(stru[u].mem[pos].first <= 4){//是基本元素
break;
}
u = stru[u].mem[pos].first;
}
cout << outp << endl;
}
else{
cout << mt() % (8 * kk) << endl;
}
cout << endl;
}
return 0;
}