Time limit per test: 3.0 seconds
Memory limit: 1024 megabytes
在面向对象程序设计中,常常会运用到函数的扩展与重写。当一个类继承某个类的时候,它可以调用所有父类可以调用的函数。它可以声明新的函数。当新的函数签名与父类的某个函数一致时,就会发生函数的覆盖(重写)。所以,在子类的实例调用某个函数时,它会调用最近的父类(有可能是它自己)的那个函数实现。
这里我们不考虑访问权限等情况,我们只关心某个类在调用某个函数时,这个函数是在哪个类中实现的。
Input
输入具有如下格式:
np2 p3 … pnt1 a11 a12 … a1t1t2 a21 a22 … a2t2⋮tn an1 an2 … antnqu1 r1u2 r2⋮uq rq
解释与数据规模约定:
- n 表示有 n 个类,这些类从 1 到 n 编号。2≤n≤105。
- pi 表示第 i 个类的父类编号。1≤pi≤i−1。第 1 个类是所有类共同的祖先类,这个类没有父类。
- ti 表示在第 i 个类中定义了多少个函数,ai1,ai2,…,aiti 表示第 i 个类中的函数列表。同一个类的函数列表中不会出现两个相同的函数。1≤aij≤106。0≤ti≤106,∑ti≤106。
- q 表示询问个数。1≤q≤105。
- ui,ri 表示第 i 个询问,询问第 ui 个类的实例在调用 ri 函数时,调用的是哪个类中的版本。1≤ui≤n,1≤ri≤106。
Output
对于每个询问,输出答案。如果调用不合法(会导致编译错误),输出 −1。
Examples
5 1 2 3 3 2 2 1 0 2 5 2 2 4 5 1 5 4 3 4 5 2 4 5 1 3
-1 3 4 -1
Note
样例等价于下面的 Java 代码。
class Class1 { void function2() { System.out.println("1"); } void function1() { System.out.println("1"); } } class Class2 extends Class1 { } class Class3 extends Class2 { void function5() { System.out.println("3"); } void function2() { System.out.println("3"); } } class Class4 extends Class3 { void function4() { System.out.println("4"); } void function5() { System.out.println("4"); } } class Class5 extends Class3 { void function5() { System.out.println("5"); } } void test() { new Class3().function4(); new Class5().function2(); new Class4().function5(); new Class1().function3(); }
题解:
离线处理. 对每种函数维护一个栈, 栈中记录类名. DFS 的时候更新栈 (进入时压入, 退出时弹出). 对于某个节点上的所有查询, 答案就是对应函数的栈顶元素.
给了一个有n
个节点的树,保证根节点为1
,每一个节点可能控制着若干函数,最后有q
组询问,每组询问查询当前节点a
的第b
个函数被哪个节点控制,查询不合法就输出-1
.
我们用邻接表先把这个树和每一个查询存起来,然后用一遍dfs
,在遍历整个树的时候先查看当前节点控制着哪些函数,然后添加在对应的栈中去,然后查看当前节点是否有询问,如果有的时候就去栈中找答案。然后继续dfs
,在回溯的时候进行出栈操作吗,整个过程是离线的,只需要对一棵树进行一遍dfs
即可
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
struct node {
int funs,id;
};
vector<struct node> query[maxn];
vector<int> Store[maxn],way[maxn];
stack<int> Fun[maxn*10];
int arr[maxn],ans[maxn];
void dfs(int rt)
{
for(auto v:Store[rt])
Fun[v].push(rt);
for(auto v:query[rt]) {
if(Fun[v.funs].empty()) ans[v.id] = -1;
else ans[v.id] = Fun[v.funs].top();
}
for(auto v:way[rt]) dfs(v);
for(auto v:Store[rt]) Fun[v].pop();
}
inline void init()
{
for(int i=0;i<maxn;i++) {
way[i].clear();Store[i].clear();query[i].clear();
while(!Fun[i].empty()) Fun[i].pop();
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
init();
for(int i=2,x;i<=n;i++) {
scanf("%d",&x);
way[x].push_back(i);
}
for(int i=1,x,y;i<=n;i++) {
scanf("%d",&x);
while(x--) {
scanf("%d",&y);
Store[i].push_back(y);
}
}
int q,u;
node temp ;
scanf("%d",&q);
for(int i=1;i<=q;i++) {
scanf("%d%d",&u,&temp.funs);
temp.id = i;
query[u].push_back(temp);
}
dfs(1);
for(int i=1;i<=q;i++) printf("%d\n",ans[i]);
}
return 0;
}
可持久化数组写法
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
int rt[maxn],tot;
int fnt[maxn]; /// 父亲节点
/// 可持久化内存计算 O(maxn << 2 + maxn * log(maxn)) = (4*maxn + maxn * log(maxn)) = 25*maxn; 所以开32倍
/// 内存为32*3+2 * 1e6 = 98 * 1e6 = 1e8
/// 最大内存为 1024mb = 1024*1024kb = 1024*1024*1024b = 1e9
/// 时间计算 最大1e6次更新,每次更新O(log(le6)) = 2*1e7
/// q次查询 1e5 * log(1e6) = 2 * 1e6
/// 综上时间复杂度 O(1e7) 3s足够了
struct SegTree
{
int val[maxn<<5],lson[maxn<<5],rson[maxn<<5];
void init() {
memset(val,0,sizeof(val));
memset(lson,0,sizeof(lson));
memset(rson,0,sizeof(rson));
}
void update(int pos,int v,int l,int r,int old,int& now) {
now = ++tot;
lson[now] = lson[old];rson[now] = rson[old];
if(l == r) {
val[now] = v;
return ;
}
int mid = (l + r) >> 1;
if(pos <= mid) update(pos,v,l,mid,lson[now],lson[now]);
else update(pos,v,mid+1,r,rson[now],rson[now]);
}
int query(int pos,int l,int r,int now) {
if(!now) return 0;
if(l == r) return val[now];
int mid = (l+r)>>1;
if(pos <= mid) return query(pos,l,mid,lson[now]);
return query(pos,mid+1,r,rson[now]);
}
}seg;
void init() {
seg.init();
memset(rt,0,sizeof(rt));
tot = 0;
}
int main()
{
init();
int n;
scanf("%d",&n);
for(int i=2;i<=n;i++) scanf("%d",&fnt[i]);
for(int i=1,t,x;i<=n;i++) {
rt[i] = rt[fnt[i]];
scanf("%d",&t);
while(t--)
{
scanf("%d",&x);
seg.update(x,i,1,maxn,rt[i],rt[i]);
}
}
int q;scanf("%d",&q);
int u,x;
while(q--)
{
scanf("%d%d",&u,&x);
int ans = seg.query(x,1,maxn,rt[u]);
if(!ans) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}