-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution_1.java
182 lines (170 loc) · 5.27 KB
/
Solution_1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
*
* @author WEL
2
4 5
UPDATE 2 2 2 4
QUERY 1 1 1 3 3 3
UPDATE 1 1 1 23
QUERY 2 2 2 4 4 4
QUERY 1 1 1 3 3 3
2 4
UPDATE 2 2 2 1
QUERY 1 1 1 1 1 1
QUERY 1 1 1 2 2 2
QUERY 2 2 2 2 2 2
*/
public class Solution_1 {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Solution_1 a = new Solution_1();
Scanner in = new Scanner(System.in);
int testCases = Integer.parseInt(in.nextLine());
List<TestCase> testCaseList = new ArrayList<TestCase>();
for(int i=0;i<testCases;i++) {
Solution_1.TestCase testCase = a.new TestCase();
String secondLine = in.nextLine();
String[] secondLineElems = secondLine.split("\\s");
int cubeSize = Integer.parseInt(secondLineElems[0]);
int commandSize = Integer.parseInt(secondLineElems[1]);
testCase.setCubeSize(cubeSize);
testCase.setCommandSize(commandSize);
List<String> commands = new ArrayList<String>();
for(int j=0;j<commandSize;j++) {
commands.add(in.nextLine());
}
testCase.setCommands(commands);
//Create a map having non zero elements
Map<Solution_1.Location,Integer> nonZeroElements = new HashMap<Solution_1.Location,Integer>();
for(String command: commands) {
String[] commandElems = command.split("\\s");
//UPDATE 2 2 2 4
if("UPDATE".equalsIgnoreCase(commandElems[0])) {
if(commandElems.length!=5) {
throw new Exception("Insufficient Input");
}
int x = Integer.parseInt(commandElems[1]);
int y = Integer.parseInt(commandElems[2]);
int z = Integer.parseInt(commandElems[3]);
Solution_1.Location loc = a.new Location(x,y,z);
nonZeroElements.put(loc, Integer.parseInt(commandElems[4]));
//System.out.println(commandElems[4]);
}
//QUERY 1 1 1 2 2 2 4
if("QUERY".equalsIgnoreCase(commandElems[0])) {
if(commandElems.length!=7) {
throw new Exception("Insufficient Input");
}
int x1 = Integer.parseInt(commandElems[1]);
int y1 = Integer.parseInt(commandElems[2]);
int z1 = Integer.parseInt(commandElems[3]);
int x2 = Integer.parseInt(commandElems[4]);
int y2 = Integer.parseInt(commandElems[5]);
int z2 = Integer.parseInt(commandElems[6]);
List<Integer> vals = new ArrayList<Integer>();
nonZeroElements.forEach((k, v) -> {
if(k.getX()>=x1 && k.getY()>=y1 && k.getZ()>=z1 &&
k.getX()<=x2 && k.getY()<=y2 && k.getZ()<=z2) {
vals.add(v);
}
});
int sum = 0;
for(Integer v: vals) {
sum += v.intValue();
}
System.out.println(sum);
}
}
testCaseList.add(testCase);
}
}
class TestCase{
private int commandSize;
public int getCommandSize() {
return commandSize;
}
public void setCommandSize(int commandSize) {
this.commandSize = commandSize;
}
private int cubeSize;
public int getCubeSize() {
return cubeSize;
}
public void setCubeSize(int cubeSize) {
this.cubeSize = cubeSize;
}
public List<String> getCommands() {
return commands;
}
public void setCommands(List<String> commands) {
this.commands = commands;
}
List<String> commands;
}
class Location{
private int x;
public int getX() {
return x;
}
public Location(int x, int y, int z) {
super();
this.x = x;
this.y = y;
this.z = z;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
private int y;
private int z;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + x;
result = prime * result + y;
result = prime * result + z;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Location other = (Location) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (x != other.x)
return false;
if (y != other.y)
return false;
if (z != other.z)
return false;
return true;
}
private Solution_1 getOuterType() {
return Solution_1.this;
}
}
}