exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
95d1bf1e5710cf7072f02f07e961e440
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s < h_i < h_b$$$ and $$$w_s < w_i < w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Scanner; /** * * 二位前缀和 * */ public class E { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); RealFastReader r = new RealFastReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = r.ni(); while (t-- > 0) { int n = r.ni(); int q = r.ni(); int[][] rec = new int[1001][1001]; for (int i = 0; i < n; i++) { int x= r.ni(); int y = r.ni(); rec[x][y] ++; } long[][] sum = new long[1001][1001]; for (int i = 1; i <=1000 ; i++) { for (int j = 1; j <=1000 ; j++) { sum[i][j] = sum[i-1][j]+sum[i][j-1] + (long)rec[i][j] * i * j -sum[i-1][j-1]; } } for (int i = 0; i < q; i++) { int x1 = r.ni(); int y1 = r.ni(); int x2 = r.ni(); int y2 = r.ni(); long ans = sum[x2-1][y2-1] - sum[x2-1][y1] - sum[x1][y2-1] + sum[x1][y1]; out.println(ans); } } out.close(); } static public class RealFastReader { InputStream is; public RealFastReader(final InputStream is) { this.is = is; } private byte[] inbuf = new byte[8192]; public int lenbuf = 0, ptrbuf = 0; public int readByte() { if (lenbuf == -1) { throw new InputMismatchException(); } if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) { return -1; } } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public double nd() { return Double.parseDouble(ns()); } public char nc() { return (char) skip(); } public String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) { map[i] = ns(m); } return map; } public int[][] nmi(int n, int m) { int[][] map = new int[n][]; for (int i = 0; i < n; i++) { map[i] = na(m); } return map; } public int ni() { int num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
e1856ec16f52207ee39329c9f075c72c
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.Scanner; /** * * 二位前缀和 * */ public class E { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int q = sc.nextInt(); int[][] rec = new int[1001][1001]; for (int i = 0; i < n; i++) { int x= sc.nextInt(); int y = sc.nextInt(); rec[x][y] ++; } long[][] sum = new long[1001][1001]; for (int i = 1; i <=1000 ; i++) { for (int j = 1; j <=1000 ; j++) { sum[i][j] = sum[i-1][j]+sum[i][j-1] + (long)rec[i][j] * i * j -sum[i-1][j-1]; } } for (int i = 0; i < q; i++) { int x1 = sc.nextInt(); int y1 = sc.nextInt(); int x2 = sc.nextInt(); int y2 = sc.nextInt(); long ans = sum[x2-1][y2-1] - sum[x2-1][y1] - sum[x1][y2-1] + sum[x1][y1]; System.out.println(ans); } } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
cce25c4d601063002c05caa651b95fb1
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.*; import java.util.*; public class Sort { static BufferedReader bf; static PrintWriter out; static Scanner sc; static StringTokenizer st; static long mod = (long)(1e9+7); static long mod2 = 998244353; static long[]fact = new long[200005]; static long[]invFact = new long[200005]; public static void main (String[] args)throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new Scanner(System.in); // solve(); int t = nextInt(); // fact[0] = 1; // invFact[0] = 1; // for(int i = 1;i<=200004;i++){ // fact[i] = (fact[i-1] * i)%mod; // invFact[i] = (binaryExpo(fact[i],mod-2)); // } while(t-->0){ solve(); } } public static void solve() throws IOException{ int n = nextInt(); int q = nextInt(); int[][]arr = new int[n][2]; long[][]dp = new long[1001][1001]; for(int i = 0 ;i<n;i++){ arr[i][0] = nextInt(); arr[i][1] = nextInt(); dp[arr[i][0]][arr[i][1]]++; } for(int i = 1;i<=1000;i++){ for(int j = 1;j<=1000;j++){ long cur = dp[i][j] * i * j ; dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] + cur; } // if(i >= 999){ // println(Arrays.toString(dp[i][1000])); // } } for(int i = 0;i<q;i++){ int sh = nextInt()+1; int sw = nextInt()+1; int bh = nextInt()-1; int bw = nextInt()-1; if(sh > bh || sw > bw){ out.println(0); } else{ long want = dp[bh][bw] - (dp[sh-1][bw] + dp[bh][sw-1]) + dp[sh-1][sw-1]; out.println(want); } } out.flush(); } public static void query(int l,int r){ out.println("? "+l + " "+r); out.flush(); } public static int min(int node,int low,int high,int tlow,int thigh,int[][]tree){ if(low >= tlow&& high <= thigh)return tree[node][0]; if(high < tlow || low > thigh)return Integer.MAX_VALUE; int mid = (low + high)/2; // println(low+" "+high+" "+tlow+" "+thigh); return Math.min(min(node*2,low,mid,tlow,thigh,tree) ,min(node*2+1,mid+1,high,tlow,thigh,tree)); } public static int max(int node,int low,int high,int tlow,int thigh,int[][]tree){ if(low >= tlow && high <= thigh)return tree[node][1]; if(high < tlow || low > thigh)return Integer.MIN_VALUE; int mid = (low + high)/2; return Math.max(max(node*2,low,mid,tlow,thigh,tree) ,max(node*2+1,mid+1,high,tlow,thigh,tree)); } public static long binaryExpo(long base,long expo){ if(expo == 0)return 1; long val = binaryExpo(base,expo/2); val = val * val % mod; if(expo%2 == 1){ val = base * val % mod; } return val; } public static long nCk(int n,int k){ if(k > n)return 0; return fact[n] * invFact[n-k] % mod * invFact[k] % mod; } public static boolean isSorted(int[]arr){ for(int i =1;i<arr.length;i++){ if(arr[i] < arr[i-1]){ return false; } } return true; } //function to find the topological sort of the a DAG public static boolean hasCycle(int[]indegree,List<List<Integer>>list,int n,List<Integer>topo){ Queue<Integer>q = new LinkedList<>(); for(int i =1;i<indegree.length;i++){ if(indegree[i] == 0){ q.add(i); topo.add(i); } } while(!q.isEmpty()){ int cur = q.poll(); List<Integer>l = list.get(cur); for(int i = 0;i<l.size();i++){ indegree[l.get(i)]--; if(indegree[l.get(i)] == 0){ q.add(l.get(i)); topo.add(l.get(i)); } } } if(topo.size() == n)return false; return true; } // function to find the parent of any given node with path compression in DSU public static int find(int val,int[]parent){ if(val == parent[val])return val; return parent[val] = find(parent[val],parent); } // function to connect two components public static void union(int[]rank,int[]parent,int u,int v){ int a = find(u,parent); int b= find(v,parent); if(a == b)return; if(rank[a] == rank[b]){ parent[b] = a; rank[a]++; } else{ if(rank[a] > rank[b]){ parent[b] = a; } else{ parent[a] = b; } } } // public static int findN(int n){ int num = 1; while(num < n){ num *=2; } return num; } // code for input public static void print(String s ){ System.out.print(s); } public static void print(int num ){ System.out.print(num); } public static void print(long num ){ System.out.print(num); } public static void println(String s){ System.out.println(s); } public static void println(int num){ System.out.println(num); } public static void println(long num){ System.out.println(num); } public static void println(){ System.out.println(); } public static int Int(String s){ return Integer.parseInt(s); } public static long Long(String s){ return Long.parseLong(s); } public static String[] nextStringArray()throws IOException{ return bf.readLine().split(" "); } public static String nextString()throws IOException{ return bf.readLine(); } public static long[] nextLongArray(int n)throws IOException{ String[]str = bf.readLine().split(" "); long[]arr = new long[n]; for(int i =0;i<n;i++){ arr[i] = Long.parseLong(str[i]); } return arr; } public static int[][] newIntMatrix(int r,int c)throws IOException{ int[][]arr = new int[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Integer.parseInt(str[j]); } } return arr; } public static long[][] newLongMatrix(int r,int c)throws IOException{ long[][]arr = new long[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Long.parseLong(str[j]); } } return arr; } static class pair{ int one; int two; pair(int one,int two){ this.one = one ; this.two =two; } } public static long gcd(long a,long b){ if(b == 0)return a; return gcd(b,a%b); } public static long lcm(long a,long b){ return (a*b)/(gcd(a,b)); } public static boolean isPalindrome(String s){ int i = 0; int j = s.length()-1; while(i<=j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } // these functions are to calculate the number of smaller elements after self public static void sort(int[]arr,int l,int r){ if(l < r){ int mid = (l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); smallerNumberAfterSelf(arr, l, mid, r); } } public static void smallerNumberAfterSelf(int[]arr,int l,int mid,int r){ int n1 = mid - l +1; int n2 = r - mid; int []a = new int[n1]; int[]b = new int[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[l+i]; } for(int i =0;i<n2;i++){ b[i] = arr[mid+i+1]; } int i = 0; int j =0; int k = l; while(i<n1 && j < n2){ if(a[i] < b[j]){ arr[k++] = a[i++]; } else{ arr[k++] = b[j++]; } } while(i<n1){ arr[k++] = a[i++]; } while(j<n2){ arr[k++] = b[j++]; } } public static String next(){ while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bf.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static double nextDouble() { return Double.parseDouble(next()); } static String nextLine(){ String str = ""; try { str = bf.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // use some math tricks it might help // sometimes just try to think in straightforward plan in A and B problems don't always complecate the questions with thinking too much differently // always use long number to do 10^9+7 modulo // if a problem is related to binary string it could also be related to parenthesis // *****try to use binary search(it is a very beautiful thing it can work in some of the very unexpected problems ) in the question it might work****** // try sorting // try to think in opposite direction of question it might work in your way // if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+d or a,b,c,d in general // if the question is to much related to left and/or right side of any element in an array then try monotonic stack it could work. // in range query sums try to do binary search it could work // analyse the time complexity of program thoroughly // anylyse the test cases properly // if we divide any number by 2 till it gets 1 then there will be (number - 1) operation required // try to do the opposite operation of what is given in the problem //think about the base cases properly //If a question is related to numbers try prime factorisation or something related to number theory // keep in mind unique strings //you can calculate the number of inversion in O(n log n) // in a matrix you could sometimes think about row and cols indenpendentaly. // Try to think in more constructive(means a way to look through various cases of a problem) way. // observe the problem carefully the answer could be hidden in the given test cases itself. (A, B , C); // when we have equations like (a+b = N) and we have to find the max of (a*b) then the values near to the N/2 must be chosen as (a and b); // give emphasis to the number of occurences of elements it might help. // if a number is even then we can find the pair for each and every number in that range whose bitwise AND is zero. // if each vertex of a graph has exactly one outgoing edge then the graph is called as the functional graph and it has one property that it has exactly one cycle some where in its all the components
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
fece09d906f482b12f0cbe856d065e9b
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class TaskE { public static void main(String[] args) { FastReader reader = new FastReader(); int tt = reader.nextInt(); // int tt = 1; for (; tt > 0; tt--) { int n = reader.nextInt(); int q = reader.nextInt(); int k = 1002; long[][] arr = new long[k][k]; for (int i = 0; i < n; i++) { int h = reader.nextInt(); int w = reader.nextInt(); arr[h+1][w+1] += (long)h*w; } long[][] sumArr = new long[k][k]; for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { sumArr[i][j] = arr[i][j]; if (i > 0) { sumArr[i][j] += sumArr[i-1][j]; } if (j > 0) { sumArr[i][j] += sumArr[i][j-1]; } if (i > 0 && j > 0) { sumArr[i][j] -= sumArr[i-1][j-1]; } } } for (int i = 0; i < q; i++) { int hs = reader.nextInt(); int ws = reader.nextInt(); int hb = reader.nextInt(); int wb = reader.nextInt(); System.out.println(sumArr[hb][wb] - sumArr[hs + 1][wb] - sumArr[hb][ws + 1] + sumArr[hs + 1][ws + 1]); } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if (st.hasMoreTokens()) { str = st.nextToken("\n"); } else { str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
a1ccfdc9af1884700aa33ae0af7725b2
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.*; import java.util.*; public class CodeForces { /*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/ public static void solve(int tCase) throws IOException { int n = sc.nextInt(); int q = sc.nextInt(); int mx = 1010; long [][] dp = new long[mx][mx]; for(int i=0;i<n;i++){ int x = sc.nextInt(); int y = sc.nextInt(); dp[x][y] += x*y; } for(int i=mx-2;i>0;i--) for(int j=mx-2;j>0;j--){ dp[i][j] += dp[i+1][j]; dp[i][j] += dp[i][j+1]; dp[i][j] -=dp[i+1][j+1]; } // for(int i=0;i<=10;i++) { // for (int j = 0; j <= 10; j++) // out.print(dp[i][j]+" "); // out.println(); // } for(int i=0;i<q;i++){ int hl = sc.nextInt(); int wl = sc.nextInt(); int hu= sc.nextInt(); int wu = sc.nextInt(); // if(hu ==hl+1 || wu == wl+1){ // out.println(0); // continue; // } hl++;wl++; long ans = dp[hl][wl]; ans -= dp[hu][wl]; ans -=dp[hl][wu]; ans += dp[hu][wu]; out.println(ans); } } public static void main(String[] args) throws IOException { openIO(); int testCase = 1; testCase = sc.nextInt(); for (int i = 1; i <= testCase; i++) solve(i); closeIO(); } /*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/ /*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/ public static int mod = (int) 1e9 + 9; // public static int mod = 998244353; public static int inf_int = (int)1e9; public static long inf_long = (long)2e15; public static void _sort(int[] arr, boolean isAscending) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } public static void _sort(long[] arr, boolean isAscending) { int n = arr.length; List<Long> list = new ArrayList<>(); for (long ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // time : O(1), space : O(1) public static int _digitCount(long num,int base){ // this will give the # of digits needed for a number num in format : base return (int)(1 + Math.log(num)/Math.log(base)); } // time for pre-computation of factorial and inverse-factorial table : O(nlog(mod)) public static long[] factorial , inverseFact; public static void _ncr_precompute(int n){ factorial = new long[n+1]; inverseFact = new long[n+1]; factorial[0] = inverseFact[0] = 1; for (int i = 1; i <=n; i++) { factorial[i] = (factorial[i - 1] * i) % mod; inverseFact[i] = _modExpo(factorial[i], mod - 2); } } // time of factorial calculation after pre-computation is O(1) public static int _ncr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod); } public static int _npr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[n - r] % mod); } // euclidean algorithm time O(max (loga ,logb)) public static long _gcd(long a, long b) { while (a>0){ long x = a; a = b % a; b = x; } return b; } // lcm(a,b) * gcd(a,b) = a * b public static long _lcm(long a, long b) { return (a / _gcd(a, b)) * b; } // binary exponentiation time O(logn) public static long _modExpo(long x, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans *= x; ans %= mod; n--; } else { x *= x; x %= mod; n >>= 1; } } return ans; } // function to find a/b under modulo mod. time : O(logn) public static long _modInv(long a,long b){ return (a * _modExpo(b,mod-2)) % mod; } //sieve or first divisor time : O(mx * log ( log (mx) ) ) public static int[] _seive(int mx){ int[] firstDivisor = new int[mx+1]; for(int i=0;i<=mx;i++)firstDivisor[i] = i; for(int i=2;i*i<=mx;i++) if(firstDivisor[i] == i) for(int j = i*i;j<=mx;j+=i) if(firstDivisor[j]==j)firstDivisor[j] = i; return firstDivisor; } static class Pair<K, V>{ K ff; V ss; public Pair(K ff, V ss) { this.ff = ff; this.ss = ss; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || this.getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return ff.equals(pair.ff) && ss.equals(pair.ss); } @Override public int hashCode() { return Objects.hash(ff, ss); } @Override public String toString(){ return ff.toString()+" "+ss.toString(); } } /*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/ /*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/ static FastestReader sc; static PrintWriter out; private static void openIO() throws IOException { sc = new FastestReader(); out = new PrintWriter(System.out); } public static void closeIO() throws IOException { out.flush(); out.close(); sc.close(); } private static final class FastestReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public FastestReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastestReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) {} return b; } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); final boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return neg?-ret:ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); final boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return neg?-ret:ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); final boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); return neg?-ret:ret; } public String nextLine() throws IOException { final byte[] buf = new byte[(1<<10)]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } /*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/ } /** Some points to keep in mind : * 1. don't use Arrays.sort(primitive data type array) * 2. try to make the parameters of a recursive function as less as possible, * more use static variables. * 3. If n = 5000, then O(n^2 logn) need atleast 4 sec to work * 4. dp[2][n] works faster than dp[n][2] * 5. if split wrt 1 char use '\\' before char: .split("\\."); * **/
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
943ba93fab67d1b2c5635c84546bf3c5
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; public class Main { static Scanner sc; static PrintWriter pw; public static void main(String[] args) throws IOException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(), q = sc.nextInt(); int max = 1001; long[][] arr = new long[max][max]; for (int i = 0; i < n; i++) { int row = sc.nextInt(); int idx = sc.nextInt(); arr[row][idx] += row * idx; } for (int i = 1; i < max; i++) for (int j = 1; j < max; j++) arr[i][j] += arr[i][j - 1]; for (int i = 1; i < max; i++) for (int j = 1; j < max; j++) arr[j][i] += arr[j - 1][i]; while (q-- > 0) { int h1 = sc.nextInt(), w1 = sc.nextInt(); int h2 = sc.nextInt(), w2 = sc.nextInt(); pw.println(arr[h2 - 1][w2 - 1] + arr[h1][w1] - arr[h2 - 1][w1] - arr[h1][w2 - 1]); } } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
d0f98561a3114e0a1d124fd56f2e9d46
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; public class Main { static Scanner sc; static PrintWriter pw; public static void main(String[] args) throws IOException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(), q = sc.nextInt(); int max = 1001; long[][] arr = new long[max][max]; for (int i = 0; i < n; i++) { int row = sc.nextInt(); int idx = sc.nextInt(); arr[row][idx] += row * idx; } // pw.println(Arrays.deepToString(arr)); for (int i = 1; i < max; i++) for (int j = 1; j < max; j++) arr[i][j] += arr[i][j - 1]; // pw.println(Arrays.deepToString(arr)); while (q-- > 0) { int h1 = sc.nextInt(), w1 = sc.nextInt(); int h2 = sc.nextInt(), w2 = sc.nextInt(); long res = 0; for (int i = h1 + 1; i < h2; i++) { res += arr[i][w2 - 1] - arr[i][w1]; } pw.println(res); } } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
737dcb4855d161924f85bd474882506a
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++) { int n=sc.nextInt(); int q=sc.nextInt(); long[][] dp=new long[1001][1001]; for(int j=0;j<n;j++) { int x=sc.nextInt(); int y=sc.nextInt(); dp[x][y]+=(long)x*y; } long[][] pre=new long[1001][1001]; for(int j=1;j<1001;j++) { for(int k=1;k<1001;k++) { pre[j][k]=pre[j-1][k]+pre[j][k-1]-pre[j-1][k-1]+dp[j][k]; } } for(int j=0;j<q;j++) { int x1=sc.nextInt(); int y1=sc.nextInt(); int x2=sc.nextInt(); int y2=sc.nextInt(); System.out.println(deal(pre,x1,y1,x2,y2)); } } } private static long deal(long[][] pre, int x1, int y1, int x2, int y2) { return pre[x2-1][y2-1]-pre[x2-1][y1]-pre[x1][y2-1]+pre[x1][y1]; } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
16df7d27ab052a12b31f52f2607d8e80
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; public class Main { static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws Exception { int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int q = sc.nextInt(); long[][] a = new long[1001][1001]; for (int i = 0; i < n; i++) { int h = sc.nextInt(); int w = sc.nextInt(); a[h][w] += h*w; } for (int i = 1; i < a.length; i++) { for (int j = 1; j < a.length; j++) { a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1]; } } while(q-->0) { int hs = sc.nextInt(); int ws = sc.nextInt(); int hb = sc.nextInt(); int wb = sc.nextInt(); if(hb-hs<=1||wb-ws<=1) { pw.println(0); }else { pw.println(a[hb-1][wb-1] - a[hs][wb-1] - a[hb-1][ws] + a[hs][ws]); } } } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } public int[] nextArrint(int size) throws IOException { int[] a = new int[size]; for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt(); } return a; } public long[] nextArrlong(int size) throws IOException { long[] a = new long[size]; for (int i = 0; i < a.length; i++) { a[i] = sc.nextLong(); } return a; } public int[][] next2dArrint(int rows, int columns) throws IOException { int[][] a = new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { a[i][j] = sc.nextInt(); } } return a; } public long[][] next2dArrlong(int rows, int columns) throws IOException { long[][] a = new long[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { a[i][j] = sc.nextLong(); } } return a; } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
88b501f13ce657fe41b9483a30085a51
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.*; import java.util.*; public class Main { public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void solveE(long[][] arr, int[][] queries, int n, int q){ int BOUND = 1000; long[][] memo = new long[1011][1011]; for(long[] x: arr){ int h = (int) x[0]; int w = (int) x[1]; memo[h][w] += x[0]*x[1]; } for(int i = 1; i <= BOUND; i++){ for(int j = 1; j <= BOUND; j++){ memo[i][j] = memo[i-1][j] + memo[i][j-1] - memo[i-1][j-1] + memo[i][j]; } } for(int[] x: queries){ long b = memo[x[2]-1][x[3]-1]; b -= memo[x[2] - 1][x[1]]; b -= memo[x[0]][x[3] - 1]; b += memo[x[0]][x[1]]; out.println(Math.max(0, b)); } } public static void main(String[] args){ MyScanner scanner = new MyScanner(); int numOfTests = scanner.nextInt(); for(int t = 1; t <= numOfTests; t++){ int n = scanner.nextInt(); int q = scanner.nextInt(); long[][] arr = new long[n][2]; for(int i = 0; i < n; i++){ arr[i][0] = scanner.nextLong(); arr[i][1] = scanner.nextLong(); } int[][] queries = new int[q][4]; for(int i = 0; i < q; i++){ queries[i][0] = scanner.nextInt(); queries[i][1] = scanner.nextInt(); queries[i][2] = scanner.nextInt(); queries[i][3] = scanner.nextInt(); } solveE(arr, queries, n, q); } out.close(); } private static void solve(int n){ } private static void verify(int[] arr){ int odd = 0, even = 0; for(int i = 0; i < arr.length; i++){ if(i % 2 == 1){ odd ^= arr[i]; } else{ even ^= arr[i]; } } if(odd != even){ out.println("Wrong Answer"); } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
3c258e896911b3fbfc30bd1b1a97426f
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++) { int n=sc.nextInt(); int q=sc.nextInt(); long[][] dp=new long[1001][1001]; for(int j=0;j<n;j++) { int x=sc.nextInt(); int y=sc.nextInt(); dp[x][y]+=(long)x*y; } long[][] pre=new long[1001][1001]; for(int j=1;j<1001;j++) { for(int k=1;k<1001;k++) { pre[j][k]=pre[j-1][k]+pre[j][k-1]-pre[j-1][k-1]+dp[j][k]; } } for(int j=0;j<q;j++) { int x1=sc.nextInt(); int y1=sc.nextInt(); int x2=sc.nextInt(); int y2=sc.nextInt(); System.out.println(deal(pre,x1,y1,x2,y2)); } } } private static long deal(long[][] pre, int x1, int y1, int x2, int y2) { return pre[x2-1][y2-1]-pre[x2-1][y1]-pre[x1][y2-1]+pre[x1][y1]; } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
712f2c75bc873ecc70101431eec75a86
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; public class Main { // static int [] arr; // static boolean prime[] = new boolean[1000]; // static int l; // static String s; static StringBuilder sb; // static HashSet<L> hs; // static HashSet<Long> hs = new HashSet<Long>(); // static int ans; // static boolean checked []; //static final int mod = 1000000007; // static int[][] dp; // static int[] w ,v; static int n,k; // static int arr[][]; // static long ans; static Scanner sc; static PrintWriter out; //static int n, m, w, t; // static char [] a , b; //static StringBuilder sb; // static int ans; static int arr[]; static long[] sum; static long dp[][]; static long ans; //static char[] []arr; static ArrayList<Integer> v; public static void main(String[] args) throws IOException, InterruptedException { sc = new Scanner(System.in); out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int q = sc.nextInt(); long arr[][] = new long[1002][1002]; for(int i = 0 ; i < n ; i++) { int x = sc.nextInt(); int y = sc.nextInt(); arr[x][y]++; } long[][] cumsumpr = new long[1002][1002]; for(int i = 1 ; i <= 1000 ; i++) { cumsumpr[i][1] = arr[i][1] * i; for(int j = 2 ; j <= 1000 ; j++) { cumsumpr[i][j] = cumsumpr[i][j-1] + arr[i][j]*i*j; } } for(int i = 0 ; i < q ;i++) { int as = sc.nextInt(); int bs = sc.nextInt(); int ab = sc.nextInt(); int bb = sc.nextInt(); long ans = 0; for(int j = as+1 ; j <ab ; j++) { ans+=cumsumpr[j][bb-1] - cumsumpr[j][bs]; } out.println(ans); } } out.close(); } public static void solve() { } public static int bs(int target) { int l = 0; int r = v.size()-1; while(l<=r) { int m = (l+r)/2; if(v.get(m) < target) l = m+1; else r = m-1; } return l; } public static void swap(int i , int j ,int [] arr) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // public static int trace(int idx,int cost){ // if(cost> t || idx == m) // return 0; // int take = v[idx] + solve(idx+1,cost + 3*w*d[idx]); // //int leave = solve(idx+1,t); // if(dp[idx][cost] == take){ // sb.append(d[idx]+" "+v[idx]+"\n"); // return 1 + trace(idx+1,cost+3*w*d[idx]); // } // // return trace(idx+1,cost); // } private static void reverse(long[] arr) { // TODO Auto-generated method stub } // recursive implementation static long arrayLCM(int[] arr, int idx) { // lcm(a,b) = (a*b/gcd(a,b)) if (idx == arr.length - 1) { return arr[idx]; } long a = arr[idx]; long b = arrayLCM(arr, idx + 1); return (a * b / gcd(a, b)); // } static int longestSubarrWthSumDivByK(int arr[], int n, int k) { // unordered map 'um' implemented as // hash table HashMap<Integer, Integer> um = new HashMap<Integer, Integer>(); // 'mod_arr[i]' stores (sum[0..i] % k) int mod_arr[] = new int[n]; int max_len = 0; long curr_sum = 0; // traverse arr[] and build up the // array 'mod_arr[]' for (int i = 0; i < n; i++) { curr_sum += arr[i]; // as the sum can be negative, // taking modulo twice mod_arr[i] = (int) ((curr_sum % k) + k) % k; // if true then sum(0..i) is // divisible by k if (mod_arr[i] == 0) // update 'max' max_len = i + 1; // if value 'mod_arr[i]' not present in 'um' // then store it in 'um' with index of its // first occurrence else if (um.containsKey(mod_arr[i]) == false) um.put(mod_arr[i], i); else // if true, then update 'max' if (max_len < (i - um.get(mod_arr[i]))) max_len = i - um.get(mod_arr[i]); } // return the required length of longest subarray // with sum divisible by 'k' return max_len; } static int longestSubArrayOfSumK(int[] arr, int n, int k) { // HashMap to store (sum, index) tuples HashMap<Integer, Integer> map = new HashMap<>(); int sum = 0, maxLen = 0; // traverse the given array for (int i = 0; i < n; i++) { // accumulate sum sum += arr[i]; // when subarray starts from index '0' if (sum == k) maxLen = i + 1; // make an entry for 'sum' if it is // not present in 'map' if (!map.containsKey(sum)) { map.put(sum, i); } // check if 'sum-k' is present in 'map' // or not if (map.containsKey(sum - k)) { // update maxLength if (maxLen < (i - map.get(sum - k))) maxLen = i - map.get(sum - k); } } return maxLen; } static boolean isPrime(long n) { if (n == 2 || n == 3) return true; if (n <= 1 || n % 2 == 0 || n % 3 == 0) return false; // To check through all numbers of the form 6k ± 1 for (long i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } static long smallestDivisor(long n) { // if divisible by 2 if (n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for (long i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } static long nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } static long fact(int n) { long res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static boolean isSorted(int[] arr) { for (int i = 0; i < arr.length - 1; i++) if (arr[i] > arr[i + 1]) return false; return true; } // static void findsubsequences(String s, String ans){ // if (s.length() == 0) { // if(ans!="") // if(ans.length()!=l) // al.add(Long.parseLong(ans)); // return; // } // // // We add adding 1st character in string // findsubsequences(s.substring(1), ans + s.charAt(0)); // // // Not adding first character of the string // // because the concept of subsequence either // // character will present or not // findsubsequences(s.substring(1), ans); //} static void sieve(int n, boolean[] prime, List<Integer> al) { for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p]) { al.add(p); for (int i = p * p; i <= n; i += p) prime[i] = false; } } } // Print all prime numbers // for(int i = 2; i <= n; i++) // { // if(prime[i] == true) // System.out.print(i + " "); // } public static void reverse(Object[] arr) { int i = 0; int j = arr.length - 1; while (i < j) { Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } } class Pair implements Comparable<Pair> { int a; int b; public Pair(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return this.a - o.a; } public String toString() { return "( " + this.a + " , " + this.b + " )\n"; } } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public int[][] nextInt2DArr(int l, int w) throws IOException { int[][] arr = new int[l][w]; for (int i = 0; i < l; i++) for (int j = 0; j < w; j++) arr[i][j] = Integer.parseInt(next()); return arr; } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextIntArr(int length) throws IOException { int[] arr = new int[length]; for (int i = 0; i < length; i++) arr[i] = Integer.parseInt(next()); return arr; } public long[] nextLongArr(int length) throws IOException { long[] arr = new long[length]; for (int i = 0; i < length; i++) arr[i] = Long.parseLong(next()); return arr; } public boolean ready() throws IOException { return br.ready(); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
c4b361edd90d8872ebd083c9a09b2638
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; import java.math.*; /** * @author Naitik * */ public class Main { static FastReader sc=new FastReader(); static int dp[][]; // static boolean v[][][]; // static int mod=998244353;; static int mod=1000000007; static long max; static int bit[]; static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { // StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); outer :while (ttt-- > 0) { int n=i(); int q=i(); int A[]=new int[n]; int B[]=new int[n]; input(A, B); int C[][]=new int[1001][1001]; for(int i=0;i<n;i++) { C[A[i]][B[i]]++; } int D[][]=new int[1001][1001]; for(int i=1;i<1001;i++) { for(int j=1;j<1001;j++) { D[i][j]=D[i][j-1]+C[i][j]*j; } } while(q-->0) { int h1=i(); int w1=i(); int h2=i(); int w2=i(); long ans=0; for(int i=h1+1;i<h2;i++) { long sum=D[i][w2-1]-D[i][w1]; ans+=i*sum; } out.println(ans); } } //System.out.println(sb.toString()); out.close(); //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 } // public static void main(String args[]) { // Scanner sc=new Scanner(System.in); // // } static class Pair implements Comparable<Pair> { int x; int y; int z; Pair(int x,int y){ this.x=x; this.y=y; // this.z=z; } @Override public int compareTo(Pair o) { if(this.x>o.x) return 1; else if(this.x<o.x) return -1; else { if(this.y>o.y) return 1; else if(this.y<o.y) return -1; else return 0; } } public int hashCode() { final int temp = 14; long ans = 1; ans =x*31+y*13; return (int)ans; } // @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null) { return false; } if (this.getClass() != o.getClass()) { return false; } Pair other = (Pair)o; if (this.x != other.x || this.y!=other.y) { return false; } return true; } // /* FOR TREE MAP PAIR USE */ // public int compareTo(Pair o) { // if (x > o.x) { // return 1; // } // if (x < o.x) { // return -1; // } // if (y > o.y) { // return 1; // } // if (y < o.y) { // return -1; // } // return 0; // } } //static int find(int A[],int a) { // if(A[a]==a) // return a; // return A[a]=find(A, A[a]); //} //FENWICK TREE static void update(int i, int x){ for(; i < bit.length; i += (i&-i)) bit[i] += x; } static int sum(int i){ int ans = 0; for(; i > 0; i -= (i&-i)) ans += bit[i]; return ans; } //END static void add(HashMap<Long,Integer> map,long v) { if(!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v)+1); } } static void remove(HashMap<Long,Integer> map,long v) { if(map.containsKey(v)) { map.put(v, map.get(v)-1); if(map.get(v)==0) map.remove(v); } } static void add(HashMap<Integer,Integer> map,int v) { if(!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v)+1); } } static void remove(HashMap<Integer,Integer> map,int v) { if(map.containsKey(v)) { map.put(v, map.get(v)-1); if(map.get(v)==0) map.remove(v); } } static void add(TreeMap<Integer,Integer> map,int v) { if(!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v)+1); } } static void add(TreeMap<Long,Integer> map,long v) { if(!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v)+1); } } static void remove(TreeMap<Integer,Integer> map,int v) { if(map.containsKey(v)) { map.put(v, map.get(v)-1); if(map.get(v)==0) map.remove(v); } } static void remove(TreeMap<Long,Integer> map,long v) { if(map.containsKey(v)) { map.put(v, map.get(v)-1); if(map.get(v)==0) map.remove(v); } } static long modInverse(long n, int p) { return power(n, p - 2, p); } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void reverse(long A[]) { int n=A.length; long B[]=new long[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void reverse(int A[]) { int n=A.length; int B[]=new int[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static int[][] input(int n,int m){ int A[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=i(); } } return A; } static char[][] charinput(int n,int m){ char A[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) { A[i][j]=s.charAt(j); } } return A; } static int nextPowerOf2(int n) { if(n==0) return 1; n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } static int highestPowerof2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static long highestPowerof2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long [] prefix(long A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] prefix(int A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] suffix(long A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static long [] suffix(int A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static void fill(int dp[]) { Arrays.fill(dp, -1); } static void fill(int dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(int dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(int dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static void fill(long dp[]) { Arrays.fill(dp, -1); } static void fill(long dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(long dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(long dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long power(long x, long y, long p) { if(y==0) return 1; if(x==0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static void print(int A[]) { for(int i : A) { out.print(i+" "); } out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Integer> hash(int A[]){ HashMap<Integer,Integer> map=new HashMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static HashMap<Long,Integer> hash(long A[]){ HashMap<Long,Integer> map=new HashMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Integer,Integer> tree(int A[]){ TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Long,Integer> tree(long A[]){ TreeMap<Long,Integer> map=new TreeMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
59cb1bb1f1d5268ba490a4cc59019f92
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.Scanner; public class E1722 { public static void main(String[] args) { Scanner in = new Scanner(System.in); StringBuilder out = new StringBuilder(); int T = in.nextInt(); for (int t=0; t<T; t++) { long[][] stat = new long[1001][1001]; int N = in.nextInt(); int Q = in.nextInt(); for (int n=0; n<N; n++) { int H = in.nextInt(); int W = in.nextInt(); stat[H][W] += H*W; } for (int h=0; h<=1000; h++) { for (int w=1; w<=1000; w++) { stat[h][w] += stat[h][w-1]; } } for (int w=0; w<=1000; w++) { for (int h=1; h<=1000; h++) { stat[h][w] += stat[h-1][w]; } } for (int q=0; q<Q; q++) { int HS = in.nextInt(); int WS = in.nextInt(); int HB = in.nextInt()-1; int WB = in.nextInt()-1; long answer = stat[HB][WB] - stat[HB][WS] - stat[HS][WB] + stat[HS][WS]; out.append(answer).append('\n'); } } System.out.print(out); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
9669336d43e4b0e8cf9e43c27c9fba06
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.Scanner; public class CountingRectangles { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t > 0) { solve(scanner); t--; } } public static void solve(Scanner scanner) { int n = scanner.nextInt(); int q = scanner.nextInt(); long[][] nums = new long[1010][1010]; long[][] sum = new long[nums.length + 1][nums.length + 1]; for (int i = 0; i < n; i++) { int h = scanner.nextInt(); int w = scanner.nextInt(); nums[h][w] += (long)h * w; } for (int i = 1; i < sum.length; i++) { for (int j = 1; j < sum[0].length; j++) { sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + nums[i - 1][j - 1]; } } for (int i = 0; i < q; i++) { int h1 = scanner.nextInt()+1; int w1 = scanner.nextInt()+1; int h2 = scanner.nextInt()-1; int w2 = scanner.nextInt()-1; h1++;h2++;w1++;w2++; long ans = sum[h2][w2]-sum[h2][w1-1]-sum[h1-1][w2]+sum[h1-1][w1-1]; System.out.println(ans); } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
ea22868aa8f4dc38f3fe4e8e7ae7774d
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.*; import java.util.*; public class AAAPractice { BufferedReader br; StringTokenizer st; BufferedWriter bw; public static void main(String[] args) throws Exception { new AAAPractice().run(); } void run() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out)); solve(); } void solve() throws IOException { int t = ni(); while (t-- > 0) { int n = ni(); int q = ni(); data1[] input = dataArray1(n); data[] query = dataArray(q); long ans[][] = new long[1001][1001]; for (int i = -1; ++i < n;) { int x = input[i].h; int y = input[i].w; ans[x][y] += x * y; } for (int i = 0; ++i < 1001;) ans[0][i] += ans[0][i - 1]; for (int i = 0; ++i < 1001;) ans[i][0] += ans[i - 1][0]; for (int i = 0; ++i < 1001;) { for (int j = 0; ++j < 1001;) ans[i][j] += ans[i - 1][j] + ans[i][j - 1] - ans[i - 1][j - 1]; } for (int i = -1; ++i < q;) { long a = 0; int x1 = query[i].a; int x2 = query[i].c; int y1 = query[i].b; int y2 = query[i].d; if (Math.abs(x1 - x2) < 2 || Math.abs(y1 - y2) < 2) a = 0; else a = ans[x2 - 1][y2 - 1] - ans[x2 - 1][y1] - ans[x1][y2 - 1] + ans[x1][y1]; bw.write(a + "\n"); } } bw.flush(); } /////////////////////////////////////// FOR INPUT /////////////////////////////////////// /////////////////////////////////////// public static class data1 { int h, w; public data1(int a, int b) { h = a; w = b; } } public data1[] dataArray1(int n) { data1 d[] = new data1[n]; for (int i = -1; ++i < n;) d[i] = new data1(ni(), ni()); return d; } public static class data { int a, b, c, d; public data(int a, int b, int x, int y) { this.a = a; this.b = b; c = x; d = y; } } public data[] dataArray(int n) { data d[] = new data[n]; for (int i = -1; ++i < n;) d[i] = new data(ni(), ni(), ni(), ni()); return d; } int[] nai(int n) { int a[] = new int[n]; for (int i = -1; ++i < n;) a[i] = ni(); return a; } Integer[] naI(int n) { Integer a[] = new Integer[n]; for (int i = -1; ++i < n;) a[i] = ni(); return a; } long[] nal(int n) { long a[] = new long[n]; for (int i = -1; ++i < n;) a[i] = nl(); return a; } char[] nac() { char c[] = nextLine().toCharArray(); return c; } char[][] nmc(int n) { char c[][] = new char[n][]; for (int i = -1; ++i < n;) c[i] = nac(); return c; } int[][] nmi(int r, int c) { int a[][] = new int[r][c]; for (int i = -1; ++i < r;) a[i] = nai(c); return a; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } byte nb() { return Byte.parseByte(next()); } short ns() { return Short.parseShort(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
3faebb2471e87cdec09e9e027162589a
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class countingRectangles { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tests = sc.nextInt(); while(tests-->0) { long[][] array = new long[1001][1001]; int n = sc.nextInt(); int queries = sc.nextInt(); while(n-->0) { int h = sc.nextInt(); int w = sc.nextInt(); array[h][w] += h*w; } //2D prefix for(int i = 0; i < 1001; i++) { for(int j = 1; j < 1001; j++) { array[i][j] += array[i][j-1]; } } for(int j = 0 ; j < 1001 ; j++) { for(int i = 1; i < 1001 ; i++) { array[i][j] += array[i-1][j]; } } while(queries-->0) { int hs = sc.nextInt(); int ws = sc.nextInt(); int hb = sc.nextInt() -1 ; int wb = sc.nextInt() -1 ; long answer = array[hb][wb] - array[hb][ws] - array[hs][wb] + array[hs][ws]; System.out.println(answer); } } sc.close(); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
f9840ea8444bfff83c3328ba727e3f2b
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
// Mahakal // Remainder: Agar ni ban raha to demotivate ni hona. Yehi chance hai sikhne ka. import java.util.*; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Main { static PrintWriter out = new PrintWriter(System.out); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } } static FastReader sc; public static void main(String[] args) { sc = new FastReader(); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int q = sc.nextInt(); int r = 1002; int c = 1002; long sum[][] = new long[r][c]; for(int i=0;i<n;i++) { int h = sc.nextInt(); int w = sc.nextInt(); sum[h][w] += (h*w); } for(int i=1;i<r;i++) sum[0][i]+=sum[0][i-1]; for(int i=1;i<c;i++) sum[i][0]+=sum[i-1][0]; for(int i=2;i<r;i++) { for(int j=2;j<c;j++) { sum[i][j] = sum[i][j]+ sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1]; } } while(q-->0) { long ans = 0l; int hs = sc.nextInt()+1; int ws = sc.nextInt()+1; int hb = sc.nextInt()-1; int wb = sc.nextInt()-1; ans = sum[hb][wb]-sum[hb][ws-1]-sum[hs-1][wb]+sum[hs-1][ws-1]; println(ans); } } out.flush(); out.close(); } public static int lowerbound(int l,int r,int a[],int val) { int ans = l-1; while(l<=r) { int mid = l+(r-l)/2; if(a[mid]<val) { ans = mid; l = mid+1; }else r =mid-1; } return ans; } // 2 // 1 2 2 2 3 4 4 5 5 5 public static int dis(int x,int y) { return Math.abs(x-y); } public static int dir[][] = {{1,0},{0,1},{-1,0},{0,-1}}; public static class pair{ int v1; int v2; pair(int v1,int v2){ this.v1 = v1; this.v2 = v2; } } public static class graph{ int v; int maxbit; ArrayList<int[]> edgelist = new ArrayList<>(); ArrayList<int[]> adj[]; public static int[][] table; graph(int v){ this.v = v; adj = new ArrayList[v]; for(int i=0;i<v;i++) adj[i] = new ArrayList<>(); maxbit = 19; } public void addD(int f,int t){ int a[] = {f,t}; adj[f].add(a); edgelist.add(new int[] {f,t}); } public void addUD(int f,int t) { addD(f,t); addD(t,f); } public void addD(int f,int t,int w){ int a[] = {f,t,w}; adj[f].add(a); edgelist.add(new int[] {f,t,w}); } public void addUD(int f,int t,int w) { addD(f,t,w); addD(t,f,w); } public void printEdgelist() { for(int ele[]:edgelist) { println(ele); } } public void parentArray(int par[],int src,int parent) { for(int ele[]:adj[src]) { int t = ele[1]; if(t==parent) continue; par[t] = src; parentArray(par,t,src); } } public boolean IsBipartite(int i,boolean visited[]) { int color[] = new int[v]; Arrays.fill(color, -1); Queue<int[]> queue = new ArrayDeque<>(); queue.add(new int[] {i,1,-1}); while(queue.size()>0) { int top[] = queue.poll(); int src = top[0]; int pc = top[1]; int parent = top[2]; visited[src] = true; if(color[src]!=-1) { if(color[src]==pc) return false; }else color[src] = 1-pc; for(int ele[]:adj[src]) { int t = ele[1]; if(t==parent) continue; else { if(!visited[t]) queue.add(new int[] {t,color[src],src}); } } } return true; } public void printGraph() { for(int i=0;i<v;i++) { out.print(i+" - "); for(int ele[]:adj[i]) { out.print(ele[1]+" "); } out.println(); } } public void fillLevel(int src,int d,int level[],int parent) { level[src] = d; for(int ele[]:this.adj[src]) { int t = ele[1]; if(t==parent) continue; fillLevel(t,d+1,level,src); } } public void LcaTable(int parent[]) { table = new int[maxbit+1][v]; table[0] = parent; for(int i=1;i<=maxbit;i++) { for(int j=0;j<v;j++) { table[i][j] = table[i-1][table[i-1][j]]; } } } public int getLCA(int u,int v,int level[],int parent[]) { if(level[u]>level[v]) { int temp = u; u = v; v = temp; } int k = level[v]-level[u]; for(int i=0;i<maxbit;i++) { int bit = 1<<i; if((bit&k)>0) { v = table[i][v]; } } if(v==u) return u; for(int i=maxbit;i>=0;i--) { int p1 = table[i][u]; int p2 = table[i][v]; if(p1==p2) continue; else { u = p1; v = p2; } } return table[0][u]; } } //============= For loop bhi ni likha jaa raha====================== public static int[] Intfor(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = sc.nextInt(); return a; } public static long[] Longfor(int n) { long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = sc.nextLong(); return a; } public static double[] Doublefor(int n) { double a[] = new double[n]; for(int i=0;i<n;i++) a[i] = sc.nextDouble(); return a; } public static String[] Stringfor(int n) { String a[] = new String[n]; for(int i=0;i<n;i++) a[i] = sc.next(); return a; } // =========================MergeSort Krdo===================================== public static int[] mergeSort(int arr[],int l,int r) { if(l>r) return new int[] {}; if(l==r) return new int[] {arr[l]}; int mid = l+(r-l)/2; int larr[] = mergeSort(arr,l,mid); int rarr[] = mergeSort(arr,mid+1,r); int newarr[] = merge(larr,rarr); return newarr; } public static int[] merge(int a[],int b[]) { int m[] = new int[a.length+b.length]; int i=0; int j=0; int ind = 0; while(i<a.length && j<b.length) { if(a[i]<b[j]) { m[ind] = a[i]; i++;ind++; }else { m[ind++] = b[j++]; } } while(i<a.length) m[ind++] = a[i++]; while(j<b.length) m[ind++] = b[j++]; return m; } // =================== Fast Print krne ke lie itna mehnat================== public static void print(String str) { out.print(str); } public static void println() { out.println(); } public static void print(int a) { out.print(a); } public static void print(char c) { out.print(c); } public static void print(char c[]) { out.print(Arrays.toString(c)); } public static void print(int a[]) { out.println(Arrays.toString(a)); } public static void print(String a[]) { out.print(Arrays.toString(a)); } public static void print(float a) { out.print(a); } public static void print(double a) { out.print(a); } public static void print(long a[]) { out.print(Arrays.toString(a)); } public static void println(String str) { out.println(str); } public static void println(int a) { out.println(a); } public static void println(char c) { out.println(c); } public static void println(char c[]) { out.println(Arrays.toString(c)); } public static void println(int a[]) { out.println(Arrays.toString(a)); } public static void println(String a[]) { out.println(Arrays.toString(a)); } public static void println(float a) { out.println(a); } public static void println(double a) { out.println(a); } public static void println(long a[]) { out.println(Arrays.toString(a)); } public static void print(int a[][]) { for(int ele[]:a) { out.println(Arrays.toString(ele)); } out.println(); } public static void print(char a[][]) { for(char ele[]:a) { out.println(Arrays.toString(ele)); } } public static void print(long a[][]) { for(long ele[]:a) { out.println(Arrays.toString(ele)); } } public static void print(String a[][]) { for(String ele[]:a) { out.println(Arrays.toString(ele)); } } public static void println(long a) { out.println(a); } public static void println(Long a) { out.println(a); } public static void println(Integer a) { out.println(a); } public static void println(Long a[]) { out.println(Arrays.toString(a)); } public static void println(Integer a[]) { out.println(Arrays.toString(a)); } public static void println(boolean a[]) { out.println(Arrays.toString(a)); } public static void forEach(int a[]) { for(int ele:a) print(ele+" "); println(); } public static void forEach(long a[]) { for(long ele:a) print(ele+" "); println(); } public static void forEach(String a[]) { for(String ele:a) print(ele+" "); println(); } public static void forEach(char a[]) { for(char ele:a) print(ele+" "); println(); } public static void forEach(double a[]) { for(double ele:a) print(ele+" "); println(); } // ================================================== }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
d08f6855c9013332c8f4527b5338cb2a
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(in.readLine()); while (t-- > 0) { StringTokenizer st1 = new StringTokenizer(in.readLine()); int n = Integer.parseInt(st1.nextToken()); int q = Integer.parseInt(st1.nextToken()); long[][] area = new long[1001][1001]; for (int i=0; i<n; i++) { StringTokenizer st2 = new StringTokenizer(in.readLine()); int hi = Integer.parseInt(st2.nextToken()); int wi = Integer.parseInt(st2.nextToken()); area[hi][wi] += hi*wi; } long[][] prefixSum = new long[1001][1001]; for (int hi=1; hi<1001; hi++) { for (int wi=1; wi<1001; wi++) { prefixSum[hi][wi] = area[hi][wi] + prefixSum[hi-1][wi] + prefixSum[hi][wi-1] - prefixSum[hi-1][wi-1]; } } for (int i=0; i<q; i++) { StringTokenizer st3 = new StringTokenizer(in.readLine()); int hs = Integer.parseInt(st3.nextToken()); int ws = Integer.parseInt(st3.nextToken()); int hb = Integer.parseInt(st3.nextToken()) - 1; int wb = Integer.parseInt(st3.nextToken()) - 1; long ans = prefixSum[hb][wb] - prefixSum[hb][ws] - prefixSum[hs][wb] + prefixSum[hs][ws]; out.println(ans); } } in.close(); out.close(); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
33fbea3a04dfd9c031a376b3adc5c64a
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); Solve solver = new Solve(); int T = 1; T = in.nextInt(); for (int i = 1; i <= T; i++) solver.solve(i, in, out); out.close(); } static class Solve { public int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static final long inf = 0x3f3f3f3f; static final int N = (int) 2e5 + 7; static final int mod = (int) 1e9 + 7; static final int[] monthDay = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; public void solve(int testNumber, InputReader in, OutputWriter out) { //ow.println("Case #"+testNumber+": "); //write code here //long startTime = System.currentTimeMillis(); long[][] sum = new long[1010][1010]; int n = in.nextInt(); int q = in.nextInt(); for (int i = 0; i < n; i++) { int h = in.nextInt(); int w = in.nextInt(); sum[h][w] += h * w; } for (int i = 1; i <= 1000; i++) for (int j = 1; j <= 1000; j++) sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; while (q-- > 0) { int h1 = in.nextInt(); int w1 = in.nextInt(); int h2 = in.nextInt(); int w2 = in.nextInt(); long ans = sum[h2 - 1][w2 - 1] - sum[h1][w2 - 1] - sum[h2 - 1][w1] + sum[h1][w1]; out.println(ans); } //long endTime = System.currentTimeMillis();int time=(int) (endTime - startTime);System.out.println("运行时间:" + time + "ms"); //here is end! } static void test(int[] nums, OutputWriter out) { for (int num : nums) out.print(num + " "); out.println(); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } long sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
1a9bcdd7cabb2590aae4e347eb39c978
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(reader.readLine()); long[][] dp= new long[1006][1006]; while(n-- > 0){ for(int i = 0 ; i <=1001;i++){ for(int j = 0 ; j <= 1001;j++){ dp[i][j]= 0; } } String[] s1 = reader.readLine().split(" "); int r = Integer.parseInt(s1[0]), c = Integer.parseInt(s1[1]); while(r-- >0){ String[] s2 = reader.readLine().split(" "); int i = Integer.parseInt(s2[0]),j = Integer.parseInt(s2[1]); dp[i][j] += 1L*i*j; } for(int i = 1; i <= 1001; i++) { for(int j = 1; j <= 1001; j++) { dp[i][j] += dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]; } } while(c-->0){ String[] s3 = reader.readLine().split(" "); int a = Integer.parseInt(s3[0]),b = Integer.parseInt(s3[1]),d = Integer.parseInt(s3[2]),e = Integer.parseInt(s3[3]); System.out.println(dp[d-1][e-1] - dp[a][e-1]-dp[d-1][b]+dp[a][b]); } } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
e29f308bc5e3c37eb68872ebf928c287
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
/* Goal: Become better in CP! Key: Consistency and Discipline Desire: SDE @ Google USA Motto: Do what i Love <=> Love what i do If you don't use your brain 100%, it deteriorates gradually */ import java.util.*; import java.io.*; import java.math.*; public class Code { static StringBuffer str=new StringBuffer(); static BufferedReader bf; static PrintWriter pw; static int n, q; static int h[], w[]; static void solve(int te) throws Exception{ long dp[][]=new long[1001][1001]; for(int i=0;i<n;i++){ dp[h[i]][w[i]]+=h[i]*w[i]; } for(int i=0;i<=1000;i++){ for(int j=1;j<=1000;j++){ dp[i][j]+=dp[i][j-1]; } } for(int j=0;j<=1000;j++){ for(int i=1;i<=1000;i++){ dp[i][j]+=dp[i-1][j]; } } for(int i=0;i<q;i++){ long ans=0; String s[]=bf.readLine().trim().split("\\s+"); int hs=Integer.parseInt(s[0]); int ws=Integer.parseInt(s[1]); int hb=Integer.parseInt(s[2]); int wb=Integer.parseInt(s[3]); ans+=dp[hb-1][wb-1]-dp[hb-1][ws]-dp[hs][wb-1]+dp[hs][ws]; // for(int p=hs+1;p<hb;p++){ // ans+=dp[p][wb-1]-dp[p][ws]; // } str.append(ans).append("\n"); } } public static void main(String[] args) throws java.lang.Exception { boolean lenv=false; if(lenv){ bf = new BufferedReader( new FileReader("input.txt")); pw=new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); }else{ bf = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new OutputStreamWriter(System.out)); } int q1 = Integer.parseInt(bf.readLine().trim()); for(int te=1;te<=q1;te++) { String s[]=bf.readLine().trim().split("\\s+"); n=Integer.parseInt(s[0]); q=Integer.parseInt(s[1]); List<long []> l=new ArrayList<>(); h=new int[n]; w=new int[n]; for(int i=0;i<n;i++){ s=bf.readLine().trim().split("\\s+"); h[i]=Integer.parseInt(s[0]); w[i]=Integer.parseInt(s[1]); } solve(te); } pw.print(str); pw.flush(); // System.out.println(str); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
ff7302978e2c6d77e18521bf92f3bd48
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
/* Goal: Become better in CP! Key: Consistency and Discipline Desire: SDE @ Google USA Motto: Do what i Love <=> Love what i do If you don't use your brain 100%, it deteriorates gradually */ import java.util.*; import java.io.*; import java.math.*; public class Code { static StringBuffer str=new StringBuffer(); static BufferedReader bf; static PrintWriter pw; static int n, q; static int h[], w[]; static void solve(int te) throws Exception{ long dp[][]=new long[1001][1001]; for(int i=0;i<n;i++){ dp[h[i]][w[i]]+=h[i]*w[i]; } for(int i=0;i<=1000;i++){ for(int j=1;j<=1000;j++){ dp[i][j]+=dp[i][j-1]; } } for(int i=0;i<q;i++){ long ans=0; String s[]=bf.readLine().trim().split("\\s+"); int hs=Integer.parseInt(s[0]); int ws=Integer.parseInt(s[1]); int hb=Integer.parseInt(s[2]); int wb=Integer.parseInt(s[3]); for(int p=hs+1;p<hb;p++){ ans+=dp[p][wb-1]-dp[p][ws]; } str.append(ans).append("\n"); } } public static void main(String[] args) throws java.lang.Exception { boolean lenv=false; if(lenv){ bf = new BufferedReader( new FileReader("input.txt")); pw=new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); }else{ bf = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new OutputStreamWriter(System.out)); } int q1 = Integer.parseInt(bf.readLine().trim()); for(int te=1;te<=q1;te++) { String s[]=bf.readLine().trim().split("\\s+"); n=Integer.parseInt(s[0]); q=Integer.parseInt(s[1]); List<long []> l=new ArrayList<>(); h=new int[n]; w=new int[n]; for(int i=0;i<n;i++){ s=bf.readLine().trim().split("\\s+"); h[i]=Integer.parseInt(s[0]); w[i]=Integer.parseInt(s[1]); } solve(te); } pw.print(str); pw.flush(); // System.out.println(str); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
a65a407e5850f0375e48f4237cfb7e04
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.*; public class Main { static InputReader sc; public static void main(String[] args) throws Exception { sc=new InputReader(System.in); int T=sc.nextInt(); while(T-->0) { int n=sc.nextInt(); int q=sc.nextInt(); long[][] map=new long[1005][1005]; for(int i=1;i<=n;i++) { int h=sc.nextInt(); int w=sc.nextInt(); map[h][w]+=h*w; } long[][] pre=new long[1005][1005]; for(int i=1;i<=1000;i++) { for(int j=1;j<=1000;j++) pre[i][j]=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1]+map[i][j]; } while(q-->0) { int x1=sc.nextInt()+1; int y1=sc.nextInt()+1; int x2=sc.nextInt()-1; int y2=sc.nextInt()-1; System.out.println((pre[x2][y2]+pre[x1-1][y1-1]-pre[x1-1][y2]-pre[x2][y1-1])); } } } static class InputReader { BufferedReader br; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public int nextInt() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } boolean negative = false; if (c == '-') { negative = true; c = br.read(); } int x = 0; while (c > 32) { x = x * 10 + c - '0'; c = br.read(); } return negative ? -x : x; } public long nextLong() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } boolean negative = false; if (c == '-') { negative = true; c = br.read(); } long x = 0; while (c > 32) { x = x * 10 + c - '0'; c = br.read(); } return negative ? -x : x; } public String next() throws IOException { int c = br.read(); while (c <= 32) { c = br.read(); } StringBuilder sb = new StringBuilder(); while (c > 32) { sb.append((char) c); c = br.read(); } return sb.toString(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
1ebe1758af132bbfd22b0f7b11a0ec25
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; public class myclass { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String args[]) { FastReader sc = new FastReader(); int t= sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int q = sc.nextInt(); long arr[][] = new long[1005][1005]; for(int i=0 ; i<n ; i++) { int x = sc.nextInt(); int y = sc.nextInt(); arr[x][y] += y; } for(int j=0 ; j<1005 ; j++) { for(int i = 1 ; i<1005 ; i++) arr[j][i] += arr[j][i-1]; } for(int i=0 ; i<q ; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int d = sc.nextInt(); long ans = 0; for(int j = a+1 ; j<c ; j++) { ans += (j * (arr[j][d-1]-arr[j][b])); } System.out.println(ans); } } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
c55246800955bfff8d1f976ea575978f
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
/*========================================================================== * AUTHOR: RonWonWon * CREATED: 13.10.2022 22:55:40 /*==========================================================================*/ import java.io.*; import java.util.*; public class EE { public static void main(String[] args) throws IOException { /* in.ini(); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); */ long startTime = System.nanoTime(); int t = in.nextInt(), T = 1; while(t-->0) { int n = in.nextInt(), q = in.nextInt(); long sum[][] = new long[1001][1001]; for(int i=0;i<n;i++){ int h = in.nextInt(), w = in.nextInt(); sum[h][w] += (long)h*w; } for(int i=1;i<=1000;i++){ for(int j=1;j<=1000;j++){ sum[i][j] += sum[i-1][j] + sum[i][j-1]; sum[i][j] -= sum[i-1][j-1]; } } while(q-->0){ int hs = in.nextInt(), ws = in.nextInt(), hb = in.nextInt(), wb = in.nextInt(); long ans = sum[hb-1][wb-1]; ans -= (sum[hs][wb-1]+sum[hb-1][ws]); ans += sum[hs][ws]; out.println(ans); } //out.println("Case #"+T+": "+ans); T++; } /* startTime = System.nanoTime() - startTime; System.err.println("Time: "+(startTime/1000000)); */ out.flush(); } static FastScanner in = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); static int oo = Integer.MAX_VALUE; static long ooo = Long.MAX_VALUE; static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); void ini() throws FileNotFoundException { br = new BufferedReader(new FileReader("input.txt")); } String next() { while(!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch(IOException e) {} return st.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } long[] readArrayL(int n) { long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = nextLong(); return a; } double nextDouble() { return Double.parseDouble(next()); } double[] readArrayD(int n) { double a[] = new double[n]; for(int i=0;i<n;i++) a[i] = nextDouble(); return a; } } static final Random random = new Random(); static void ruffleSort(int[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n), temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } static void ruffleSortL(long[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n); long temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
98d910ba07f92027e6ff5210317560ea
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
/* Author:-crazy_coder- Do what i Love <=> Love what i do */ import java.io.*; import java.util.*; public class cp{ static long mod=998244353; static int ans=0; static int[] par=new int[100005]; static int[] rank=new int[100005]; static BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); static PrintWriter pw=new PrintWriter(System.out); public static void main(String[] args)throws Exception{ int T=Integer.parseInt(br.readLine()); // int t=1; // comp(); // boolean isPrime[]=sieveOfEratosthenes(1000006); while(T-->0){ solve(); } pw.flush(); } public static void solve()throws Exception{ String[] str=br.readLine().split(" "); int n=Integer.parseInt(str[0]); int q=Integer.parseInt(str[1]); // int[] arr=new int[n+1]; long[][] twoDPrefix=new long[1001][1001]; for(int i=0;i<n;i++) { str=br.readLine().split(" "); int h=Integer.parseInt(str[0]); int w=Integer.parseInt(str[1]); twoDPrefix[h][w]=twoDPrefix[h][w]+h*w*1l; } // for(int i=1;i<=6;i++) // { // for(int j=1;j<=6;j++) // { // pw.print(twoDPrefix[i][j]+" "); // } // pw.println(); // } for(int i=1;i<=1000;i++) { for(int j=1;j<=1000;j++) { twoDPrefix[i][j]=twoDPrefix[i][j]+twoDPrefix[i][j-1]; } } // for(int i=1;i<=6;i++) // { // for(int j=1;j<=6;j++) // { // pw.print(twoDPrefix[i][j]+" "); // } // pw.println(); // } for(int i=1;i<=1000;i++) { for(int j=1;j<=1000;j++) { twoDPrefix[j][i]=twoDPrefix[j][i]+twoDPrefix[j-1][i]; } } // for(int i=1;i<=6;i++) // { // for(int j=1;j<=6;j++) // { // pw.print(twoDPrefix[i][j]+" "); // } // pw.println(); // } while(q-->0) { str=br.readLine().split(" "); int hs=Integer.parseInt(str[0]); int ws=Integer.parseInt(str[1]); int hb=Integer.parseInt(str[2]); int wb=Integer.parseInt(str[3]); pw.println(twoDPrefix[hb-1][wb-1]-twoDPrefix[hs][wb-1]-twoDPrefix[hb-1][ws]+twoDPrefix[hs][ws]); } } public static boolean[] sieveOfEratosthenes(int n){ boolean isPrime[]=new boolean[n+1]; Arrays.fill(isPrime,true); isPrime[0]=false; isPrime[1]=false; for(int i=2;i*i<n;i++){ for(int j=i*i;j<=n;j+=i){ isPrime[j]=false; } } return isPrime; } //*******************************factoral strated******************************* */ public static long NCR(int n,int r){ if(r==0||r==n)return 1; return (fac[n]*invfact[r]%mod)*invfact[n-r]%mod; } static long[] fac=new long[200007]; static long[] invfact=new long[200007]; public static void comp(){ fac[0]=1; invfact[0]=1; for(int i=1;i<100;i++){ fac[i]=(fac[i-1]*i)%mod; invfact[i]=modInverse(fac[i]); } } public static long modInverse(long n){ return power(n,mod-2); } public static long power(long x,long y){ long res=1; x=x%mod; while(y>0){ if((y&1)==1)res=(res*x)%mod; y=y>>1; x=(x*x)%mod; } return res; } //*************************************DSU************************************ */ public static void initialize(int n){ for(int i=1;i<=n;i++){ par[i]=i; rank[i]=1; } } public static void union(int x,int y){ int lx=find(x); int ly=find(y); if(lx!=ly){ if(rank[lx]>rank[ly]){ par[ly]=lx; }else if(rank[lx]<rank[ly]){ par[lx]=ly; }else{ par[lx]=ly; rank[ly]++; } } } public static int find(int x){ if(par[x]==x){ return x; } int temp=find(par[x]); par[x]=temp; return temp; } //************************************DSU END********************************** */ public static boolean isPresent(ArrayList<Long> zero,long val){ for(long x:zero){ if(x==val)return true; } return false; } public static class Pair implements Comparable<Pair>{ int val; int k; Pair(int val,int k){ this.val=val; this.k=k; } public int compareTo(Pair o){ return this.val-o.val; } } public static int countDigit(int x){ return (int)Math.log10(x)+1; } //****************************function to find all factor************************************************* public static ArrayList<Long> findAllFactors(long num){ ArrayList<Long> factors = new ArrayList<Long>(); for(long i = 1; i <= num/i; ++i) { if(num % i == 0) { //if i is a factor, num/i is also a factor factors.add(i); factors.add(num/i); } } //sort the factors Collections.sort(factors); return factors; } //*************************** function to find GCD of two number******************************************* public static long gcd(long a,long b){ if(b==0)return a; return gcd(b,a%b); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
c2f6e387a5e5875270058acbf8cab85f
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class countingRectangles { public static void main(String[] args) throws IOException { BufferedReader bi = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(bi.readLine()); int t = Integer.parseInt(st.nextToken()); for(int i = 0; i < t; i++) { st = new StringTokenizer(bi.readLine()); int n = Integer.parseInt(st.nextToken()); // number of rectangles int q = Integer.parseInt(st.nextToken()); // number of queries int[][] rectangles = new int[2][n]; // 0 = h, 1 = w long[][] a = new long[1001][1001]; // not index for(int j = 0; j < n; j++) { st = new StringTokenizer(bi.readLine()); rectangles[0][j] = Integer.parseInt(st.nextToken()); rectangles[1][j] = Integer.parseInt(st.nextToken()); a[rectangles[0][j]][rectangles[1][j]] += (long)(rectangles[0][j])*(long)(rectangles[1][j]); } for(int j = 1; j <= 1000; j++) { for(int k = 1 ; k <= 1000; k++) { a[j][k] += a[j-1][k]+a[j][k-1]-a[j-1][k-1]; } } for(int j = 0; j < q; j++) { st = new StringTokenizer(bi.readLine()); int hs = Integer.parseInt(st.nextToken()); int ws = Integer.parseInt(st.nextToken()); int hb = Integer.parseInt(st.nextToken()); int wb = Integer.parseInt(st.nextToken()); out.println(a[hb-1][wb-1]-a[hb-1][ws]-a[hs][wb-1]+a[hs][ws]); } } out.close(); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
d189b540414d27b0f21bad822065ff20
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.Scanner; public class E1722 { public static void main(String[] args) { Scanner in = new Scanner(System.in); StringBuilder out = new StringBuilder(); int T = in.nextInt(); for (int t=0; t<T; t++) { long[][] stat = new long[1001][1001]; int N = in.nextInt(); int Q = in.nextInt(); for (int n=0; n<N; n++) { int H = in.nextInt(); int W = in.nextInt(); stat[H][W] += H*W; } for (int h=0; h<=1000; h++) { for (int w=1; w<=1000; w++) { stat[h][w] += stat[h][w-1]; } } for (int w=0; w<=1000; w++) { for (int h=1; h<=1000; h++) { stat[h][w] += stat[h-1][w]; } } for (int q=0; q<Q; q++) { int HS = in.nextInt(); int WS = in.nextInt(); int HB = in.nextInt()-1; int WB = in.nextInt()-1; long answer = stat[HB][WB] - stat[HB][WS] - stat[HS][WB] + stat[HS][WS]; out.append(answer).append('\n'); } } System.out.print(out); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
d6827b75ae1f9ccfc7d895450b698ac9
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
/*############################################################################################################ ########################################## >>>> Diaa12360 <<<< ############################################### ########################################### Just Nothing ################################################# #################################### If You Need it, Fight For IT; ######################################### ###############################################.-. 1 5 9 2 .-.################################################ ############################################################################################################*/ import java.io.*; import java.util.*; public class Solution { static final int maxN = 1000000000; static int dx[] = {1, 0, 0, -1, 1, -1, 1, -1}; static int dy[] = {0, -1, 1, 0, 1, -1, -1, 1}; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // in = new BufferedReader(new FileReader("in.text")); StringBuilder out = new StringBuilder(); StringTokenizer tk; int t = anInt(in.readLine()); while (t-- > 0) { long[][] sum = new long[1010][1010]; tk = new StringTokenizer(in.readLine()); int n = anInt(tk.nextToken()), q = anInt(tk.nextToken()); for (int i = 0; i < n; i++) { tk = new StringTokenizer(in.readLine()); int h = anInt(tk.nextToken()), w = anInt(tk.nextToken()); sum[h][w] += h * w; } for (int i = 1; i <= 1000; i++) for (int j = 1; j <= 1000; j++) sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; while (q-- > 0) { tk = new StringTokenizer(in.readLine()); int h1 = anInt(tk.nextToken()), w1 = anInt(tk.nextToken()), h2 = anInt(tk.nextToken()), w2 = anInt(tk.nextToken()); long ans = sum[h2 - 1][w2 - 1] - sum[h1][w2 - 1] - sum[h2 - 1][w1] + sum[h1][w1]; out.append(ans).append('\n'); } } System.out.println(out); } static int lcm(int a, int b) { return a / gcd(a, b) * b; } static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } //all primes static ArrayList<Integer> primes; static boolean[] primesB; //sieve algorithm static void sieve(int n) { primes = new ArrayList<>(); primesB = new boolean[n + 1]; for (int i = 2; i <= n; i++) { primesB[i] = true; } for (int i = 2; i * i <= n; i++) { if (primesB[i]) { for (int j = i * i; j <= n; j += i) { primesB[j] = false; } } } for (int i = 0; i <= n; i++) { if (primesB[i]) { primes.add(i); } } } // Function to find gcd of array of // numbers static int findGCD(int[] arr, int n) { int result = arr[0]; for (int element : arr) { result = gcd(result, element); if (result == 1) { return 1; } } return result; } private static int anInt(String s) { return Integer.parseInt(s); } } class Pair<K, V> implements Comparable<Pair<K, V>> { K first; V second; Pair(K f, V s) { first = f; second = s; } @Override public int compareTo(Pair<K, V> o) { return 0; } } class PairInt implements Comparable<PairInt> { int first; int second; PairInt(int f, int s) { first = f; second = s; } @Override public int compareTo(PairInt o) { if (this.first > o.first) { return 1; } else if (this.first < o.first) return -1; else { if (this.second < o.second) return 1; else if (this.second == o.second) return -1; return 0; } } @Override public String toString() { return "<" + first + ", " + second + ">"; } } /* 5 1 5 4 3 2 1 1 4 5 3 1 2 3 5 4 1 3 4 5 3 5 5 3 1 2 2 2 1 1 4 2 5 1 5 5 2 1 1 1 2 1 2 3 4 5 10 1 3 1 5 2 3 5 7 4 3 5 1 1 1 1 2 1 4 2 4 1 2 4 1 3 1 3 3 3 3 2 5 4 1 5 3 2 1 1 5 1 2 3 3 5 3 3 6 8 4 */
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
815867f4ce352d849bbbede215945fb0
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; public class codeforces1722E { public static void main(String[] args) throws Exception { FastScanner in = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int numCases = in.nextInt(); while (numCases-->0) { int n = in.nextInt(); int q = in.nextInt(); long arr[][] = new long[1001][1001]; long prefix [][] = new long[1001][1001]; while (n-->0) { int h = in.nextInt(); int w = in.nextInt(); arr[h][w]+=h*w; } for (int a = 1; a<1001;a++) { for (int b = 1; b<1001;b++) { prefix[a][b] = arr[a][b]+prefix[a-1][b]+prefix[a][b-1]-prefix[a-1][b-1]; } } while (q-->0) { int h1 = in.nextInt(); int w1 = in.nextInt(); int h2 = in.nextInt(); int w2 = in.nextInt(); pw.println(prefix[h2-1][w2-1]-prefix[h2-1][w1]-prefix[h1][w2-1]+prefix[h1][w1]); } } pw.close(); } private static class FastScanner { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; private FastScanner() throws IOException { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private short nextShort() throws IOException { short ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = (short) (ret * 10 + c - '0'); while ((c = read()) >= '0' && c <= '9'); if (neg) return (short) -ret; return ret; } private int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } private char nextChar() throws IOException { byte c = read(); while (c <= ' ') c = read(); return (char) c; } private String nextString() throws IOException { StringBuilder ret = new StringBuilder(); byte c = read(); while (c <= ' ') c = read(); do { ret.append((char) c); } while ((c = read()) > ' '); return ret.toString(); } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
7ebb656996d7bd5719fa503c79aaaf45
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class countRec{ public static void main(String[] args) throws IOException{ BufferedReader inp = new BufferedReader(new InputStreamReader(System.in)); int t= Integer.parseInt(inp.readLine()); while(t-->0){ String tmp[] = inp.readLine().split(" "); int n = Integer.parseInt(tmp[0]); int q = Integer.parseInt(tmp[1]); long area[][] = new long[1001][1001]; for(int i = 0;i<n;i++){ tmp = inp.readLine().split(" "); int h = Integer.parseInt(tmp[0]); int w = Integer.parseInt(tmp[1]); area[h][w] += h*w; } for(int i = 1;i<1001;i++){ for(int j = 1;j<1001;j++){ area[i][j] += area[i-1][j] + area[i][j-1]-area[i-1][j-1]; } } for(int i=0;i<q;i++){ tmp = inp.readLine().split(" "); int h1 = Integer.parseInt(tmp[0]); int w1 = Integer.parseInt(tmp[1]); int h2 = Integer.parseInt(tmp[2]); int w2 = Integer.parseInt(tmp[3]); System.out.println(area[h2-1][w2-1]-area[h2-1][w1]-area[h1][w2-1]+area[h1][w1]); } } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
7eee032d372ff75769ac8fa15125282f
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Solution { public static class CFScanner { BufferedReader br; StringTokenizer st; public CFScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { // int n = in.nextInt(); // long n = in.nextLong(); // double n = in.nextDouble(); // String s = in.next(); CFScanner in = new CFScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int T = in.nextInt(); for (int t = 1; t <= T; t++) { int n = in.nextInt(); int q = in.nextInt(); int[][] rects = new int[n][2]; for (int i = 0; i < n; i++) { rects[i][0] = in.nextInt(); rects[i][1] = in.nextInt(); } int[][] qs = new int[q][4]; for (int i = 0; i < q; i++) { qs[i][0] = in.nextInt(); qs[i][1] = in.nextInt(); qs[i][2] = in.nextInt(); qs[i][3] = in.nextInt(); } getArea(rects, qs, out); // out.println(); } out.close(); } private static void getArea(int[][] rects, int[][] qs, PrintWriter out) { long[][] table = new long[1001][1001]; for (int[] rect : rects) { int h = rect[0]; int w = rect[1]; table[h][w] += (long)h * (long)w; } for (int r = 0; r <= 1000; r++) { for (int c = 0; c <= 1000; c++) { long addUp = r - 1 >= 0 ? table[r - 1][c] : 0L; long addLeft = c - 1 >= 0 ? table[r][c - 1] : 0L; long remove = r - 1 >= 0 && c - 1 >= 0 ? table[r - 1][c - 1] : 0L; table[r][c] += addUp + addLeft - remove; } } for (int[] q : qs) { int h1 = q[0]; int w1 = q[1]; int h2 = q[2]; int w2 = q[3]; long area = table[h2 - 1][w2 - 1] - table[h2 - 1][w1] - table[h1][w2 - 1] + table[h1][w1]; out.println(area); } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
f606510aa2e22fa839e5f99dba48a411
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.StringTokenizer; public class E { static long[][]sum,a; static PrintWriter out; public static void main(String[]args) throws IOException { Scanner sc=new Scanner(System.in); out=new PrintWriter(System.out); int t=sc.nextInt(); a:while(t-->0) { int n=sc.nextInt(),q=sc.nextInt(); sum=new long[1001][1001]; a=new long[1001][1001]; for(int i=0;i<n;i++) { int x=sc.nextInt(),y=sc.nextInt(); a[x][y]+=1l*x*y; } pref(); while(q-->0) { int x1=sc.nextInt()+1,y1=sc.nextInt()+1, x2=sc.nextInt()-1,y2=sc.nextInt()-1; out.println(query(x1,y1,x2,y2)); } } out.close(); } private static long query(int x1, int y1, int x2, int y2) { if(x1>x2||y1>y2)return 0; long cur=sum[x2][y2]; if(x1>0)cur-=sum[x1-1][y2]; if(y1>0)cur-=sum[x2][y1-1]; if(x1>0&&y1>0)cur+=sum[x1-1][y1-1]; return cur; } private static void pref() { long[][]r=new long[1001][1001]; for(int i=0;i<r.length;i++) { r[i][0]=a[i][0]; for(int j=1;j<r.length;j++) { r[i][j]=r[i][j-1]+a[i][j]; } } for(int j=0;j<r.length;j++)sum[0][j]=r[0][j]; for(int i=1;i<r.length;i++) { for(int j=0;j<r.length;j++) { sum[i][j]=sum[i-1][j]+r[i][j]; } } // out.println(Arrays.deepToString(a)); // out.println(Arrays.deepToString(sum)); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public boolean hasNext() {return st.hasMoreTokens();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready(); } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
e2aab71dbb4863106111ad7970964fed
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class cr { public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); String[] out = new String[t]; for (int i = 0; i < t; i++) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int q = Integer.parseInt(st.nextToken()); long[][] grid = new long[(int) (1e3+1)][(int) (1e3+1)]; for (int j = 0; j < n; j++) { st = new StringTokenizer(br.readLine()); int r = Integer.parseInt(st.nextToken()); int c = Integer.parseInt(st.nextToken()); long area = r*c; grid[r][c] += area; } long[][] prefix = new long[grid.length][grid.length]; for (int j = 1; j < grid.length; j++) { for (int k = 1; k < grid.length; k++) { prefix[j][k] = prefix[j-1][k] + prefix[j][k-1] - prefix[j-1][k-1] + grid[j][k]; } } StringBuilder sb = new StringBuilder(""); for (int j = 0; j < q; j++) { st = new StringTokenizer(br.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); int c = Integer.parseInt(st.nextToken()); int d = Integer.parseInt(st.nextToken()); if (a+1 == c || b+1 == d) { sb.append("0\n"); } else { sb.append(sum(prefix,a+1,b+1,c-1,d-1) + "\n"); } } out[i] = sb.toString(); } br.close(); for (String s : out) { System.out.print(s); } } static long sum(long[][] prefix, int r1, int c1, int r2, int c2) { return prefix[r2][c2] - prefix[r1-1][c2] - prefix[r2][c1-1] + prefix[r1-1][c1-1]; } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
6b35c875be4b342cbea1586eb5f85d75
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.security.cert.X509CRL; import java.util.*; import java.lang.*; import java.util.stream.Collector; import java.util.stream.Collectors; @SuppressWarnings("unused") public class Main { static InputStream is; static PrintWriter out; //static String INPUT = "in.txt"; static String INPUT = ""; static String OUTPUT = ""; //global const //private final static long BASE = 998244353L; private final static int ALPHABET = (int)('z') - (int)('a') + 1; private final static long BASE = 1000000007l; private final static int INF_I = 1000000000; private final static long INF_L = 10000000000000000l; private final static int MAXN = 200100; private final static int MAXK = 27; private final static int[] DX = {-1,0,1,0}; private final static int[] DY = {0,1,0,-1}; static boolean isSquare(int X) { int sq = (int)Math.round(Math.sqrt(X)); return (sq*sq == X); } static long query(long[][] S, int h1, int w1, int h2, int w2) { return S[h2][w2] - S[h2][w1-1] - S[h1-1][w2] + S[h1-1][w1-1]; } static void solve() { //int ntest = 1; int ntest = readInt(); for (int test=0;test<ntest;test++) { int N = readInt(), Q = readInt(); long[][] S = new long[1001][1001]; for (int i=0; i<N; i++) { int h = readInt(), w = readInt(); S[h][w] += 1l*h*w; } for (int i=1; i<=1000; i++) for (int j=1; j<=1000; j++) S[i][j] = S[i-1][j] + S[i][j-1] - S[i-1][j-1] + S[i][j]; for (int q=0; q<Q; q++) { int h1 = readInt(), w1 = readInt(); int h2 = readInt(), w2 = readInt(); out.println(query(S,h1+1,w1+1, h2-1, w2-1)); } } } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); if (INPUT=="") { is = System.in; } else { File file = new File(INPUT); is = new FileInputStream(file); } if (OUTPUT == "") out = new PrintWriter(System.out); else out = new PrintWriter(OUTPUT); solve(); out.flush(); long G = System.currentTimeMillis(); } private static class MultiSet<T extends Comparable<T>> { private TreeSet<T> set; private Map<T, Integer> count; public MultiSet() { this.set = new TreeSet<>(); this.count = new HashMap<>(); } public void add(T x) { this.set.add(x); int o = this.count.getOrDefault(x, 0); this.count.put(x, o+1); } public void remove(T x) { int o = this.count.getOrDefault(x, 0); if (o==0) return; this.count.put(x, o-1); if (o==1) this.set.remove(x); } public void removeAll(T x) { int o = this.count.getOrDefault(x, 0); if (o==0) return; this.count.put(x, 0); this.set.remove(x); } public T first() { return this.set.first(); } public T last() { return this.set.last(); } public int getCount(T x) { return this.count.getOrDefault(x, 0); } public int size() { int res = 0; for (T x: this.set) res += this.count.get(x); return res; } } private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> { private T x; private T y; public Point(T x, T y) { this.x = x; this.y = y; } public T getX() {return x;} public T getY() {return y;} @Override public int compareTo(Point<T> o) { int cmp = x.compareTo(o.getX()); if (cmp==0) return y.compareTo(o.getY()); return cmp; } } private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> { public ClassComparator() {} @Override public int compare(T a, T b) { return a.compareTo(b); } } private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> { public ListComparator() {} @Override public int compare(List<T> o1, List<T> o2) { for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) { int c = o1.get(i).compareTo(o2.get(i)); if (c != 0) { return c; } } return Integer.compare(o1.size(), o2.size()); } } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double readDouble() { return Double.parseDouble(readString()); } private static char readChar() { return (char)skip(); } private static String readString() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] readChar(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] readTable(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = readChar(m); return map; } private static int[] readIntArray(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = readInt(); return a; } private static long[] readLongArray(int n) { long[] a = new long[n]; for (int i=0;i<n;i++) a[i] = readLong(); return a; } private static int readInt() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long readLong() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
7b97e015268013bf981cca224ec61944
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
//SHIVAM GARG import java.util.*; import java.io.*; import java.lang.*; public class Codechef { public static FastReader reader = new FastReader(); public static int[] primes; public static void main(String[] args) throws java.lang.Exception { int n = reader.nextInt(); for(int i=0; i<n; i++){ solve(); } } public static void solve(){ int n = reader.nextInt(); int q = reader.nextInt(); List<int[]> list = new ArrayList<>(); int mx = 0, my=0; for(int i=0; i<n; i++){ int[] arr = new int[2]; arr[0] = reader.nextInt(); arr[1] = reader.nextInt(); mx = Math.max(mx, arr[0]); my = Math.max(my, arr[1]); list.add(arr); } List<int[]> query = new ArrayList<int[]>(); for(int i=0; i<q; i++){ int[] arr = new int[4]; arr[0] = reader.nextInt(); arr[1] = reader.nextInt(); arr[2] = reader.nextInt(); arr[3] = reader.nextInt(); mx = Math.max(mx, Math.max(arr[0], arr[2])); my = Math.max(my, Math.max(arr[1], arr[3])); query.add(arr); } long[][] inp = new long[mx+1][my+1]; for(int[] arr : list){ int xx = arr[0]; int yy = arr[1]; inp[xx][yy] = inp[xx][yy] + xx*yy; } long[][] tracker = new long[mx+1][my+1]; for(int i=0; i<= mx; i++){ for(int j=0; j<=my; j++){ tracker[i][j] = (i>0 ? tracker[i-1][j] : 0) + (j>0 ? tracker[i][j-1] : 0) - (i>0 && j>0 ? tracker[i-1][j-1] : 0) + inp[i][j]; } } for(int i=0; i<q; i++){ int[] arr = query.get(i); int hs = arr[0]; int ws = arr[1]; int hb = arr[2]-1; int wb = arr[3]-1; long req = tracker[hb][wb] + tracker[hs][ws] - tracker[hs][wb] - tracker[hb][ws]; System.out.println(req); } } public static int gcd_one(int n ){ if(n == 1) return 2; if(n == 2) return 3; if(n == 3) return 4; int sqrt = ((int)Math.sqrt(n))+1; for(int i= sqrt; i< primes.length; i++){ if(primes[i] == 1){ return i*i; } } return -1; } public static int[] sieve(int n){ //-1-> not prime , 1-> prime primes = new int[n+1]; primes[1] = -1; for(int i =2 ; i<=n; i = i+2){ primes[i]= -1; } primes[2] = 1; for(int i =3 ; i<=n; i = i+2){ if(primes[i] == 0 ){ for(int j = i; j<=n; j = j+i){ primes[j]= -1; } primes[i] =1; } } return primes; } static int gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } public static HashMap<Integer, Integer> factors(long n) { HashMap < Integer, Integer > tracker = new HashMap < Integer, Integer > (); if (n % 2 == 0) { tracker.put(2, 0); } while (n % 2 == 0) { tracker.put(2, tracker.get(2) + 1); n = n / 2; } int tt = (int) Math.sqrt(n) + 3; for (int i = 3; i < tt; i = i + 2) { if (i > n) { break; } if (n % i == 0) { tracker.put(i, 0); } while (n % i == 0) { tracker.put(i, tracker.get(i) + 1); n = n / i; } } return tracker; } /* Iterative Function to calculate (x^y)%p in O(log y) */ public static long prime = 1000000007; public static long[] fact; static long power(long x, long y) { // Initialize result long res = 1; // Update x if it is more than or // equal to p x = x % prime; while (y > 0) { // If y is odd, multiply x with result if (y % 2 == 1) res = (res * x) % prime; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % prime; } return res; } // Returns n^(-1) mod p static long modInverse(long n) { return power(n, prime - 2); } // Returns nCr % p using Fermat's little theorem. static long nCrModPFermat(int n, int r) { // Base case if (r == 0) return 1; long a1 = fact[n]; long a2 = modInverse(fact[r]) % prime; long a3 = modInverse(fact[n - r]) % prime; return ((fact[n] * a2) % prime * a3) % prime; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { //e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int ii = 0; ii < n; ii++) { arr[ii] = this.nextInt(); } return arr; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char nextChar(){ return next().charAt(0); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { //e.printStackTrace(); } return str; } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
34a06af9d0ba7c75df43997402f940d0
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import javax.print.DocFlavor; import java.math.BigInteger; import java.util.*; import java.io.*; public class Vaibhav { static long bit[]; static boolean prime[]; public static long lcm(long x, long y) { return (x * y) / gcd(x, y); } static class Pair {//implements Comparable<Pair> { int peldo; int bachalo; Pair(int peldo, int bachalo) { this.peldo = peldo; this.bachalo = bachalo; } /* //public int compareTo(Pair o){ return this.cqm-o.cqm; }*/ } //BIT public static void update(long bit[], int i, int x) { for (; i < bit.length; i += (i & (-i))) { bit[i] += x; } } public static long sum(int i) { long sum = 0; for (; i > 0; i -= (i & (-i))) { sum += bit[i]; } return sum; } static long power(long x, long y, long p) { if (y == 0) return 1; if (x == 0) return 0; long res = 1l; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if (y == 0) return 1; if (x == 0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readlongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static long nCk(int n, int k) { long res = 1; for (int i = n - k + 1; i <= n; ++i) res *= i; for (int i = 2; i <= k; ++i) res /= i; return res; } static BigInteger bi(String str) { return new BigInteger(str); } // Author - vaibhav_1710 static FastScanner fs = null; static long ans; static long mod = 1_000_000_007; static ArrayList<Integer> al[]; static long dp[][][]; static int n; static int k; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); outer: while (t-- > 0) { int n = fs.nextInt(); int q = fs.nextInt(); long g[][] = new long[1001][1001]; for(int i=0;i<n;i++){ long x = fs.nextLong(); long y = fs.nextLong(); g[(int)x][(int)y] += (x*y); } long sum[][] = new long[1001][1001]; for(int i=1;i<1001;i++){ for(int j=1;j<1001;j++){ sum[i][j] = sum[i-1][j] + sum[i][j-1] + g[i][j] - sum[i-1][j-1]; } } for(int i=0;i<q;i++){ int x1 = fs.nextInt(); int y1 = fs.nextInt(); int x2 = fs.nextInt(); int y2 = fs.nextInt(); if(x1+2>x2 || y1+2>y2) out.println(0); else{ long ans = sum[x2-1][y2-1] - sum[x2-1][y1] - sum[x1][y2-1] + sum[x1][y1]; out.println(ans); } } } out.close(); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
26f66a190aa29e79d94d866c04075f5d
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; public class Balabizo { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tt = sc.nextInt(); while(tt-->0) { int n =sc.nextInt() , q = sc.nextInt(); long[][] a = new long[1001][1001]; for(int i=0; i<n ;i++){ int h = sc.nextInt() , w = sc.nextInt(); a[h][w] += (long) h * w; } for(int i=0; i<a.length ;i++){ for(int j=1; j<a.length ;j++) a[i][j] += a[i][j-1]; } while(q-- > 0){ int h1 = sc.nextInt()+1 , w1 = sc.nextInt() , h2 = sc.nextInt(), w2 = sc.nextInt()-1; long ans = 0; for(int i=h1; i<h2 ;i++) ans += a[i][w2] - a[i][w1]; pw.println(ans); } } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws IOException { br = new BufferedReader(new FileReader(file)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
175ae69ec88f5a25971608c363670309
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class CF1 { static FastReader sc=new FastReader(); // static long dp[][]; // static boolean v[][][]; // static int mod=998244353;; // static int mod=1000000007; static long oset[]; static int oset_p; static long mod=1000000007; // static int max; // static int bit[]; //static long fact[]; //static HashMap<Long,Long> mp; //static StringBuffer sb=new StringBuffer(""); //static HashMap<Integer,Integer> map; //static List<Integer>list; //static int m; //static StringBuffer sb=new StringBuffer(); // static PriorityQueue<Integer>heap; //static int dp[]; // static boolean cycle; static PrintWriter out=new PrintWriter(System.out); // static int msg[]; static List<int[]>ans; public static void main(String[] args) { // StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); // Queue<int[]>q=new LinkedList<>(); outer :while (ttt-- > 0) { int n=i(); int q=i(); long count[][]=new long[1002][1002]; for(int i=0;i<n;i++) { int h=i(); int w=i(); count[h][w]++; } long prefix[][]=new long[1002][1002]; for(int i=1;i<1002;i++) { for(int j=1;j<1002;j++) { if(count[i][j]!=0) { prefix[i][j]=(count[i][j]*((long)i*(long)j))+prefix[i-1][j]; count[i][j]=0; } else prefix[i][j]=prefix[i-1][j]; } } for(int i=1;i<1002;i++) { for(int j=1;j<1002;j++) { prefix[i][j]+=prefix[i][j-1]; } } // for(int i=1;i<=5;i++) // { // for(int j=1;j<=5;j++) // out.print(prefix[i][j]+" "); // // pln(""); // } // for(int i=0;i<q;i++) { int hs=i(); int ws=i(); int hb=i(); int wb=i(); out.println(((prefix[hb-1][wb-1]-prefix[hb-1][ws])-(prefix[hs][wb-1]-prefix[hs][ws]))); } } out.close(); } public static boolean helper(String t,String str[],int index,int i_index,int len,Integer ind[]) { if(index==len) return true; if(index>len) return false; for(int i=index;i>=i_index;i--) { for(int j=0;j<str.length;j++) { String s=str[j]; if(check(s,t,i,len)) { ans.add(new int[] {ind[j]+1,i+1}); if(helper(t,str,i+s.length(),i+1,len,ind)) return true; ans.remove(ans.size()-1); } } } return false; } public static boolean check(String s,String t,int index,int len) { int p=0; while(p<s.length()&&index<len) { if(s.charAt(p)!=t.charAt(index)) return false; p++; index++; } return p==s.length(); } public static boolean equals(String t,String s,int pt) { int ps=0; for(int i=pt;i<t.length();i++) { if(s.charAt(ps)==t.charAt(i)) { ps++; } else return false; if(ps>=s.length()) return true; } return false; } public static List<Integer> expectedMoney(int N, int M, List<Integer>A, List<Integer>B) { Deque<Integer>dq=new LinkedList<>(); for(int i=N-1;i>0;i--) dq.add(A.get(i)); int sum=0; for(int i=N-1;i>0;i--) sum+=B.get(i); int ans[]=new int[N+1]; int ns=sum; // out.println(sum); for(int i=1;i<=N;i++) { ns+=B.get(A.get(i)); int t=0; // pln(ns+""); while(!dq.isEmpty()&&ns>M) {t=B.get(dq.poll()); ns-= t;} ns-=B.get(A.get(i)); // pln((M-(ns+t))+""); int term=(1+dq.size())*B.get(A.get(i)); if((M-(ns+t))>0) term+=(M-(ns+t)); double k=(double)term/(double)N; String s=Double.toString(k); StringBuffer sb1=new StringBuffer(); StringBuffer sb2=new StringBuffer(); boolean b=false; int c=4; // out.println(s); for(int j=0;j<s.length();j++) { if(s.charAt(j)=='.') { b=true;continue;} if(b) { sb2.append(s.charAt(j)); } else sb1.append(s.charAt(j)); } // out.println(sb1+" "+sb2); for(int j=sb2.length();j<=4;j++) sb2.append(0); StringBuffer sb3=new StringBuffer(); for(int j=0;j<4;j++) sb3.append(sb2.charAt(j)); sb1.append(sb3); String an=sb1.toString(); // out.println(an); int n=Integer.parseInt(an); ans[A.get(i)]=n; dq.addFirst(A.get(i)); ns+=B.get(A.get(i)); } List<Integer>res=new ArrayList<>(); for(int i=1;i<=N;i++) res.add(ans[i]); return res; } static int findPar(int x,int parent[]) { if(parent[x]==x) return x; return parent[x]=findPar(parent[x],parent); } static void union(int u,int v,int parent[],int rank[]) { int x=findPar(u,parent); int y=findPar(v,parent); if(x==y) return; if(rank[x]>rank[y]) { parent[y]=x; } else if(rank[y]>rank[x]) parent[x]=y; else { parent[y]=x; rank[x]++; } } static class Pair implements Comparable<Pair> { int x; int y; // int z; Pair(int x,int y){ this.x=x; this.y=y; // this.z=z; } @Override public int compareTo(Pair o) { if(this.x>o.x) return 1; else if(this.x<o.x) return -1; else { if(this.y>o.y) return 1; else if(this.y<o.y) return -1; else return 0; } } public int hashCode() { final int temp = 14; int ans = 1; ans =x*31+y*13; return ans; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null) { return false; } if (this.getClass() != o.getClass()) { return false; } Pair other = (Pair)o; if (this.x != other.x || this.y!=other.y) { return false; } return true; } // /* FOR TREE MAP PAIR USE */ // public int compareTo(Pair o) { // if (x > o.x) { // return 1; // } // if (x < o.x) { // return -1; // } // if (y > o.y) { // return 1; // } // if (y < o.y) { // return -1; // } // return 0; // } } static ArrayList<Long> gLL() { return new ArrayList<>(); } static ArrayList<Integer> gL() { return new ArrayList<>(); } static StringBuffer gsb() { return new StringBuffer(); } static int find(int A[],int a) { if(A[a]==a) return a; return A[a]=find(A, A[a]); } //static int find(int A[],int a) { // if(A[a]==a) // return a; // return find(A, A[a]); //} //FENWICK TREE static class BIT{ int bit[]; BIT(int n){ bit=new int[n+1]; } int lowbit(int i){ return i&(-i); } int query(int i){ int res=0; while(i>0){ res+=bit[i]; i-=lowbit(i); } return res; } void update(int i,int val){ while(i<bit.length){ bit[i]+=val; i+=lowbit(i); } } } //END static long summation(long A[],int si,int ei) { long ans=0; for(int i=si;i<=ei;i++) ans+=A[i]; return ans; } static void add(long v,Map<Long,Long>mp) { if(!mp.containsKey(v)) { mp.put(v, (long)1); } else { mp.put(v, mp.get(v)+(long)1); } } static void remove(long v,Map<Long,Long>mp) { if(mp.containsKey(v)) { mp.put(v, mp.get(v)-(long)1); if(mp.get(v)==0) mp.remove(v); } } public static int upper(List<Long>A,long k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A.get(mid)<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static int upper(long A[],long k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static int lower(List<Long>A,long k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A.get(mid)<=k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } public static int lower(long A[],long k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } static void pln(String s) { out.println(s); } static void p(String s) { out.print(s); } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void reverse(long A[]) { int n=A.length; long B[]=new long[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void reverse(int A[]) { int n=A.length; int B[]=new int[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static long[] inputL1(int n) { long arr[]=new long[n+1]; for(int i=1;i<=n;i++) arr[i]=l(); return arr; } static int[] input1(int n) { int arr[]=new int[n+1]; for(int i=1;i<=n;i++) arr[i]=i(); return arr; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static long[][] inputL(int n,int m){ long A[][]=new long[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=l(); } } return A; } static int[][] input(int n,int m){ int A[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=i(); } } return A; } static char[][] charinput(int n,int m){ char A[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) { A[i][j]=s.charAt(j); } } return A; } static int nextPowerOf2(int n) { if(n==0) return 1; n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } static int highestPowerof2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static long highestPowerof2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long [] prefix(long A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] prefix(int A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] suffix(long A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static long [] suffix(int A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static void fill(int dp[]) { Arrays.fill(dp, -1); } static void fill(int dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(int dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(int dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static void fill(long dp[]) { Arrays.fill(dp, -1); } static void fill(long dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(long dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(long dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long power(long x, long y, long p) { if(y==0) return 1; if(x==0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static void dln(String s) { out.println(s); } static void d(String s) { out.print(s); } static void print(int A[]) { for(int i : A) { out.print(i+" "); } out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Long> hash(int A[]){ HashMap<Integer,Long> map=new HashMap<Integer, Long>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+(long)1); } else { map.put(i, (long)1); } } return map; } static HashMap<Long,Long> hash(long A[]){ HashMap<Long,Long> map=new HashMap<Long, Long>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+(long)1); } else { map.put(i, (long)1); } } return map; } static TreeMap<Integer,Integer> tree(int A[]){ TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Long,Integer> tree(long A[]){ TreeMap<Long,Integer> map=new TreeMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
d3d351ae9ff7557b84dc563f67e2650f
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.BufferedReader; import java.io.Closeable; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.time.Clock; import java.time.LocalDateTime; import java.util.*; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; public class Solution { private void solve() throws IOException { int n = nextInt(); int q = nextInt(); int MX = 1000; long[][] d = new long[MX + 1][MX + 1]; for (int i = 0; i < n; i++) { int x = nextInt(); int y = nextInt(); d[x][y] += (long) x * y; } for (int i = 0; i <= MX; i++) { for (int j = 1; j <= MX; j++) { d[i][j] += d[i][j - 1]; } } for (; q > 0; q--) { int x1 = nextInt(); int y1 = nextInt(); int x2 = nextInt(); int y2 = nextInt(); long res = 0; for (int i = x1 + 1; i < x2; i++) { res += d[i][y2 - 1] - d[i][y1]; } out.println(res); } } private static final boolean runNTestsInProd = true; private static final boolean printCaseNumber = false; private static final boolean assertInProd = false; private static final boolean logToFile = false; private static final boolean readFromConsoleInDebug = false; private static final boolean writeToConsoleInDebug = true; private static final boolean testTimer = false; private static Boolean isDebug = null; private BufferedReader in; private StringTokenizer line; private PrintWriter out; public static void main(String[] args) throws Exception { isDebug = Arrays.asList(args).contains("DEBUG_MODE"); if (isDebug) { log = logToFile ? new PrintWriter("logs/j_solution_" + System.currentTimeMillis() + ".log") : new PrintWriter(System.out); clock = Clock.systemDefaultZone(); } new Solution().run(); } private void run() throws Exception { in = new BufferedReader(new InputStreamReader(!isDebug || readFromConsoleInDebug ? System.in : new FileInputStream("input.txt"))); out = !isDebug || writeToConsoleInDebug ? new PrintWriter(System.out) : new PrintWriter("output.txt"); try (Timer totalTimer = new Timer("total")) { int t = runNTestsInProd || isDebug ? nextInt() : 1; for (int i = 0; i < t; i++) { if (printCaseNumber) { out.print("Case #" + (i + 1) + ": "); } if (testTimer) { try (Timer testTimer = new Timer("test #" + (i + 1))) { solve(); } } else { solve(); } if (isDebug) { out.flush(); } } } in.close(); out.flush(); out.close(); } private void println(Object... objects) { boolean isFirst = true; for (Object o : objects) { if (!isFirst) { out.print(" "); } else { isFirst = false; } out.print(o.toString()); } out.println(); } private int[] nextIntArray(int n) throws IOException { return nextIntArray(n, 0); } private int[] nextIntArray(int n, int delta) throws IOException { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt() + delta; } return res; } private long[] nextLongArray(int n) throws IOException { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } private char[] nextTokenChars() throws IOException { return nextToken().toCharArray(); } private String nextToken() throws IOException { while (line == null || !line.hasMoreTokens()) { line = new StringTokenizer(in.readLine()); } return line.nextToken(); } private static void assertPredicate(boolean p) { if ((isDebug || assertInProd) && !p) { throw new RuntimeException(); } } private static void assertPredicate(boolean p, String message) { if ((isDebug || assertInProd) && !p) { throw new RuntimeException(message); } } private static <T> void assertNotEqual(T unexpected, T actual) { if ((isDebug || assertInProd) && Objects.equals(actual, unexpected)) { throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual); } } private static <T> void assertEqual(T expected, T actual) { if ((isDebug || assertInProd) && !Objects.equals(actual, expected)) { throw new RuntimeException("assertEqual: " + expected + " != " + actual); } } private static PrintWriter log = null; private static Clock clock = null; private static void log(Object... objects) { log(true, objects); } private static void logNoDelimiter(Object... objects) { log(false, objects); } private static void log(boolean printDelimiter, Object[] objects) { if (isDebug) { StringBuilder sb = new StringBuilder(); sb.append(LocalDateTime.now(clock)).append(" - "); boolean isFirst = true; for (Object o : objects) { if (!isFirst && printDelimiter) { sb.append(" "); } else { isFirst = false; } sb.append(o.toString()); } log.println(sb); log.flush(); } } private static class Timer implements Closeable { private final String label; private final long startTime = isDebug ? System.nanoTime() : 0; public Timer(String label) { this.label = label; } @Override public void close() throws IOException { if (isDebug) { long executionTime = System.nanoTime() - startTime; String fraction = Long.toString(executionTime / 1000 % 1_000_000); logNoDelimiter("Timer[", label, "]: ", executionTime / 1_000_000_000, '.', "00000".substring(0, 6 - fraction.length()), fraction, 's'); } } } private static <T> T timer(String label, Supplier<T> f) throws Exception { if (isDebug) { try (Timer timer = new Timer(label)) { return f.get(); } } else { return f.get(); } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
1d081e1e5ba159118bb63b4b4237715e
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
//package codeforces.round817div4; import java.io.*; import java.util.*; import static java.lang.Math.*; public class E { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); solve(in.nextInt()); //solve(1); } /* General tips 1. It is ok to fail, but it is not ok to fail for the same mistakes over and over! 2. Train smarter, not harder! 3. If you find an answer and want to return immediately, don't forget to flush before return! */ /* Read before practice 1. Set a timer based on a problem's difficulty level: 45 minutes at your current target practice level; 2. During a problem solving session, focus! Do not switch problems or even worse switch to do something else; 3. If fail to solve within timer limit, read editorials to get as little help as possible to get yourself unblocked; 4. If after reading the entire editorial and other people's code but still can not solve, move this problem to to-do list and re-try in the future. 5. Keep a practice log about new thinking approaches, good tricks, bugs; Review regularly; 6. Also try this new approach suggested by um_nik: Solve with no intention to read editorial. If getting stuck, skip it and solve other similar level problems. Wait for 1 week then try to solve again. Only read editorial after you solved a problem. 7. Remember to also submit in the original problem link (if using gym) so that the 1 v 1 bot knows which problems I have solved already. 8. Form the habit of writing down an implementable solution idea before coding! You've taken enough hits during contests because you rushed to coding! */ /* Read before contests and lockout 1 v 1 Mistakes you've made in the past contests: 1. Tried to solve without going through given test examples -> wasting time on solving a different problem than asked; 2. Rushed to coding without getting a comprehensive sketch of your solution -> implementation bugs and WA; Write down your idea step by step, no need to rush. It is always better to have all the steps considered before hand! Think about all the past contests that you have failed because slow implementation and implementation bugs! This will be greatly reduced if you take your time to get a thorough idea steps! 3. Forgot about possible integer overflow; When stuck: 1. Understand problem statements? Walked through test examples? 2. Take a step back and think about other approaches? 3. Check rank board to see if you can skip to work on a possibly easier problem? 4. If none of the above works, take a guess? */ static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { int n = in.nextInt(), q = in.nextInt(); long[][] ps = new long[1002][1002]; for(int i = 0; i < n; i++) { int h = in.nextInt(), w = in.nextInt(); ps[h][w] += h * w; } for(int i = 0; i <= 1001; i++) { for(int j = 0; j <= 1001; j++) { ps[i][j] += ((i > 0 ? ps[i - 1][j] : 0) + (j > 0 ? ps[i][j - 1] : 0) - (i > 0 && j > 0 ? ps[i - 1][j - 1] : 0)); } } for(int i = 0; i < q; i++) { int hs = in.nextInt(), ws = in.nextInt(), hb = in.nextInt(), wb = in.nextInt(); long ans = ps[hb - 1][wb - 1] - ps[hs][wb - 1] - ps[hb - 1][ws] + ps[hs][ws]; out.println(max(ans, 0)); } } out.close(); } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("src/input.in")); out = new PrintWriter(new FileOutputStream("src/output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } List<Integer>[] readGraphOneIndexed(int n, int m) { List<Integer>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List<Integer>[] readGraphZeroIndexed(int n, int m) { List<Integer>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } /* A more efficient way of building an undirected graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildUndirectedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building an undirected weighted graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][][] buildUndirectedWeightedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt], weight = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; weight[i] = w; } int[][][] adj = new int[nodeCnt + 1][][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]][2]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]][0] = end2[i]; adj[end1[i]][idxForEachNode[end1[i]]][1] = weight[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]][0] = end1[i]; adj[end2[i]][idxForEachNode[end2[i]]][1] = weight[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building a directed graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildDirectedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]] = to[i]; idxForEachNode[from[i]]++; } return adj; } /* A more efficient way of building a directed weighted graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][][] buildDirectedWeightedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt], weight = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; weight[i] = w; } int[][][] adj = new int[nodeCnt + 1][][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]][2]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]][0] = to[i]; adj[from[i]][idxForEachNode[from[i]]][1] = weight[i]; idxForEachNode[from[i]]++; } return adj; } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
3495b1ba70b8ec987e827caccb8c008d
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class e { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int numC = Integer.parseInt(br.readLine()); for (int c = 0; c < numC; c++) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int q = Integer.parseInt(st.nextToken()); long[][] arr = new long[1001][1001]; for (int i = 0; i < n; i++) { st = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st.nextToken()); int h = Integer.parseInt(st.nextToken()); arr[l][h] += l * h; } long[][] pref = new long[1001][1001]; pref[1][1] = arr[1][1]; for (int i = 1; i <= 1000; i++) { pref[1][i] = pref[1][i - 1] + arr[i][1]; } for (int i = 1; i <= 1000; i++) { pref[i][1] = pref[i - 1][1] + arr[i][1]; } for (int i = 1; i <= 1000; i++) { for (int j = 1; j <= 1000; j++) { pref[i][j] = arr[i][j] + pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1]; } } for (int i = 0; i < q; i++) { st = new StringTokenizer(br.readLine()); int sl = Integer.parseInt(st.nextToken()); int sh = Integer.parseInt(st.nextToken()); int bl = Integer.parseInt(st.nextToken()); int bh = Integer.parseInt(st.nextToken()); pw.println(pref[bl - 1][bh - 1] - pref[sl][bh - 1] - pref[bl - 1][sh] + pref[sl][sh]); } } pw.close(); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
67c293bff65908f8f058fd5114b718c1
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; public class Main { static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); static int dx[]={0,0,-1,1},dy[]={-1,1,0,0}; static final double pi=3.1415926536; static long mod=1000000007; // static long mod=998244353; static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; static long MAXL=Long.MAX_VALUE; static long MINL=Long.MIN_VALUE; static ArrayList<pair> graph[]; static long fact[]; static long seg[]; static int dp[][]; // static long dp[][]; public static void main (String[] args) throws java.lang.Exception { // code goes here int t=I(); outer:while(t-->0) { int n=I(); int q=I(); int a[][]=new int[n][2]; int r_max=MIN; int c_max=MIN; for(int i=0;i<n;i++){ a[i][0]=I(); a[i][1]=I(); r_max=max(r_max,a[i][0]); c_max=max(c_max,a[i][1]); } long temp[][]=new long[1001][1001]; for(int i=0;i<n;i++){ temp[a[i][0]][a[i][1]]+=1L*a[i][0]*a[i][1]; } // for(int i=0;i<7;i++){ // for(int j=0;j<7;j++){ // out.print(temp[i][j]+" "); // }out.println(); // } for(int i=0;i<1001;i++){ for(int j=1;j<1001;j++){ temp[i][j]+=temp[i][j-1]; } } for(int i=0;i<1001;i++){ for(int j=1;j<1001;j++){ temp[j][i]+=temp[j-1][i]; } } // for(int i=0;i<7;i++){ // for(int j=0;j<7;j++){ // out.print(temp[i][j]+" "); // }out.println(); // } while(q-->0){ int aa=I(),bb=I(),cc=I(),dd=I(); if(cc-aa==1 || dd-bb==1){ out.println("0"); }else{ out.println(temp[cc-1][dd-1]+temp[aa][bb]-temp[aa][dd-1]-temp[cc-1][bb]); } } } out.close(); } public static class pair { long a; long b; public pair(long aa,long bb) { a=aa; b=bb; } } public static class myComp implements Comparator<pair> { public int compare(pair p1,pair p2) { if(p1.a-p1.b==p2.a-p2.b)return 0; if(p1.a-p1.b>p2.a-p2.b)return -1; return 1; } //sort in ascending order. // public int compare(pair p1,pair p2) // { // if(p1.a==p2.a) // return 0; // else if(p1.a<p2.a) // return -1; // else // return 1; // } // sort in descending order. // public int compare(pair p1,pair p2) // { // if(p1.b==p2.b) // return 0; // else if(p1.b<p2.b) // return 1; // else // return -1; // } } // public static void setGraph(int n,int m)throws IOException // { // graph=new ArrayList[n+1]; // for(int i=0;i<=n;i++){ // graph[i]=new ArrayList<>(); // } // for(int i=0;i<m;i++){ // int u=I(),v=I(); // graph[u].add(v); // graph[v].add(u); // } // } //LOWER_BOUND and UPPER_BOUND functions //It returns answer according to zero based indexing. public static int lower_bound(pair[] arr,int X,int start, int end) //start=0,end=n-1 { if(start>end)return -1; // if(arr.get(end)<X)return end; if(arr[end].a<X)return end; // if(arr.get(start)>X)return -1; if(arr[start].a>X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; if(arr[mid].a==X){ // if(arr.get(mid)==X){ //Returns last index of lower bound value. if(mid<end && arr[mid+1].a==X){ // if(mid<end && arr.get(mid+1)==X){ left=mid+1; }else{ return mid; } } // if(arr.get(mid)==X){ //Returns first index of lower bound value. // if(arr[mid]==X){ // // if(mid>start && arr.get(mid-1)==X){ // if(mid>start && arr[mid-1]==X){ // right=mid-1; // }else{ // return mid; // } // } else if(arr[mid].a>X){ // else if(arr.get(mid)>X){ if(mid>start && arr[mid-1].a<X){ // if(mid>start && arr.get(mid-1)<X){ return mid-1; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1].a>X){ // if(mid<end && arr.get(mid+1)>X){ return mid; }else{ left=mid+1; } } } return left; } //It returns answer according to zero based indexing. public static int upper_bound(long arr[],long X,int start,int end) //start=0,end=n-1 { if(arr[0]>=X)return start; if(arr[arr.length-1]<X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; if(arr[mid]==X){ //returns first index of upper bound value. if(mid>start && arr[mid-1]==X){ right=mid-1; }else{ return mid; } } // if(arr[mid]==X){ //returns last index of upper bound value. // if(mid<end && arr[mid+1]==X){ // left=mid+1; // }else{ // return mid; // } // } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid+1; }else{ left=mid+1; } } } return left; } //END //Segment Tree Code public static void buildTree(long a[],int si,int ss,int se) { if(ss==se){ seg[si]=a[ss]; return; } int mid=(ss+se)/2; buildTree(a,2*si+1,ss,mid); buildTree(a,2*si+2,mid+1,se); seg[si]=max(seg[2*si+1],seg[2*si+2]); } // public static void update(int si,int ss,int se,int pos,int val) // { // if(ss==se){ // // seg[si]=val; // return; // } // int mid=(ss+se)/2; // if(pos<=mid){ // update(2*si+1,ss,mid,pos,val); // }else{ // update(2*si+2,mid+1,se,pos,val); // } // // seg[si]=min(seg[2*si+1],seg[2*si+2]); // if(seg[2*si+1].a < seg[2*si+2].a){ // seg[si].a=seg[2*si+1].a; // seg[si].b=seg[2*si+1].b; // }else{ // seg[si].a=seg[2*si+2].a; // seg[si].b=seg[2*si+2].b; // } // } public static long query1(int si,int ss,int se,int qs,int qe) { if(qs>se || qe<ss)return 0; if(ss>=qs && se<=qe)return seg[si]; int mid=(ss+se)/2; long p1=query1(2*si+1,ss,mid,qs,qe); long p2=query1(2*si+2,mid+1,se,qs,qe); return max(p1,p2); } public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b) { int i=0,j=0; while(i<a.size() && j<b.size()){ if(a.get(i)<=b.get(j)){ f.add(a.get(i)); i++; }else{ f.add(b.get(j)); j++; } } while(i<a.size()){ f.add(a.get(i)); i++; } while(j<b.size()){ f.add(b.get(j)); j++; } } //Segment Tree Code end //Prefix Function of KMP Algorithm public static int[] KMP(char c[],int n) { int pi[]=new int[n]; for(int i=1;i<n;i++){ int j=pi[i-1]; while(j>0 && c[i]!=c[j]){ j=pi[j-1]; } if(c[i]==c[j])j++; pi[i]=j; } return pi; } public static long kadane(long a[],int n) //largest sum subarray { long max_sum=Long.MIN_VALUE,max_end=0; for(int i=0;i<n;i++){ max_end+=a[i]; if(max_sum<max_end){max_sum=max_end;} if(max_end<0){max_end=0;} } return max_sum; } public static ArrayList<Long> primeFact(long x) { ArrayList<Long> arr=new ArrayList<>(); if(x%2==0){ arr.add(2L); while(x%2==0){ x/=2; } } for(long i=3;i*i<=x;i+=2){ if(x%i==0){ arr.add(i); while(x%i==0){ x/=i; } } } if(x>0){ arr.add(x); } return arr; } public static long nPr(int n,int r) { long ans=divide(fact(n),fact(n-r),mod); return ans; } public static long nCr(int n,int r) { long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod); return ans; } public static boolean isSorted(int a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static boolean isSorted(long a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static int computeXOR(int n) //compute XOR of all numbers between 1 to n. { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } public static int np2(int x) { x--; x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x++; return x; } public static int hp2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static long hp2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static ArrayList<Integer> primeSieve(int n) { ArrayList<Integer> arr=new ArrayList<>(); boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } // Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n); public static class FenwickTree { int farr[]; int n; public FenwickTree(int c) { n=c+1; farr=new int[n]; } // public void update_range(int l,int r,long p) // { // update(l,p); // update(r+1,(-1)*p); // } public void update(int x,int p) { for(;x<n;x+=x&(-x)) { farr[x]+=p; } } public int get(int x) { int ans=0; for(;x>0;x-=x&(-x)) { ans=ans+farr[x]; } return ans; } } //Disjoint Set Union //NOTE: call find function for all the index in the par array at last, //in order to set parent of every index properly. public static class DSU { int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } public static boolean isVowel(char c) { if(c=='a' || c=='e' || c=='i' || c=='u' || c=='o')return true; return false; } public static boolean isInteger(double N) { int X = (int)N; double temp2 = N - X; if (temp2 > 0) { return false; } return true; } public static boolean isPalindrome(String s) { int n=s.length(); for(int i=0;i<=n/2;i++){ if(s.charAt(i)!=s.charAt(n-i-1)){ return false; } } return true; } public static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static long fact(long n) { long fact=1; for(long i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static long fact(int n) { long fact=1; for(int i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void printArray(long a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(int a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(char a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]); } out.println(); } public static void printArray(String a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(boolean a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(pair a[]) { for(pair p:a){ out.println(p.a+"->"+p.b); } } public static void printArray(int a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(String a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(boolean a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(long a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(char a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(ArrayList<?> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printMapI(HashMap<?,?> hm){ for(Map.Entry<?,?> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } public static void printMap(HashMap<Long,ArrayList<Integer>> hm){ for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){ out.print(e.getKey()+"->"); ArrayList<Integer> arr=e.getValue(); for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); }out.println(); } } public static void printGraph(ArrayList<Integer> graph[]) { int n=graph.length; for(int i=0;i<n;i++){ out.print(i+"->"); for(int j:graph[i]){ out.print(j+" "); }out.println(); } } //Modular Arithmetic public static long add(long a,long b) { a+=b; if(a>=mod)a-=mod; return a; } public static long sub(long a,long b) { a-=b; if(a<0)a+=mod; return a; } public static long mul(long a,long b) { return ((a%mod)*(b%mod))%mod; } public static long divide(long a,long b,long m) { a=mul(a,modInverse(b,m)); return a; } public static long modInverse(long a,long m) { int x=0,y=0; own p=new own(x,y); long g=gcdExt(a,m,p); if(g!=1){ out.println("inverse does not exists"); return -1; }else{ long res=((p.a%m)+m)%m; return res; } } public static long gcdExt(long a,long b,own p) { if(b==0){ p.a=1; p.b=0; return a; } int x1=0,y1=0; own p1=new own(x1,y1); long gcd=gcdExt(b,a%b,p1); p.b=p1.a - (a/b) * p1.b; p.a=p1.b; return gcd; } public static long pwr(long m,long n) { long res=1; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m); } n=n>>1; m=(m*m); } return res; } public static long modpwr(long m,long n) { long res=1; m=m%mod; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m)%mod; } n=n>>1; m=(m*m)%mod; } return res; } public static class own { long a; long b; public own(long val,long index) { a=val; b=index; } } //Modular Airthmetic public static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(char[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { char tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } //max & min public static int max(int a,int b){return Math.max(a,b);} public static int min(int a,int b){return Math.min(a,b);} public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));} public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));} public static long max(long a,long b){return Math.max(a,b);} public static long min(long a,long b){return Math.min(a,b);} public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));} public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));} public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} //end public static int[] I(int n)throws IOException{int a[]=new int[n];for(int i=0;i<n;i++){a[i]=I();}return a;} public static long[] IL(int n)throws IOException{long a[]=new long[n];for(int i=0;i<n;i++){a[i]=L();}return a;} public static long[] prefix(int a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;} public static long[] prefix(long a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;} public static int I()throws IOException{return sc.I();} public static long L()throws IOException{return sc.L();} public static String S()throws IOException{return sc.S();} public static double D()throws IOException{return sc.D();} } class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try { st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int I(){ return Integer.parseInt(next());} long L(){ return Long.parseLong(next());} double D(){return Double.parseDouble(next());} String S(){ String str = ""; try { str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
91948e0d35456b631f3b7488e72d7674
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.text.*; public class Main{ public static void main(String args[]) throws IOException{ Read sc=new Read(); int t=sc.nextInt(); for(int i=0;i<t;i++){ int n=sc.nextInt(); int q=sc.nextInt(); int count[][]=new int[1005][1005]; for(int j=0;j<n;j++){ int a=sc.nextInt(); int b=sc.nextInt(); count[a][b]++; } long preSum[][]=new long[1005][1005]; for(int j=1;j<=1001;j++){ for(int k=1;k<=1001;k++){ long mm=(long)count[j][k]*k*j; preSum[j][k]=mm+preSum[j-1][k]+preSum[j][k-1]-preSum[j-1][k-1]; } } for(int j=0;j<q;j++){ int a1=sc.nextInt(); int b1=sc.nextInt(); int a2=sc.nextInt(); int b2=sc.nextInt(); sc.println(preSum[a2-1][b2-1]+preSum[a1][b1]-preSum[a2-1][b1]-preSum[a1][b2-1]); } } //sc.print(0); sc.bw.flush(); sc.bw.close(); } } //记住看数字范围,需要开long吗,需要用BigInteger吗,需要手动处理字符串吗,复杂度数量级控制在1e7或者以下了吗 //开数组的数据范围最高不能超过1e7,数据范围再大就要用哈希表离散化了 //基本数据类型不能自定义sort排序,二维数组就可以了 class Read{ BufferedReader bf; StringTokenizer st; BufferedWriter bw; public Read(){ bf=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); bw=new BufferedWriter(new OutputStreamWriter(System.out)); } public String nextLine() throws IOException{ return bf.readLine(); } public String next() throws IOException{ while(!st.hasMoreTokens()){ st=new StringTokenizer(bf.readLine()); } return st.nextToken(); } public char nextChar() throws IOException{ //确定下一个token只有一个字符的时候再用 return next().charAt(0); } public int nextInt() throws IOException{ return Integer.parseInt(next()); } public long nextLong() throws IOException{ return Long.parseLong(next()); } public double nextDouble() throws IOException{ return Double.parseDouble(next()); } public float nextFloat() throws IOException{ return Float.parseFloat(next()); } public byte nextByte() throws IOException{ return Byte.parseByte(next()); } public short nextShort() throws IOException{ return Short.parseShort(next()); } public BigInteger nextBigInteger() throws IOException{ return new BigInteger(next()); } public void println(int a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(int a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(String a) throws IOException{ bw.write(a); bw.newLine(); return; } public void print(String a) throws IOException{ bw.write(a); return; } public void println(long a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(long a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(double a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(double a) throws IOException{ bw.write(String.valueOf(a)); return; } public void print(BigInteger a) throws IOException{ bw.write(a.toString()); return; } public void print(char a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(char a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
cc9189b0f1519e04d5206ba9be4b5799
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); for (int tNum = 1; tNum <= t; tNum++) { long[][] map = new long[1010][1010]; System.gc(); StringTokenizer st = new StringTokenizer(br.readLine()); int N = Integer.parseInt(st.nextToken()); int Q = Integer.parseInt(st.nextToken()); for (int i = 0; i < N; i++) { st = new StringTokenizer(br.readLine()); int row = Integer.parseInt(st.nextToken()); int col = Integer.parseInt(st.nextToken()); map[row][col] += row * col; } for (int i = 1; i <= 1000; i++) { for (int j = 1; j <= 1000; j++) { map[i][j] += map[i][j - 1]; } } for (int q = 0; q < Q; q++) { long answer = 0; st = new StringTokenizer(br.readLine()); int rs = Integer.parseInt(st.nextToken()); int cs = Integer.parseInt(st.nextToken()); int re = Integer.parseInt(st.nextToken()); int ce = Integer.parseInt(st.nextToken()); for(int row = rs + 1; row < re; row++) { answer += map[row][ce - 1] - map[row][cs]; } sb.append(answer).append("\n"); } } System.out.print(sb.toString()); } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
3a7cbc00b820f737fcec2a9237af1831
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.*; import java.util.*; public class MainE { static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public boolean hasNext() { try { String string = reader.readLine(); if (string == null) { return false; } tokenizer = new StringTokenizer(string); return tokenizer.hasMoreTokens(); } catch (IOException e) { return false; } } } static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static void ini() { } static String yes = "YES"; static String no = "NO"; static int ipInf = Integer.MAX_VALUE-5; static int inInf = Integer.MIN_VALUE+5; static long lpInf = Long.MAX_VALUE - 5; static long lnInf = Long.MIN_VALUE + 5; public static void main(String[] args) { int t = in.nextInt(); ini(); while (t -- > 0) { solve(); } out.close(); } static void solve() { int n = in.nextInt(); int q = in.nextInt(); long[][] matrix = new long[1005][1005]; int h, w; for (int i = 0; i < n; i++) { h = in.nextInt(); w = in.nextInt(); matrix[h][w] += (long) h * w; } long[][] pre_sum = new long[1005][1005]; for (int i = 1; i < 1005; i++) { for (int j = 1; j < 1005; j++) { pre_sum[i][j] = pre_sum[i-1][j] + pre_sum[i][j-1] - pre_sum[i-1][j-1] + matrix[i][j]; } } int qh1, qw1, qh2, qw2; for (int i = 0; i < q; i++) { qh1 = in.nextInt()+1; qw1 = in.nextInt()+1; qh2 = in.nextInt()-1; qw2 = in.nextInt()-1; out.println(pre_sum[qh2][qw2] + pre_sum[qh1-1][qw1-1] - pre_sum[qh1-1][qw2] - pre_sum[qh2][qw1-1]); } } static void printArr (int[] arr) { int n = arr.length; if (n == 0) return; for (int i = 0; i < n-1; i++) { out.print(arr[i] + " "); } out.println(arr[n-1]); } static void printArr (long[] arr) { int n = arr.length; if (n == 0) return; for (int i = 0; i < n-1; i++) { out.print(arr[i] + " "); } out.println(arr[n-1]); } static long _gcd (long a, long b) { long temp; while (true) { temp = a % b; if (temp == 0) return b; a = b; b = temp; } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
65cdd8db2b73809f0b3a256e7cf2fabe
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; public class E_Counting_Rectangles { static FastReader sc; static void solve() { StringBuilder res = new StringBuilder(); int n = sc.nextInt(), q = sc.nextInt(); long[][] arr = new long[1001][1001]; for (int i = 0; i < n; i++) { int u = sc.nextInt(), v = sc.nextInt(); arr[u][v]++; } for (int i = 1; i < 1001; i++) { for (int j = 1; j < 1001; j++) { arr[i][j] *= i * j; arr[i][j] += arr[i - 1][j] + arr[i][j - 1] - arr[i - 1][j - 1]; } } while (q-- > 0) { int u = sc.nextInt(), v = sc.nextInt(); int x = sc.nextInt(), y = sc.nextInt(); long ans = arr[x - 1][y - 1] - arr[x - 1][v] - arr[u][y - 1] + arr[u][v]; res.append(ans).append("\n"); } System.out.print(res); } public static void main(String[] args) throws IOException { sc = new FastReader(); int tt = sc.nextInt(); while (tt-- > 0) { solve(); } } static <E> void debug(E a) { System.err.println(a); } static void debug(int... a) { System.err.println(Arrays.toString(a)); } static int maxOf(int... array) { return Arrays.stream(array).max().getAsInt(); } static int minOf(int... array) { return Arrays.stream(array).parallel().reduce(Math::min).getAsInt(); } static <E> void print(E res) { System.out.println(res); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] readLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
d08d41a49312286a98d789db0fba23b5
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.io.*; import java.util.*; public class Solution { static Solution2 admin = new Solution2(); public static void main(String[] args) { admin.start(); } } class Solution2 { //---------------------------------INPUT READER-----------------------------------------// public BufferedReader br; StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine());} catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long n) {int[]ret=new int[(int)n]; for(int i=0;i<n;i++) ret[i]=ni(); return ret;} long[] nal(long n) {long[]ret=new long[(int)n]; for(int i=0;i<n;i++) ret[i]=nl(); return ret;} Integer[] nA(long n) {Integer[]ret=new Integer[(int)n]; for(int i=0;i<n;i++) ret[i]=ni(); return ret;} Long[] nAl(long n) {Long[]ret=new Long[(int)n]; for(int i=0;i<n;i++) ret[i]=nl(); return ret;} //--------------------------------------PRINTER------------------------------------------// PrintWriter w; void p(int i) {w.println(i);} void p(long l) {w.println(l);} void p(double d) {w.println(d);} void p(String s) { w.println(s);} void pr(int i) {w.print(i);} void pr(long l) {w.print(l);} void pr(double d) {w.print(d);} void pr(String s) { w.print(s);} void pl() {w.println();} //--------------------------------------VARIABLES-----------------------------------------// long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; long mod = 1000000007; { w = new PrintWriter(System.out); br = new BufferedReader(new InputStreamReader(System.in)); try {if(new File(System.getProperty("user.dir")).getName().equals("LOCAL")) { w = new PrintWriter(new OutputStreamWriter(new FileOutputStream("output.txt"))); br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));} } catch (Exception ignore) { } } //----------------------START---------------------// void start() { int t = ni(); while(t-- > 0) solve(); w.close(); } void solve() { int n = ni(), q = ni(); long[][] mat = new long[1002][1002]; for(int i = 0; i < n; i++) { int a = ni(), b = ni(); mat[a][b] += (long)a * b; } for(int i = 0; i < 1002; i++) { for(int j = 1; j < 1002; j++) { mat[i][j] += mat[i][j-1]; } } for(int j = 0; j < 1002; j++) { for(int i = 1; i < 1002; i++) { mat[i][j] += mat[i-1][j]; } } while(q-- > 0) { int a = ni(), b = ni(), c = ni(), d = ni(); long ans = mat[c-1][d-1]; ans -= mat[c-1][b]; ans -= mat[a][d-1]; ans += mat[a][b]; p(ans); } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
532264c2714dbdbe3b13993518386b4a
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; public class A { static Scanner sc; static PrintWriter pw; public static long[][] prefixSum(long a[][]) { int R = a.length; int C = a[0].length; long psa[][] = new long[R][C]; psa[0][0] = a[0][0]; for (int i = 1; i < C; i++) psa[0][i] = psa[0][i - 1] + a[0][i]; for (int i = 1; i < R; i++) psa[i][0] = psa[i - 1][0] + a[i][0]; for (int i = 1; i < R; i++) for (int j = 1; j < C; j++) psa[i][j] = psa[i - 1][j] + psa[i][j - 1] - psa[i - 1][j - 1] + a[i][j]; return psa; } static int n = 1000, m = 1000; static long[][] pre; static long sum(int i, int j) { if (i < 0 || j < 0 || i >= n || j >= m) return 0; return pre[i][j]; } static long calc(int x1, int y1, int x2, int y2) { return sum(x2, y2) + sum(x1 - 1, y1 - 1) - sum(x1 - 1, y2) - sum(x2, y1 - 1); } public static void main(String[] args) throws Exception { pw = new PrintWriter(System.out); sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int q = sc.nextInt(); long[][] arr = new long[1000][1000]; for (int i = 0; i < n; i++) { int h = sc.nextInt() - 1; int w = sc.nextInt() - 1; arr[h][w] += (h + 1) * (w + 1); } pre = prefixSum(arr); // for (int i = 0; i < 4; i++) { // for (int j = 0; j < 4; j++) { // pw.print(pre[i][j] + " "); // } // pw.println(); // } while (q-- > 0) { int h1 = sc.nextInt() - 1; int w1 = sc.nextInt() - 1; int h2 = sc.nextInt() - 1; int w2 = sc.nextInt() - 1; if (h2 - h1 <= 1 || w2 - w1 <= 1) { pw.println(0); } else { pw.println(calc(h1 + 1, w1 + 1, h2 - 1, w2 - 1)); } } } pw.flush(); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String s) throws Exception { br = new BufferedReader(new FileReader(new File(s))); } public String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
76850df31f0ba9c5dbc985971e381568
train_109.jsonl
1661871000
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s &lt; h_i &lt; h_b$$$ and $$$w_s &lt; w_i &lt; w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { Reader in = new Reader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for (int t = 0; t < T; t++) { int N = in.nextInt(), Q = in.nextInt(); long[][] preSum = new long[1001][1001]; for (int i = 0; i < N; i++) { int x = in.nextInt(), y = in.nextInt(); preSum[x][y] += x * y; } for (int i = 0; i <= 1000; i++) { for (int j = 0; j <= 1000; j++) { if (i > 0) { preSum[i][j] += preSum[i - 1][j]; } if (j > 0) { preSum[i][j] += preSum[i][j - 1]; } if (i > 0 && j > 0) { preSum[i][j] -= preSum[i - 1][j - 1]; } } } for (int q = 0; q < Q; q++) { int x1 = in.nextInt(), y1 = in.nextInt(), x2 = in.nextInt(), y2 = in.nextInt(); if (x1 + 1 == x2 || y1 + 1 == y2) { out.println(0); continue; } long res = preSum[x2 - 1][y2 - 1] - preSum[x1][y2 - 1] - preSum[x2 - 1][y1] + preSum[x1][y1]; out.println(res); } } out.close(); } static class Reader { BufferedReader in; StringTokenizer st; public Reader() { in = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } public String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } public String next() throws IOException { while (!st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } } public static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i : arr) { list.add(i); } Collections.sort(list); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } } }
Java
["3\n\n2 1\n\n2 3\n\n3 2\n\n1 1 3 4\n\n5 5\n\n1 1\n\n2 2\n\n3 3\n\n4 4\n\n5 5\n\n3 3 6 6\n\n2 1 4 5\n\n1 1 2 10\n\n1 1 100 100\n\n1 1 3 3\n\n3 1\n\n999 999\n\n999 999\n\n999 998\n\n1 1 1000 1000"]
6 seconds
["6\n41\n9\n0\n54\n4\n2993004"]
Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 &lt; 2$$$ (comparing heights) and $$$1 &lt; 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 &lt; 3$$$ (comparing heights) and $$$3 &lt; 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.
Java 8
standard input
[ "brute force", "data structures", "dp", "implementation" ]
9e9c7434ebf0f19261012975fe9ee7d0
The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s &lt; h_b,\ w_s &lt; w_b \leq 1000$$$) — the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
1,600
For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.
standard output
PASSED
865487f854f4ea20c16bff4ecaf62aa4
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.*; import java.util.*; import java.util.stream.Collectors; public class WordGame { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringBuilder result = new StringBuilder(); int tc = Integer.parseInt(reader.readLine()); while (tc --> 0) { int wordCount = Integer.parseInt(reader.readLine()); HashMap<String, Integer> map = new HashMap<>(); ArrayList<String> s1 = new ArrayList<>(Arrays.stream(reader.readLine().split(" ")).peek(s -> { if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); }).collect(Collectors.toList()));// ArrayList<String> s2 = new ArrayList<>(Arrays.stream(reader.readLine().split(" ")).peek(s -> { if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); }).collect(Collectors.toList())); ArrayList<String> s3 = new ArrayList<>(Arrays.stream(reader.readLine().split(" ")).peek(s -> { if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); }).collect(Collectors.toList())); int p1 = 0, p2 = 0, p3 = 0; for (String s : s1) { int point = map.get(s); if (point == 1) p1 += 3; else if (point == 2) p1 += 1; } for (String s : s2) { int point = map.get(s); if (point == 1) p2 += 3; else if (point == 2) p2 += 1; } for (String s : s3) { int point = map.get(s); if (point == 1) p3 += 3; else if (point == 2) p3 += 1; } result.append(p1).append(" ").append(p2).append(" ").append(p3).append("\n"); } System.out.println(result.substring(0, result.length() - 1)); } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
0beb6b31c22663fd200177d7c3fb529e
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.*; import java.util.*; import java.util.stream.Collectors; public class WordGame { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringBuilder result = new StringBuilder(); int tc = Integer.parseInt(reader.readLine()); while (tc --> 0) { int wordCount = Integer.parseInt(reader.readLine()); Hashtable<String, Integer> map = new Hashtable<>(); ArrayList<String> s1 = new ArrayList<>(Arrays.stream(reader.readLine().split(" ")).parallel().peek(s -> { if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); }).collect(Collectors.toList())); ArrayList<String> s2 = new ArrayList<>(Arrays.stream(reader.readLine().split(" ")).parallel().peek(s -> { if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); }).collect(Collectors.toList())); ArrayList<String> s3 = new ArrayList<>(Arrays.stream(reader.readLine().split(" ")).parallel().peek(s -> { if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); }).collect(Collectors.toList())); int p1 = 0, p2 = 0, p3 = 0; for (String s : s1) { int point = map.get(s); if (point == 1) p1 += 3; else if (point == 2) p1 += 1; } for (String s : s2) { int point = map.get(s); if (point == 1) p2 += 3; else if (point == 2) p2 += 1; } for (String s : s3) { int point = map.get(s); if (point == 1) p3 += 3; else if (point == 2) p3 += 1; } result.append(p1).append(" ").append(p2).append(" ").append(p3).append("\n"); } System.out.println(result.substring(0, result.length() - 1)); } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
d3af35f525b8fdfbe6757ea6a53036a3
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.*; import java.util.*; import java.util.stream.Collectors; public class WordGame { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringBuilder result = new StringBuilder(); int tc = Integer.parseInt(reader.readLine()); while (tc --> 0) { int wordCount = Integer.parseInt(reader.readLine()); Hashtable<String, Integer> map = new Hashtable<>(); ArrayList<String> s1 = new ArrayList<>(Arrays.stream(reader.readLine().split(" ")).peek(s -> { if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); }).collect(Collectors.toList())); ArrayList<String> s2 = new ArrayList<>(Arrays.stream(reader.readLine().split(" ")).peek(s -> { if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); }).collect(Collectors.toList())); ArrayList<String> s3 = new ArrayList<>(Arrays.stream(reader.readLine().split(" ")).peek(s -> { if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); }).collect(Collectors.toList())); int p1 = 0, p2 = 0, p3 = 0; for (String s : s1) { int point = map.get(s); if (point == 1) p1 += 3; else if (point == 2) p1 += 1; } for (String s : s2) { int point = map.get(s); if (point == 1) p2 += 3; else if (point == 2) p2 += 1; } for (String s : s3) { int point = map.get(s); if (point == 1) p3 += 3; else if (point == 2) p3 += 1; } result.append(p1).append(" ").append(p2).append(" ").append(p3).append("\n"); } System.out.println(result.substring(0, result.length() - 1)); } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
cd8b1606a5e0d85f8c5ec3bf464e68a4
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.*; import java.util.*; import java.util.stream.Collectors; public class WordGame { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringBuilder result = new StringBuilder(); int tc = Integer.parseInt(reader.readLine()); while (tc --> 0) { int wordCount = Integer.parseInt(reader.readLine()); HashMap<String, Integer> map = new HashMap<>(); ArrayList<String> s1 = new ArrayList<>(Arrays.stream(reader.readLine().split(" ")).peek(s -> { if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); }).collect(Collectors.toList())); ArrayList<String> s2 = new ArrayList<>(Arrays.stream(reader.readLine().split(" ")).peek(s -> { if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); }).collect(Collectors.toList())); ArrayList<String> s3 = new ArrayList<>(Arrays.stream(reader.readLine().split(" ")).peek(s -> { if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); }).collect(Collectors.toList())); int p1 = 0, p2 = 0, p3 = 0; for (String s : s1) { int point = map.get(s); if (point == 1) p1 += 3; else if (point == 2) p1 += 1; } for (String s : s2) { int point = map.get(s); if (point == 1) p2 += 3; else if (point == 2) p2 += 1; } for (String s : s3) { int point = map.get(s); if (point == 1) p3 += 3; else if (point == 2) p3 += 1; } result.append(p1).append(" ").append(p2).append(" ").append(p3).append("\n"); } System.out.println(result.substring(0, result.length() - 1)); } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
b61a2098cbb78b6499c29a7f7f001dea
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.*; import java.util.*; public class WordGame { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringBuilder result = new StringBuilder(); int tc = Integer.parseInt(reader.readLine()); while (tc --> 0) { int wordCount = Integer.parseInt(reader.readLine()); String [] s1 = reader.readLine().split(" "); String [] s2 = reader.readLine().split(" "); String [] s3 = reader.readLine().split(" "); HashMap<String, Integer> map = new HashMap<>(); int p1 = 0, p2 = 0, p3 = 0; for (String s : s1) if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); for (String s : s2) if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); for (String s : s3) if (map.containsKey(s)) map.put(s, map.get(s) + 1); else map.put(s, 1); for (String s : s1) { int point = map.get(s); if (point == 1) p1 += 3; else if (point == 2) p1 += 1; } for (String s : s2) { int point = map.get(s); if (point == 1) p2 += 3; else if (point == 2) p2 += 1; } for (String s : s3) { int point = map.get(s); if (point == 1) p3 += 3; else if (point == 2) p3 += 1; } result.append(p1).append(" ").append(p2).append(" ").append(p3).append("\n"); } System.out.println(result.substring(0, result.length() - 1)); } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
8f9a1bdf89c8f8d39ede554a397a373a
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.*; import java.util.*; public class S { public static HashMap<String,Integer> map = new HashMap<>(); public static String[][] strs; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); out: for (int i=0;i<t;i++){ int n = Integer.parseInt(br.readLine()); //# words written strs=new String[3][n]; map = new HashMap<>(); for (int j=0;j<3;j++){ //3 people writing words StringTokenizer s = new StringTokenizer(br.readLine()); for (int k=0;k<n;k++){ //written words String curr = s.nextToken(); if (map.get(curr)==null){ map.put(curr,1); }else{ int currCout = map.get(curr).intValue(); currCout+=1; map.remove(curr); map.put(curr,currCout); } strs[j][k]=curr; } } String ans = ""; for (int a=0;a<3;a++){ int pts = 0; for (int b=0;b<n;b++){ if (map.get(strs[a][b])==1){ pts+=3; }else if (map.get(strs[a][b])==2){ pts+=1; } } ans+=pts+" "; } System.out.println(ans); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
9e2dbf78665893f29274de1b2a9556fb
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import static java.lang.Character.isWhitespace; import java.io.*; import java.util.*; public final class Main { void solve(InputReader in, StringBuilder sb) { int t = in.nextInt(); while (t-- > 0) { var wordCount = new HashMap<String, Integer>(); int count = in.nextInt(); var words1 = Arrays.stream(in.nextLine().split(" ")).map(String::trim).toList(); for (String word : words1) { wordCount.put(word, wordCount.getOrDefault(word, 0) + 1); } var words2 = Arrays.stream(in.nextLine().split(" ")).map(String::trim).toList(); for (String word : words2) { wordCount.put(word, wordCount.getOrDefault(word, 0) + 1); } var words3 = Arrays.stream(in.nextLine().split(" ")).map(String::trim).toList(); for (String word : words3) { wordCount.put(word, wordCount.getOrDefault(word, 0) + 1); } int score1 = 0, score2 = 0, score3 = 0; for (String word : words1) { int c = wordCount.getOrDefault(word, 0); score1 += switch (c) { case 1 -> 3; case 2 -> 1; default -> 0; }; } for (String word : words2) { int c = wordCount.getOrDefault(word, 0); score2 += switch (c) { case 1 -> 3; case 2 -> 1; default -> 0; }; } for (String word : words3) { int c = wordCount.getOrDefault(word, 0); score3 += switch (c) { case 1 -> 3; case 2 -> 1; default -> 0; }; } sb.append(score1).append(" ").append(score2).append(" ").append(score3).append("\n"); } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); StringBuilder sb = new StringBuilder(); Main solver = new Main(); solver.solve(in, sb); System.out.println(sb.toString()); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String nextLine() { try { return reader.readLine(); } catch (Exception e) { throw new RuntimeException(e); } } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
618c3d69c5138ac8d38964b4c0a409e9
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; /** * Three guys play a game: first, each person writes down n distinct words of length 3. * Then, they total up the number of points as follows: * * if a word was written by one person — that person gets 3 points, * if a word was written by two people — each of the two gets 1 point, * if a word was written by all — nobody gets any points. * * In the end, how many points does each player have? */ public class WordGame { private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int testCases = Integer.parseInt(reader.readLine()); for (int i = 0; i < testCases; i++) { countScore(); } } private static void countScore() throws IOException { int numberOfWords = Integer.parseInt(reader.readLine()); Set<String> first = Arrays.stream(reader.readLine().split(" ")).collect(Collectors.toSet()); Set<String> second = Arrays.stream(reader.readLine().split(" ")).collect(Collectors.toSet()); Set<String> third = Arrays.stream(reader.readLine().split(" ")).collect(Collectors.toSet()); int firstScore = 0; int secondScore = 0; int thirdScore = 0; for (String s : first) { if (second.contains(s) && third.contains(s)) { // no points second.remove(s); third.remove(s); } else if (second.contains(s) || third.contains(s)) { // one point only firstScore++; if (second.contains(s)) { secondScore++; second.remove(s); } else { thirdScore++; third.remove(s); } } else { // three points firstScore += 3; } } for (String s : second) { if (third.contains(s)) { // one point only secondScore++; thirdScore++; third.remove(s); } else { // three points secondScore += 3; } } thirdScore += third.size() * 3; System.out.println(firstScore + " " + secondScore + " " + thirdScore); } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
e540f74e61e0a8688d00dc47f4efde4f
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; /** * Three guys play a game: first, each person writes down n distinct words of length 3. * Then, they total up the number of points as follows: * * if a word was written by one person — that person gets 3 points, * if a word was written by two people — each of the two gets 1 point, * if a word was written by all — nobody gets any points. * * In the end, how many points does each player have? */ public class WordGame { private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int testCases = Integer.parseInt(reader.readLine()); for (int i = 0; i < testCases; i++) { countScore(); } } private static void countScore() throws IOException { int numberOfWords = Integer.parseInt(reader.readLine()); Set<String> first = Arrays.stream(reader.readLine().split(" ")).collect(Collectors.toSet()); Set<String> second = Arrays.stream(reader.readLine().split(" ")).collect(Collectors.toSet()); Set<String> third = Arrays.stream(reader.readLine().split(" ")).collect(Collectors.toSet()); int firstScore = checkUniqueness(first, second, third); int secondScore = checkUniqueness(second, first, third); int thirdScore = checkUniqueness(third, second, first); System.out.println(firstScore + " " + secondScore + " " + thirdScore); } private static int checkUniqueness(Set<String> toCount, Set<String> other, Set<String> another) { int score = 0; for (String s : toCount) { if (other.contains(s) && another.contains(s)) { // no points } else if (other.contains(s) || another.contains(s)) { // one point only score++; } else { // three score += 3; } } return score; } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
0549203c2569b92d75bca7febfb06f6f
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = Integer.valueOf(sc.nextLine()); for(int i = 0; i < t; i ++) { HashSet<String> s[] = new HashSet[3]; for(int j = 0; j < s.length; j ++) s[j] = new HashSet<>(); int count = Integer.valueOf(sc.nextLine()); for(int j = 0; j < 3; j ++) { for(int k = 0; k < count; k ++) { s[j].add(sc.next()); } } sc.nextLine(); int a = 0, b = 0, c = 0; for(String ss : s[0]) a += get(ss, s[1], s[2]); for(String ss : s[1]) b += get(ss, s[0], s[2]); for(String ss : s[2]) c += get(ss, s[0], s[1]); System.out.println(a + " " + b + " " + c); } } public static String cs = "Timur"; public static boolean ok (String s, String s2) { s = s.replace("G", "B"); s2 = s2.replace("G", "B"); return s.equals(s2); } public static int get(String s, HashSet<String> ss, HashSet<String> ss2) { if (ss.contains(s) && ss2.contains(s)){ return 0; } if (ss.contains(s) || ss2.contains(s)){ return 1; } return 3; } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
895b26849475f496b8636f5bef30ff8f
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class code3 { public static void main(String[] args) throws IOException { Kattio in = new Kattio(); int t=in.nextInt(); while(t-->0){ int n=in.nextInt(); int[] res=new int[3]; String[] s1=new String[n]; String[] s2=new String[n]; String[] s3=new String[n]; for(int i=0;i<n;i++) s1[i]=in.next(); for(int i=0;i<n;i++) s2[i]=in.next(); for(int i=0;i<n;i++) s3[i]=in.next(); Map<String,Integer> map=new HashMap<>(); for(int i=0;i<n;i++){ map.put(s1[i],map.getOrDefault(s1[i],0)+1); map.put(s2[i],map.getOrDefault(s2[i],0)+1); map.put(s3[i],map.getOrDefault(s3[i],0)+1); } for(int i=0;i<n;i++){ if(map.get(s1[i])==2){ res[0]+=1; }else if(map.get(s1[i])==1) { res[0]+=3; } if(map.get(s2[i])==2) res[1]+=1; else if(map.get(s2[i])==1) res[1]+=3; if(map.get(s3[i])==2) res[2]+=1; else if(map.get(s3[i])==1) res[2]+=3; } for(int i=0;i<2;i++){ System.out.print(res[i]); System.out.print(' '); } System.out.print(res[2]); System.out.println(" "); } } } class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // 标准 IO public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // 文件 IO public Kattio(String intput, String output) throws IOException { super(output); r = new BufferedReader(new FileReader(intput)); } // 在没有其他输入时返回 null public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) {} return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
debc4ef10d3b61293a421c87eb9f72b5
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; //min public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t= sc.nextInt(); while (t-->0) { int n=sc.nextInt(); HashMap<String,ArrayList<Integer>> map=new HashMap<>(); HashSet<String> set=new HashSet<>(); for(int i=0;i<3;i++) { for(int j=0;j<n;j++) { ArrayList<Integer> list=new ArrayList<>(); String str=sc.next(); if(!set.contains(str)) { list.add(i); set.add(str); map.put(str,list); } else { map.get(str).add(i); } } } int[] score =new int[3]; for(String s:set) { if(map.get(s).size()==1) { score[map.get(s).get(0)]+=3; } else if(map.get(s).size()==2) { score[map.get(s).get(0)]++; score[map.get(s).get(1)]++; } } System.out.println(score[0]+" "+score[1]+" "+score[2]); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
3bdecd4c1d1f9463615e9de55e0dd33e
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class c { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numC = sc.nextInt(); for (int c = 0; c < numC; c++) { int n = sc.nextInt(); HashMap<String, ArrayList<Integer>> hm = new HashMap<String, ArrayList<Integer>>(); HashSet<String> hs = new HashSet<String>(); for (int i = 0; i < 3; i++) { for (int j = 0; j < n; j++) { String str = sc.next(); ArrayList<Integer> list = new ArrayList<Integer>(); if (!hm.containsKey(str)) { list.add(i); hs.add(str); hm.put(str, list); } else { hm.get(str).add(i); } } } int[] scores = new int[3]; for (String s : hs) { if (hm.get(s).size() == 1) { scores[hm.get(s).get(0)] += 3; } if (hm.get(s).size() == 2) { scores[hm.get(s).get(0)] ++; scores[hm.get(s).get(1)] ++; } } System.out.println(scores[0] + " " + scores[1] + " " + scores[2]); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
8344f451e38b19b1b6ac3fdcd222e3d1
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.HashMap; import java.util.Scanner; /* The pain you feel today will be the strength you feel tomorrow */ import java.util.*; public class Bb { public static void main(String[] args) { Scanner S = new Scanner(System.in); int T = S.nextInt(); while (T-- > 0) { HashMap<String, Integer> Mp = new HashMap<String, Integer>(); ArrayList<ArrayList<String>> Arr = new ArrayList<ArrayList<String>>(); int N = S.nextInt(); for (int i = 0; i < 3; i++) { Arr.add(new ArrayList<String>()); for (int j = 1; j <= N; j++) { String Curr = S.next(); Arr.get(i).add(Curr); Mp.put(Curr, Mp.getOrDefault(Curr, 0) + 1); } } int Ans[] = new int[3]; for (int i = 0; i < 3; i++) { for (String X : Arr.get(i)) { if (Mp.get(X) == 1) { Ans[i] += 3; } else if (Mp.get(X) == 2) { Ans[i]++; } } } for (int X : Ans) { System.out.print(X + " "); } System.out.println(); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
5f333be8784a8dcb0fcbc78088ce3b3f
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Comparator; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main{ public static void main (String[] abc) throws IOException{ BufferedReader sc =new BufferedReader(new InputStreamReader(System.in)); int casos= Integer.parseInt(sc.readLine()); while(casos>0){ int nropa= Integer.parseInt(sc.readLine()); PriorityQueue<String> cp1=new PriorityQueue<>(nropa,new StringComparator()); PriorityQueue<String> cp2=new PriorityQueue<>(nropa,new StringComparator()); PriorityQueue<String> cp3=new PriorityQueue<>(nropa,new StringComparator()); PriorityQueue<String> cpt=new PriorityQueue<>(nropa*3,new StringComparator()); StringTokenizer st1=new StringTokenizer(sc.readLine()); StringTokenizer st2=new StringTokenizer(sc.readLine()); StringTokenizer st3=new StringTokenizer(sc.readLine()); for(int k=0;k<nropa;k++){ String r1=st1.nextToken(); String r2=st2.nextToken(); String r3=st3.nextToken(); cp1.add(r1); cp2.add(r2); cp3.add(r3); cpt.add(r1); cpt.add(r2); cpt.add(r3); } int a=0; int b=0; int c=0; String resp1=cpt.remove(); while(!cp1.isEmpty()||!cp3.isEmpty()||!cp2.isEmpty()){ int r=1; while(!cpt.isEmpty()&&resp1.equals(cpt.peek())){ resp1=cpt.remove(); r++; } if(!cp1.isEmpty()&&cp1.peek().equals(resp1)){ cp1.remove(); if(r==1){r=0;} a=a+(3-r); } if(!cp2.isEmpty()&&cp2.peek().equals(resp1)){ if(r==1){r=0;} b=b+(3-r); cp2.remove(); } if(!cp3.isEmpty()&&cp3.peek().equals(resp1)){ if(r==1){r=0;} cp3.remove(); c=c+(3-r); } if(!cpt.isEmpty()){resp1=cpt.remove();} } System.out.print(a+" "+b+" "+c); casos--; if(casos>0){ System.out.println(); } } } } class StringComparator implements Comparator<String>{ // Overriding compare()method of Comparator // for descending order of cgpa @Override public int compare(String s1, String s2) { if (s1.compareTo(s2)<0) return 1; else if (s1.compareTo(s2) > 0) return -1; return 0; } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
b9f2e8a2b971d52a2d7d8a66cc899705
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashMap; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Exception{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); HashMap<String, Integer> cont; String input; StringTokenizer st; String[][] players; int[] points; int cases = Integer.parseInt(reader.readLine()); for (int i = 0; i < cases; i++) { cont = new HashMap<>(); int words = Integer.parseInt(reader.readLine()); players = new String[3][words]; for (int j = 0; j < 3; j++) { input = reader.readLine(); players[j] = input.split(" "); for (int k = 0; k < words; k++) { if(!cont.containsKey(players[j][k])) cont.put(players[j][k], 1); else cont.put(players[j][k], cont.get(players[j][k]) + 1); } } points = new int[3]; for (int j = 0; j < 3; j++) { for (int k = 0; k < words; k++) { int p = cont.get(players[j][k]); if(p == 1) points[j] += 3; if(p == 2) points[j]++; } } for (int j = 0; j < 2; j++) System.out.print(points[j] + " "); System.out.println(points[2]); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
a4dea7a72708fa1e5da589b0c34bca69
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; public class Codeforces { public static void main(String[] args) { Scanner lta = new Scanner(System.in); int t = lta.nextInt(); while (t-- >0) { int n = lta.nextInt(); /* String[] s1 = new String[n]; String[] s2 = new String[n]; String[] s3 = new String[n]; for (int i = 0;i < n;i++){ s1[i] = lta.next(); } for (int i = 0;i < n;i++){ s2[i] = lta.next(); } for (int i = 0;i < n;i++){ s3[i] = lta.next(); } Arrays.sort(s1); Arrays.sort(s2); Arrays.sort(s3); for (int i = 0;i < n;i++) { ArrayList<String> alist = new ArrayList<>(); if (!alist.contains(s1[i])){ alist.add(s1[i]); } if (!alist.contains(s2[i])){ alist.add(s2[i]); } if (!alist.contains(s3[i])){ alist.add(s3[i]); } if (!s1[i].equals(s2[i]) && !s2[i].equals(s3[i]) && !s3[i].equals(s1[i])){ first = first + 3; second = second + 3; third = third + 3; } else if (alist.size() == 1){ alist.clear(); } else if (alist.size() == 2){ if ((alist.get(0) == s1[i] && alist.get(0) == s2[i]) || (alist.get(1) == s1[i] && alist.get(1) == s2[i])){ first = first + 1; second = second + 1; third = third + 3; } else if ((alist.get(0) == s2[i] && alist.get(0) == s3[i]) || (alist.get(1) == s2[i] && alist.get(1) == s3[i])){ second = second + 1; third = third + 1; first = first + 3; } else { first = first + 1; third = third + 1; second = second + 3; } } alist.clear(); } System.out.println(first + " " + second + " " + third);*/ String[] a = new String[n]; String[] b = new String[n]; String[] c = new String[n]; int first1 = 0; int second1 = 0; int third1 = 0; HashSet<String> hashSet1 = new HashSet<>(); HashSet<String> hashSet2 = new HashSet<>(); HashSet<String> hashSet3 = new HashSet<>(); for (int i = 0;i < n;i++){ a[i] = lta.next(); hashSet1.add(a[i]); } for (int i = 0;i < n;i++){ b[i] = lta.next(); hashSet2.add(b[i]); } for (int i = 0;i < n;i++){ c[i] = lta.next(); hashSet3.add(c[i]); } for (int i = 0;i < n;i++){ if (hashSet2.contains(a[i]) && hashSet3.contains(a[i])){ first1 = first1 + 0; } else if (hashSet2.contains(a[i]) || hashSet3.contains(a[i])){ first1 = first1 + 1; } else { first1 = first1 + 3; } } for (int i = 0;i < n;i++){ if (hashSet1.contains(b[i]) && hashSet3.contains(b[i])){ second1 = second1 + 0; } else if (hashSet1.contains(b[i]) || hashSet3.contains(b[i])){ second1 = second1 + 1; } else { second1 = second1 + 3; } } for (int i = 0;i < n;i++){ if (hashSet2.contains(c[i]) && hashSet1.contains(c[i])){ third1 = third1 + 0; } else if (hashSet2.contains(c[i]) || hashSet1.contains(c[i])){ third1 = third1 + 1; } else { third1 = third1 + 3; } } System.out.println(first1 + " " + second1 + " " + third1); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
f0c2423c647a5228e859159c8e6e1818
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; public class Codeforces { public static void main(String[] args) { Scanner lta = new Scanner(System.in); int t = lta.nextInt(); while (t-- >0) { int n = lta.nextInt(); String[] a = new String[n]; String[] b = new String[n]; String[] c = new String[n]; int first1 = 0; int second1 = 0; int third1 = 0; HashSet<String> hashSet1 = new HashSet<>(); HashSet<String> hashSet2 = new HashSet<>(); HashSet<String> hashSet3 = new HashSet<>(); for (int i = 0;i < n;i++){ a[i] = lta.next(); hashSet1.add(a[i]); } for (int i = 0;i < n;i++){ b[i] = lta.next(); hashSet2.add(b[i]); } for (int i = 0;i < n;i++){ c[i] = lta.next(); hashSet3.add(c[i]); } for (int i = 0;i < n;i++){ if (hashSet2.contains(a[i]) && hashSet3.contains(a[i])){ first1 = first1 + 0; } else if (hashSet2.contains(a[i]) || hashSet3.contains(a[i])){ first1 = first1 + 1; } else { first1 = first1 + 3; } } for (int i = 0;i < n;i++){ if (hashSet1.contains(b[i]) && hashSet3.contains(b[i])){ second1 = second1 + 0; } else if (hashSet1.contains(b[i]) || hashSet3.contains(b[i])){ second1 = second1 + 1; } else { second1 = second1 + 3; } } for (int i = 0;i < n;i++){ if (hashSet2.contains(c[i]) && hashSet1.contains(c[i])){ third1 = third1 + 0; } else if (hashSet2.contains(c[i]) || hashSet1.contains(c[i])){ third1 = third1 + 1; } else { third1 = third1 + 3; } } System.out.println(first1 + " " + second1 + " " + third1); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
cdd3a68ccea5cf922ab86b681b54dfba
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Vector; public class CF_1722C_WordGame { public static void scores(int n,String[][] str) { Map<String,Integer> map=new HashMap<>(); for(int i=0;i<3;i++) { for(int j=0;j<n;j++) { if(map.containsKey(str[i][j])) { map.put(str[i][j],map.get(str[i][j])+1); continue; } map.put(str[i][j], 1); } } for(int i=0;i<3;i++) { int cur=0; for(int j=0;j<n;j++) { if(map.get(str[i][j])==1) { cur+=3; } else if(map.get(str[i][j])==2) { cur++; } } System.out.print(cur+" "); } } public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0) { int n=sc.nextInt();//每人写几个单词 sc.nextLine(); String[][] str=new String[3][n]; for(int i=0;i<3;i++) { for(int j=0;j<n;j++) { str[i][j]=sc.next(); } } scores(n,str); System.out.println(""); t--; } sc.close(); } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
798be8cd1b562c43d11158731136e260
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class WordGame { public static void main(String[] args) throws IOException { //BufferedReader buf = new BufferedReader(new FileReader("input/WordGame/input.txt")); BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); int testCases = Integer.parseInt(buf.readLine()); for (int i = 0; i < testCases; i++) { int numberOfWords = Integer.parseInt(buf.readLine()); Set<String> wordsOfFirst = Arrays.stream(buf.readLine().split(" ")).collect(Collectors.toSet()); Set<String> wordsOfSecond = Arrays.stream(buf.readLine().split(" ")).collect(Collectors.toSet()); Set<String> wordsOfThird = Arrays.stream(buf.readLine().split(" ")).collect(Collectors.toSet()); // FIRST int totalPoints = 0; int tmpPoints; for (String searchWord : wordsOfFirst) { if (wordsOfSecond.contains(searchWord) && wordsOfThird.contains(searchWord)) { continue; } else if (!wordsOfSecond.contains(searchWord) && !wordsOfThird.contains(searchWord)) { tmpPoints = 3; } else { tmpPoints = 1; } totalPoints += tmpPoints; } System.out.print(totalPoints); // SECOND totalPoints = 0; for (String searchWord : wordsOfSecond) { if (wordsOfFirst.contains(searchWord) && wordsOfThird.contains(searchWord)) { continue; } else if (!wordsOfFirst.contains(searchWord) && !wordsOfThird.contains(searchWord)) { tmpPoints = 3; } else { tmpPoints = 1; } totalPoints += tmpPoints; } System.out.print(" " + totalPoints); // THIRD totalPoints = 0; for (String searchWord : wordsOfThird) { if (wordsOfSecond.contains(searchWord) && wordsOfFirst.contains(searchWord)) { continue; } else if (!wordsOfSecond.contains(searchWord) && !wordsOfFirst.contains(searchWord)) { tmpPoints = 3; } else { tmpPoints = 1; } totalPoints += tmpPoints; } System.out.print(" " + totalPoints); System.out.println(); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
4cfc5e43a205c8484c49b46267ad9ab6
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.StringTokenizer; public class WordGame { static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException{ int T = Integer.parseInt(in.readLine()); for (int i=0;i<T;i++){ solve(); } } static void solve() throws IOException{ int N = Integer.parseInt(in.readLine()); String[][] words = new String[3][N]; int[] points = new int[3]; HashMap<String,Integer> count = new HashMap<>(); for (int i=0;i<3;i++){ StringTokenizer tokenizer = new StringTokenizer(in.readLine()); for (int j=0;j<N;j++){ words[i][j] = tokenizer.nextToken(); count.put(words[i][j],count.getOrDefault(words[i][j],0)+1); } } for (int i=0;i<N;i++){ if (count.get(words[0][i])==1){ points[0]+=3; }else if (count.get(words[0][i])==2){ points[0]+=1; } } for (int i=0;i<N;i++){ if (count.get(words[1][i])==1){ points[1]+=3; }else if (count.get(words[1][i])==2){ points[1]+=1; } } for (int i=0;i<N;i++){ if (count.get(words[2][i])==1){ points[2]+=3; }else if (count.get(words[2][i])==2){ points[2]+=1; } } System.out.println(points[0]+" "+points[1]+" "+points[2]); } static boolean hasElement(String[] arr,String element){ for (int i=0;i<arr.length;i++){ if (arr[i].equals(element)){ return true; } } return false; } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
88847085f81b8c18f2150cabd787b73e
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc=new FastReader(); int tc=sc.nextInt(); while(tc-->0) { int n=sc.nextInt(); HashSet<String> s1=new HashSet<>(); HashSet<String> s2=new HashSet<>(); HashSet<String> s3=new HashSet<>(); for(int i=0;i<n;i++) { s1.add(sc.next()); } for(int i=0;i<n;i++) { s2.add(sc.next()); } for(int i=0;i<n;i++) { s3.add(sc.next()); } int p1=0,p2=0,p3=0; for(String s:s1) { if(s2.contains(s)&&s3.contains(s)) {} else if(!s2.contains(s)&& !s3.contains(s)) { p1+=3; } else { p1+=1; } } for(String s:s2) { if(s1.contains(s)&&s3.contains(s)) {} else if(!s1.contains(s)&& !s3.contains(s)) { p2+=3; } else { p2+=1; } } for(String s:s3) { if(s2.contains(s)&&s1.contains(s)) {} else if(!s2.contains(s)&& !s1.contains(s)) { p3+=3; } else { p3+=1; } } System.out.println(p1+" "+p2+" "+p3); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
9920f75a2d4610f2f2c7fe7323112abf
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; public class Codeforces { public static void main(String[] args) { Scanner lta = new Scanner(System.in); int t = lta.nextInt(); while (t-- >0) { int n = lta.nextInt(); String[] a = new String[n]; String[] b = new String[n]; String[] c = new String[n]; int first1 = 0; int second1 = 0; int third1 = 0; HashSet<String> hashSet1 = new HashSet<>(); HashSet<String> hashSet2 = new HashSet<>(); HashSet<String> hashSet3 = new HashSet<>(); for (int i = 0;i < n;i++){ a[i] = lta.next(); hashSet1.add(a[i]); } for (int i = 0;i < n;i++){ b[i] = lta.next(); hashSet2.add(b[i]); } for (int i = 0;i < n;i++){ c[i] = lta.next(); hashSet3.add(c[i]); } for (int i = 0;i < n;i++){ if (hashSet2.contains(a[i]) && hashSet3.contains(a[i])){ first1 = first1 + 0; } else if (hashSet2.contains(a[i]) || hashSet3.contains(a[i])){ first1 = first1 + 1; } else { first1 = first1 + 3; } } for (int i = 0;i < n;i++){ if (hashSet1.contains(b[i]) && hashSet3.contains(b[i])){ second1 = second1 + 0; } else if (hashSet1.contains(b[i]) || hashSet3.contains(b[i])){ second1 = second1 + 1; } else { second1 = second1 + 3; } } for (int i = 0;i < n;i++){ if (hashSet2.contains(c[i]) && hashSet1.contains(c[i])){ third1 = third1 + 0; } else if (hashSet2.contains(c[i]) || hashSet1.contains(c[i])){ third1 = third1 + 1; } else { third1 = third1 + 3; } } System.out.println(first1 + " " + second1 + " " + third1); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
7d2d73c19960b11abf9c5b68b0a10753
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; public class Codeforces { public static void main(String[] args) { Scanner lta = new Scanner(System.in); int t = lta.nextInt(); while (t-- >0) { int n = lta.nextInt(); /* String[] s1 = new String[n]; String[] s2 = new String[n]; String[] s3 = new String[n]; for (int i = 0;i < n;i++){ s1[i] = lta.next(); } for (int i = 0;i < n;i++){ s2[i] = lta.next(); } for (int i = 0;i < n;i++){ s3[i] = lta.next(); } Arrays.sort(s1); Arrays.sort(s2); Arrays.sort(s3); for (int i = 0;i < n;i++) { ArrayList<String> alist = new ArrayList<>(); if (!alist.contains(s1[i])){ alist.add(s1[i]); } if (!alist.contains(s2[i])){ alist.add(s2[i]); } if (!alist.contains(s3[i])){ alist.add(s3[i]); } if (!s1[i].equals(s2[i]) && !s2[i].equals(s3[i]) && !s3[i].equals(s1[i])){ first = first + 3; second = second + 3; third = third + 3; } else if (alist.size() == 1){ alist.clear(); } else if (alist.size() == 2){ if ((alist.get(0) == s1[i] && alist.get(0) == s2[i]) || (alist.get(1) == s1[i] && alist.get(1) == s2[i])){ first = first + 1; second = second + 1; third = third + 3; } else if ((alist.get(0) == s2[i] && alist.get(0) == s3[i]) || (alist.get(1) == s2[i] && alist.get(1) == s3[i])){ second = second + 1; third = third + 1; first = first + 3; } else { first = first + 1; third = third + 1; second = second + 3; } } alist.clear(); } System.out.println(first + " " + second + " " + third);*/ String[] a = new String[n]; String[] b = new String[n]; String[] c = new String[n]; int first1 = 0; int second1 = 0; int third1 = 0; HashSet<String> hashSet1 = new HashSet<>(); HashSet<String> hashSet2 = new HashSet<>(); HashSet<String> hashSet3 = new HashSet<>(); for (int i = 0;i < n;i++){ a[i] = lta.next(); hashSet1.add(a[i]); } for (int i = 0;i < n;i++){ b[i] = lta.next(); hashSet2.add(b[i]); } for (int i = 0;i < n;i++){ c[i] = lta.next(); hashSet3.add(c[i]); } for (int i = 0;i < n;i++){ if (hashSet2.contains(a[i]) && hashSet3.contains(a[i])){ first1 = first1 + 0; } else if (hashSet2.contains(a[i]) || hashSet3.contains(a[i])){ first1 = first1 + 1; } else { first1 = first1 + 3; } } for (int i = 0;i < n;i++){ if (hashSet1.contains(b[i]) && hashSet3.contains(b[i])){ second1 = second1 + 0; } else if (hashSet1.contains(b[i]) || hashSet3.contains(b[i])){ second1 = second1 + 1; } else { second1 = second1 + 3; } } for (int i = 0;i < n;i++){ if (hashSet2.contains(c[i]) && hashSet1.contains(c[i])){ third1 = third1 + 0; } else if (hashSet2.contains(c[i]) || hashSet1.contains(c[i])){ third1 = third1 + 1; } else { third1 = third1 + 3; } } System.out.println(first1 + " " + second1 + " " + third1); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
d2bef960714aa8803df843ed5b1cdaf2
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); long times = Long.parseLong(reader.readLine()); for (int i = 0; i < times; i++) { int size = Integer.parseInt(reader.readLine()); var input = new HashSet<String>(Arrays.asList(reader.readLine().split(" "))); var input2 = new HashSet<String>(Arrays.asList(reader.readLine().split(" "))); var input3 = new HashSet<String>(Arrays.asList(reader.readLine().split(" "))); //var input = Arrays.asList(reader.readLine().split(" ")).stream().map(item -> { // return Long.parseLong(item); //}).toArray(Long[]::new); var result = resolve(size, input, input2, input3); System.out.println(result); } } public static String resolve(int n, Set<String> input, Set<String> input2, Set<String> input3) { int p1 = 0; int p2 = 0; int p3 = 0; for (String string : input) { int p = 3; if (input2.remove(string)) { p = 2; } var b = input3.remove(string); if (b && p == 3) { p = 1; } else if (b && p == 2) { p = 0; } switch (p) { case 1: p1 ++; p3 ++; break; case 2: p1 ++; p2 ++; break; case 3: p1 += 3; break; default: break; } } for (String string : input2) { if (input3.remove(string)) { p2++; p3++; } else { p2 +=3; } } p3 += input3.size()*3; return p1 + " " + p2 + " " + p3; } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
efbc2723a0093c3ebcc86ce9b270ea87
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.HashMap; import java.util.Scanner; public class c1722c_2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while (t-->0) { int n=sc.nextInt(); String[] s1=new String[n]; String[] s2=new String[n]; String[] s3=new String[n]; for (int i = 0; i < n; i++) { s1[i]= sc.next(); } for (int i = 0; i < n; i++) { s2[i]=sc.next(); } for (int i = 0; i < n; i++) { s3[i]= sc.next(); } HashMap<String,Integer>hm=new HashMap<>(); for (int i = 0; i < n; i++) { if(hm.containsKey(s1[i])) { hm.replace(s1[i],hm.get(s1[i])+1 ); } else { hm.put(s1[i],1); } } for (int i = 0; i < n; i++) { if(hm.containsKey(s2[i])) { hm.replace(s2[i],hm.get(s2[i])+1 ); } else { hm.put(s2[i],1); } } for (int i = 0; i < n; i++) { if(hm.containsKey(s3[i])) { hm.replace(s3[i],hm.get(s3[i])+1 ); } else { hm.put(s3[i],1); } } int sc1=0; int sc2=0; int sc3=0; for (int i = 0; i < n; i++) { if(hm.get(s1[i])==1) sc1+=3; else if(hm.get(s1[i])==2) sc1+=1; } for (int i = 0; i < n; i++) { if(hm.get(s2[i])==1) sc2+=3; else if(hm.get(s2[i])==2) sc2+=1; } for (int i = 0; i < n; i++) { if(hm.get(s3[i])==1) sc3+=3; else if(hm.get(s3[i])==2) sc3+=1; } System.out.println(sc1+" "+sc2+" "+sc3); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
9bad272a88c7d15415d3667a94a53548
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import static java.lang.System.out; import static java.lang.Math.abs; import static java.lang.Math.min; import static java.lang.Math.max; import static java.lang.Math.log; import java.util.*; import java.lang.*; import java.io.*; public class a_Codeforces { public static void main(String[] args) throws java.lang.Exception { FastReader sc = new FastReader(); FastWriter out = new FastWriter(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int ans1 = 0, ans2 = 0, ans3 = 0; Map<String, Integer> map = new HashMap<String, Integer>(); String[] arr = new String[3 * n]; for (int i = 0; i < 3 * n; i++) { String s = sc.next(); arr[i] = s; if (map.containsKey(s)) { int key = map.get(s); map.remove(s); map.put(s, ++key); } else map.put(s, 1); } for (int i = 0; i < n; i++) { if (map.get(arr[i]) == 2) { ans1 += 1; } else if (map.get(arr[i]) == 1) { ans1 += 3; } } for (int i = n; i < 2 * n; i++) { if (map.get(arr[i]) == 2) { ans2 += 1; } else if (map.get(arr[i]) == 1) { ans2 += 3; } } for (int i = 2 * n; i < 3 * n; i++) { if (map.get(arr[i]) == 2) { ans3 += 1; } else if (map.get(arr[i]) == 1) { ans3 += 3; } } out.println(ans1 + " " + ans2 + " " + ans3); } out.close(); } /* * int[] arr = new int[n]; * for (int i=0; i<n; i++){ * arr[i] = sc.nextInt(); * } */ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
1fd5260158c19b283a0ab4dde0223271
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; public class Codechef { public static void main(String[]args){ Scanner sc=new Scanner(System.in); int test=sc.nextInt(); while(test-->0) { solve(sc); } sc.close(); } public static void solve(Scanner sc){ int n=sc.nextInt(); int res1=0,res2=0,res3=0; ArrayList<String>arr1=new ArrayList<>(); ArrayList<String>arr2=new ArrayList<>(); ArrayList<String>arr3=new ArrayList<>(); Map<String,Integer> hash=new HashMap<>(); for(int i=0;i<n;i++) { String str=sc.next(); arr1.add(str); hash.put(str,hash.getOrDefault(str,0)+1); } for(int i=0;i<n;i++) { String str=sc.next(); arr2.add(str); hash.put(str,hash.getOrDefault(str,0)+1); } for(int i=0;i<n;i++) { String str=sc.next(); arr3.add(str); hash.put(str,hash.getOrDefault(str,0)+1); } for(int i=0;i<n;i++) { int a=hash.get(arr1.get(i)); if(a==2)res1+=1; if(a==1)res1+=3; int b=hash.get(arr2.get(i)); if(b==2)res2+=1; if(b==1)res2+=3; int c=hash.get(arr3.get(i)); if(c==2)res3+=1; if(c==1)res3+=3; } System.out.println(res1+" "+res2+" "+res3); } } class define{ public static final long power( long a,long b) { long res = 1; while (b > 0) { if ((b & 1)==1) res = (res * a); a = a * a; b >>= 1; } return res; } public static final int power( int a,int b) { int res = 1; while (b > 0) { if ((b & 1)==1) res = (res * a); a = a * a; b >>= 1; } return res; } public static final void printarray( long a[]) { for(long i:a) System.out.print(i+" "); System.out.println(); } public static final void printarray( int a[]) { for(int i:a) System.out.print(i+" "); System.out.println(); } public static final void swap( long a,long b) { a=a^b; b=a^b; a=a^b; System.out.println(a+" "+b); } public static final void swap( int a,int b) { a=a^b; b=a^b; a=a^b; System.out.println(a+" "+b); } public static final int maxxorbetweentwonumber( int a,int b) { int store=a^b; int count=0; while(store>0) { count++; store=store>>1; } int res=1; int ans=0; while(count-->0) { ans+=res; res=res<<1; } return ans; } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
2a5550954aa437992a6c830a185b70e3
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); while(tc-- > 0){ int n = sc.nextInt(); boolean ans = true; HashMap<String, Integer> map1 = new HashMap<>(); HashMap<String, Integer> map2 = new HashMap<>(); HashMap<String, Integer> map3 = new HashMap<>(); for(int i = 0;i < n; i++){ String s = sc.next(); map1.put(s, 1); } for(int i = 0;i < n; i++){ String s = sc.next(); map2.put(s, 1); } for(int i = 0;i < n; i++){ String s = sc.next(); map3.put(s, 1); } int a = 0; int b = 0; int c = 0; for(String s: map1.keySet()){ if(map2.containsKey(s) && map3.containsKey(s)){ map2.remove(s); map3.remove(s); }else if(map2.containsKey(s)){ a += 1; b += 1; map2.remove(s); }else if(map3.containsKey(s)){ a += 1; c += 1; map3.remove(s); }else{ a += 3; } } for(String s: map2.keySet()){ if(map3.containsKey(s)){ b += 1; c += 1; map3.remove(s); }else{ b += 3; } } for(String s: map3.keySet()){ c += 3; } System.out.println(a + " " + b+ " " + c); } sc.close(); } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
302929398835a4682960a7f68e654092
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; public class Main { public static void main(String []args) { Scanner in = new Scanner(System.in); int t = 1; t = in.nextInt(); while (t-->0) { int n=in.nextInt(); HashMap<String,Integer> mp=new HashMap<String,Integer>(); String X[][]=new String[3][n]; for (int i=0; i<3; i++) { for (int j=0; j<n; j++) { X[i][j]=in.next(); mp.put(X[i][j],mp.containsKey(X[i][j])?mp.get(X[i][j])+1:1); } } for (int i=0; i<3; i++) { int c=0; for (int j=0; j<n; j++) { int xx= mp.get(X[i][j]); if(xx==1) c+=3; else if(xx==2) c++; } System.out.print(c+" "); } System.out.println();} } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
142fbc15f3737abe731511710223eb4a
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.StringTokenizer; public class C { private static Scan sc = new Scan(); public static void main(String[] args) { int n = sc.nextInt(); for (int i = 0; i < n; i++) { solve(); } } private static void solve() { int n = sc.nextInt(); HashSet<String> a = new HashSet<String>(); HashSet<String> b = new HashSet<String>(); HashSet<String> c = new HashSet<String>(); for (int i = 0; i < n; i++) { a.add(sc.next()); } for (int i = 0; i < n; i++) { b.add(sc.next()); } for (int i = 0; i < n; i++) { c.add(sc.next()); } HashSet<String> d = new HashSet<String>(a); d.retainAll(b); HashSet<String> e = new HashSet<String>(a); e.retainAll(c); HashSet<String> f = new HashSet<String>(b); f.retainAll(c); HashSet<String> g = new HashSet<String>(d); g.retainAll(e); g.retainAll(f); int x = 3 * n - (d.size() + e.size()) * 2 + g.size(); int y = 3 * n - (d.size() + f.size()) * 2 + g.size(); int z = 3 * n - (e.size() + f.size()) * 2 + g.size(); System.out.println(x + " " + y + " " + z); } private static class Scan { BufferedReader br; StringTokenizer st; public Scan() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } String nextLine() { String line = ""; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
e5c373cac4a136617512ce7a62333752
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; public class Codeforces { public static void main(String[] args) { Scanner S = new Scanner(System.in); int T = S.nextInt(); while(T-- > 0) { HashMap<String,Integer>Mp = new HashMap<String,Integer>(); ArrayList<ArrayList<String>>Arr = new ArrayList<ArrayList<String>>(); int N = S.nextInt(); for(int i=0;i<3;i++) { Arr.add(new ArrayList<String>()); for(int j=1;j<=N;j++) { String Curr = S.next(); Arr.get(i).add(Curr); Mp.put(Curr , Mp.getOrDefault(Curr , 0) + 1); } } int Ans[] = new int[3]; for(int i=0;i<3;i++) { for(String X : Arr.get(i)) { if(Mp.get(X) == 1)Ans[i]+=3; else if(Mp.get(X) == 2)Ans[i]++; } } for(int X : Ans)System.out.print(X + " "); System.out.println(); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
641d9a4f21c7b5cfdce1b8e795b8810b
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; public class Codeforces { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); sc.nextLine(); HashMap<String,Integer> temp = new HashMap<>(); String[][] arr = new String[3][n]; for (int i = 0; i < 3; i++) { for (int j = 0; j < n; j++) { arr[i][j] = sc.next(); if(temp.containsKey(arr[i][j])){ temp.put(arr[i][j],temp.get(arr[i][j])+1); }else { temp.put(arr[i][j],1); } } } for (int i = 0; i < 3; i++) { int sum = 0; for (int j = 0; j < n; j++) { if (temp.get(arr[i][j])==1){ sum += 3; }else if(temp.get(arr[i][j])==2){ sum += 1; } } System.out.println(sum+" "); } System.out.println(); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
89dfb0004ae0aa91158d93dbb2d0cb29
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; import java.io.*; public class WG { // For fast input output static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // end of fast i/o code public static void main(String[] args) { FastReader in = new FastReader(); long t = in.nextLong(); while (t > 0) { int n = in.nextInt(); HashSet<String> set1 = new HashSet<String>(); HashSet<String> set2 = new HashSet<String>(); HashSet<String> set3 = new HashSet<String>(); HashSet<String> set = new HashSet<String>(); for (int i = 1; i <= 3*n; i++) { String s = in.next(); if(i <= n) set1.add(s); else if(i <= 2*n) set2.add(s); else set3.add(s); set.add(s); } int point1 = 0, point2 = 0, point3 = 0; for(String s : set) { if(set1.contains(s) && set2.contains(s) && set3.contains(s)) { continue; } else if(set1.contains(s) && set2.contains(s)){ point1++; point2++; } else if(set1.contains(s) && set3.contains(s)){ point1++; point3++; } else if(set2.contains(s) && set3.contains(s)){ point2++; point3++; } else if(set1.contains(s)){ point1 += 3; } else if(set2.contains(s)){ point2 += 3; } else point3 += 3; } System.out.println(point1+" "+point2+" "+point3); t--; } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
7092d6a2a2560f0df3c9d9cf94d3fcca
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; import java.io.*; public class CWordGame { // For fast input output static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // end of fast i/o code public static void main(String[] args) { FastReader in = new FastReader(); long t = in.nextLong(); while (t > 0) { int n = in.nextInt(); HashSet<String> set1 = new HashSet<String>(); HashSet<String> set2 = new HashSet<String>(); HashSet<String> set3 = new HashSet<String>(); for (int i = 1; i <= n; i++) { String s = in.next(); // System.out.println("1 "+s); set1.add(s); } for (int i = 1; i <= n; i++) { String s = in.next(); // System.out.println("2 "+s); set2.add(s); } for (int i = 1; i <= n; i++) { String s = in.next(); // System.out.println("3 "+s); set3.add(s); } int point1 = 0, point2 = 0, point3 = 0; for(String s : set1) { if(set2.contains(s) && set3.contains(s)) continue; else if(set2.contains(s) || set3.contains(s)) point1 += 1; else point1 += 3; } for(String s : set2) { if(set1.contains(s) && set3.contains(s)) continue; else if(set1.contains(s) || set3.contains(s)) point2 += 1; else point2 += 3; } for(String s : set3) { if(set2.contains(s) && set1.contains(s)) continue; else if(set2.contains(s) || set1.contains(s)) point3 += 1; else point3 += 3; } System.out.println(point1+" "+point2+" "+point3); t--; } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
bda5cad5a383c0c742193681df781526
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.Scanner; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Set; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int nn; nn = scan.nextInt(); while(nn > 0) { int n = scan.nextInt(); scan.nextLine(); String[][] a = new String[3][n]; Map <String, Integer> tmp = new HashMap<String, Integer>(); for (int i = 0; i < 3; i++) { for (int j = 0; j < n; j++) { a[i][j] = scan.next(); if (tmp.containsKey(a[i][j])) { tmp.put(a[i][j], tmp.get(a[i][j]) + 1); } else { tmp.put(a[i][j], 1); } } } int[] ans = new int[3]; for (int i = 0; i < 3; i++) { ans[i] = 0; } for (int i = 0; i < 3; i++) { for (int j = 0; j < n; j++) { if (tmp.get(a[i][j]) == 1) { ans[i] += 3; } else if (tmp.get(a[i][j]) == 2) { ans[i] += 1; } } } for (int i = 0 ; i < 3; i++) { System.out.print(ans[i] + " "); } System.out.println(""); nn--; //damn gravy u so vicious } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
748e5526401c5ba9d751fc41f987c13a
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; public class Wordgame { public static void main(String args[]) { Scanner in = new Scanner(System.in); int t = in.nextInt() ; while( t-- > 0 ) { HashMap<String,Integer> m = new HashMap<String, Integer>() ; int n = in.nextInt() ; String s[][] = new String[3][n] ; for( int i = 0 ; i < 3 ; i = i + 1 ) { for( int j = 0 ; j < n ; j = j + 1 ) { String k = s[i][j] = in.next() ; m.put( k, m.getOrDefault(k,0) + 1 ) ; } } // System.out.println(m); for( int i = 0 ; i < 3 ; i = i + 1 ) { int p = 0 ; for( int j = 0 ; j < n ; j = j + 1 ) { if( m.get(s[i][j]) == 2 ) p = p + 1 ; else if( m.get( s[i][j] ) == 1 ) p = p + 3 ; else p = p + 0 ; } System.out.print(p+" "); } System.out.println() ; } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
2710b1af680375df766b0a03b0ba8b66
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Scanner; public class main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); int tc = Integer.parseInt(in.readLine()); StringBuilder str = new StringBuilder(); while (tc-- != 0) { int n = Integer.parseInt(in.readLine()); String[] one = in.readLine().split(" "); String[] two = in.readLine().split(" "); String[] three = in.readLine().split(" "); HashMap<String, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { map.put(one[i], map.getOrDefault(one[i], 0)+1); map.put(two[i], map.getOrDefault(two[i], 0)+1); map.put(three[i], map.getOrDefault(three[i], 0)+1); } int oneS = 0, twoS = 0, threeS = 0; for (int i = 0; i < n; i++) { oneS += (map.get(one[i]) == 1 ? 3 : (map.get(one[i]) == 2 ? 1 : 0)); twoS += (map.get(two[i]) == 1 ? 3 : (map.get(two[i]) == 2 ? 1 : 0)); threeS += (map.get(three[i]) == 1 ? 3 : (map.get(three[i]) == 2 ? 1 : 0)); } str.append(oneS + " " + twoS + " " + threeS); str.append("\n"); } System.out.println(str.toString()); } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
f3c19f461a9b5ca95646bff1ccc14989
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; /** * @Author Create by jiaxiaozheng * @Date 2022/9/1 */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { int m=scanner.nextInt(); Map<String,Integer> map=new HashMap<>(); List<List<String>> list=new ArrayList<>(); for (int j = 0; j < 3; j++) { list.add(new ArrayList<>()); for (int k = 0; k < m; k++) { String s=scanner.next(); list.get(j).add(s); map.put(s,map.getOrDefault(s,0)+1); } } int[] res=new int[3]; for (int j = 0; j < 3; j++) { for (int k = 0; k < m; k++) { int r=map.get(list.get(j).get(k)); if (r==1){ res[j]+=3; } else if (r==2) { res[j]+=1; } } } System.out.printf("%d %d %d\n",res[0],res[1],res[2]); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
de7c4ee69851a26a61ffb7aa20979016
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.*; import java.util.*; public class word { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; public static void main(String args[]) throws IOException{ int t = readInt(); for(int i=0; i<t; i++) { int n = readInt(); int s1 = 0; int s2 = 0; int s3 = 0; HashSet<String>word1 = new HashSet<String>(); HashSet<String>word2 = new HashSet<String>(); HashSet<String>word3 = new HashSet<String>(); for(int j=0; j<n; j++) { word1.add(next()); } for(int j=0; j<n; j++) { word2.add(next()); } for(int j=0; j<n; j++) { word3.add(next()); } for(String s: word1) { if(word2.contains(s)&&word3.contains(s)) { word2.remove(s); word3.remove(s); } else if(word2.contains(s)) { s1+=1; s2+=1; word2.remove(s); } else if(word3.contains(s)) { s1+=1; s3+=1; word3.remove(s); } else { s1+=3; } } for(String s:word2) { if(word3.contains(s)){ word3.remove(s); s2+=1; s3+=1; } else { s2+=3; } } s3+= word3.size()*3; System.out.println(s1+" "+s2+" "+s3); } } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static String readLine() throws IOException { return br.readLine().trim(); } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
bebece7ce5eba4774edbd3c42219f9f2
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; public class WordGame2 { public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); for (int i = 0; i < t; i++) { int n=s.nextInt(); String[] s1=new String[n]; String[] s2=new String[n]; String[] s3=new String[n]; HashMap<String,Integer> set=new HashMap<>(); for (int j = 0; j < n; j++) { s1[j]=s.next(); } for (int j = 0; j < n; j++) { s2[j]=s.next(); } for (int j = 0; j < n; j++) { s3[j]=s.next(); } for (int j = 0; j < n; j++) { if(set.containsKey(s1[j])){ set.replace(s1[j],set.get(s1[j])+1); } else{ set.put(s1[j],1); } } for (int j = 0; j < n; j++) { if(set.containsKey(s2[j])){ set.replace(s2[j],set.get(s2[j])+1); } else{ set.put(s2[j],1); } } for (int j = 0; j < n; j++) { if(set.containsKey(s3[j])){ set.replace(s3[j],set.get(s3[j])+1); } else{ set.put(s3[j],1); } } int p1=0,p2=0,p3=0; for (int j = 0; j < n; j++) { if(set.get(s1[j])==1){ p1+=3; } else if(set.get(s1[j])==2){ p1++; } } for (int j = 0; j < n; j++) { if(set.get(s2[j])==1){ p2+=3; } else if(set.get(s2[j])==2){ p2++; } } for (int j = 0; j < n; j++) { if(set.get(s3[j])==1){ p3+=3; } else if(set.get(s3[j])==2){ p3++; } } System.out.println(p1+" "+p2+" "+p3); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
44a7e69cd354ca8f16ac9d74903652fb
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
// package codeForces; import java.util.*; import java.lang.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { // ************************************************************************************************************// static Scanner sc = new Scanner(System.in); // ************************************************************************************************************// static ArrayList<Long> getArrayList(int N) { ArrayList<Long> arr = new ArrayList<>(); for (int i = 0; i < N; i++) { arr.add(sc.nextLong()); } return arr; } static <T> void printArrayList(ArrayList<T> arr) { for (T ele : arr) { System.out.print(ele + " "); } System.out.println(); } static <T> void printArray(T[] arr) { for (T ele : arr) { System.out.print(ele + " "); } System.out.println(); } static Long[] getLongArray(int N) { Long arr[] = new Long[N]; for (int i = 0; i < N; i++) { arr[i] = sc.nextLong(); } return arr; } static Long gcd(Long a, Long b) { Long res = Math.min(a, b); while (res > 0) { if (a % res == 0 && b % res == 0) { break; } res--; } return res; } static void swapCharacters(StringBuilder str, int a, int b) { char temp = str.charAt(a); str.setCharAt(a, str.charAt(b)); str.setCharAt(b, temp); } // ************************************************************************************************************// // write your code here static boolean isEven(int n) { if (n % 2 == 0) { return true; } else { return false; } } static boolean isPrime(int n) { if (n == 0 || n == 1) { return false; } for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } static void solve() { int n = sc.nextInt(); ArrayList<String> arr1 = new ArrayList<>(); ArrayList<String> arr2 = new ArrayList<>(); ArrayList<String> arr3 = new ArrayList<>(); HashMap<String , Integer> hm = new HashMap<>(); for(int i=0;i<n ;i++){ arr1.add(sc.next()); if(hm.containsKey(arr1.get(i))){ int oldValue = hm.get(arr1.get(i)); hm.put(arr1.get(i) ,oldValue+1); } else{ hm.put(arr1.get(i) ,1); } } for(int i=0;i<n ;i++){ arr2.add(sc.next()); if(hm.containsKey(arr2.get(i))){ int oldValue = hm.get(arr2.get(i)); hm.put(arr2.get(i) ,oldValue+1); } else{ hm.put(arr2.get(i) ,1); } } for(int i=0;i<n ;i++){ arr3.add(sc.next()); if(hm.containsKey(arr3.get(i))){ int oldValue = hm.get(arr3.get(i)); hm.put(arr3.get(i) ,oldValue+1); } else{ hm.put(arr3.get(i) ,1); } } int sum1 = 0; for(int i=0;i<arr1.size();i++){ int r = hm.get(arr1.get(i)); if(r ==1 ){ sum1 += 3; } else if( r == 2){ sum1 += 1; } else if(r == 3){ sum1 += 0; } } int sum2 = 0; for(int i=0;i<arr2.size();i++){ int r = hm.get(arr2.get(i)); if(r ==1 ){ sum2 += 3; } else if( r == 2){ sum2 += 1; } else if(r == 3){ sum2 += 0; } } int sum3 = 0; for(int i=0;i<arr3.size();i++){ int r = hm.get(arr3.get(i)); if(r ==1 ){ sum3 += 3; } else if( r == 2){ sum3 += 1; } else if(r == 3){ sum3 += 0; } } System.out.print(sum1 + " " + sum2+ " "+sum3); } // ************************************************************************************************************// public static void main(String[] args) throws java.lang.Exception { long testCase = sc.nextLong(); for (long i = 0; i < testCase; i++) { solve(); System.out.println(); } } } //
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
3ae3e87e227e487a1e265d4ffd1a99c4
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.io.*; import java.util.*; public class Main { static InputReader r; static int T; static StringBuilder answer = new StringBuilder(); private static void solution() throws IOException { int col = r.nextInt(); String[] r1 = r.nextLine().split(" "); String[] r2 = r.nextLine().split(" "); String[] r3 = r.nextLine().split(" "); Map<String, Integer> map = new HashMap<>(); int r1s = col * 3; int r2s = 0; int r3s = 0; for (String s : r1) { map.put(s, 1); } for (String s : r2) { if (map.containsKey(s)) { r1s -= 2; r2s += 1; map.put(s, 3); continue; } map.put(s, 2); r2s += 3; } for (String s : r3) { if (map.containsKey(s)) { Integer value = map.get(s); if (value == 1) { r1s -= 2; r3s += 1; } if (value == 2) { r2s -= 2; r3s += 1; } if (value == 3) { r1s -= 1; r2s -= 1; } continue; } r3s += 3; } answer.append(r1s).append(' ').append(r2s).append(' ').append(r3s).append('\n'); } private static void input() throws IOException { r = new InputReader(); T = r.nextInt(); } public static void main(String[] args) throws IOException { input(); for (int t = 0; t < T; t++) { solution(); } System.out.println(answer.toString()); } private static class InputReader { StringTokenizer st; BufferedReader r; public InputReader(String filePath) throws FileNotFoundException { this(new FileReader(filePath)); } public InputReader() { this(new InputStreamReader(System.in)); } private InputReader(InputStreamReader reader) { r = new BufferedReader(reader); st = new StringTokenizer(""); } public int nextInt() throws IOException { if (!st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return Integer.parseInt(st.nextToken()); } public char[] nextCharArr() throws IOException { return r.readLine().toCharArray(); } public String nextLine() throws IOException { return r.readLine(); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
9696103b5e294346fb2c6821a9860b1d
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class p2{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n =sc.nextInt(); int tot1=0,tot2=0,tot3=0; Map<String,Integer> m1 = new HashMap<>(); Map<String,Integer> m2 = new HashMap<>(); Map<String,Integer> m3 = new HashMap<>(); for(int i=0;i<n;i++) { String e =sc.next(); m1.put(e, 1); } for(int i=0;i<n;i++) { String e =sc.next(); m2.put(e, 1); } for(int i=0;i<n;i++) { String e =sc.next(); m3.put(e, 1); } for(String e:m1.keySet()){ int i2=0,i3=0; if(m2.containsKey(e)) { i2 = 1; m2.remove(e); } if(m3.containsKey(e)) { i3 = 1; m3.remove(e); } if(i2 ==1 && i3!=1) { tot1++; tot2++; } else if(i2!=1 && i3==1) { tot1++; tot3++; } else if(i2!=1 && i3!=1) { tot1 = tot1+3; } } for(String e:m2.keySet()) { int i3=0; if(m3.containsKey(e)) { i3 =1; m3.remove(e); } if(i3==1) { tot2++; tot3++; } else if(i3!=1) { tot2 = tot2+3; } } tot3 = tot3+3*m3.size(); System.out.println(tot1+" "+tot2+" "+tot3); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
8295842d85f8a1bd9db812035c3991c2
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scn=new Scanner(System.in); int t=scn.nextInt(); while(t-->0){ int n=scn.nextInt(); HashMap<String,Integer>map=new HashMap<>(); ArrayList<String> v=new ArrayList<>(); for(int i=0;i<3*n;i++){ String c=scn.next(); v.add(c); if (map.containsKey(c)) { map.put(c, map.get(c) + 1); } else { map.put(c, 1); } } int one=0,two=0,three=0; for(int i=0;i<n;i++){ if(map.get(v.get(i))==1)one+=3; else if(map.get(v.get(i))==2)one+=1; } for(int i=n;i<2*n;i++){ if(map.get(v.get(i))==1)two+=3; else if(map.get(v.get(i))==2)two+=1; } for(int i=2*n;i<3*n;i++){ if(map.get(v.get(i))==1)three+=3; else if(map.get(v.get(i))==2)three+=1; } System.out.println(one+" "+two+" "+three); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
9c0af2087c4756aae96ab316766d13f8
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
// package learn; import java.util.*; import java.lang.*; import java.math.*; public class Codechef{ static Scanner sc= new Scanner(System.in); public static void main(String[] args){ int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); HashSet<String> set1 = new HashSet<>(); for(int i=0;i<n;i++){ String s= sc.next(); set1.add(s); } HashSet<String> set2 = new HashSet<>(); for(int i=0;i<n;i++){ String s= sc.next(); set2.add(s); } HashSet<String> set3 = new HashSet<>(); for(int i=0;i<n;i++){ String s= sc.next(); set3.add(s); } int one=0,two=0,three=0; Iterator<String> i = set1.iterator(); while(i.hasNext()){ String p=i.next(); if(set2.contains(p)){ if(!set3.contains(p)){ one+=1; } } else if(set3.contains(p)){ if(!set2.contains(p)){ one+=1; } } else if(!set2.contains(p) && !set3.contains(p)){ one+=3; } } Iterator<String> j = set2.iterator(); while(j.hasNext()){ String p=j.next(); if(set1.contains(p)){ if(!set3.contains(p)){ two+=1; } } else if(set3.contains(p)){ if(!set1.contains(p)){ two+=1; } } else if(!set1.contains(p) && !set3.contains(p)){ two+=3; } } Iterator<String> k = set3.iterator(); while(k.hasNext()){ String p= k.next(); if(set2.contains(p)){ if(!set1.contains(p)){ three+=1; } } else if(set1.contains(p)){ if(!set2.contains(p)){ three+=1; } } else if(!set2.contains(p) && !set1.contains(p)){ three+=3; } } System.out.println(one+" "+two+" "+three); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
e6a927032ff22eadd9bce2dc305610d2
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.ArrayList; import java.util.HashSet; import java.util.Scanner; public class Vasya { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t > 0) { t--; int n = sc.nextInt(); ArrayList<String> p1 = new ArrayList<>(); ArrayList<String> p2 = new ArrayList<>(); ArrayList<String> p3 = new ArrayList<>(); HashSet<String> p1h = new HashSet<>(); HashSet<String> p2h = new HashSet<>(); HashSet<String> p3h = new HashSet<>(); for(int i = 0 ; i < n ; i++){ p1.add(sc.next()); p1h.add(p1.get(i)); } for(int i = 0 ; i < n ; i++){ p2.add(sc.next()); p2h.add(p2.get(i)); } for(int i = 0 ; i < n ; i++){ p3.add(sc.next()); p3h.add(p3.get(i)); } int p1s = 0, p2s = 0, p3s = 0; for(int i = 0 ; i < n ; i++){ if(p2h.contains(p1.get(i)) && !p3h.contains(p1.get(i))){ p1s++; } if(!p2h.contains(p1.get(i)) && p3h.contains(p1.get(i))){ p1s++; } if(!p2h.contains(p1.get(i)) && !p3h.contains(p1.get(i))){ p1s+=3; } } for(int i = 0 ; i < n ; i++){ if(p1h.contains(p2.get(i)) && !p3h.contains(p2.get(i))){ p2s++; } if(!p1h.contains(p2.get(i)) && p3h.contains(p2.get(i))){ p2s++; } if(!p1h.contains(p2.get(i)) && !p3h.contains(p2.get(i))){ p2s+=3; } } for(int i = 0 ; i < n ; i++){ if(p1h.contains(p3.get(i)) && !p2h.contains(p3.get(i))){ p3s++; } if(!p1h.contains(p3.get(i)) && p2h.contains(p3.get(i))){ p3s++; } if(!p1h.contains(p3.get(i)) && !p2h.contains(p3.get(i))){ p3s+=3; } } System.out.println(p1s+" "+p2s+" "+p3s); } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
91f23b2d31aeaece3339617a8c46118c
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.text.*; public class Main{ public static void main(String args[]) throws IOException{ Read sc=new Read(); int n=sc.nextInt(); for(int i=0;i<n;i++){ Map<String,Integer> map=new HashMap<>(); int a=sc.nextInt(); String ss[][]=new String[3][a]; for(int j=0;j<3;j++){ for(int k=0;k<a;k++){ String s=sc.next(); map.put(s,map.getOrDefault(s,0)+1); ss[j][k]=s; } } for(int j=0;j<3;j++){ int count=0; for(String s:ss[j]){ int b=map.get(s); if(b==1){ count+=3; } else if(b==2){ count+=1; } } sc.print(count+" "); } sc.println(""); } //sc.print(0); sc.bw.flush(); sc.bw.close(); } } //记住看数字范围,需要开long吗,需要用BigInteger吗,需要手动处理字符串吗,复杂度数量级控制在1e7或者以下了吗 //开数组的数据范围最高不能超过1e7,数据范围再大就要用哈希表离散化了 //基本数据类型不能自定义sort排序,二维数组就可以了 class Read{ BufferedReader bf; StringTokenizer st; BufferedWriter bw; public Read(){ bf=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); bw=new BufferedWriter(new OutputStreamWriter(System.out)); } public String nextLine() throws IOException{ return bf.readLine(); } public String next() throws IOException{ while(!st.hasMoreTokens()){ st=new StringTokenizer(bf.readLine()); } return st.nextToken(); } public char nextChar() throws IOException{ //确定下一个token只有一个字符的时候再用 return next().charAt(0); } public int nextInt() throws IOException{ return Integer.parseInt(next()); } public long nextLong() throws IOException{ return Long.parseLong(next()); } public double nextDouble() throws IOException{ return Double.parseDouble(next()); } public float nextFloat() throws IOException{ return Float.parseFloat(next()); } public byte nextByte() throws IOException{ return Byte.parseByte(next()); } public short nextShort() throws IOException{ return Short.parseShort(next()); } public BigInteger nextBigInteger() throws IOException{ return new BigInteger(next()); } public void println(int a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(int a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(String a) throws IOException{ bw.write(a); bw.newLine(); return; } public void print(String a) throws IOException{ bw.write(a); return; } public void println(long a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(long a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(double a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(double a) throws IOException{ bw.write(String.valueOf(a)); return; } public void print(BigInteger a) throws IOException{ bw.write(a.toString()); return; } public void print(char a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(char a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output
PASSED
2f44e73679aa7bc4473f114a83f96b60
train_109.jsonl
1661871000
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?
256 megabytes
import java.util.*; import java.io.*; public class Main { static FastReader in=new FastReader(); static final Random random=new Random(); static long mod=1000000007L; public static void main(String args[]) throws IOException { int t=in.nextInt(); while(t-->0){ HashMap<String,Integer>m=new HashMap<>(); int n=in.nextInt(); String str1[]=new String[n]; String str2[]=new String[n]; String str3[]=new String[n]; for(int i=0;i<n;i++){ str1[i]=in.next(); m.put(str1[i],m.getOrDefault(str1[i],0)+1); } for(int i=0;i<n;i++){ str2[i]=in.next(); m.put(str2[i],m.getOrDefault(str2[i],0)+1); } for(int i=0;i<n;i++){ str3[i]=in.next(); m.put(str3[i],m.getOrDefault(str3[i],0)+1); } int f=0,s=0,th=0; for(int i=0;i<n;i++){ int t1=m.get(str1[i]); if(t1==1)f+=3; else if(t1==2)f+=1; } for(int i=0;i<n;i++){ int t1=m.get(str2[i]); if(t1==1)s+=3; else if(t1==2)s+=1; } for(int i=0;i<n;i++){ int t1=m.get(str3[i]); if(t1==1)th+=3; else if(t1==2)th+=1; } print(f+" "+s+" "+th); } } static int max(int a, int b) { if(a<b) return b; return a; } static void ruffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static < E > void print(E res) { System.out.println(res); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if(a<0) return -1*a; return a; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int [] readintarray(int n) { int res [] = new int [n]; for(int i = 0; i<n; i++)res[i] = nextInt(); return res; } long [] readlongarray(int n) { long res [] = new long [n]; for(int i = 0; i<n; i++)res[i] = nextLong(); return res; } } }
Java
["3\n\n1\n\nabc\n\ndef\n\nabc\n\n3\n\norz for qaq\n\nqaq orz for\n\ncod for ces\n\n5\n\niat roc hem ica lly\n\nbac ter iol ogi sts\n\nbac roc lly iol iat"]
1 second
["1 3 1 \n2 2 6 \n9 11 5"]
NoteIn the first test case: The word $$$\texttt{abc}$$$ was written by the first and third guys — they each get $$$1$$$ point. The word $$$\texttt{def}$$$ was written by the second guy only — he gets $$$3$$$ points.
Java 17
standard input
[ "data structures", "implementation" ]
f8a89510fefbbc8e5698efe8a0c30927
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of words written by each person. The following three lines each contain $$$n$$$ distinct strings — the words written by each person. Each string consists of $$$3$$$ lowercase English characters.
800
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy.
standard output