Statistics

Problem Statement for "CuboidJoin"

Problem Statement

A cuboid is a rectangular solid. Given a few cuboids, which may or may not overlap, you have to find out the total volume enclosed by them. A volume is considered enclosed if it falls completely within at least one of the specified cuboids. A volume that is in between several cuboids, but not inside any of them, is not considered enclosed.

The input is given as a int[], cuboids. The first 6 elements of cuboids describe the first cuboid, the next 6 the second cuboid and so on. The 6 elements specify the left-bottom-front vertex and then the right-top-back vertex. Each vertex is specified by 3 elements which refer to its x, y and z coordinates respectively.

For instance, if cuboids = {0,0,0,1,1,1}, it would mean there is one cuboid whose left-bottom-front vertex is (0,0,0) and whose right-top-back vertex is (1,1,1). In this case the volume enclosed would be 1. However, if cuboids = {0,0,0,1,1,1,1,1,1,2,2,2}, it would mean there are two cuboids, one whose left-bottom-front vertex is (0,0,0) and right-top-back vertex is (1,1,1), and another whose left-bottom-front vertex is (1,1,1) and right-top-back vertex is (2,2,2). In this case, the enclosed volume would be 2.

Definition

Class:
CuboidJoin
Method:
totalVolume
Parameters:
int[]
Returns:
long
Method signature:
long totalVolume(int[] cuboids)
(be sure your method is public)

Notes

  • The cuboids are always aligned with the grid. In other words, the cuboids are never tilted or twisted with respect to the axes.
  • The x coordinate increases from left to right, the y coordinate from bottom to top, and the z coordinate from front to back.

Constraints

  • cuboids will contain between 0 and 30 elements, inclusive.
  • The number of elements in cuboids will be a multiple of 6.
  • Each element of cuboids will be between -5000 and 5000, inclusive.
  • As is explained in the problem statement, each cuboid is specified as a block of six elements x1,y1,z1,x2,y2,z2. In each such block, x1 is less than or equal to x2, y1 is less than or equal to y2 and z1 is less than or equal to z2.

