import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; public class Main { /** * This class represents a problem setting. */ class Problem implements IProblem { int[] NK; int[][] XYHP; int[][] AXAYWHD; @Override public void set(ProblemReader reader) throws IOException { NK = reader.nextIntArray(); XYHP = reader.nextIntArray2(NK[0]); AXAYWHD = reader.nextIntArray2(NK[1]); } @Override public String toString() { // TODO return null; } } /** * Solver for the problem. * It's the most important part of codes in this file. */ class Solver implements IProblemSolver { @Override public String solve(Problem problem) { int N = problem.NK[0], K = problem.NK[1]; int[][] XYHP = problem.XYHP; int[][] AXAYWHD = problem.AXAYWHD; return StringUtils.toString(solve(N, K, XYHP, AXAYWHD)); } private int solve(int N, int K, int[][] XYHP, int[][] AXAYWHD) { int[][] d = new int[1001][1001]; for (int i = 0; i < K; i++) { int AX = AXAYWHD[i][0], AY = AXAYWHD[i][1], W = AXAYWHD[i][2], H = AXAYWHD[i][3], D = AXAYWHD[i][4]; int bx = AX + W + 1, by = AY + H + 1; d[AX + 500][AY + 500] += D; if (bx <= 500) { d[bx + 500][AY + 500] -= D; } if (by <= 500) { d[AX + 500][by + 500] -= D; } if (bx <= 500 && by <= 500) { d[bx + 500][by + 500] += D; } } for (int y = 0; y <= 1000; y++) { for (int x = 1; x <= 1000; x++) { d[x][y] += d[x - 1][y]; } } for (int x = 0; x <= 1000; x++) { for (int y = 1; y <= 1000; y++) { d[x][y] += d[x][y - 1]; } } int res = 0; for (int i = 0; i < N; i++) { int X = XYHP[i][0], Y = XYHP[i][1], HP = XYHP[i][2]; HP -= d[X + 500][Y + 500]; if (HP > 0) { res += HP; } } return res; } } /** * This class is a solver to confirm the main solver. * This solver often works only for a problem with a small number setting. */ class Solver2 implements IProblemSolver { @Override public String solve(Problem problem) { // TODO return null; } } class Factory implements IProblemFactory { @Override public Problem newProblem() { // TODO return null; } @Override public Problem newProblem(ProblemReader reader) throws IOException { Problem problem = new Problem(); problem.set(reader); return problem; } } ProblemProcessor processor; Main() { Init(); } void Init() { ProblemProcessorConfig config = new ProblemProcessorConfig<>(); config.factory = new Factory(); config.solver = new Solver(); config.solver2 = new Solver2(); processor = new ProblemProcessor(config); } void process(String mode) throws IOException { processor.process(mode); } public static void main(String args[]) throws IOException { String mode = "solve"; new Main().process(mode); } /* Common library */ interface IProblem { abstract void set(ProblemReader reader) throws IOException; } interface IProblemFactory

{ abstract P newProblem(); abstract P newProblem(ProblemReader reader) throws IOException; } interface IProblemSolver

{ abstract String solve(P problem); } class ProblemReader { BufferedReader br = null; ProblemReader() { br = new BufferedReader(new InputStreamReader(System.in)); } int nextInt() throws IOException { return StringUtils.toInt(br.readLine()); } long nextLong() throws IOException { return StringUtils.toLong(br.readLine()); } double nextDouble() throws IOException { return StringUtils.toDouble(br.readLine()); } String nextString() throws IOException { return br.readLine(); } int[] nextIntArray() throws IOException { return StringUtils.toIntArray(br.readLine()); } long[] nextLongArray() throws IOException { return StringUtils.toLongArray(br.readLine()); } double[] nextDoubleArray() throws IOException { return StringUtils.toDoubleArray(br.readLine()); } String[] nextStringArray() throws IOException { return StringUtils.toStringArray(br.readLine()); } int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] nextLongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } double[] nextDoubleArray(int n) throws IOException { double[] a = new double[n]; for (int i = 0; i < n; i++) { a[i] = nextDouble(); } return a; } String[] nextStringArray(int n) throws IOException { String[] strs = new String[n]; for (int i = 0; i < n; i++) { strs[i] = nextString(); } return strs; } int[][] nextIntArray2(int n) throws IOException { int[][] a = new int[n][]; for (int i = 0; i < n; i++) { a[i] = nextIntArray(); } return a; } long[][] nextLongArray2(int n) throws IOException { long[][] a = new long[n][]; for (int i = 0; i < n; i++) { a[i] = nextLongArray(); } return a; } double[][] nextDoubleArray2(int n) throws IOException { double[][] a = new double[n][]; for (int i = 0; i < n; i++) { a[i] = nextDoubleArray(); } return a; } String[][] nextStringArray2(int n) throws IOException { String[][] strs = new String[n][]; for (int i = 0; i < n; i++) { strs[i] = nextStringArray(); } return strs; } void close() throws IOException { if (br != null) { br.close(); } } } class ProblemCreator

{ IProblemFactory

factory; ProblemCreator(IProblemFactory

factory) { this.factory = factory; } void create() throws FileNotFoundException { P problem = factory.newProblem(); System.out.println(problem.toString()); } } class ProblemSolverComparator

{ IProblemFactory

factory; IProblemSolver

solver1, solver2; ProblemSolverComparator(IProblemFactory

factory, IProblemSolver

solver1, IProblemSolver

solver2) { this.factory = factory; this.solver1 = solver1; this.solver2 = solver2; } void compare() throws IOException { ProblemReader reader = null; try { reader = new ProblemReader(); P problem = factory.newProblem(reader); String res1 = solver1.solve(problem); String res2 = solver2.solve(problem); if (res1.equals(res2)) { System.out.println(res1); } else { System.err.println(res1 + " " + res2); } } finally { if (reader != null) { reader.close(); } } } } class ProblemAnswerer

{ IProblemFactory

factory; IProblemSolver

solver; ProblemAnswerer(IProblemFactory

factory, IProblemSolver

solver) { this.factory = factory; this.solver = solver; } void answer() throws IOException { ProblemReader reader = null; try { reader = new ProblemReader(); P problem = factory.newProblem(reader); String res = solver.solve(problem); System.out.println(res); } finally { if (reader != null) { reader.close(); } } } } class ProblemProcessorConfig

{ IProblemFactory

factory; IProblemSolver

solver, solver2; } class ProblemProcessor

{ ProblemProcessorConfig

config; ProblemCreator

creator; ProblemSolverComparator

comparator; ProblemAnswerer

answerer; ProblemProcessor(ProblemProcessorConfig

config) { this.config = config; creator = new ProblemCreator

(config.factory); comparator = new ProblemSolverComparator

(config.factory, config.solver, config.solver2); answerer = new ProblemAnswerer

(config.factory, config.solver); } void process(String mode) throws IOException { switch (mode) { case "create": creator.create(); break; case "debug": comparator.compare(); break; case "solve": answerer.answer(); break; } } } static class StringUtils { static String toString(int a) { return String.valueOf(a); } static String toString(long a) { return String.valueOf(a); } static String toString(double a) { return String.valueOf(a); } static String toString(String str) { return str; } static String toString(int[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { if (i < a.length - 1) { sb.append(a[i] + " "); } else { sb.append(a[i]); } } return sb.toString(); } static String toString(long[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { if (i < a.length - 1) { sb.append(a[i] + " "); } else { sb.append(a[i]); } } return sb.toString(); } static String toString(double[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { if (i < a.length - 1) { sb.append(a[i] + " "); } else { sb.append(a[i]); } } return sb.toString(); } static String toString(String[] strs) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < strs.length; i++) { if (i < strs.length - 1) { sb.append(strs[i] + " "); } else { sb.append(strs[i]); } } return sb.toString(); } static String toString(int[][] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j < a[i].length - 1) { sb.append(a[i][j] + " "); } else { sb.append(a[i][j]); } } sb.append("\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } static String toString(long[][] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j < a[i].length - 1) { sb.append(a[i][j] + " "); } else { sb.append(a[i][j]); } } sb.append("\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } static String toString(double[][] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j < a[i].length - 1) { sb.append(a[i][j] + " "); } else { sb.append(a[i][j]); } } sb.append("\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } static String toString(String[][] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j < a[i].length - 1) { sb.append(a[i][j] + " "); } else { sb.append(a[i][j]); } } sb.append("\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } static int toInt(String s) throws IOException { return Integer.parseInt(s); } static long toLong(String s) throws IOException { return Long.parseLong(s); } static double toDouble(String s) throws IOException { return Double.parseDouble(s); } static int[] toIntArray(String s) throws IOException { String[] tokens = s.split(" "); int[] a = new int[tokens.length]; for (int i = 0; i < tokens.length; i++) { a[i] = Integer.parseInt(tokens[i]); } return a; } static long[] toLongArray(String s) throws IOException { String[] tokens = s.split(" "); long[] a = new long[tokens.length]; for (int i = 0; i < tokens.length; i++) { a[i] = Long.parseLong(tokens[i]); } return a; } static double[] toDoubleArray(String s) throws IOException { String[] tokens = s.split(" "); double[] a = new double[tokens.length]; for (int i = 0; i < tokens.length; i++) { a[i] = Double.parseDouble(tokens[i]); } return a; } static String[] toStringArray(String s) throws IOException { return s.split(" "); } } }