Skip to content

Instantly share code, notes, and snippets.

@neotrinity
Last active January 3, 2016 09:09
Show Gist options
  • Save neotrinity/8441197 to your computer and use it in GitHub Desktop.
Save neotrinity/8441197 to your computer and use it in GitHub Desktop.
Gold Mines - Hacker Earth - I have defensively coded the constraints
#include <stdio.h>
#include <time.h>
using namespace std;
int main()
{
// declarations & initialisations
//time_t start = time(NULL);
//int sec;
unsigned short num_rows, num_cols = 0;
unsigned short num_queries = 0;
unsigned short num_executions = 0;
//unsigned long* gold_data;
//unsigned long** gold;
register unsigned short row_lo = 0;
register unsigned short col_lo = 0;
register unsigned short row_hi = 0;
register unsigned short col_hi = 0;
// read rows & columns
scanf("%hu%hu", &num_rows, &num_cols);
// declare and initialise the contriner
//gold_data = new unsigned long[num_rows * num_cols];
//gold = new unsigned long*[num_rows];
//for (unsigned short a = 0; a < num_rows; ++a)
// gold[a] = gold_data + num_cols * a;
unsigned long gold[1000][1000];
// read in the gold
for (unsigned short i=0; i<num_rows; ++i) {
for (unsigned short j=0; j<num_cols; ++j) {
scanf("%lu", &gold[i][j]);
}
}
// read the number of queries and size the results vector
scanf("%hu", &num_queries);
//results.resize(num_queries);
while (num_executions < num_queries)
{
// read and process the queries
scanf("%hu%hu%hu%hu", &row_lo, &col_lo, &row_hi, &col_hi);
// remember, the grid is zero-indexed
--row_lo;
--col_lo;
--row_hi;
--col_hi;
register unsigned long long total_gold = 0;
for (unsigned short r=row_lo; r<= row_hi; ++r)
{
for (unsigned short c=col_lo; c<= col_hi; ++c)
{
total_gold += gold[r][c];
}
}
fprintf(stdout, "%llu\n", total_gold);
fflush(stdout);
++num_executions;
}
//sec = (int) time(NULL) - start;
//fprintf(stdout, "%d seconds\n", sec);
return 0;
}
// Compile flags
// g++ -std=c++0x -w -O2 -formit-frame-pointer -lm goldmines.cpp
def find_gold(x1, y1, x2, y2):
return sum([sum(row[y1-1:y2]) for row in array[x1-1:x2]])
R, C = map(int, raw_input().split())
array = []
if 1 <= R and R <= 1000 and 1 <= C and C <= 1000:
can_i = True
for row in xrange(R):
stritems = raw_input().split()
if len(stritems) > C:
can_i = False
break
array.append(tuple(map(int, stritems)))
if can_i:
no_of_runs = int(raw_input())
for run in xrange(no_of_runs):
run_params = map(int, raw_input().split())
x1, y1, x2, y2 = run_params
if 1 <= x1 and x1 <= x2 and x2 <= R and 1 <= y1 and y2 <= y2 and y2 <= C:
print find_gold(x1, y1, x2, y2)
else:
break
R, C = map(int, raw_input().split())
arr = []
for row in xrange(R):
arr.extend(map(int, raw_input().split()))
no_of_runs = int(raw_input())
def find_gold(x1, y1, x2, y2):
s = 0
for x in xrange(x1-1, x2):
s+=sum(arr[(x*R)+(y1-1):(x*R)+y2])
return s
for run in xrange(no_of_runs):
run_params = map(int, raw_input().split())
print find_gold(*run_params)
import array
def find_gold(x1, y1, x2, y2):
s = 0
for x in xrange(x1-1, x2):
s+=sum(arr[(x*R)+(y1-1):(x*R)+y2])
return s
R, C = map(int, raw_input().split())
arr = array.array('L')
if 1 <= R and R <= 1000 and 1 <= C and C <= 1000:
can_i = True
for row in xrange(R):
stritems = raw_input().split()
if len(stritems) > C:
can_i = False
break
arr.extend(map(long, stritems))
if can_i:
no_of_runs = int(raw_input())
for run in xrange(no_of_runs):
run_params = map(int, raw_input().split())
if len(run_params) != 4:
break
x1, y1, x2, y2 = run_params
if 1 <= x1 and x1 <= x2 and x2 <= R and 1 <= y1 and y2 <= y2 and y2 <= C:
print find_gold(x1, y1, x2, y2)
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment