采用嵌套hashmap来解决
详情见代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
//采用双map结构
static Map<Integer,Node> map=new HashMap<>();
//快速输出
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception{
Read read=new Read();
int n=read.nextInt();
int q=read.nextInt();
for(int i=0;i<q;i++) {
int flag=read.nextInt();
int a=read.nextInt()-1;
int b=read.nextInt()-1;
if(flag==1){
int num=read.nextInt();
//必须做一个特判,如果第二层map不存在才需要新建
if(!map.containsKey(a)) {
map.put(a,new Node(b, num));
}
else {
map.get(a).map.put(b,num);
}
}
else {
out.println(map.get(a).map.get(b));
}
}
out.flush();
}
}
class Read{
StreamTokenizer st=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public int nextInt() throws Exception{
st.nextToken();
return (int)st.nval;
}
public long nextLong() throws Exception{
st.nextToken();
return (long)st.nval;
}
}
class Node{
int x,y;
HashMap<Integer, Integer> map=new HashMap<>();
Node(int x,int y){
this.x=x;
this.y=y;
map.put(x, y);
}
}