Examples

  1. {0,0,0,1,1,1}

    Returns: 1

    There is just one cuboid which is a 1-by-1-by-1 cube. Hence, the volume enclosed is 1*1*1 = 1.

  2. {0,0,0,1,1,1,1,1,1,2,2,2}

    Returns: 2

    There are two cuboids, both of which are 1-by-1-by-1 cubes. Hence, the volume enclosed is 1*1*1 + 1*1*1 = 2.

  3. {0,0,0,4,4,4,0,0,0,1,1,1}

    Returns: 64

    There are two cuboids. One is a 1-by-1-by-1 cube and another is a 4-by-4-by-4 cube. The volume enclosed is 4*4*4 = 64 as the 1-by-1-by-1 cube is completely enclosed within the 4-by-4-by-4 cube.

  4. {-5000,-5000,-5000,5000,5000,5000}

    Returns: 1000000000000

  5. {-275,-1020,-2918,921,2673,3863,497,-3921,-1831,3059,618,811,-3106,-1159,-2082,996,3280,3052,4387,1735,244,4469,4692,3278,-369,-366,2870,968,-91,3502}

    Returns: 129892376024

  6. {1140,2751,-1253,1580,4433,4795,-4815,9,1222,-4309,1585,2128,-1496,-1921,1236,602,-1300,4118,458,1438,-839,2715,2588,2766,-4210,-1597,1999,-1705,2013,4861}

    Returns: 44191502582

  7. {-3149,-3139,-3843,-2590,2472,4666,-988,-4182,-4745,3101,233,-2290,-2875,-4122,1943,-2849,-2273,3240,-1973,-965,733,3805,3670,3578,1310,-1837,-4989,3393,-578,3941}

    Returns: 162823810512

  8. {-3209,-2934,-4688,-390,4393,-2577,-1816,-4149,1709,3216,-3087,2316,1524,-3311,-4251,2600,-106,1701,-2551,-227,-3024,4191,2343,4417,-783,-1519,-4952,4777,1232,-4142}

    Returns: 204837483171

  9. {1051,3609,-184,1699,3707,3002,-1864,-971,-2605,-1682,4838,4526,237,-1757,-3464,1168,3418,3713,-4939,-3614,-4969,-1319,1112,-38,-1798,-3032,1504,1635,1387,4041}

    Returns: 157034258652

  10. {-4007,3046,-3249,-3882,3136,-1863,-2235,-4619,719,-983,-465,4795,-4604,1716,-2222,-481,4009,4864,-4528,-3039,-1258,3956,3269,3905,-902,-1654,-3847,3791,-531,4370}

    Returns: 337888555093

  11. {-3654,-4797,-3059,2630,-2833,-2183,-31,2833,-4614,3574,3736,-2747,-738,-2735,-1061,930,-2388,-533,-1940,2772,-4131,4112,2816,-1434,1703,2688,-583,4918,4441,4344}

    Returns: 45680906570

  12. {-3770,-4962,-3697,-1858,1899,1502,2996,-3343,-1433,4308,883,2898,-4693,-1637,-2455,-1226,-883,1635,-4956,1156,326,-4914,2180,1333,-3469,-3008,-2343,643,-1338,558}

    Returns: 108813743682

  13. {-3455,-3695,-3168,-2513,2084,-1226,2377,-3969,2228,2991,-1299,3672,-1282,-385,-4254,4016,1923,-583,-4034,1854,2090,-3546,4497,2329,-3423,996,-1457,-2216,1597,3177}

    Returns: 61370812544

  14. {548,1638,-773,658,4076,1596,-1075,-4477,-3619,2927,4021,1956,-3555,-195,-4047,3234,1830,1900,-1854,-3824,-4964,-1360,-2896,-2442,-3494,-2621,-1016,-1110,767,-926}

    Returns: 228322641839

  15. {-3146,3452,1161,678,3830,3285,-1494,-2099,2146,3603,2090,4040,-4539,213,-3485,-3112,3394,-3254,-2211,-285,-2471,308,1770,214,-41,-912,-1529,591,1083,4362}

    Returns: 62664619356

  16. {-4310,-3729,-382,4745,950,3324,-526,-1309,-3543,1536,812,-550,-3839,1583,42,-2710,1817,2019,1369,-1714,-4534,3902,3803,-1543,-3807,-3917,-4568,-443,-156,2191}

    Returns: 266020880902

  17. {3517,-3723,988,3525,2747,2609,-3660,4490,-2768,-830,4774,-562,-54,-1750,596,4444,3646,3494,863,-4315,-4648,2402,-177,1727,-1268,196,-4074,4877,1704,4541}

    Returns: 170172198529

  18. {437,-1775,-1847,4588,-919,2249,-1993,-3754,2564,-349,-1525,3332,-4675,341,-2407,-1672,4690,4987,-2529,783,-3405,3394,3661,-54,-3085,-3169,-4386,4213,1334,1347}

    Returns: 328240104918

  19. {-1996,523,-4338,2684,4967,-3434,-2932,-2169,2559,3272,-318,3320,3204,-2234,683,3864,-438,1291,-2242,130,-4871,-655,1099,978,295,-4310,848,2917,-1165,967}

    Returns: 37538683497

  20. {-3729,-4309,-4950,4564,296,-4184,-4161,3403,-2314,1225,4282,-769,-3272,-3245,-4452,-2137,-341,4370,-3145,-514,-2415,892,1719,1027,-4026,-3529,-4994,-2663,3715,-234}

    Returns: 129256462914

  21. {-1878,-1472,-299,-766,-992,2850,-1724,-455,2919,-60,116,3013,2701,-1800,-1383,4179,3083,4110,656,-4336,2045,1210,-1937,2371,-4148,2693,2062,1084,4568,2472}

    Returns: 45868880254

  22. {2575,1370,605,2782,4773,1347,-4182,-331,-1945,-3188,-263,-902,-1598,-4951,-1270,-929,-268,-255,-3747,-4190,-4605,3932,-1062,217,-470,-2252,1287,2231,-1561,2154}

    Returns: 119091254924

  23. {2426,-4598,-2912,4036,-738,-2171,197,-1394,3018,1800,1652,4364,2469,-412,-52,3054,2780,2418,-960,-3588,-1691,4038,2795,1169,-898,-2173,-1310,860,-784,4526}

    Returns: 112402823822

  24. {-4555,476,-3556,-3362,2877,-1146,-450,-4543,498,2018,-3076,4408,-3990,-3654,-3922,155,661,-2169,-4875,-4412,-4191,-3239,812,3298,599,4522,-4776,2703,4812,-1033}

    Returns: 112054693016

  25. {-5000,-5000,-5000,5000,5000,5000 ,-5000,-5000,-5000,5000,5000,5000 ,-5000,-5000,-5000,5000,5000,5000 ,-5000,-5000,-5000,5000,5000,5000 ,-5000,-5000,-5000,5000,5000,5000}

    Returns: 1000000000000

  26. {-5000,-5000,-5000,5000,5000,5000 ,-5000,-5000,-5000,0,5000,5000 ,0,-5000,-5000,5000,5000,5000 ,-5000,-5000,-5000,5000,0,5000 ,-5000,0,-5000,5000,5000,5000}

    Returns: 1000000000000

  27. {0,0,0,5000,5000,5000 ,-5000,-5000,-5000,0,5000,5000 ,0,-5000,-5000,5000,5000,5000 ,-5000,-5000,-5000,5000,0,5000 ,-5000,0,-5000,5000,5000,5000}

    Returns: 1000000000000

  28. {0,0,0,4,4,4 ,3,3,3,8,8,8}

    Returns: 188

  29. {0,0,0,4,4,4 ,3,3,3,7,7,7 ,6,6,6,10,10,10}

    Returns: 190

  30. {0,0,0,4,4,4 ,1,1,1,3,3,3}

    Returns: 64

  31. {0,0,0,5,5,5 ,1,1,1,4,4,4 ,2,2,2,3,3,3}

    Returns: 125

  32. {0,0,0,4,4,4 ,2,-2,-2,7,6,6}

    Returns: 352

  33. {0,0,0,4,4,4 ,2,-2,-2,7,6,6 ,5,0,0,9,4,4}

    Returns: 384

  34. {0,0,0,9,4,4 ,2,-2,-2,7,6,6}

    Returns: 384

  35. {0,0,0,4,4,4 ,3,2,0,7,6,4 ,3,-1,0,8,3,4}

    Returns: 176

  36. {0,0,0,9,4,4 ,0,2,0,9,6,4 ,2,-2,-2,7,6,6}

    Returns: 416

  37. {0,0,0,4,4,4 ,2,2,2,8,3,3 ,6,0,0,10,4,4}

    Returns: 130

  38. {-5,3,-1,2,4,0,1,-5,1,2,-2,1,-3,-4,-3,-1,0,4,-2,-5,-1,0,-2,-1,1,-3,-5,2,3,0}

    Returns: 93

  39. {1,-5,-5,4,3,5,0,-4,-3,2,-2,2,-1,2,-5,1,2,0,-1,1,0,2,4,1,-4,-3,-2,0,-1,4}

    Returns: 305

  40. {-3,-1,2,3,-1,5,-3,0,-2,2,1,-1,-3,2,-4,3,5,5,4,-5,1,5,-3,2,-4,0,-3,-3,5,1}

    Returns: 189

  41. {-1,-2,-3,-1,-2,0,4,-5,-1,4,1,3,-5,3,0,-3,4,5,0,2,-2,5,4,0,1,-2,1,5,2,2}

    Returns: 46

  42. {-1,5,2,2,5,5,0,-5,-4,5,-2,-3,-5,1,-4,5,3,-1,-3,-3,-1,4,3,5,-5,-4,1,0,4,1}

    Returns: 327

  43. {0,0,0,1,2,3,5,5,5,6,6,6}

    Returns: 7

  44. {}

    Returns: 0

  45. {0,0,0,1,1,0}

    Returns: 0

    The answer is 0 because "flat" solids enclose no volume.

  46. {-1302,2052,-965,-197,4268,3029,-860,-1763,-3318,-228,441,2418,770,1668,-3785,1635,2842,-2862,-1939,-870,-2904,-1419,-804,2252,881,-4270,-4068,3590,-423,3226}

    Returns: 94898721340

  47. {-2379,-4984,1650,471,-145,1870,-2764,-3169,-3851,67,581,4844,-3142,2803,-2890,2854,3565,-1454,-1639,-2811,2488,3699,3108,4687,-3094,-3703,7,3025,-3670,1075}

    Returns: 157245450108

  48. {1430,-2074,-4886,1603,2210,-293,-771,-4027,-4386,2688,-1097,2217,-4731,-1105,-3863,2774,3908,1286,-3494,-3104,-1166,2423,2895,2156,-1155,-1433,-2990,4130,2500,-1061}

    Returns: 310389718846

  49. {-4767,-4746,-1274,2425,2455,4012,-1570,1703,-2748,470,4125,2979,307,-589,-1686,624,1413,4834,-1048,1127,-2736,526,3573,3871,-1596,-4655,-1738,1420,4078,491}

    Returns: 311557135452

  50. {-501,-4903,-1517,1528,-3490,356,-4423,-1956,-3435,4976,-302,327,1378,-2608,717,4534,4990,2737,-4216,-2081,-1326,4460,273,3786,-4795,-1441,-3305,914,3500,4342}

    Returns: 336659119546

  51. {1435,-4189,-4454,4304,-2988,632,-247,-3691,-2631,2541,1800,-1834,-309,-2919,1557,3426,3626,2909,-2634,-4242,-3114,2527,2690,1119,4257,2661,1929,4665,2829,4062}

    Returns: 197301897874

  52. {-141,-829,-2171,1975,4434,4439,1293,-1676,-3323,4597,4272,4686,-4934,-1744,3234,-3370,3638,3719,-4117,-2076,854,-409,2773,1498,-684,874,-1979,3267,3340,3997}

    Returns: 231338733304

  53. {-826,-3435,-4569,942,261,458,-158,233,-1792,1892,1517,-357,2994,1591,-223,3126,4848,159,-1027,-1030,391,1551,-81,3752,-4796,4066,-4503,284,4332,1365}

    Returns: 52785968962

  54. {-823,-919,-40,369,3496,2703,-4788,1900,-1199,451,3885,2837,-4710,-661,-2860,-3469,1407,-607,-1445,205,-1592,2284,511,-1561,-2922,-2936,1263,1237,1358,2465}

    Returns: 75210408186

  55. {913,-2119,-2268,1475,1910,-1470,-2603,-3081,-2918,-1563,1373,1687,-1414,-1354,-2013,2547,3680,4567,245,2585,-779,2125,2992,187,1132,3047,-4414,2620,3269,4430}

    Returns: 154242551694

  56. {-3243,-917,922,354,364,1183,-4259,-4523,2913,301,831,3721,-3266,-3573,1228,1073,1286,1874,-2537,-3262,-1462,1433,4764,4087,603,-1988,-2396,4736,4907,-228}

    Returns: 245265661180

  57. { 0, 0, 0, 1, 2, 3, 5, 5, 5, 6, 6, 6 }

    Returns: 7

  58. { 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1 }

    Returns: 1

  59. { -123, 43, 345, 12, 344, 4999, 30, 45, 360, 800, 300, 700, -50, 20, 345, 12, 344, 4999, 50, 79, -4999, 4999, 90, 0, 245, 23, 21, 456, 1245, 4566 }

    Returns: 1688249645

  60. { -5000, -5000, -5000, 5000, 5000, 5000 }

    Returns: 1000000000000

  61. { 0, 0, 0, 4, 4, 4, 0, 0, 0, 1, 1, 1 }

    Returns: 64

  62. { 0, 0, 0, 1, 1, 1, 0, 0, 0, 22, 22, 22, 5, 5, 5, 17, 17, 17, 9, 9, 9, 13, 13, 13 }

    Returns: 10648

  63. { 0, 0, 0, 4, 4, 4, 2, 2, 2, 5, 5, 5, 3, 3, 3, 6, 6, 6 }

    Returns: 102

  64. { 0, 0, 0, 10, 10, 10, 5, 5, 5, 20, 20, 20, 7, 7, 7, 30, 30, 30 }

    Returns: 14220

  65. { 1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 8, 100, 120, 130, 1000, 1000, 1000, 1, 1, 1, 10, 2, 3 }

    Returns: 689040083

  66. { 0, 0, 0, 4, 4, 4, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 4, 4, 4 }

    Returns: 64

  67. { 100, 100, 100, 200, 200, 200, 150, 50, 120, 250, 80, 300, 0, 0, 0, 60, 60, 60, -2000, -2000, -2000, -1000, -1000, -1000, -2100, -2320, -2280, -2001, 0, -1 }

    Returns: 1525196720

  68. { 0, 0, 0, 50, 50, 50, 25, 25, 25, 100, 100, 100 }

    Returns: 531250

  69. { 0, 0, 0, 10, 10, 10, 0, 0, 0, 10, 10, 10, 5, 5, 5, 15, 15, 15, 5, 5, 5, 15, 15, 15, 5, 5, 5, 15, 15, 15 }

    Returns: 1875

  70. { 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1 }

    Returns: 1

  71. { 0, 0, 0, 22, 22, 22, 5, 5, 5, 17, 17, 17, 10, 10, 10, 12, 12, 12 }

    Returns: 10648


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: