Cube Summation
Problem Statement :
You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coordinate between y1 and y2 (inclusive) and z coordinate between z1 and z2 (inclusive). Input Format The first line contains an integer T, the number of test-cases. T testcases follow. For each test case, the first line will contain two integers N and M separated by a single space. N defines the N * N * N matrix. M defines the number of operations. The next M lines will contain either 1. UPDATE x y z W 2. QUERY x1 y1 z1 x2 y2 z2 Output Format Print the result for each QUERY. Constrains 1 <= T <= 50 1 <= N <= 100 1 <= M <= 1000 1 <= x1 <= x2 <= N 1 <= y1 <= y2 <= N 1 <= z1 <= z2 <= N 1 <= x,y,z <= N -109 <= W <= 109 Sample Input 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 Sample Output 4 4 27 0 1 1
Solution :
Solution in C :
In C ++ :
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<string.h>
#include<cstring>
#include<stack>
#include<queue>
#include<cassert>
#include<cmath>
using namespace std;
#define LL long long int
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define INF 1000000000
#define debug(args...) do {cerr << #args << ": "; dbg,args; cerr << endl;} while(0)
LL BIT[110][110][110];
LL old[110][110][110];
void update(int x, int y,int z, LL w){
int i,j,k;
x += 5;
y += 5;
z += 5;
for(i=x;i<110;i+=(i&-i)){
for(j=y;j<110;j+=(j&-j)){
for(k=z;k<110;k+=(k&-k))
BIT[i][j][k] += w;
}
}
}
LL query(int x, int y, int z){
int i,j,k;
LL ret =0;
x += 5;
y += 5;
z += 5;
for(i=x;i>0;i-=(i&-i)){
for(j=y;j>0;j-=(j&-j)){
for(k=z;k>0;k-=(k&-k))
ret += BIT[i][j][k];
}
}
return ret;
}
int main(){
int t,n,m,x,y,z,w;
scanf("%d",&t);
string type;
while(t--){
memset(BIT,0,sizeof(BIT));
memset(old,0,sizeof(old));
cin >> n >> m;
while(m--){
cin >> type;
if(type == "UPDATE"){
scanf("%d %d %d %d",&x,&y,&z,&w);
update(x,y,z,w-old[x][y][z]);
old[x][y][z] = w;
}
else{
int x1,y1,z1,x2,y2,z2;
scanf("%d %d %d %d %d %d",&x1,&y1,&z1,&x2,&y2,&z2);
printf("%Ld\n",
query(x2,y2,z2) -
query(x2,y2,z1-1) -
query(x2,y1-1, z2) -
query(x1-1, y2, z2) +
query(x1-1, y1-1, z2) +
query(x1-1, y2, z1-1) +
query(x2, y1-1, z1-1) -
query(x1-1,y1-1,z1-1)
);
}
}
}
return 0;
}
In Java :
import java.util.Scanner;
public class Solution {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
new Solution().ss();
}
private void ss() {
int nrt = sc.nextInt();
for (int i = 0; i < nrt; i++) {
solve();
}
}
long mat[][][];
int n;
private void update(int x, int yy, int zz, long val) {
while (x <= n) {
int y = yy;
while (y <= n) {
int z = zz;
while (z <= n) {
mat[x][y][z] += val;
z += (z & -z);
}
y += (y & -y);
}
x += (x & -x);
}
}
private long sum(int x, int yy, int zz) {
long rez = 0;
while (x > 0) {
int y = yy;
while (y > 0) {
int z = zz;
while (z > 0) {
rez += mat[x][y][z];
z -= (z & -z);
}
y -= (y & -y);
}
x -= (x & -x);
}
return rez;
}
private void solve() {
n = sc.nextInt();
int m = sc.nextInt();
mat = new long[101][101][101];
long[][][] actual = new long[101][101][101];
for (int i = 0; i < m; i++) {
String op = sc.next();
if (op.equals("UPDATE")) {
int x = sc.nextInt(), y = sc.nextInt(), z = sc.nextInt();
long w = sc.nextLong();
// x--; y--; z--;
update(x, y, z, w - actual[x][y][z]);
actual[x][y][z] = w;
} else {
int x1 = sc.nextInt(), y1 = sc.nextInt(), z1 = sc.nextInt();
int x2 = sc.nextInt(), y2 = sc.nextInt(), z2 = sc.nextInt();
// x1--; y1--; z1--;
// x2--; y2--; z2--;
long v1 = sum(x2,y2,z2)- sum(x1-1,y2,z2) - sum(x2,y1-1,z2) + sum(x1-1,y1-1,z2);
long v2 = sum(x2,y2,z1-1) - sum(x1-1,y2,z1-1) - sum(x2,y1-1,z1-1) + sum(x1-1,y1-1,z1-1);
System.out.println(v1 - v2);
}
}
}
}
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define max 3001
#define max1 101
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t,n,m,x1,x2,y1,y2,z1,z2;
int i,j,k,x[max],y[max],z[max];
long W[max];
char str[7];
scanf("%d",&t);
j=0;
while(t--)
{
long mat[max1][max1][max1]={0};
scanf("%d%d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%s",str);
if(str[0]=='U')
{
scanf("%d%d%d%ld",&x[j],&y[j],&z[j],&W[j]);
mat[x[j]][y[j]][z[j]]=W[j];
j++;
}
else
{
scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);
long sum=0;
for(k=0;k<j;k++)
{
if(x[k]>=x1 && x[k]<=x2)
{
if(y[k]>=y1 && y[k]<=y2)
{
if(z[k]>=z1 && z[k]<=z2)
{
if(W[k]==mat[x[k]][y[k]][z[k]])
sum+=W[k];
}
}
}
}
printf("%ld\n",sum);
}
}
}
return 0;
}
In Python3 :
numTests = int(input())
#Making the data structure
cube = [];
for i in range(0, 100):
cube.append([]);
for j in range(0, 100):
cube[i].append([])
for k in range(0, 100):
cube[i][j].append(0)
while numTests != 0:
indexList = [[0,0,0,0]]
line = input()
tokens = line.split(' ')
N = int(tokens[0])
numOp = int(tokens[1])
#Updating the data structure according to the queries
while numOp != 0:
query = input()
tokens = query.split(' ')
if(tokens[0] == 'UPDATE'):
x = int(tokens[1]) - 1
y = int(tokens[2]) - 1
z = int(tokens[3]) - 1
w = int(tokens[4])
cube[x][y][z] = w
tempList = []
tempList.append(x)
tempList.append(y)
tempList.append(z)
tempList.append(w)
flag = 0
for index in indexList:
if index[0] == x and index[1] == y and index[2] == z:
index[3] = w
flag = 1
if flag == 0:
indexList.append(tempList)
elif(tokens[0] == 'QUERY'):
x1 = int(tokens[1]) - 1
y1 = int(tokens[2]) - 1
z1 = int(tokens[3]) - 1
x2 = int(tokens[4]) - 1
y2 = int(tokens[5]) - 1
z2 = int(tokens[6]) - 1
sum = 0
for index in indexList:
if index[0] >= x1 and index[0] <= x2:
if index[1] >= y1 and index[1] <= y2:
if index[2] >= z1 and index[2] <= z2:
sum += index[3]
print(sum)
numOp -= 1
numTests -= 1
View More Similar Problems
Inserting a Node Into a Sorted Doubly Linked List
Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function
View Solution →Reverse a doubly linked list
This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.
View Solution →Tree: Preorder Traversal
Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's
View Solution →Tree: Postorder Traversal
Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the
View Solution →Tree: Inorder Traversal
In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func
View Solution →Tree: Height of a Binary Tree
The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary
View Solution →