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
|
58b6224d2585e6d6e708331e29df52a4
|
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.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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 17
|
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 < h_b,\ w_s < 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
|
2ef61c121eb5765581c2bd3a73dad4f2
|
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.*;
import java.util.*;
import java.awt.*;
public class Main {
public static void main(String[] args) throws IOException {
var in=new FastInput();
StringBuffer res=new StringBuffer();
var t=in.getIntArray(1)[0];
for (int i = 0; i < t; i++) {
var nq=in.getAutoIntArray();
var data=new int[nq[0]][2];
for (int j = 0; j < data.length; j++) {
data[j]=in.getAutoIntArray();
}
long[][] pre=new long[1001][1001];
long[][] fix=new long[1001][1001];
for(var item:data){
pre[item[0]][item[1]]+=item[0]*item[1];
}
for (int j = 1; j < fix.length; j++) {
for (int j2 = 1; j2 < fix.length; j2++) {
fix[j][j2]=-fix[j-1][j2-1]+fix[j-1][j2]+fix[j][j2-1]+pre[j][j2];
}
}
for (int j = 0; j < nq[1]; j++) {
var mid=in.getAutoIntArray();
long a=fix[mid[2]-1][mid[1]]+fix[mid[0]][mid[3]-1]-fix[mid[0]][mid[1]];
long b=fix[mid[2]-1][mid[3]-1];
res.append(b-a);
res.append("\n");
}
}
System.out.println(res.toString().substring(0,res.length()-1));
}
}
class FastInput {
BufferedReader in = null;
public FastInput() {
in = new BufferedReader(new InputStreamReader(System.in));
}
public int[] getAutoIntArray() throws IOException {
String[] data=in.readLine().split(" ");
int[] res=new int[data.length];
for (int i = 0; i < res.length; i++) {
res[i]=Integer.valueOf(data[i]);
}
return res;
}
public int[] getIntArray(int len) throws IOException {
int[] res = new int[len];
String[] data = in.readLine().split(" ");
for (int i = 0; i < res.length; i++) {
res[i] = Integer.valueOf(data[i]);
}
return res;
}
public ArrayList<Integer> getIntArrayList(int len) throws IOException {
ArrayList<Integer> res = new ArrayList<>();
String[] data = in.readLine().split(" ");
for (int i = 0; i < len; i++) {
res.add(Integer.valueOf(data[i]));
}
return res;
}
public Integer[] getIntegerArray(int len) throws IOException {
Integer[] res = new Integer[len];
String[] data = in.readLine().split(" ");
for (int i = 0; i < res.length; i++) {
res[i] = Integer.valueOf(data[i]);
}
return res;
}
public long[] getLongArray(int len) throws IOException {
long[] res = new long[len];
String[] data = in.readLine().split(" ");
for (int i = 0; i < res.length; i++) {
res[i] = Long.valueOf(data[i]);
}
return res;
}
public ArrayList<Long> getLongArrayList(int len) throws IOException {
ArrayList<Long> res = new ArrayList<>();
String[] data = in.readLine().split(" ");
for (int i = 0; i < len; i++) {
res.add(Long.valueOf(data[i]));
}
return res;
}
public double[] getDoubleArray(int len) throws IOException {
double[] res = new double[len];
String[] data = in.readLine().split(" ");
for (int i = 0; i < res.length; i++) {
res[i] = Double.valueOf(data[i]);
}
return res;
}
public int[] getStringToIntArray(int len) throws IOException {
int[] res=new int[len];
String s=in.readLine();
for (int i = 0; i < res.length; i++) {
if(s.charAt(i)=='0'){
res[i]=0;
}else{
res[i]=1;
}
}
return res;
}
public static void printArray(int[] x){
StringBuffer res=new StringBuffer();
for (int i = 0; i < x.length; i++) {
res.append(x[i]);
if(i+1!=x.length)
res.append(" ");
}
System.out.println(res);
}
public static void printArray(long[] x){
if(x==null) {
System.out.println("null");
return ;
}
StringBuffer res=new StringBuffer();
for (int i = 0; i < x.length; i++) {
res.append(x[i]);
if(i+1!=x.length)
res.append(" ");
}
System.out.println(res);
}
public static String getArrayString(long[] x){
if(x==null) {
return "null";
}
StringBuffer res=new StringBuffer();
for (int i = 0; i < x.length; i++) {
res.append(x[i]);
if(i+1!=x.length)
res.append(" ");
}
return res.toString();
}
public static String getArrayString(int[] x){
if(x==null) {
return "null";
}
StringBuffer res=new StringBuffer();
for (int i = 0; i < x.length; i++) {
res.append(x[i]);
if(i+1!=x.length)
res.append(" ");
}
return res.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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 17
|
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 < h_b,\ w_s < 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
|
b06faecc8ce57392b4adad929c4da195
|
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.util.*;
import java.io.*;
public class Template {
FastScanner in;
PrintWriter out;
Random rnd = new Random();
final int MAX_VAL = (1<<31) - 1;
int rndInt() {
return rnd.nextInt(MAX_VAL);
}
public void solve() throws IOException {
int t = in.nextInt();
for (int cs = 0; cs < t; cs++) {
int n = in.nextInt();
int q = in.nextInt();
long[][] a = new long[1001][1001];
for (int i = 0; i < n; ++i) {
int x = in.nextInt();
int y = in.nextInt();
a[x][y]++;
}
for (int x = 1; x <= 1000; x++)
for (int y = 1; y <= 1000; y++)
a[x][y] = a[x][y] * x * y + a[x - 1][y] + a[x][y - 1] - a[x - 1][y - 1];
for (int i = 0; i < q; i++){
int x1 = in.nextInt() + 1;
int y1 = in.nextInt() + 1;
int x2 = in.nextInt() - 1;
int y2 = in.nextInt() - 1;
out.println(a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1]);
}
}
}
public void run() {
try {
in = new FastScanner(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(Reader f) {
br = new BufferedReader(f);
}
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());
}
}
public static void main(String[] arg) {
new Template().run();
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 17
|
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 < h_b,\ w_s < 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
|
79c8a64e8d5bec676f7da0e89636cf29
|
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.*;
import java.util.*;
public class CF1722E extends PrintWriter {
CF1722E() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1722E o = new CF1722E(); o.main(); o.flush();
}
static final int A = 1000;
void main() {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int q = sc.nextInt();
long[][] aa = new long[A + 1][A + 1];
while (n-- > 0) {
int i = sc.nextInt();
int j = sc.nextInt();
aa[i][j] += i * j;
}
for (int i = 1; i < A; i++)
for (int j = 1; j < A; j++)
aa[i][j] += aa[i][j - 1];
for (int j = 1; j < A; j++)
for (int i = 1; i < A; i++)
aa[i][j] += aa[i - 1][j];
while (q-- > 0) {
int i0 = sc.nextInt();
int j0 = sc.nextInt();
int i1 = sc.nextInt() - 1;
int j1 = sc.nextInt() - 1;
println(i0 < i1 && j0 < j1 ? aa[i1][j1] - aa[i0][j1] - aa[i1][j0] + aa[i0][j0] : 0);
}
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 17
|
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 < h_b,\ w_s < 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
|
4052dc7e091214f5ccd913c3ebd6289f
|
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.util.*;
import java.lang.*;
import java.io.*;
public final 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 {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// static void print(ArrayList<Long> arr){
// BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
// for(int i=0;i<arr.size()-1;i++) {
// out.write(arr.get(i)+" ");
// }
// out.write(arr.get(arr.size()-1)+"\n");
// out.flush();
// }
public static void main(String[] args) throws java.lang.Exception{
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0) {
int n =sc.nextInt(), q=sc.nextInt();
long[][] pre = new long[1001][1001];
for(int i=0;i<n;i++) {
int h=sc.nextInt(),w=sc.nextInt();
pre[h][w]+=(long)h*w;
}
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];
}
}
for(int i=0;i<q;i++) {
int hs=sc.nextInt(),ws=sc.nextInt(),hb=sc.nextInt(),wb=sc.nextInt();
System.out.println(
pre[hb-1][wb-1]-pre[hb-1][ws]-pre[hs][wb-1]+pre[hs][ws]);
}
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
15b94450a3f407ddfb0f5498661403dd
|
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.*;
import java.util.*;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
public class Solution{
public 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;
}
}
public static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static class Pair{
int s;
int end;
public Pair(int a, int e) {
end = e;
s = a;
}
}
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) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
public static void main(String[] args) throws Exception
{
FastReader scn = new FastReader();
OutputStream outputStream = System.out;
OutputWriter output = new OutputWriter(outputStream);
int t = scn.nextInt();
long mod = 1000000007L;
// String[] pal = {"0000", "0110","0220","0330","0440","0550","1001","1111","1221","1331","1441","1551","2002","2112","2222","2332"};
while(t>0) {
// int n = scn.nextInt();
// long max =0, min= Integer.MAX_VALUE;
// long[] a = new long[n];
// for(int i=0;i<n;i++) {
// a[i]= scn.nextLong();
// max = Math.max(max,a[i]);
// min = Math.min(min, a[i]);
// }
int n = scn.nextInt();
int q = scn.nextInt();
long[][] a = new long[n][2];
long[][] m1 = new long[1001][1001];
for(int i=0;i<n;i++) {
a[i][0] = scn.nextLong();
a[i][1] = scn.nextLong();
m1[(int) a[i][0]][(int)(a[i][1])]++;
}
for(long key = 1;key<=1000;key++) {
for(int i=1;i<=1000;i++) {
m1[(int) key][i] = (long)(m1[(int) key][i-1])+(long)(i)*m1[(int) key][i];
}
}
while(q>0) {
long hs = scn.nextLong();
long ws = scn.nextLong();
long hb = scn.nextLong();
long wb = scn.nextLong();
long ans = 0;
for(long key=1;key<=1000;key++) {
if(key>hs && key<hb) {
ans=ans+key*(long)(m1[(int) key][(int)(wb)-1]-m1[(int) key][(int)(ws)]);
}
}
output.println(ans);
q--;
}
t--;
}
output.close();
}
public static int checkL(char[][] a, int r, int c, boolean[][] mark) {
int cnt=0;
for(int i=Math.max(r-1,0);i<=Math.min(r+2,a.length-1);i++) {
for(int j= Math.max(c-1,0);j<=Math.min(c+2,a[0].length-1);j++) {
if(a[i][j]=='*') {
cnt++;
}
}
}
char ch11 = a[r][c];
char ch12 = a[r][c+1];
char ch21 = a[r+1][c];
char ch22 = a[r+1][c+1];
int n = a.length;
int m = a[0].length;
if(cnt==4) {
if(ch22!='*' && r+2<n && c+2<m && a[r+2][c+2]=='*') {
cnt--;
}else if(ch21!='*' && r+2<n && c-1>=0 && a[r+2][c-1]=='*') {
cnt--;
}else if(ch11!='*' && r-1>=0 && c-1>=0 && a[r-1][c-1]=='*') {
cnt--;
}else if(ch12!='*' && r-1>=0 && c+2<m && a[r-1][c+2]=='*') {
cnt--;
}
}
if(cnt==0) {
return 1;
}
if(cnt>3) {
return 4;
}
if(cnt<3) {
return 2;
}
if((ch11=='*' && ch12=='*' && ch22=='*' && ch21!='*')|| (ch11=='*' && ch21=='*' && ch22=='*' && ch12!='*') || (ch12=='*'&&ch22=='*'&&ch21=='*'&&ch11!='*')||(ch11=='*'&&ch12=='*'&&ch21=='*'&&ch22!='*')) {
for(int i=r;i<r+2;i++) {
for(int j=c;j<c+2;j++) {
if(a[i][j]=='*') {
mark[i][j] = true;
}
}
}
return 1;
}
return 1;
}
public static void addTime(int h, int m, int ah, int am) {
int rm = m+am;
int rh = (h+ah+(rm/60))%24;
rm = rm%60;
}
public static long power(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
2e1bc21de6881596992fcf127f6100aa
|
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.*;
import java.util.Arrays;
import java.util.*;
import java.util.StringTokenizer;
public class copy {
static int log=30;
static int[][] ancestor;
static int[] depth;
static void sieveOfEratosthenes(int n, ArrayList<Integer> arr) {
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] is not changed, then it is a
// prime
if (prime[p]) {
// Update all multiples of 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]) {
arr.add(i);
}
}
}
public static long fac(long N, long mod) {
if (N == 0)
return 1;
if(N==1)
return 1;
return ((N % mod) * (fac(N - 1, mod) % mod)) % mod;
}
static long power(long x, long y, long p) {
// Initialize result
long res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static long nCrModPFermat(int n, int r,
long p,long[] fac) {
if (n < r)
return 0;
// Base case
if (r == 0)
return 1;
return ((fac[n] % p * (modInverse(fac[r], p)
% p)) % p * (modInverse(fac[n-r], p)
% p)) % p;
}
public static int find(int[] parent, int x) {
if (parent[x] == x)
return x;
return find(parent, parent[x]);
}
public static void merge(int[] parent, int[] rank, int x, int y,int[] child) {
int x1 = find(parent, x);
int y1 = find(parent, y);
if (rank[x1] > rank[y1]) {
parent[y1] = x1;
child[x1]+=child[y1];
} else if (rank[y1] > rank[x1]) {
parent[x1] = y1;
child[y1]+=child[x1];
} else {
parent[y1] = x1;
child[x1]+=child[y1];
rank[x1]++;
}
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static int power(int x, int y, int p)
{
int 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;
}
// Returns n^(-1) mod p
static int modInverse(int n, int p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static int nCrModPFermat(int n, int r,
int p,int[] fac)
{
if (n<r)
return 0;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
// int[] fac = new int[n + 1];
// fac[0] = 1;
//
// for (int i = 1; i <= n; i++)
// fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}
public static long[][] ncr(int n,int r)
{
long[][] dp=new long[n+1][r+1];
for(int i=0;i<=n;i++)
dp[i][0]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=r;j++)
{
if(j>i)
continue;
dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
}
}
return dp;
}
public static boolean prime(long N)
{
int c=0;
for(int i=2;i*i<=N;i++)
{
if(N%i==0)
++c;
}
return c==0;
}
public static int sparse_ancestor_table(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] child)
{
int c=0;
for(int i:arr.get(x))
{
if(i!=parent)
{
// System.out.println(i+" hello "+x);
depth[i]=depth[x]+1;
ancestor[i][0]=x;
// if(i==2)
// System.out.println(parent+" hello");
for(int j=1;j<log;j++)
ancestor[i][j]=ancestor[ancestor[i][j-1]][j-1];
c+=sparse_ancestor_table(arr,i,x,child);
}
}
child[x]=1+c;
return child[x];
}
public static int lca(int x,int y)
{
if(depth[x]<depth[y])
{
int temp=x;
x=y;
y=temp;
}
x=get_kth_ancestor(depth[x]-depth[y],x);
if(x==y)
return x;
// System.out.println(x+" "+y);
for(int i=log-1;i>=0;i--)
{
if(ancestor[x][i]!=ancestor[y][i])
{
x=ancestor[x][i];
y=ancestor[y][i];
}
}
return ancestor[x][0];
}
public static int get_kth_ancestor(int K,int x)
{
if(K==0)
return x;
int node=x;
for(int i=0;i<log;i++)
{
if(K%2!=0)
{
node=ancestor[node][i];
}
K/=2;
}
return node;
}
public static ArrayList<Integer> primeFactors(int n)
{
// Print the number of 2s that divide n
ArrayList<Integer> factors=new ArrayList<>();
if(n%2==0)
factors.add(2);
while (n%2==0)
{
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
if(n%i==0)
factors.add(i);
while (n%i == 0)
{
n /= i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
{
factors.add(n);
}
return factors;
}
static long ans=1,mod=1000000007;
public static void recur(long X,long N,int index,ArrayList<Integer> temp)
{
// System.out.println(X);
if(index==temp.size())
{
System.out.println(X);
ans=((ans%mod)*(X%mod))%mod;
return;
}
for(int i=0;i<=60;i++)
{
if(X*Math.pow(temp.get(index),i)<=N)
recur(X*(long)Math.pow(temp.get(index),i),N,index+1,temp);
else
break;
}
}
public static int upper(ArrayList<Integer> temp,int X)
{
int l=0,r=temp.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(temp.get(mid)==X)
return mid;
// System.out.println(mid+" "+temp.get(mid));
if(temp.get(mid)<X)
l=mid+1;
else
{
if(mid-1>=0 && temp.get(mid-1)>=X)
r=mid-1;
else
return mid;
}
}
return -1;
}
public static int lower(ArrayList<Integer> temp,int X)
{
int l=0,r=temp.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(temp.get(mid)==X)
return mid;
// System.out.println(mid+" "+temp.get(mid));
if(temp.get(mid)>X)
r=mid-1;
else
{
if(mid+1<temp.size() && temp.get(mid+1)<=X)
l=mid+1;
else
return mid;
}
}
return -1;
}
public static int[] check(String shelf,int[][] queries)
{
int[] arr=new int[queries.length];
ArrayList<Integer> indices=new ArrayList<>();
for(int i=0;i<shelf.length();i++)
{
char ch=shelf.charAt(i);
if(ch=='|')
indices.add(i);
}
for(int i=0;i<queries.length;i++)
{
int x=queries[i][0]-1;
int y=queries[i][1]-1;
int left=upper(indices,x);
int right=lower(indices,y);
if(left<=right && left!=-1 && right!=-1)
{
arr[i]=indices.get(right)-indices.get(left)+1-(right-left+1);
}
else
arr[i]=0;
}
return arr;
}
static boolean check;
public static void check(ArrayList<ArrayList<Integer>> arr,int x,int[] color,boolean[] visited)
{
visited[x]=true;
PriorityQueue<Integer> pq=new PriorityQueue<>();
for(int i:arr.get(x))
{
if(color[i]<color[x])
pq.add(color[i]);
if(color[i]==color[x])
check=false;
if(!visited[i])
check(arr,i,color,visited);
}
int start=1;
while(pq.size()>0)
{
int temp=pq.poll();
if(temp==start)
++start;
else
break;
}
if(start!=color[x])
check=false;
}
static boolean cycle;
public static void cycle(boolean[] stack,boolean[] visited,int x,ArrayList<ArrayList<Integer>> arr)
{
if(stack[x])
{
cycle=true;
return;
}
visited[x]=true;
for(int i:arr.get(x))
{
if(!visited[x])
{
cycle(stack,visited,i,arr);
}
}
stack[x]=false;
}
public static int check(char[][] ch,int x,int y)
{
int cnt=0;
int c=0;
int N=ch.length;
int x1=x,y1=y;
while(c<ch.length)
{
if(ch[x][y]=='1')
++cnt;
// if(x1==0 && y1==3)
// System.out.println(x+" "+y+" "+cnt);
x=(x+1)%N;
y=(y+1)%N;
++c;
}
return cnt;
}
public static void s(char[][] arr,int x)
{
char start=arr[arr.length-1][x];
for(int i=arr.length-1;i>0;i--)
{
arr[i][x]=arr[i-1][x];
}
arr[0][x]=start;
}
public static void shuffle(char[][] arr,int x,int down)
{
int N= arr.length;
down%=N;
char[] store=new char[N-down];
for(int i=0;i<N-down;i++)
store[i]=arr[i][x];
for(int i=0;i<arr.length;i++)
{
if(i<down)
{
// Printing rightmost
// kth elements
arr[i][x]=arr[N + i - down][x];
}
else
{
// Prints array after
// 'k' elements
arr[i][x]=store[i-down];
}
}
}
public static String form(int C1,char ch1,char ch2)
{
char ch=ch1;
String s="";
for(int i=1;i<=C1;i++)
{
s+=ch;
if(ch==ch1)
ch=ch2;
else
ch=ch1;
}
return s;
}
public static void printArray(long[] arr)
{
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
public static boolean check(long mid,long[] arr,long K)
{
long[] arr1=Arrays.copyOfRange(arr,0,arr.length);
long ans=0;
for(int i=0;i<arr1.length-1;i++)
{
if(arr1[i]+arr1[i+1]>=mid)
{
long check=(arr1[i]+arr1[i+1])/mid;
// if(mid==5)
// System.out.println(check);
long left=check*mid;
left-=arr1[i];
if(left>=0)
arr1[i+1]-=left;
ans+=check;
}
// if(mid==5)
// printArray(arr1);
}
// if(mid==5)
// System.out.println(ans);
ans+=arr1[arr1.length-1]/mid;
return ans>=K;
}
public static long search(long sum,long[] arr,long K)
{
long l=1,r=sum/K;
while(l<=r)
{
long mid=(l+r)/2;
if(check(mid,arr,K))
{
if(mid+1<=sum/K && check(mid+1,arr,K))
l=mid+1;
else
return mid;
}
else
r=mid-1;
}
return -1;
}
public static void primeFactors(int n,HashSet<Integer> hp)
{
// Print the number of 2s that divide n
ArrayList<Integer> factors=new ArrayList<>();
if(n%2==0)
hp.add(2);
while (n%2==0)
{
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
if(n%i==0)
hp.add(i);
while (n%i == 0)
{
n /= i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
{
hp.add(n);
}
}
public static boolean check(String s)
{
HashSet<Character> hp=new HashSet<>();
char ch=s.charAt(0);
for(int i=1;i<s.length();i++)
{
// System.out.println(hp+" "+s.charAt(i));
if(hp.contains(s.charAt(i)))
{
// System.out.println(i);
// System.out.println(hp);
// System.out.println(s.charAt(i));
return false;
}
if(s.charAt(i)!=ch)
{
hp.add(ch);
ch=s.charAt(i);
}
}
return true;
}
public static int check_end(String[] arr,boolean[] st,char ch)
{
for(int i=0;i<arr.length;i++)
{
if(ch==arr[i].charAt(0) && !st[i] && ch==arr[i].charAt(arr[i].length()-1))
return i;
}
for(int i=0;i<arr.length;i++)
{
if(ch==arr[i].charAt(0) && !st[i])
return i;
}
return -1;
}
public static int check_start(String[] arr,boolean[] st,char ch)
{
for(int i=0;i<arr.length;i++)
{
// if(ch=='B')
// {
// if(!st[i])
// System.out.println(arr[i]+" hello");
// }
if(ch==arr[i].charAt(arr[i].length()-1) && !st[i] && ch==arr[i].charAt(0))
return i;
}
for(int i=0;i<arr.length;i++)
{
// if(ch=='B')
// {
// if(!st[i])
// System.out.println(arr[i]+" hello");
// }
if(ch==arr[i].charAt(arr[i].length()-1) && !st[i])
return i;
}
return -1;
}
public static boolean palin(int N)
{
String s="";
while(N>0)
{
s+=N%10;
N/=10;
}
int l=0,r=s.length()-1;
while(l<=r)
{
if(s.charAt(l)!=s.charAt(r))
return false;
++l;
--r;
}
return true;
}
public static boolean check(long org_s,long org_d,long org_n,long check_ele)
{
if(check_ele<org_s)
return false;
if((check_ele-org_s)%org_d!=0)
return false;
long num=(check_ele-org_s)/org_d;
// if(check_ele==5)
// System.out.println(num+" "+org_n);
return num+1<=org_n;
}
public static long check(long c,long c_diff,long mod,long b_start,long c_start, long c_end,long b_end,long b_diff)
{
// System.out.println(c);
long max=Math.max(c,b_diff);
long min=Math.min(c,b_diff);
long lcm=(max/gcd(max,min))*min;
// System.out.println(lcm);
// System.out.println(c);
// System.out.println(c_diff);
// if(b_diff>c)
// {
long start_point=c_diff/c-c_diff/lcm;
// System.out.println(start_point);
// }
// else
// {
// start_point=c_diff/b_diff-c_diff/c;
// }
// System.out.println(c+" "+start_point);
return (start_point%mod*start_point%mod)%mod;
}
public static boolean check_bounds(int x, int y, int N, int M)
{
return x>=0 && x<N && y>=0 && y<M;
}
static boolean found=false;
public static void check(int x,int y,int[][] arr,boolean status[][])
{
if(arr[x][y]==9)
{
found=true;
return;
}
status[x][y]=true;
if(check_bounds(x-1,y, arr.length,arr[0].length)&& !status[x-1][y])
check(x-1,y,arr,status);
if(check_bounds(x+1,y, arr.length,arr[0].length)&& !status[x+1][y])
check(x+1,y,arr,status);
if(check_bounds(x,y-1, arr.length,arr[0].length)&& !status[x][y-1])
check(x,y-1,arr,status);
if(check_bounds(x,y+1, arr.length,arr[0].length)&& !status[x][y+1])
check(x,y+1,arr,status);
}
public static int check(String s1,String s2,int M)
{
int ans=0;
for(int i=0;i<M;i++)
{
ans+=Math.abs(s1.charAt(i)-s2.charAt(i));
}
return ans;
}
public static int check(int[][] arr,int dir1,int dir2,int x1,int y1)
{
int sum=0,N=arr.length,M=arr[0].length;
int x=x1+dir1,y=y1+dir2;
while(x<N && x>=0 && y<M && y>=0)
{
sum+=arr[x][y];
x=x+dir1;
y+=dir2;
}
return sum;
}
public static int check(long[] pref,long X,int N)
{
if(X>pref[N-1])
return -1;
// System.out.println(pref[0]);
if(X<=pref[0])
return 1;
int l=0,r=N-1;
while(l<=r)
{
int mid=(l+r)/2;
if(pref[mid]>=X)
{
if(mid-1>=0 && pref[mid-1]<X)
return mid+1;
else
r=mid-1;
}
else
l=mid+1;
}
return -1;
}
private static long mergeAndCount(long[] arr, int l,
int m, int r)
{
// Left subarray
long[] left = Arrays.copyOfRange(arr, l, m + 1);
// Right subarray
long[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l;long swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
// Merge sort function
private static long mergeSortAndCount(long[] arr, int l,
int r)
{
// Keeps track of the inversion count at a
// particular node of the recursion tree
long count = 0;
if (l < r) {
int m = (l + r) / 2;
// Total inversion count = left subarray count
// + right subarray count + merge count
// Left subarray count
count += mergeSortAndCount(arr, l, m);
// Right subarray count
count += mergeSortAndCount(arr, m + 1, r);
// Merge count
count += mergeAndCount(arr, l, m, r);
}
return count;
}
public static long check(long L,long R)
{
long ans=0;
for(int i=1;i<=Math.pow(10,8);i++)
{
long A=i*(long)i;
if(A<L)
continue;
long upper=(long)Math.floor(Math.sqrt(A-L));
long lower=(long)Math.ceil(Math.sqrt(Math.max(A-R,0)));
if(upper>=lower)
ans+=upper-lower+1;
}
return ans;
}
public static int check(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[]store)
{
int index=0;
ArrayList<Integer> temp=arr.get(x);
for(int i:temp)
{
if(i!=parent)
{
index+=check(arr,i,x,store);
}
}
store[x]=index;
return index+1;
}
public static void finans(int[][] store,ArrayList<ArrayList<Integer>> arr,int x,int parent)
{
// ++delete;
// System.out.println(x);
if(store[x][0]==0 && store[x][1]==0)
return;
if(store[x][0]!=0 && store[x][1]==0)
{
++delete;
ans+=store[x][0];
return;
}
if(store[x][0]==0 && store[x][1]!=0)
{
++delete;
ans+=store[x][1];
return;
}
ArrayList<Integer> temp=arr.get(x);
if(store[x][0]!=0 && store[x][1]!=0)
{
++delete;
if(store[x][0]>store[x][1])
{
ans+=store[x][0];
for(int i=temp.size()-1;i>=0;i--)
{
if(temp.get(i)!=parent)
{
finans(store,arr,temp.get(i),x);
break;
}
}
}
else
{
ans+=store[x][1];
for(int i=0;i<temp.size();i++)
{
if(temp.get(i)!=parent)
{
finans(store,arr,temp.get(i),x);
break;
}
}
}
}
}
public static int dfs(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] store)
{
int index1=-1,index2=-1;
for(int i=0;i<arr.get(x).size();i++)
{
if(arr.get(x).get(i)!=parent)
{
if(index1==-1)
{
index1=i;
}
else
index2=i;
}
}
if(index1==-1)
{
return 0;
}
if(index2==-1)
{
return store[arr.get(x).get(index1)];
}
// System.out.println(x);
// System.out.println();;
return Math.max(store[arr.get(x).get(index1)]+dfs(arr,arr.get(x).get(index2),x,store),store[arr.get(x).get(index2)]+dfs(arr,arr.get(x).get(index1),x,store));
}
static int delete=0;
public static boolean bounds(int x,int y,int N,int M)
{
return x>=0 && x<N && y>=0 && y<M;
}
public static int gcd_check(ArrayList<Integer> temp,char[] ch, int[] arr)
{
ArrayList<Integer> ini=new ArrayList<>(temp);
for(int i=0;i<temp.size();i++)
{
for(int j=0;j<temp.size();j++)
{
int req=temp.get(j);
temp.set(j,arr[req-1]);
}
boolean status=true;
for(int j=0;j<temp.size();j++)
{
if(ch[ini.get(j)-1]!=ch[temp.get(j)-1])
status=false;
}
if(status)
return i+1;
}
return temp.size();
}
static long LcmOfArray(int[] arr, int idx)
{
// lcm(a,b) = (a*b/gcd(a,b))
if (idx == arr.length - 1){
return arr[idx];
}
int a = arr[idx];
long b = LcmOfArray(arr, idx+1);
return (a*b/gcd(a,b)); //
}
public static boolean check(ArrayList<Integer> arr,int sum)
{
for(int i=0;i<arr.size();i++)
{
for(int j=i+1;j<arr.size();j++)
{
for(int k=j+1;k<arr.size();k++)
{
if(arr.get(i)+arr.get(j)+arr.get(k)==sum)
return true;
}
}
}
return false;
}
// Returns true if str1 is smaller than str2.
static boolean isSmaller(String str1, String str2)
{
// Calculate lengths of both string
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2)
return true;
if (n2 < n1)
return false;
for (int i = 0; i < n1; i++)
if (str1.charAt(i) < str2.charAt(i))
return true;
else if (str1.charAt(i) > str2.charAt(i))
return false;
return false;
}
public static int check(List<String> history)
{
int[][] arr=new int[history.size()][history.get(0).length()];
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[0].length;j++)
{
arr[i][j]=history.get(i).charAt(j)-48;
}
}
for(int i=0;i<arr.length;i++)
Arrays.sort(arr[i]);
int sum=0;
for(int i=0;i<arr[0].length;i++)
{
int max=0;
for(int j=0;j<arr.length;j++)
max=Math.max(max,arr[j][i]);
sum+=max;
}
return sum;
}
// Function for find difference of larger numbers
static String findDiff(String str1, String str2)
{
// Before proceeding further, make sure str1
// is not smaller
if (isSmaller(str1, str2)) {
String t = str1;
str1 = str2;
str2 = t;
}
// Take an empty string for storing result
String str = "";
// Calculate length of both string
int n1 = str1.length(), n2 = str2.length();
// Reverse both of strings
str1 = new StringBuilder(str1).reverse().toString();
str2 = new StringBuilder(str2).reverse().toString();
int carry = 0;
// Run loop till small string length
// and subtract digit of str1 to str2
for (int i = 0; i < n2; i++) {
// Do school mathematics, compute difference of
// current digits
int sub
= ((int)(str1.charAt(i) - '0')
- (int)(str2.charAt(i) - '0') - carry);
// If subtraction is less then zero
// we add then we add 10 into sub and
// take carry as 1 for calculating next step
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str += (char)(sub + '0');
}
// subtract remaining digits of larger number
for (int i = n2; i < n1; i++) {
int sub = ((int)(str1.charAt(i) - '0') - carry);
// if the sub value is -ve, then make it
// positive
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str += (char)(sub + '0');
}
// reverse resultant string
return new StringBuilder(str).reverse().toString();
}
static int nCr(int n, int r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
// Returns factorial of n
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
public static void fill(int[][] dp,int[] arr,int N)
{
for(int i=1;i<=N;i++)
dp[i][0]=arr[i];
for (int j = 1; j <= dp[0].length; j++)
for (int i = 1; i + (1 << j) <= N; i++)
dp[i][j] = Math.max(dp[i][j-1], dp[i + (1 << (j - 1))][j - 1]);
}
static int start=0;
// static boolean status;
public static void dfs(ArrayList<ArrayList<Integer>> arr,int[] A,int[] B,int x,int time,int parent,int K,int coins)
{
if(time>K)
return;
ans=Math.max(ans,coins);
for(int i:arr.get(x))
{
if(i!=parent)
{
dfs(arr,A,B,i,time+1,x,K,coins);
dfs(arr,A,B,i,time+1+B[i],x,K,coins+A[i]);
}
}
}
public static boolean dfs_diameter(ArrayList<ArrayList<Integer>> arr,int x,int parent,int node1,int node2,int[] child)
{
boolean status=false;
for(int i:arr.get(x))
{
if(i!=parent)
{
boolean ch=dfs_diameter(arr,i,x,node1,node2,child);
status= status || ch;
if(!ch && x==node1)
ans+=child[i];
child[x]+=child[i];
}
}
child[x]+=1;
return status || (x==node2);
}
public static boolean check_avai(int x,int y,int sx,int sy,int di)
{
// if(x==4 && y==4)
// System.out.println((Math.abs(x-sx)+Math.abs(y-sy))<=di);
return (Math.abs(x-sx)+Math.abs(y-sy))<=di;
}
public static int lower(ArrayList<div> arr, long x,long y)
{
int l=0,r=arr.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(arr.get(mid).x<=x || arr.get(mid).y<=y)
l=mid+1;
else
{
if(mid-1>=0 && arr.get(mid-1).x> x && arr.get(mid-1).y> y)
r=mid-1;
else
return mid;
}
}
return -1;
}
public static int higher(ArrayList<div> arr, long x,long y)
{
int l=0,r=arr.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(arr.get(mid).x>=x || arr.get(mid).y>=y)
r=mid-1;
else
{
if(mid+1<arr.size() && arr.get(mid+1).x<x && arr.get(mid+1).y<y )
l=mid+1;
else
return mid;
}
}
return -1;
}
public static void recur(HashSet<Integer> a,HashSet<Integer> b,long[] aa,long []ab,int bit,boolean[] bits)
{
// System.out.println("hello");
// System.out.println(bit);
if(a.size()==0)
return;
if(bit<0)
return;
// if(bit<=4)
// {
// System.out.println(bit+" hello");
// System.out.println(a);
// System.out.println(b);
// }
HashSet<Integer> b0=new HashSet<>();
HashSet<Integer> b1=new HashSet<>();
HashSet<Integer> a0=new HashSet<>();
HashSet<Integer> a1=new HashSet<>();
for(int i:a)
{
if((aa[i]>>bit)%2==0)
a0.add(i);
else
a1.add(i);
}
for(int i:b)
{
if((ab[i]>>bit)%2==0)
b0.add(i);
else
b1.add(i);
}
// if(bit==0 && a.size()==2)
// {
// System.out.println(a0);
// System.out.println(a1);
// System.out.println(b0);
// System.out.println(b1);
// }
if(a0.size()!=b1.size()) {
bits[bit] = false;
// System.out.println(bit+" MCBC");
}
else if(bits[bit]){
recur(a0, b1, aa, ab, bit - 1, bits);
recur(a1, b0, aa, ab, bit - 1, bits);
}
recur(a, b, aa, ab, bit - 1, bits);
}
public static void main(String[] args) throws IOException{
Reader.init(System.in);
// BufferedWriter output = new BufferedWriter(new FileWriter("C:/Users/asus/Downloads/test2.txt"));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int T=Reader.nextInt();
long[][] dp=new long[1001][1001];
long[][] check=new long[1001][1001];
for(int m=1;m<=T;m++)
{
int N=Reader.nextInt();
int q=Reader.nextInt();
ArrayList<div> arr=new ArrayList<>();
for(int i=0;i<=1000;i++)
{
Arrays.fill(dp[i],0);
Arrays.fill(check[i],0);
}
for(int i=0;i<N;i++)
{
int x=Reader.nextInt();
int y=Reader.nextInt();
dp[x][y]+=(long)(x)*y;
check[x][y]+=(long)(x)*y;
}
for(int i=1;i<=1000;i++)
{
for(int j=1;j<=1000;j++)
dp[i][j]+=dp[i][j-1];
}
for(int i=1;i<=1000;i++)
{
for(int j=1;j<=1000;j++)
dp[j][i]+=dp[j-1][i];
}
for(int i=0;i<q;i++)
{
int x1=Reader.nextInt();
int y1=Reader.nextInt();
int x2=Reader.nextInt();
int y2=Reader.nextInt();
// int index1=lower(arr,x1*y1);
// int index2=higher(arr,x2*y2);
if(x2>x1+1 && y2>y1+1)
{
output.write(dp[x2-1][y2-1]-dp[x1][y2-1]-dp[x2-1][y1]+dp[x1][y1]+"\n");
}
else
output.write(0+"\n");
}
}
// output.close();
output.flush();
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
class TreeNode
{
int data;
TreeNode left;
TreeNode right;
TreeNode(int data)
{
left=null;
right=null;
this.data=data;
}
}
class div {
long x;
long y;
long ar;
div(long x,long y,long ar) {
this.x=x;
this.y=y;
this.ar=ar;
// this.coins=coins;
}
}
class kar implements Comparator<div>
{
public int compare(div o1,div o2) {
if (o1.x < o2.x)
return -1;
else if (o1.x > o2.x)
return 1;
else {
if (o1.y < o2.y)
return -1;
else if (o1.y > o2.y)
return 1;
else
return 0;
}
}
}
class trie_node
{
trie_node[] arr;
trie_node()
{
arr=new trie_node[26];
}
public static void insert(trie_node root,String s)
{
trie_node tmp=root;
for(int i=0;i<s.length();i++)
{
if(tmp.arr[s.charAt(i)-97]!=null)
{
tmp=tmp.arr[s.charAt(i)-97];
}
else
{
tmp.arr[s.charAt(i)-97]=new trie_node();
tmp=tmp.arr[s.charAt(i)-97];
}
}
}
public static boolean search(trie_node root,String s)
{
trie_node tmp=root;
for(int i=0;i<s.length();i++)
{
if(tmp.arr[s.charAt(i)-97]!=null)
{
tmp=tmp.arr[s.charAt(i)-97];
}
else
{
return false;
}
}
return true;
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
bfdbf5da74438f296da861669b0fa320
|
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.*;
import java.util.*;
import static java.util.Collections.swap;
public class Solver {
public static void main(String[] args) throws IOException {
int t = nextInt();
long[][] mass;
long[][] mass1;
for (int i = 0; i < t; i++) {
mass = new long[1005][1005];
mass1 = new long[1005][1005];
int n = nextInt();
int q = nextInt();
for (int j = 0; j < n; j++) {
int a = nextInt();
int b = nextInt();
mass[a][b] += ((long)a)*b;
}
for (int j = 1; j < 1005; j++) {
for (int k = 1; k < 1005; k++) {
mass1[k][j] = mass[k][j] + mass1[k-1][j] + mass1[k][j-1] - mass1[k-1][j-1];
}
}
for (int j = 0; j < q; j++) {
int a = nextInt();
int b = nextInt();
int x = nextInt();
int y = nextInt();
out.println(mass1[x-1][y-1] - mass1[x-1][b] - mass1[a][y-1] + mass1[a][b]);
}
}
out.close();
}
static class Pair {
int x, type;
public Pair(int x, int type) {
this.x = x;
this.type = type;
}
}
static class PairComparator implements Comparator<Pair> {
@Override
public int compare(Pair o1, Pair o2) {
if (o1.x != o2.x) return Integer.compare(o1.x, o2.x);
return Integer.compare(o1.type, o2.type);
}
}
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
static StringTokenizer in = new StringTokenizer("");
public static boolean hasNext() throws IOException {
if (in.hasMoreTokens()) return true;
String s;
while ((s = br.readLine()) != null) {
in = new StringTokenizer(s);
if (in.hasMoreTokens()) return true;
}
return false;
}
public static String nextToken() throws IOException {
while (!in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
bd651014d662e88ae0abbad90bb8b698
|
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.util.*;
import java.io.*;
import java.math.*;
public class Main {
static void read(int arr[], int start, int end, FastReader in) {
for (int i = start; i < end; i++) {
arr[i] = in.nextInt();
}
}
static int sumArr(int arr[]) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static int min(int arr[]) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
min = Math.min(min, arr[i]);
}
return min;
}
public static void sort(int[] arr) {
ArrayList<Integer> ls = new ArrayList<>();
for (int x : arr) {
ls.add(x);
}
Collections.sort(ls);
for (int i = 0; i < arr.length; i++) {
arr[i] = ls.get(i);
}
}
public static void sortrev(int[] arr) {
ArrayList<Integer> ls = new ArrayList<>();
for (int x : arr) {
ls.add(x);
}
Collections.sort(ls,Collections.reverseOrder());
for (int i = 0; i < arr.length; i++) {
arr[i] = ls.get(i);
}
}
public static void sort(long[] arr) {
ArrayList<Long> ls = new ArrayList<>();
for (long x : arr) {
ls.add(x);
}
Collections.sort(ls);
for (int i = 0; i < arr.length; i++) {
arr[i] = ls.get(i);
}
}
static int max(int arr[]) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
max = Math.max(max, arr[i]);
}
return max;
}
static long max(long arr[]) {
long max = Long.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
max = Math.max(max, arr[i]);
}
return max;
}
static int max(int a, int b) {
return Math.max(a, b);
}
static long max(long a, long b){
return (a>b)?a:b;
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long min(long a, long b){
return Math.min(a, b);
}
public static boolean isPrime(long n) {
if (n < 2) {
return false;
}
if (n == 2 || n == 3) {
return true;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
long sqrtN = (long) Math.sqrt(n) + 1;
for (long i = 6L; i <= sqrtN; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0) {
return false;
}
}
return true;
}
static class Pair {
int first;
int second;
Pair(int f, int s) {
first = f;
second = s;
}
@Override
public String toString() {
return "first: " + first + " second: " + second;
}
}
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;
}
void read(int arr[]) {
for (int i = 0; i < arr.length; i++) {
arr[i] = nextInt();
}
}
void read(long arr[]) {
for (int i = 0; i < arr.length; i++) {
arr[i] = nextLong();
}
}
void read(char arr[][]){
for(int i=0;i<arr.length;i++){
String s=next();
for(int j=0;j<s.length();j++)
arr[i][j]=s.charAt(j);
}
}
void read(Pair arr[]) {
for (int i = 0; i < arr.length; i++) {
int x = nextInt();
int y = nextInt();
arr[i] = new Pair(x, y);
}
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void println() throws IOException{
println("");
}
public void printarr(int arr[]) throws IOException {
println(Arrays.toString(arr));
}
public void printarr(long arr[]) throws IOException {
println(Arrays.toString(arr));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void close() throws IOException {
bw.close();
}
}
public static int count(String s, char c) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c) {
count++;
}
}
return count;
}
public static long countSetBits(long n) {
long count = 0;
while (n != 0) {
count += (n & 1); // check last bit
n >>= 1;
}
return count;
}
static long powerLL(long x, long n, long MOD) {
long result = 1;
while (n > 0) {
if (n % 2 == 1) {
result = result * x % MOD;
}
n = n / 2;
x = x * x % MOD;
}
return result;
}
static long countBits(long number) {
return (long) (Math.log(number)
/ Math.log(2) + 1);
}
static boolean isPowerOfTwo(long n) {
if (n == 0) {
return false;
}
while (n != 1) {
if (n % 2 != 0) {
return false;
}
n = n / 2;
}
return true;
}
public static int LCS(String s1, String s2){
int l1=s1.length();
int l2=s2.length();
int dp[][]=new int[l1+1][l2+1];
for(int i=0;i<=l1;i++){
for(int j=0;j<=l2;j++){
if(i==0 || j==0)
dp[i][j]=0;
else{
if(s1.charAt(i-1)==s2.charAt(j-1))
dp[i][j]=1+dp[i-1][j-1];
else
dp[i][j]=Math.max(dp[i-1][j], dp[i][j-1]);
}
}
}
return dp[l1][l2];
}
public static String LCSstring(String s1, String s2){
int l1=s1.length();
int l2=s2.length();
int dp[][]=new int[l1+1][l2+1];
for(int i=0;i<=l1;i++){
for(int j=0;j<=l2;j++){
if(i==0 || j==0)
dp[i][j]=0;
else{
if(s1.charAt(i-1)==s2.charAt(j-1))
dp[i][j]=1+dp[i-1][j-1];
else
dp[i][j]=Math.max(dp[i-1][j], dp[i][j-1]);
}
}
}
String ans="";
int i=l1,j=l2;
while(i>0 && j>0){
if(s1.charAt(i-1)==s2.charAt(j-1)){
ans=s1.charAt(i-1)+ans;
i--;
j--;
}
else if(dp[i-1][j]>dp[i][j-1]){
i--;
}
else
j--;
}
return ans;
}
public static int factorial(int n)
{
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
public static long factorial(long n)
{
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
public static int nCr(int n, int r,int a) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
public static boolean[] sieve()
{
int n=10000000;
// Create a boolean array
// "prime[0..n]" and
// initialize all entries
// it as true. A value in
// prime[i] will finally be
// false if i is Not a
// prime, else true.
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] is not changed, then it is a
// prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
public static boolean isSorted(int arr[]){
int sor[]=new int[arr.length];
for(int i=0;i<arr.length;i++)
sor[i]=arr[i];
sort(sor);
boolean check=true;
for(int i=0;i<arr.length;i++){
if(sor[i]!=arr[i]){
check=false;
break;
}
}
return check;
}
public static int find(int arr[], int n){
for(int i=0;i<arr.length;i++)
if(arr[i]==n)
return i;
return -1;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static long lcm(long a, long b){
return (a/gcd(a,b))*b;
}
//
// Array versions of TreeSet.floor and TreeSet.ceiling
//
// Greatest element less than or equal to val
private int arrayFloor(int[] arr, int val) {
int lo = 0;
int hi = arr.length-1;
int max = Integer.MIN_VALUE;
while (lo <= hi) {
int mid = lo+(hi-lo)/2;
if (arr[mid] <= val) {
max = arr[mid];
lo = mid+1;
} else {
hi = mid-1;
}
}
return max;
}
// Smallest element greater than or equal to val
private int arrayCeiling(int[] arr, int val) {
int lo = 0;
int hi = arr.length-1;
int min = Integer.MAX_VALUE;
while (lo <= hi) {
int mid = lo+(hi-lo)/2;
if (arr[mid] >= val) {
min = arr[mid];
hi = mid-1;
} else {
lo = mid+1;
}
}
return min;
}
public static long sum(long arr[]){
long sum=0;
for(long p:arr) sum+=p;
return sum;
}
public static long sum(int arr[]){
long sum=0;
for(int p:arr) sum+=p;
return sum;
}
public static void solve(FastReader in,FastWriter out){
try{
//-----------------------------------------
int n=in.nextInt();
int q=in.nextInt();
long arr[][]=new long[1001][1001];
for(int i=0;i<n;i++){
int x=in.nextInt();
int y=in.nextInt();
arr[x][y]+=((long)x)*((long)y);
}
for(int i=1;i<=1000;i++){
for(int j=1;j<=1000;j++){
arr[i][j]+=arr[i-1][j]+arr[i][j-1]-arr[i-1][j-1];
}
}
while(q--!=0){
int hs=in.nextInt();
int ws=in.nextInt();
int hb=in.nextInt();
int wb=in.nextInt();
out.println(arr[hb-1][wb-1]-arr[hb-1][ws]-arr[hs][wb-1]+arr[hs][ws]);
}
//-----------------------------------------
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
FastReader in = new FastReader();
FastWriter out = new FastWriter();
int tc=1;
tc=in.nextInt();
while(tc-- != 0) solve(in,out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
5904a6e54c506814d4e97327046410ec
|
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
|
/*LoudSilence*/
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Solution {
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static FastScanner s = new FastScanner();
static FastWriter out = new FastWriter();
final static int mod = (int)1e9 + 7;
final static int INT_MAX = Integer.MAX_VALUE;
final static int INT_MIN = Integer.MIN_VALUE;
final static long LONG_MAX = Long.MAX_VALUE;
final static long LONG_MIN = Long.MIN_VALUE;
final static double DOUBLE_MAX = Double.MAX_VALUE;
final static double DOUBLE_MIN = Double.MIN_VALUE;
final static float FLOAT_MAX = Float.MAX_VALUE;
final static float FLOAT_MIN = Float.MIN_VALUE;
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static class FastScanner{BufferedReader br;StringTokenizer st;
public FastScanner() {if(System.getProperty("ONLINE_JUDGE") == null){try {br = new BufferedReader(new FileReader("E:\\Competitive Coding\\input.txt"));}
catch (FileNotFoundException e) {br = new BufferedReader(new InputStreamReader(System.in));}}else{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());}
List<Integer> readIntList(int n){List<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextInt()); return arr;}
List<Long> readLongList(int n){List<Long> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextLong()); return arr;}
int[] readIntArr(int n){int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.nextInt(); return arr;}
long[] readLongArr(int n){long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = s.nextLong(); return arr;}
String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}}
static class FastWriter{private BufferedWriter bw;public FastWriter(){if(System.getProperty("ONLINE_JUDGE") == null){try {this.bw = new BufferedWriter(new FileWriter("E:\\Competitive Coding\\output.txt"));}
catch (IOException e) {this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}}else{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 debug(int object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");}
public void debug(long object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");}
public void close() throws IOException{bw.close();}}
public static void println(Object str) throws IOException{out.println(""+str);}
public static void println(Object str, int nextLine) throws IOException{out.print(""+str);}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
public static ArrayList<Integer> seive(int n){ArrayList<Integer> list = new ArrayList<>();int arr[] = new int[n+1];for(long i = 2; i <= n; i++) {if(arr[(int)i] == 1) {continue;}else {list.add((int)i);for(long j = i*i; j <= n; j = j + i) {arr[(int)j] = 1;}}}return list;}
public static long gcd(long a, long b){if(a > b) {a = (a+b)-(b=a);}if(a == 0L){return b;}return gcd(b%a, a);}
public static void swap(int[] arr, int i, int j) {arr[i] = arr[i] ^ arr[j]; arr[j] = arr[j] ^ arr[i]; arr[i] = arr[i] ^ arr[j];}
public static boolean isPrime(long n){if(n < 2){return false;}if(n == 2 || n == 3){return true;}if(n%2 == 0 || n%3 == 0){return false;}long sqrtN = (long)Math.sqrt(n)+1;for(long i = 6L; i <= sqrtN; i += 6) {if(n%(i-1) == 0 || n%(i+1) == 0) return false;}return true;}
public static long mod_add(long a, long b){ return (a%mod + b%mod)%mod;}
public static long mod_sub(long a, long b){ return (a%mod - b%mod + mod)%mod;}
public static long mod_mul(long a, long b){ return (a%mod * b%mod)%mod;}
public static long modInv(long a, long b){ return expo(a, b-2)%b;}
public static long mod_div(long a, long b){return mod_mul(a, modInv(b, mod));}
public static long expo (long a, long n){if(n == 0){return 1;}long recAns = expo(mod_mul(a,a), n/2);if(n % 2 == 0){return recAns;}else{return mod_mul(a, recAns);}}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// Pair class
static class Pair implements Comparable<Pair>{
long f;
long s;
public Pair(long f, long s){
this.f = f;
this.s = s;
}
public String toString(){
return "("+f+", "+s+")";
}
@Override
public int compareTo(Pair o) {
if(f == o.f) return s-o.s < 0 ? -1 : 1;
else return f-o.f < 0 ? -1 : 1;
}
}
// public static class Pair<X extends Comparable<X>,Y extends Comparable<Y>> implements Comparable<Pair<X,Y>>{
// X first;
// Y second;
// public Pair(X first, Y second){
// this.first = first;
// this.second = second;
// }
// public String toString(){
// return "( " + first+" , "+second+" )";
// }
// @Override
// public int compareTo(Pair<X, Y> o) {
// int t = first.compareTo(o.first);
// if(t == 0) return second.compareTo(o.second);
// return t;
// }
// }
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// Code begins
public static void solve() throws IOException {
int n = s.nextInt();
int q = s.nextInt();
long arr[][] = new long[1001][1001];
for(int i = 0; i < n; i++){
int hi = s.nextInt();
int wi = s.nextInt();
arr[hi][wi] += hi*wi;
}
long pref[][] = new long[1001][1001];
for(int i = 1; i < 1001; i++){
for(int j = 1; j < 1001; j++){
pref[i][j] = arr[i][j] + pref[i][j-1];
}
}
for(int j = 1; j < 1001; j++){
for(int i = 1; i < 1001; i++){
pref[i][j] += pref[i-1][j];
}
}
//pref[i][j] = sum of area of rect whole hi <= i and wi <= j
while (q-->0){
int hs = s.nextInt();
int ws = s.nextInt();
int hb = s.nextInt();
int wb = s.nextInt();
long ans = pref[hb-1][wb-1] - pref[hb-1][ws] - pref[hs][wb-1] + pref[hs][ws];
println(ans);
}
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
public static void main(String[] args) throws IOException {
int test = s.nextInt();
for(int t = 1; t <= test; t++) {
solve();
}
out.close();
}
}
/*public static boolean allsame(int arr[]){
for(int i = 0; i < arr.length-1; i++){
if(arr[i] != arr[i+1]) return false;
}
return true;
}
public static List<List<Integer>> permutation(int arr[], int i){
if(i == 0){
List<List<Integer>> ans = new ArrayList<>();
List<Integer> li = new ArrayList<>();
li.add(arr[i]);
ans.add(li);
return ans;
}
int temp = arr[i];
List<List<Integer>> rec = permutation(arr, i-1);
List<List<Integer>> ans = new ArrayList<>();
for(List<Integer> li : rec){
for(int j = 0; j <= li.size(); j++){
List<Integer> list = new ArrayList<>();
for(int ele: li){
list.add(ele);
}
list.add(j, temp);
ans.add(list);
}
}
return 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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
46cdfb1655b5f2f6fa9665ecc4ddce3e
|
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
|
/*LoudSilence*/
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Solution {
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static FastScanner s = new FastScanner();
static FastWriter out = new FastWriter();
final static int mod = (int)1e9 + 7;
final static int INT_MAX = Integer.MAX_VALUE;
final static int INT_MIN = Integer.MIN_VALUE;
final static long LONG_MAX = Long.MAX_VALUE;
final static long LONG_MIN = Long.MIN_VALUE;
final static double DOUBLE_MAX = Double.MAX_VALUE;
final static double DOUBLE_MIN = Double.MIN_VALUE;
final static float FLOAT_MAX = Float.MAX_VALUE;
final static float FLOAT_MIN = Float.MIN_VALUE;
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static class FastScanner{BufferedReader br;StringTokenizer st;
public FastScanner() {if(System.getProperty("ONLINE_JUDGE") == null){try {br = new BufferedReader(new FileReader("E:\\Competitive Coding\\input.txt"));}
catch (FileNotFoundException e) {br = new BufferedReader(new InputStreamReader(System.in));}}else{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());}
List<Integer> readIntList(int n){List<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextInt()); return arr;}
List<Long> readLongList(int n){List<Long> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextLong()); return arr;}
int[] readIntArr(int n){int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.nextInt(); return arr;}
long[] readLongArr(int n){long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = s.nextLong(); return arr;}
String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}}
static class FastWriter{private BufferedWriter bw;public FastWriter(){if(System.getProperty("ONLINE_JUDGE") == null){try {this.bw = new BufferedWriter(new FileWriter("E:\\Competitive Coding\\output.txt"));}
catch (IOException e) {this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}}else{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 debug(int object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");}
public void debug(long object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");}
public void close() throws IOException{bw.close();}}
public static void println(Object str) throws IOException{out.println(""+str);}
public static void println(Object str, int nextLine) throws IOException{out.print(""+str);}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
public static ArrayList<Integer> seive(int n){ArrayList<Integer> list = new ArrayList<>();int arr[] = new int[n+1];for(long i = 2; i <= n; i++) {if(arr[(int)i] == 1) {continue;}else {list.add((int)i);for(long j = i*i; j <= n; j = j + i) {arr[(int)j] = 1;}}}return list;}
public static long gcd(long a, long b){if(a > b) {a = (a+b)-(b=a);}if(a == 0L){return b;}return gcd(b%a, a);}
public static void swap(int[] arr, int i, int j) {arr[i] = arr[i] ^ arr[j]; arr[j] = arr[j] ^ arr[i]; arr[i] = arr[i] ^ arr[j];}
public static boolean isPrime(long n){if(n < 2){return false;}if(n == 2 || n == 3){return true;}if(n%2 == 0 || n%3 == 0){return false;}long sqrtN = (long)Math.sqrt(n)+1;for(long i = 6L; i <= sqrtN; i += 6) {if(n%(i-1) == 0 || n%(i+1) == 0) return false;}return true;}
public static long mod_add(long a, long b){ return (a%mod + b%mod)%mod;}
public static long mod_sub(long a, long b){ return (a%mod - b%mod + mod)%mod;}
public static long mod_mul(long a, long b){ return (a%mod * b%mod)%mod;}
public static long modInv(long a, long b){ return expo(a, b-2)%b;}
public static long mod_div(long a, long b){return mod_mul(a, modInv(b, mod));}
public static long expo (long a, long n){if(n == 0){return 1;}long recAns = expo(mod_mul(a,a), n/2);if(n % 2 == 0){return recAns;}else{return mod_mul(a, recAns);}}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// Pair class
static class Pair implements Comparable<Pair>{
long f;
long s;
public Pair(long f, long s){
this.f = f;
this.s = s;
}
public String toString(){
return "("+f+", "+s+")";
}
@Override
public int compareTo(Pair o) {
if(f == o.f) return s-o.s < 0 ? -1 : 1;
else return f-o.f < 0 ? -1 : 1;
}
}
// public static class Pair<X extends Comparable<X>,Y extends Comparable<Y>> implements Comparable<Pair<X,Y>>{
// X first;
// Y second;
// public Pair(X first, Y second){
// this.first = first;
// this.second = second;
// }
// public String toString(){
// return "( " + first+" , "+second+" )";
// }
// @Override
// public int compareTo(Pair<X, Y> o) {
// int t = first.compareTo(o.first);
// if(t == 0) return second.compareTo(o.second);
// return t;
// }
// }
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// Code begins
public static void solve() throws IOException {
int n = s.nextInt();
int q = s.nextInt();
long arr[][] = new long[1001][1001];
for(int i = 0; i < n; i++){
int hi = s.nextInt();
int wi = s.nextInt();
arr[hi][wi] += hi*wi;
}
long pref[][] = new long[1001][1001];
for(int i = 0; i < 1001; i++) pref[i][0] = arr[i][0];
for(int i = 1; i < 1001; i++){
for(int j = 1; j < 1001; j++){
pref[i][j] = arr[i][j] + pref[i][j-1];
}
}
while (q-->0){
int hs = s.nextInt();
int ws = s.nextInt();
int hb = s.nextInt();
int wb = s.nextInt();
long ans = 0l;
for(int i = hs+1; i < hb; i++){
ans += pref[i][wb-1] - pref[i][ws];
}
println(ans);
}
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
public static void main(String[] args) throws IOException {
int test = s.nextInt();
for(int t = 1; t <= test; t++) {
solve();
}
out.close();
}
}
/*public static boolean allsame(int arr[]){
for(int i = 0; i < arr.length-1; i++){
if(arr[i] != arr[i+1]) return false;
}
return true;
}
public static List<List<Integer>> permutation(int arr[], int i){
if(i == 0){
List<List<Integer>> ans = new ArrayList<>();
List<Integer> li = new ArrayList<>();
li.add(arr[i]);
ans.add(li);
return ans;
}
int temp = arr[i];
List<List<Integer>> rec = permutation(arr, i-1);
List<List<Integer>> ans = new ArrayList<>();
for(List<Integer> li : rec){
for(int j = 0; j <= li.size(); j++){
List<Integer> list = new ArrayList<>();
for(int ele: li){
list.add(ele);
}
list.add(j, temp);
ans.add(list);
}
}
return 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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
11e0760bf2ce611eb50d5d34beb2cd30
|
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.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class c{
static int []f=new int [2000001];
//static int []f1;
//static int []f2;
//static int []size1;
//static int []size2;
//static int []a=new int [500001];
static int max=Integer.MAX_VALUE;
public static void main(String []args) {
MyScanner s=new MyScanner();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int t=s.nextInt();
while(t-->0) {
int n=s.nextInt();
int q=s.nextInt();
long [][]f=new long[1001][1001];
for(int i=0;i<n;i++) {
int a=s.nextInt();
int b=s.nextInt();
f[a][b]+=a*b;
}
long [][]sum=new long[1002][1002];
for(int i=1;i<1002;i++) {
for(int j=1;j<1002;j++)
sum[i][j]=f[i-1][j-1]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
}
while(q-->0) {
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
int d=s.nextInt();
long k=sum[c][d]-sum[a+1][d]-sum[c][b+1]+sum[a+1][b+1];
k=Math.max(0, k);
System.out.println(k);
}
}
}
public static void swap(char []a,int j) {
char x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
public static boolean is(int j) {
for(int i=2;i<=(int )Math.sqrt(j);i++) {
if(j%i==0)return false;
}
return true;
}
public static String addStrings(String num1, String num2) {
StringBuilder s=new StringBuilder();
int i=num1.length()-1;
int j=num2.length()-1;
int res=0;
while(i>=0||j>=0||res!=0){
if(i>=0)res+=num1.charAt(i)-'0';
if(j>=0)res+=num2.charAt(j)-'0';
s.append(res%10);
res/=10;
i--;j--;
}
return s.reverse().toString();
}
public static int find (int []father,int x) {
if(x!=father[x])
x=find(father,father[x]);
return father[x];
}
public static void union(int []father,int x,int y,int []size) {
x=find(father,x);
y=find(father,y);
if(x==y)
return ;
if(size[x]<size[y]) {
int tem=x;
x=y;
y=tem;
}
father[y]=x;
size[x]+=size[y];
return ;
}
public static void shufu(int []f) {
for(int i=0;i<f.length;i++) {
int k=(int)(Math.random()*(f.length));
int t=f[k];
f[k]=f[0];
f[0]=t;
}
}
public static int gcd(int x,int y) {
return y==0?x:gcd(y,x%y);
}
/*
public static void buildertree(int k,int l,int r) {
if(l==r)
{
f[k]=a[l];
return ;
}
int m=l+r>>1;
buildertree(k+k,l,m);
buildertree(k+k+1,m+1,r);
f[k]=
}
public static void update(int u,int l,int r,int x,int c)
{
if(l==x && r==x)
{
f[u]=c;
return;
}
int mid=l+r>>1;
if(x<=mid)update(u<<1,l,mid,x,c);
else if(x>=mid+1)update(u<<1|1,mid+1,r,x,c);
f[u]=Math.max(f[u+u], f[u+u+1]);
}
public static int query(int k,int l,int r,int x,int y) {
if(x==l&&y==r) {
return f[k];
}
int m=l+r>>1;
if(y<=m) {
return query(k+k,l,m,x,y);
}
else if(x>m)return query(k+k+1,m+1,r,x,y);
else {
int i=query(k+k,l,m,x,m),j=query(k+k+1,m+1,r,m+1,y);
return Math.max(j, Math.max(i+j, i));
}
}
public static void calc(int k,int l,int r,int x,int z) {
f[k]+=z;
if(l==r) {
return ;
}
int m=l+r>>1;
if(x<=m)
calc(k+k,l,m,x,z);
else calc(k+k+1,m+1,r,x,z);
}
*/
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", "");
cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname;
final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in");
br = new BufferedReader(new InputStreamReader(fin.exists()
? new FileInputStream(fin) : System.in));
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
public String next() {
try {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
da41b330981087aec4a40f9b347f82b7
|
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.util.Arrays;
import java.util.Scanner;
public class _1722E {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Test case template
Scanner sc = new Scanner(System.in);
int cases=sc.nextInt();
while(cases--!=0) {
//For every Testcase
int n=sc.nextInt();
int q=sc.nextInt();
long arr[][] = new long[1002][1002];
long pref[][] = new long[1002][1002];
for(int i=0;i<=1001;i++) {
for(int j=0;j<=1001;j++) {
arr[i][j]=pref[i][j]=0;
}
}
for(int i=0;i<n;i++) {
int h=sc.nextInt();
int w=sc.nextInt();
arr[h][w]+=h*w;
}
for(int i=1;i<=1000;i++) {
for(int j=1;j<=1000;j++) {
pref[i][j]=pref[i-1][j]+pref[i][j-1]-pref[i-1][j-1]+arr[i][j];
}
}
for(int i=0;i<q;i++) {
int hs=sc.nextInt();
int ws=sc.nextInt();
int hb=sc.nextInt();
int wb=sc.nextInt();
System.out.println(pref[hb-1][wb-1]-pref[hb-1][ws]-pref[hs][wb-1]+pref[hs][ws]);
}
//Slow approach
// int[] my_rec_hei=new int[n];
// int[] my_rec_wid=new int[n];
// long[] area = new long[n];
// for(int i=0;i<n;i++) {
// my_rec_hei[i]=sc.nextInt();
// my_rec_wid[i]=sc.nextInt();
// area[i]=my_rec_hei[i]*my_rec_wid[i];
// }
// for(int i=0;i<q;i++) {
// int hs=sc.nextInt();
// int ws=sc.nextInt();
// int hb=sc.nextInt();
// int wb=sc.nextInt();
//
// long sum=0;
// for(int j=0;j<n;j++) {
// if(my_rec_hei[j]>hs && my_rec_wid[j]>ws) {
// if(my_rec_hei[j]<hb && my_rec_wid[j]<wb) {
// sum+=area[j];
// }
// }
// }
// System.out.println(sum);
// }
}
}
}
//Solving using Prefix SUms
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
c3ecfb59a3c91f9775c37b3ba76f2715
|
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.util.Scanner;
public class CountingRectangles2{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int inputs = in.nextInt();
for (int i = 0; i < inputs; i++) {
int rectangles = in.nextInt();
int queries = in.nextInt();
long[][] blank = new long[1005][1005]; //[heights][widths] stores areas
for(int j = 0; j<rectangles; j++){
int height = in.nextInt();
int width = in.nextInt();
blank[height][width] += height*width;
}
long[][] prefixsum = new long[1005][1005];
for (int j = 1; j <=1001; j++) {
for (int j2 = 1; j2 <=1001; j2++) {
prefixsum[j][j2] = prefixsum[j-1][j2]+prefixsum[j][j2-1]-prefixsum[j-1][j2-1]+ blank[j][j2];
}
} for (int j = 0; j < queries; j++) {
int minheight = in.nextInt();
int minwidth = in.nextInt();
int maxheight = in.nextInt();
int maxwidth = in.nextInt();
long output = prefixsum[maxheight-1][maxwidth-1]-prefixsum[maxheight-1][minwidth]- prefixsum[minheight][maxwidth-1]+prefixsum[minheight][minwidth];
System.out.println(output);
}
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
bbb50f8bdf503c73904d9734fcc9dd2b
|
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
|
/*
_oo0oo_
o8888888o
88" . "88
(| -_- |)
0\ = /0
___/`---'\___
.' \\| |// '.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' |_/ |
\ .-\__ '-' ___/-. /
___'. .' /--.--\ `. .'___
."" '< `.___\_<|>_/___.' >' "".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `_. \_ __\ /__ _/ .-` / /
=====`-.____`.___ \_____/___.-`___.-'=====
`=---='
*/
import java.util.*;
import java.math.*;
import java.io.*;
import java.lang.Math.*;
public class KickStart2020 {
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());
}
float nextFloat() {
return Float.parseFloat(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
public static class Pair implements Comparable<Pair> {
public int index;
public long value;
public Pair(int index, long value) {
this.index = index;
this.value = value;
}
@Override
public int compareTo(Pair other) {
// multiplied to -1 as the author need descending sort order
if(other.index < this.index) return 1;
if(other.index > this.index) return -1;
return 0;
}
@Override
public String toString() {
return this.index + " " + this.value;
}
}
static int isPrime(long d) {
if (d == 1)
return -1;
for (int i = 2; i <= (long) Math.sqrt(d); i++) {
if (d % i == 0)
return i;
}
return -1;
}
static boolean isPali(String n) {
String s = n;
int l = 0;
int r = s.length() - 1;
while(l < r) if(s.charAt(l++) != s.charAt(r--)) return false;
return true;
}
static void decimalTob(long n, int k , int arr[], int i) {
arr[i] += (n % k);
n /= k;
if(n > 0) {
decimalTob(n, k, arr, i + 1);
}
}
static long powermod(long x, long y, long mod) {
if(y == 0) return 1;
long value = powermod(x, y / 2, mod);
if(y % 2 == 0) return (value * value) % mod;
return (value * (value * x) % mod) % mod;
}
static long power(long x, long y) {
if(y == 0) return 1;
long value = power(x, y / 2);
if(y % 2 == 0) return (value * value);
return value * value * x;
}
static int bS(int l, int r, int find, Integer arr[]) {
int ans = -1;
while(l <= r) {
int mid = (l + r) / 2;
if(arr[mid] >= find) {
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
return ans;
}
static void build(int index, int l, int r, int seqtree[],int arr[]) {
if(l == r) {
seqtree[index] = arr[l];
return;
}
int mid = (l + r) / 2;
build(2 * index + 1, l, mid, seqtree, arr);
build(2 * index + 2, mid + 1, r, seqtree, arr);
seqtree[index] = Math.max(seqtree[2 * index + 1], seqtree[2 * index + 2]);
}
static int query(int index, int low, int high, int l, int r, int seqtree[]) {
if(high < l || low > r) return Integer.MIN_VALUE;
if(l <= low && r >= high) {
return seqtree[index];
}
int mid = (low + high) / 2;
int left = query(2 * index + 1, low , mid, l, r, seqtree);
int right = query(2 * index + 2, mid + 1, high, l, r, seqtree);
return Math.max(left, right);
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
outerloop:
while(t-- > 0) {
int n = sc.nextInt();
int q = sc.nextInt();
long arr[][] = new long[1000 + 1][1000 + 1];
for(int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
arr[x][y] += x * y;
}
for(int i = 1; i <= 1000; i++) {
for(int j = 1;j <= 1000; j++) {
arr[i][j] += arr[i][j - 1];
}
}
for(int i = 1; i <= 1000; i++) {
for(int j = 1;j <= 1000; j++) {
arr[i][j] += arr[i - 1][j];
}
}
while(q-- > 0) {
int hs = sc.nextInt();
int ws = sc.nextInt();
int hb = sc.nextInt();
int wb = sc.nextInt();
out.println(arr[hb - 1][wb - 1] - (arr[hb - 1][ws] + arr[hs][wb - 1] - arr[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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
13d22476195dc22b2f1ce37ed7c8542b
|
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.*;
import java.util.StringTokenizer;
import java.util.*;
public class School {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int t= sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int q= sc.nextInt();
long dim[][]= new long [1001][1001];
long areas[][]= new long [1001][1001];
for (int i=0;i<n;i++){
int h=sc.nextInt();
int w=sc.nextInt();
dim[h][w]++;
}
for (int i =1;i<1001;i++){
for (int j=1;j<1001;j++){
areas[i][j]=(long)(i*j*dim[i][j])+areas[i-1][j]+areas[i][j-1]-areas[i-1][j-1];
}
}
while (q-->0){
int h1=sc.nextInt(); int w1 = sc.nextInt();
int h2= sc.nextInt()-1; int w2 =sc.nextInt()-1;
System.out.println(areas[h2][w2]-areas[h2][w1]-areas[h1][w2]+areas[h1][w1]);
}
}
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
9d6ed2b02193ff50f195b837b180beef
|
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int globalVariable = 123456789;
static String author = "pl728 on codeforces";
public static void main(String[] args) {
FastReader sc = new FastReader();
MathUtils mathUtils = new MathUtils();
ArrayUtils arrayUtils = new ArrayUtils();
int tc = sc.ni();
while (tc-- != 0) {
int n = sc.ni(), q = sc.ni();
int[] h = new int[n];
int[] w = new int[n];
for (int i = 0; i < n; i++) {
h[i] = sc.ni();
w[i] = sc.ni();
}
long[][] a = new long[1001][1001];
long[][] pref = new long[1001][1001];
for (int i = 0; i < n; i++) {
a[h[i]][w[i]] += h[i] * w[i];
}
for (int i = 1; i < 1001; i++) {
for (int j = 1; j < 1001; j++) {
pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1] + a[i][j];
}
}
for (int i = 0; i < q; i++) {
int hs = sc.ni(), ws = sc.ni(), hb = sc.ni(), wb = sc.ni();
System.out.println(pref[hb - 1][wb - 1] - pref[hb - 1][ws] - pref[hs][wb - 1] + pref[hs][ws]);
}
}
}
static class FastReader {
/**
* Uses BufferedReader and StringTokenizer for quick java I/O
* get next int, long, double, string
* get int, long, double, string arrays, both primitive and wrapped object when array contains all elements
* on one line, and we know the array size (n)
* next: gets next space separated item
* nextLine: returns entire line as space
*/
BufferedReader br;
StringTokenizer st;
public FastReader() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
// to parse something else:
// T x = T.parseT(fastReader.next());
public int ni() {
return Integer.parseInt(next());
}
public String ns() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public String[] readStringArrayLine(int n) {
String line = "";
try {
line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line.split(" ");
}
public String[] readStringArrayLines(int n) {
String[] result = new String[n];
for (int i = 0; i < n; i++) {
result[i] = this.next();
}
return result;
}
public int[] readIntArray(int n) {
int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = this.ni();
}
return result;
}
public long[] readLongArray(int n) {
long[] result = new long[n];
for (int i = 0; i < n; i++) {
result[i] = this.nl();
}
return result;
}
public Integer[] readIntArrayObject(int n) {
Integer[] result = new Integer[n];
for (int i = 0; i < n; i++) {
result[i] = this.ni();
}
return result;
}
public long nl() {
return Long.parseLong(next());
}
public char[] readCharArray(int n) {
return this.ns().toCharArray();
}
}
static class MathUtils {
public MathUtils() {
}
public long gcdLong(long a, long b) {
if (a % b == 0)
return b;
else
return gcdLong(b, a % b);
}
public long lcmLong(long a, long b) {
return a * b / gcdLong(a, b);
}
}
static class ArrayUtils {
public ArrayUtils() {
}
public static int[] reverse(int[] a) {
int n = a.length;
int[] b = new int[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = a[i];
j = j - 1;
}
return b;
}
public int sumIntArrayInt(int[] a) {
int ans = 0;
for (int i = 0; i < a.length; i++) {
ans += a[i];
}
return ans;
}
public long sumLongArrayLong(int[] a) {
long ans = 0;
for (int i = 0; i < a.length; i++) {
ans += a[i];
}
return ans;
}
}
public static int lowercaseToIndex(char c) {
return (int) c - 97;
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
50ad79fa9f30d43e007912414ed714f5
|
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.*;
import java.util.*;
public class Codeforces {
static FastReader f = new FastReader();
public static PrintWriter out = new PrintWriter(System.out);
static int primes[];
static int dp1[][];
static long ans = 0;
static List<List<Integer>> adj = new ArrayList<>();
static int up[][] = new int[200001][20];
public static void solve() {
int n = f.nextInt();
int q = f.nextInt();
long pre[][] = new long[1001][1001];
for(int i = 0 ; i < n ; i++) {
int r = f.nextInt();
int c = f.nextInt();
pre[r][c] += r*c;
}
for(int i = 1 ; i <= 1000 ; i++) {
for(int j = 2 ; j <= 1000 ; j++) {
pre[i][j] = pre[i][j] + pre[i][j - 1];
}
}
for(int j = 1; j <= 1000 ; j++) {
for(int i = 2 ; i <= 1000 ; i++) {
pre[i][j] = pre[i][j] + pre[i - 1][j];
}
}
for(int i = 0 ; i < q ; i++) {
int hs = f.nextInt();
int ws = f.nextInt();
int hb = f.nextInt();
int wb = f.nextInt();
long ans = pre[hb - 1][wb - 1] - pre[hs][wb - 1] - pre[hb - 1][ws] +
pre[hs][ws];
out.println(ans);
}
out.flush();
}
static void binaryLifting(int src, int par) {
up[src][0] = par;
for(int i = 1; i < 20 ; i++) {
if(up[src][i - 1] != -1) {
up[src][i] = up[up[src][i - 1]][i - 1];
}
else {
up[src][i] = -1;
}
}
for(int child : adj.get(src)) {
if(child != par) {
binaryLifting(child, src);
}
}
}
public static void main(String[] args) {
int t = f.nextInt();
// int t = 1;
while (t-- > 0) {
solve();
}
}
static boolean isPower2(long n) {
long temp = (long) (Math.log(n) / Math.log(2));
if (n == Math.pow(2, temp)) {
return true;
}
return false;
}
static int dp[][];
static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j))
return false;
i++;
j--;
}
return true;
}
public static List<Integer> PrimeFactors(int n) {
List<Integer> list = new ArrayList<Integer>();
int count = 0;
while (n % 2 == 0) {
count++;
n /= 2;
}
if (count > 0)
list.add(2);
for (int i = 3; i <= Math.sqrt(n); i += 2) {
count = 0;
while (n % i == 0) {
count++;
n /= i;
}
if (count > 0)
list.add(i);
}
if (n > 2)
list.add(n);
return list;
}
public static double setPrecisionK(double d) {
double ans = 0;
// If round off upto 5 digits
// ans = Math.round(d*1e5)/1e5;
return ans;
}
public static List<String[]> all_substrings(String s) {
List<String[]> substr = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
for (int j = i; j < s.length(); j++) {
substr.add(new String[] { s.substring(i, j + 1), s.substring(j + 1, s.length()) });
}
}
return substr;
}
public static int number_of_2_in_N(long n) {
int i = 0;
while (n % 2 == 0) {
i++;
n /= 2;
}
return i;
}
public static boolean is_power_of_2(long n) {
if (n == 0)
return false;
return ((n) & (n - 1)) == 0;
}
public static int highest_power_of_2(int n) {
int res = 0;
for (int i = n; i >= 1; i--) {
// If i is a power of 2
if ((i & (i - 1)) == 0) {
res = i;
break;
}
}
return res;
}
static List<Integer> fibonacci(int n) {
List<Integer> list = new ArrayList<>();
list.add(0);
list.add(1);
for (int i = 2; i < n; i++) {
list.add(list.get(i - 1) + list.get(i - 2));
}
return list;
}
static long nCr(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
// Returns factorial of n
static int factorial(int n) {
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
public static Map<Integer, Integer> prime_factors_with_freq(int n) {
// Print the number of 2s that divide n
Map<Integer, Integer> map = new HashMap<>();
int count = 0;
while (n % 2 == 0) {
// System.out.print(2 + " ");
count++;
n /= 2;
}
if (count > 0)
map.put(2, count);
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i += 2) {
// While i divides n, print i and divide n
count = 0;
while (n % i == 0) {
// System.out.print(i + " ");
count++;
n /= i;
}
if (count > 0)
map.put(i, count);
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
map.put(n, map.getOrDefault(n, 0) + 1);
return map;
}
static void sieve_of_Eratosthenes(int n) {
List<Integer> list = 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;
}
}
int idx = 0;
for (int i = 2; i <= n; i++) {
if (prime[i] == true) {
primes[idx++] = i;
}
}
}
static List<Integer> get_divisors(long n) {
List<Integer> ans = new ArrayList();
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
ans.add(i);
ans.add((int) (n / i));
}
}
return ans;
}
public static boolean isLucky(int n) {
boolean flag = true;
int nn = n;
while (n > 0) {
if (n % 10 != 4 && n % 10 != 7) {
flag = false;
break;
}
n /= 10;
}
return flag;
}
static boolean is_perfect_square(double number) {
// calculating the square root of the given number
double sqrt = Math.sqrt(number);
// finds the floor value of the square root and comparing it with zero
return ((sqrt - Math.floor(sqrt)) == 0);
}
public static long smallest_divisor(long n) {
// if divisible by 2
if (n % 2 == 0)
return 2;
// iterate from 3 to sqrt(n)
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return i;
}
return n;
}
public static boolean isPrime(long n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long factorize(long n) {
long ans = 0;
while (!(n % 2 > 0)) {
// equivalent to n=n/2;
n >>= 1;
ans++;
}
if (ans > 0)
ans = 1;
for (long i = 3; i <= (long) Math.sqrt(n); i += 2) {
int count = 0;
while (n % i == 0) {
count++;
n = n / i;
}
ans += count;
}
if (n > 2) {
ans++;
}
return ans;
}
static int count_set_bits(long n) {
int count = 0;
while (n > 0) {
n &= (n - 1);
count++;
}
return count;
}
public static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
8dd72863d23d3a02d9a4529f73f42f87
|
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static AReader scan = new AReader();
static int N = 1010;
static long[][] g = new long[N][N];
static long get(int x1,int x2,int y1,int y2){
if(x2 < x1 || y2 < y1) return 0;
return g[x2][y2] - g[x2][y1] - g[x1][y2] + g[x1][y1];
}
static void solve() {
int n = scan.nextInt();
int q = scan.nextInt();
for(int i = 1;i<=1000;i++){
for(int j = 1;j<=1000;j++){
g[i][j] = 0;
}
}
for(int i = 1;i<=n;i++){
int h = scan.nextInt();
int w = scan.nextInt();
g[h][w] += h * w;
}
for(int i = 1;i<=1000;i++){
for(int j = 1;j<=1000;j++){
g[i][j] += g[i-1][j] + g[i][j-1] - g[i-1][j-1];
}
}
while(q-->0){
int x1 = scan.nextInt();
int y1 = scan.nextInt();
int x2 = scan.nextInt();
int y2 = scan.nextInt();
x2--;
y2--;
System.out.println(get(x1,x2,y1,y2));
}
}
public static void main(String[] args) {
int T = scan.nextInt();
while (T-- > 0) {
solve();
}
}
}
class AReader {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer tokenizer = new StringTokenizer("");
private String innerNextLine() {
try {
return reader.readLine();
} catch (IOException ex) {
return null;
}
}
public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String nextLine = innerNextLine();
if (nextLine == null) {
return false;
}
tokenizer = new StringTokenizer(nextLine);
}
return true;
}
public String nextLine() {
tokenizer = new StringTokenizer("");
return innerNextLine();
}
public String next() {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
class Pair {
int x,y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
class Node{
int l,r;
int val;
int lazy;
int cnt = 0,lnum,rnum;
public Node(int l, int r) {
this.l = l;
this.r = r;
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
02743f4cd8e4500cecd448676b4addf5
|
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.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static final int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE;
static final long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE;
static final long mod = (long) 1e9 + 7;
static void solve() {
int caseNo = 1;
for (int T = sc.nextInt(); T > 1; T--, caseNo++) { solveIt(caseNo); }
solveIt(caseNo);
}
// Solution
static void solveIt(int testCaseNo) {
int n = sc.nextInt();
int q = sc.nextInt();
long a[][] = new long[1003][1003];
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
a[x][y] += (long) (x * y);
}
for (int i = 1000; i >= 1; i--) {
for (int j = 1000; j >= 1; j--) {
//
a[i][j] = a[i][j] + a[i + 1][j] + a[i][j + 1] - a[i + 1][j + 1];
}
}
while (q-- > 0) {
int hs = sc.nextInt() + 1, ws = sc.nextInt() + 1;
int hb = sc.nextInt(), wb = sc.nextInt();
long ans = a[hs][ws] - a[hs][wb] - a[hb][ws] + a[hb][wb];
out.println(ans);
}
}
public static void main(String[] args) throws Exception {
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
solve();
out.flush();
long G = System.currentTimeMillis();
sc.tr(G - S + "ms");
}
static class sc {
private static boolean endOfFile() {
if (bufferLength == -1) return true;
int lptr = ptrbuf;
while (lptr < bufferLength) {
if (!isThisTheSpaceCharacter(inputBufffferBigBoi[lptr++])) return false;
}
try {
is.mark(1000);
while (true) {
int b = is.read();
if (b == -1) {
is.reset();
return true;
} else if (!isThisTheSpaceCharacter(b)) {
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inputBufffferBigBoi = new byte[1024];
static int bufferLength = 0, ptrbuf = 0;
private static int justReadTheByte() {
if (bufferLength == -1) throw new InputMismatchException();
if (ptrbuf >= bufferLength) {
ptrbuf = 0;
try {
bufferLength = is.read(inputBufffferBigBoi);
} catch (IOException e) {
throw new InputMismatchException();
}
if (bufferLength <= 0) return -1;
}
return inputBufffferBigBoi[ptrbuf++];
}
private static boolean isThisTheSpaceCharacter(int c) { return !(c >= 33 && c <= 126); }
private static int skipItBishhhhhhh() {
int b;
while ((b = justReadTheByte()) != -1 && isThisTheSpaceCharacter(b));
return b;
}
private static double nextDouble() { return Double.parseDouble(next()); }
private static char nextChar() { return (char) skipItBishhhhhhh(); }
private static String next() {
int b = skipItBishhhhhhh();
StringBuilder sb = new StringBuilder();
while (!(isThisTheSpaceCharacter(b))) {
sb.appendCodePoint(b);
b = justReadTheByte();
}
return sb.toString();
}
private static char[] readCharArray(int n) {
char[] buf = new char[n];
int b = skipItBishhhhhhh(), p = 0;
while (p < n && !(isThisTheSpaceCharacter(b))) {
buf[p++] = (char) b;
b = justReadTheByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] readCharMatrix(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) map[i] = readCharArray(m);
return map;
}
private static int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
private static long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
private static int nextInt() {
int num = 0, b;
boolean minus = false;
while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if (b == '-') {
minus = true;
b = justReadTheByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = justReadTheByte();
}
}
private static long nextLong() {
long num = 0;
int b;
boolean minus = false;
while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if (b == '-') {
minus = true;
b = justReadTheByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = justReadTheByte();
}
}
private static void tr(Object... o) {
if (INPUT.length() != 0) System.out.println(Arrays.deepToString(o));
}
}
}
// And I wish you could sing along, But this song is a joke, and the melody I
// wrote, wrong
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
b25ebfeb95ad7188caedf52d360eb756
|
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.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static final int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE;
static final long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE;
static final long mod = (long) 1e9 + 7;
static void solve() {
int caseNo = 1;
for (int T = sc.nextInt(); T > 1; T--, caseNo++) { solveIt(caseNo); }
solveIt(caseNo);
}
// Solution
static void solveIt(int testCaseNo) {
int n = sc.nextInt();
int q = sc.nextInt();
long a[][] = new long[1003][1003];
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
a[x][y] += (long) (x * y);
}
for (int i = 1000; i >= 1; i--) {
for (int j = 1000; j >= 1; j--) {
//
a[i][j] = a[i][j] + a[i + 1][j] + a[i][j + 1] - a[i + 1][j + 1];
}
}
while (q-- > 0) {
int hs = sc.nextInt() + 1, ws = sc.nextInt() + 1;
int hb = sc.nextInt(), wb = sc.nextInt();
long ans = a[hs][ws] - a[hs][wb] - a[hb][ws] + a[hb][wb];
System.out.println(ans);
}
}
public static void main(String[] args) throws Exception {
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
solve();
out.flush();
long G = System.currentTimeMillis();
sc.tr(G - S + "ms");
}
static class sc {
private static boolean endOfFile() {
if (bufferLength == -1) return true;
int lptr = ptrbuf;
while (lptr < bufferLength) {
if (!isThisTheSpaceCharacter(inputBufffferBigBoi[lptr++])) return false;
}
try {
is.mark(1000);
while (true) {
int b = is.read();
if (b == -1) {
is.reset();
return true;
} else if (!isThisTheSpaceCharacter(b)) {
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inputBufffferBigBoi = new byte[1024];
static int bufferLength = 0, ptrbuf = 0;
private static int justReadTheByte() {
if (bufferLength == -1) throw new InputMismatchException();
if (ptrbuf >= bufferLength) {
ptrbuf = 0;
try {
bufferLength = is.read(inputBufffferBigBoi);
} catch (IOException e) {
throw new InputMismatchException();
}
if (bufferLength <= 0) return -1;
}
return inputBufffferBigBoi[ptrbuf++];
}
private static boolean isThisTheSpaceCharacter(int c) { return !(c >= 33 && c <= 126); }
private static int skipItBishhhhhhh() {
int b;
while ((b = justReadTheByte()) != -1 && isThisTheSpaceCharacter(b));
return b;
}
private static double nextDouble() { return Double.parseDouble(next()); }
private static char nextChar() { return (char) skipItBishhhhhhh(); }
private static String next() {
int b = skipItBishhhhhhh();
StringBuilder sb = new StringBuilder();
while (!(isThisTheSpaceCharacter(b))) {
sb.appendCodePoint(b);
b = justReadTheByte();
}
return sb.toString();
}
private static char[] readCharArray(int n) {
char[] buf = new char[n];
int b = skipItBishhhhhhh(), p = 0;
while (p < n && !(isThisTheSpaceCharacter(b))) {
buf[p++] = (char) b;
b = justReadTheByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] readCharMatrix(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) map[i] = readCharArray(m);
return map;
}
private static int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
private static long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
private static int nextInt() {
int num = 0, b;
boolean minus = false;
while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if (b == '-') {
minus = true;
b = justReadTheByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = justReadTheByte();
}
}
private static long nextLong() {
long num = 0;
int b;
boolean minus = false;
while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if (b == '-') {
minus = true;
b = justReadTheByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = justReadTheByte();
}
}
private static void tr(Object... o) {
if (INPUT.length() != 0) System.out.println(Arrays.deepToString(o));
}
}
}
// And I wish you could sing along, But this song is a joke, and the melody I
// wrote, wrong
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
945e321232e990b3ba5c9892f124c363
|
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.util.Scanner;
import java.util.TreeMap;
public class E2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(; t>0; t--) {
int n = sc.nextInt();
int q = sc.nextInt();
long[][] matriz = new long[1001][1001];
for(int i=0; i<n; i++) {
matriz[sc.nextInt()][sc.nextInt()]++;
}
for(int i=1; i<=1000; i++) {
long cont = 0;
for(int j=1; j<=1000; j++) {
cont += j*matriz[i][j];
matriz[i][j] = cont*i;
}
}
for(int k=0; k<q; k++) {
int hs = sc.nextInt();
int ws = sc.nextInt();
int hb = sc.nextInt();
int wb = sc.nextInt();
long result = 0;
for(int i=hs+1; i<hb; i++) {
result += -matriz[i][ws] + matriz[i][wb-1];
}
System.out.println(result);
}
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
b3d169a3ceebeb349c9a72a0b91cabae
|
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.*;
import java.util.*;
public class Main {
public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353;
public static void main(String args[]){
try {
PrintWriter o = new PrintWriter(System.out);
boolean multiTest = true;
// init
if(multiTest) {
int t = fReader.nextInt(), loop = 0;
while (loop < t) {loop++;solve(o);}
} else solve(o);
o.close();
} catch (Exception e) {e.printStackTrace();}
}
static void solve(PrintWriter o) {
try {
int n = fReader.nextInt(), q = fReader.nextInt();
long[][] preSum = new long[1005][1005];
for(int i=0;i<n;i++) {
int h = fReader.nextInt(), w = fReader.nextInt();
preSum[h+1][w+1] += 1l*h*w;
}
for(int i=1;i<=1000;i++) {
for(int j=1;j<=1000;j++) {
preSum[i][j] += preSum[i-1][j] + preSum[i][j-1] - preSum[i-1][j-1];
}
}
for(int i=0;i<q;i++) {
int hs = fReader.nextInt(), ws = fReader.nextInt();
int hb = fReader.nextInt(), wb = fReader.nextInt();
o.println(preSum[hb][wb] - preSum[hb][ws+1] - preSum[hs+1][wb] + preSum[hs+1][ws+1]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static int upper_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) <= val) l = mid + 1;
else r = mid;
}
return l;
}
public static int lower_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) < val) l = mid + 1;
else r = mid;
}
return l;
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b, a%b);
}
public static long lcm(long a, long b){
return a / gcd(a,b)*b;
}
public static boolean isPrime(long x){
boolean ok = true;
for(long i=2;i<=Math.sqrt(x);i++){
if(x % i == 0){
ok = false;
break;
}
}
return ok;
}
public static long qpow(long a, long n) {
a %= mod;
long ret = 1l;
while(n > 0) {
if((n & 1) > 0) {
ret = ret * a % mod;
}
n >>= 1;
a = a * a % mod;
}
return ret;
}
public static class DSU {
int[] parent;
int[] size;
int n;
public DSU(int n){
this.n = n;
parent = new int[n];
size = new int[n];
for(int i=0;i<n;i++){
parent[i] = i;
size[i] = 1;
}
}
public int find(int p){
while(parent[p] != p){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
public void union(int p, int q){
int root_p = find(p);
int root_q = find(q);
if(root_p == root_q) return;
if(size[root_p] >= size[root_q]){
parent[root_q] = root_p;
size[root_p] += size[root_q];
size[root_q] = 0;
}
else{
parent[root_p] = root_q;
size[root_q] += size[root_p];
size[root_p] = 0;
}
n--;
}
public int getTotalComNum(){
return n;
}
public int getSize(int i){
return size[find(i)];
}
}
public static class FenWick {
int n;
long[] tree;
public FenWick(int n){
this.n = n;
tree = new long[n+1];
}
private void add(int x, long val){
while(x <= n){
tree[x] += val;
tree[x] %= mod9;
x += x&-x;
}
}
private long query(int x){
long ret = 0l;
while(x > 0){
ret += tree[x];
ret %= mod9;
x -= x&-x;
}
return ret;
}
}
public static class Node{
String s;
Integer mn;
Integer mx;
Integer tot;
public Node(String s, Integer mn, Integer mx, Integer tot) {
this.s = s;
this.mn = mn;
this.mx = mx;
this.tot = tot;
}
@Override
public int hashCode() {
int prime = 31, res = 1;
res = prime*res + s.hashCode();
res = prime*res + mn.hashCode();
res = prime*res + tot.hashCode();
return res;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Node) {
return s.equals(((Node) obj).s);
}
return false;
}
}
public static class fReader {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer tokenizer = new StringTokenizer("");
private static String next() throws IOException{
while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException {return Integer.parseInt(next());}
public static Long nextLong() throws IOException {return Long.parseLong(next());}
public static double nextDouble() throws IOException {return Double.parseDouble(next());}
public static char nextChar() throws IOException {return next().toCharArray()[0];}
public static String nextString() throws IOException {return next();}
public static String nextLine() throws IOException {return reader.readLine();}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
11a2bf18dbc34d43af6b32eae2d4a090
|
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.*;
import java.util.*;
public class Main {
public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353;
public static void main(String args[]){
try {
PrintWriter o = new PrintWriter(System.out);
boolean multiTest = true;
// init
if(multiTest) {
int t = fReader.nextInt(), loop = 0;
while (loop < t) {loop++;solve(o);}
} else solve(o);
o.close();
} catch (Exception e) {e.printStackTrace();}
}
static void solve(PrintWriter o) {
try {
int n = fReader.nextInt(), q = fReader.nextInt();
long[][] a = new long[1005][1005];
long[][] preSum = new long[1005][1005];
for(int i=0;i<n;i++) {
int h = fReader.nextInt()-1, w = fReader.nextInt()-1;
a[h][w] += (h+1)*(w+1);
}
for(int i=1;i<1005;i++) {
for(int j=1;j<1005;j++) {
preSum[i][j] = preSum[i-1][j] + preSum[i][j-1] - preSum[i-1][j-1] + a[i-1][j-1];
}
}
for(int i=0;i<q;i++) {
int h1 = fReader.nextInt(), w1 = fReader.nextInt(), h2 = fReader.nextInt(), w2 = fReader.nextInt();
o.println(preSum[h2-1][w2-1] - preSum[h2-1][w1] - preSum[h1][w2-1] + preSum[h1][w1]);
}
} catch (Exception e){e.printStackTrace();}
}
public static int upper_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) <= val) l = mid + 1;
else r = mid;
}
return l;
}
public static int lower_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) < val) l = mid + 1;
else r = mid;
}
return l;
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b, a%b);
}
public static long lcm(long a, long b){
return a / gcd(a,b)*b;
}
public static boolean isPrime(long x){
boolean ok = true;
for(long i=2;i<=Math.sqrt(x);i++){
if(x % i == 0){
ok = false;
break;
}
}
return ok;
}
public static void reverse(int[] array){
reverse(array, 0 , array.length-1);
}
public static void reverse(int[] array, int left, int right) {
if (array != null) {
int i = left;
for(int j = right; j > i; ++i) {
int tmp = array[j];
array[j] = array[i];
array[i] = tmp;
--j;
}
}
}
public static long qpow(long a, long n){
long ret = 1l;
while(n > 0){
if((n & 1) == 1){
ret = ret * a % mod;
}
n >>= 1;
a = a * a % mod;
}
return ret;
}
public static class DSU {
int[] parent;
int[] size;
int n;
public DSU(int n){
this.n = n;
parent = new int[n];
size = new int[n];
for(int i=0;i<n;i++){
parent[i] = i;
size[i] = 1;
}
}
public int find(int p){
while(parent[p] != p){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
public void union(int p, int q){
int root_p = find(p);
int root_q = find(q);
if(root_p == root_q) return;
if(size[root_p] >= size[root_q]){
parent[root_q] = root_p;
size[root_p] += size[root_q];
size[root_q] = 0;
}
else{
parent[root_p] = root_q;
size[root_q] += size[root_p];
size[root_p] = 0;
}
n--;
}
public int getTotalComNum(){
return n;
}
public int getSize(int i){
return size[find(i)];
}
}
public static class FenWick {
int n;
long[] tree;
public FenWick(int n){
this.n = n;
tree = new long[n+1];
}
private void add(int x, long val){
while(x <= n){
tree[x] += val;
x += x&-x;
}
}
private long query(int x){
long ret = 0l;
while(x > 0){
ret += tree[x];
x -= x&-x;
}
return ret;
}
}
public static class fReader {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer tokenizer = new StringTokenizer("");
private static String next() throws IOException{
while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException {return Integer.parseInt(next());}
public static Long nextLong() throws IOException {return Long.parseLong(next());}
public static double nextDouble() throws IOException {return Double.parseDouble(next());}
public static char nextChar() throws IOException {return next().toCharArray()[0];}
public static String nextString() throws IOException {return next();}
public static String nextLine() throws IOException {return reader.readLine();}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
30159ce91010b79f0b0e6c5c7a17a811
|
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.*;
import java.util.*;
public class Main {
public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353;
public static void main(String args[]){
try {
PrintWriter o = new PrintWriter(System.out);
boolean multiTest = true;
// init
if(multiTest) {
int t = fReader.nextInt(), loop = 0;
while (loop < t) {loop++;solve(o);}
} else solve(o);
o.close();
} catch (Exception e) {e.printStackTrace();}
}
static void solve(PrintWriter o) {
try {
int n = fReader.nextInt(), q = fReader.nextInt();
long[][] a = new long[1005][1005];
long[][] preSum = new long[1005][1005];
for(int i=0;i<n;i++) {
int u = fReader.nextInt()-1, v = fReader.nextInt()-1;
a[u][v] += (u+1)*(v+1);
}
for(int i=1;i<1005;i++) {
for(int j=1;j<1005;j++) {
preSum[i][j] = preSum[i-1][j] + preSum[i][j-1] - preSum[i-1][j-1] + a[i-1][j-1];
}
}
for(int i=0;i<q;i++) {
int h1 = fReader.nextInt(), w1 = fReader.nextInt(), h2 = fReader.nextInt(), w2 = fReader.nextInt();
o.println(preSum[h2-1][w2-1] - preSum[h2-1][w1] - preSum[h1][w2-1] + preSum[h1][w1]);
}
} catch (Exception e){e.printStackTrace();}
}
public static int upper_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) <= val) l = mid + 1;
else r = mid;
}
return l;
}
public static int lower_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) < val) l = mid + 1;
else r = mid;
}
return l;
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b, a%b);
}
public static long lcm(long a, long b){
return a / gcd(a,b)*b;
}
public static boolean isPrime(long x){
boolean ok = true;
for(long i=2;i<=Math.sqrt(x);i++){
if(x % i == 0){
ok = false;
break;
}
}
return ok;
}
public static void reverse(int[] array){
reverse(array, 0 , array.length-1);
}
public static void reverse(int[] array, int left, int right) {
if (array != null) {
int i = left;
for(int j = right; j > i; ++i) {
int tmp = array[j];
array[j] = array[i];
array[i] = tmp;
--j;
}
}
}
public static long qpow(long a, long n){
long ret = 1l;
while(n > 0){
if((n & 1) == 1){
ret = ret * a % mod;
}
n >>= 1;
a = a * a % mod;
}
return ret;
}
public static class DSU {
int[] parent;
int[] size;
int n;
public DSU(int n){
this.n = n;
parent = new int[n];
size = new int[n];
for(int i=0;i<n;i++){
parent[i] = i;
size[i] = 1;
}
}
public int find(int p){
while(parent[p] != p){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
public void union(int p, int q){
int root_p = find(p);
int root_q = find(q);
if(root_p == root_q) return;
if(size[root_p] >= size[root_q]){
parent[root_q] = root_p;
size[root_p] += size[root_q];
size[root_q] = 0;
}
else{
parent[root_p] = root_q;
size[root_q] += size[root_p];
size[root_p] = 0;
}
n--;
}
public int getTotalComNum(){
return n;
}
public int getSize(int i){
return size[find(i)];
}
}
public static class FenWick {
int n;
long[] tree;
public FenWick(int n){
this.n = n;
tree = new long[n+1];
}
private void add(int x, long val){
while(x <= n){
tree[x] += val;
x += x&-x;
}
}
private long query(int x){
long ret = 0l;
while(x > 0){
ret += tree[x];
x -= x&-x;
}
return ret;
}
}
public static class fReader {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer tokenizer = new StringTokenizer("");
private static String next() throws IOException{
while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException {return Integer.parseInt(next());}
public static Long nextLong() throws IOException {return Long.parseLong(next());}
public static double nextDouble() throws IOException {return Double.parseDouble(next());}
public static char nextChar() throws IOException {return next().toCharArray()[0];}
public static String nextString() throws IOException {return next();}
public static String nextLine() throws IOException {return reader.readLine();}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
baf87a3a881571dbf16bdc67577efe70
|
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.util.Scanner;
public class CountingRectangles {
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();
long[][] f = new long[1000][1000];
for (int i = 0; i < n; ++i) {
int h = sc.nextInt();
int w = sc.nextInt();
// System.out.println((h-1) + " " + (w-1) + " " + (w * h));
f[h-1][w-1] += (long) w * h;
}
long[][] sums = new long[1000][1000];
sums[0][0] = f[0][0];
for (int i = 1; i < 1000; ++i) {
sums[i][0] = sums[i-1][0] + f[i][0];
sums[0][i] = sums[0][i-1] + f[0][i];
}
for (int i = 1; i < 1000; ++i) {
for (int j = 1; j < 1000; ++j) {
sums[i][j] = f[i][j] + sums[i-1][j] + sums[i][j-1] - sums[i-1][j-1];
}
}
//for (int i = 0; i < 10; ++i) {
// for (int j = 0; j < 10; ++j) {
// System.out.print(sums[i][j] + "\t");
// }
// System.out.println();
//}
for (int i = 0; i < q; ++i) {
int hs = sc.nextInt(), ws = sc.nextInt(), hb = sc.nextInt(), wb = sc.nextInt();
wb -= 2;
hb -= 2;
hs -= 1;
ws -= 1;
// System.out.println(hb +" "+ wb);
long ans = sums[hb][wb] + sums[hs][ws] - sums[hb][ws] - sums[hs][wb];
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
019f7f706e4b6ed7727b3650f7742ddb
|
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.*;
import java.util.*;
public class countingrectangles {
public static void main(String[] args) throws IOException {
Reader in = new Reader();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
for(int z = 0; z < t; z++) {
long[][] arr = new long[1002][1002];
long[][] prefix = new long[1002][1002];
int n = in.nextInt();
int q = in.nextInt();
for(int i = 0; i < n; i++) {
int h = in.nextInt(), w = in.nextInt();
arr[h][w] += (long) h * w;
}
// for(long[] arr1 : arr) {
// for(long num : arr1) {
// out.print(num + " ");
// }
// out.println();
// }
// out.println();
for(int i = 1; i <= 1001; i++) {
for(int j = 1; j <= 1001; j++) {
prefix[i][j] = prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1] + arr[i][j];
}
}
// for(long[] arr1 : prefix) {
// for(long num : arr1) {
// out.print(num + " ");
// }
// out.println();
// }
for(int i = 0; i < q; i++) {
int h1 = in.nextInt(), w1 = in.nextInt(), h2 = in.nextInt(), w2 = in.nextInt();
out.println(prefix[h2 - 1][w2 - 1] - prefix[h2 - 1][w1] - prefix[h1][w2 - 1] + prefix[h1][w1]);
}
}
in.close();
out.close();
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public 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;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
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 (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
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 {
if (din == null)
return;
din.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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
e73d54b185237fb7d0e3363148998bed
|
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.*;
import java.math.BigDecimal;
import java.util.*;
public class Main{
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
static StreamTokenizer st = new StreamTokenizer(bf);
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int t = I();
while(t-->0) {
int n = I(),q = I();
long a[][] = new long[1005][1005];
long pre[][] = new long[1005][1005];
for (int i = 0 ; i < n ; i++) {
int x = I(),b = I();
a[x][b] += x*b;
}
for (int i = 1 ; i <=1000 ; i++)
for (int j = 1 ; j <= 1000 ; j++)
pre[i][j] = a[i][j] + pre[i-1][j] + pre[i][j-1] - pre[i-1][j-1];
while(q-->0) {
int hs = I(),ws = I(),hb = I(),wb = I();
pw.println(pre[hb-1][wb-1] - pre[hs][wb-1] - pre[hb-1][ws] + pre[hs][ws]);
}
}
pw.flush();
}
static int I() throws IOException {
st.nextToken();
return (int)st.nval;
}
static long L() throws IOException {
st.nextToken();
return (long)st.nval;
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
2ac9a571db32dd536af07f2d3eba20bc
|
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.util.*;
import java.io.*;
public class codeforces {
public static void main(String[] args) {
FastReader in = new FastReader();
int t = in.nextInt();
while(t-->0) {
int n = in.nextInt();
int q = in.nextInt();
long[][] dp = new long[1001][1001];
for (int i = 0; i < n; i++) {
int h = in.nextInt();
int w = in.nextInt();
dp[h][w]+= w*h;
}
// now we execute prefix sum
long[][] pre = new long[1001][1001];
for (int i = 0; i<=1000; i++) {
for (int j = 0; j <= 1000; j++) {
pre[i][j] = dp[i][j];
try {pre[i][j]+=pre[i-1][j];}catch (Throwable t1){}
try {pre[i][j]+=pre[i][j-1];}catch (Throwable t2){}
try{pre[i][j]-=pre[i-1][j-1];}catch (Throwable t3) {}
}
}
// after prefix sum, we now look at the actual queries
while(q-->0) {
int h = in.nextInt();
int w = in.nextInt();
int hB = in.nextInt();
int wB = in.nextInt();
System.out.println(pre[hB-1][wB-1] - pre[hB-1][w] - pre[h][wB-1] + pre[h][w]);
}
}
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
ebb7404c76b26cc2c56fc98402226b0d
|
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.util.*;
import java.io.*;
public class Main {
static ContestScanner sc = new ContestScanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws Exception {
int T = sc.nextInt();
for(int i = 0; i < T; i++)solve();
//solve();
pw.flush();
}
static int[][] map = new int[1002][1002];
public static void solve() {
int n = sc.nextInt();
int q = sc.nextInt();
for(int i = 0; i <= 1000; i++){
Arrays.fill(map[i],0);
}
long[][] sum = new long[1002][1002];
for(int i = 0; i < n; i++){
int h = sc.nextInt();
int w = sc.nextInt();
map[h][w]++;
sum[h][w]++;
}
for(int i = 0; i <= 1000; i++){
long cnt = 0;
for(int j = 0; j <= 1000; j++){
cnt += sum[i][j] * (i * j);
if(i != 0){
sum[i][j] = cnt + sum[i-1][j];
}else{
sum[i][j] = cnt;
}
}
}
for(int j = 0; j < q; j++){
int hs = sc.nextInt();
int ws = sc.nextInt();
int he = sc.nextInt();
int we = sc.nextInt();
pw.println(sum[he-1][we-1]-sum[he-1][ws]-sum[hs][we-1]+sum[hs][ws]);
}
}
static class GeekInteger {
public static void save_sort(int[] array) {
shuffle(array);
Arrays.sort(array);
}
public static int[] shuffle(int[] array) {
int n = array.length;
Random random = new Random();
for (int i = 0, j; i < n; i++) {
j = i + random.nextInt(n - i);
int randomElement = array[j];
array[j] = array[i];
array[i] = randomElement;
}
return array;
}
public static void save_sort(long[] array) {
shuffle(array);
Arrays.sort(array);
}
public static long[] shuffle(long[] array) {
int n = array.length;
Random random = new Random();
for (int i = 0, j; i < n; i++) {
j = i + random.nextInt(n - i);
long randomElement = array[j];
array[j] = array[i];
array[i] = randomElement;
}
return array;
}
}
}
/**
* refercence : https://github.com/NASU41/AtCoderLibraryForJava/blob/master/ContestIO/ContestScanner.java
*/
class ContestScanner {
private final java.io.InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public ContestScanner(java.io.InputStream in){
this.in = in;
}
public ContestScanner(java.io.File file) throws java.io.FileNotFoundException {
this(new java.io.BufferedInputStream(new java.io.FileInputStream(file)));
}
public ContestScanner(){
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch (java.io.IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) return buffer[ptr++]; else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext()) throw new java.util.NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new java.util.NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b))
);
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b))
);
}
}
}
}
throw new ArithmeticException(
String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit)
);
}
n = n * 10 + digit;
}else if(b == -1 || !isPrintableChar(b)){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(int length){
long[] array = new long[length];
for(int i=0; i<length; i++) array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){
long[] array = new long[length];
for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length){
int[] array = new int[length];
for(int i=0; i<length; i++) array[i] = this.nextInt();
return array;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){
int[] array = new int[length];
for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length){
double[] array = new double[length];
for(int i=0; i<length; i++) array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){
double[] array = new double[length];
for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width){
long[][] mat = new long[height][width];
for(int h=0; h<height; h++) for(int w=0; w<width; w++){
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width){
int[][] mat = new int[height][width];
for(int h=0; h<height; h++) for(int w=0; w<width; w++){
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width){
double[][] mat = new double[height][width];
for(int h=0; h<height; h++) for(int w=0; w<width; w++){
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width){
char[][] mat = new char[height][width];
for(int h=0; h<height; h++){
String s = this.next();
for(int w=0; w<width; w++){
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
eddd910ca6ecc99b7c40aa84d4d61be3
|
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.util.*;
import java.io.*;
public class e {
//FastReader
public 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;
}
}
//QuickSort
public static class QuickSort {
long array[];
int length;
long array2[];
public void sort(long[] inputArr, long[] intputArr2) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
this.array2=intputArr2;
length = inputArr.length;
quickSort(0, length - 1);
}
public void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
// calculate pivot number, I am taking pivot as middle index number
long pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
// Divide into two arrays
while (i <= j) {
/**
* In each iteration, we will identify a number from left side which
* is greater then the pivot value, and also we will identify a number
* from right side which is less then the pivot value. Once the search
* is done, then we exchange both numbers.
*/
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
//move index to next position on both sides
i++;
j--;
}
}
// call quickSort() method recursively
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
private void exchangeNumbers(int i, int j) {
long temp = array[i];
array[i] = array[j];
array[j] = temp;
temp = array2[i];
array2[i] = array2[j];
array2[j] = temp;
}
}
//Power Algo
public static long power(long x, long n)
{
long pow = 1;
// loop till `n` become 0
while (n > 0)
{
// if `n` is odd, multiply the result by `x`
if ((n & 1) == 1) {
pow *= x;
}
// divide `n` by 2
n = n >> 1;
// multiply `x` by itself
x = x * x;
}
// return result
return pow;
}
//Graph & DFS
public static class Graph
{
int V; //number of nodes
LinkedList<Integer>[] adj; //adjacency list
Graph(int V)
{
this.V = V;
adj = new LinkedList[V];
for (int i = 0; i < adj.length; i++)
adj[i] = new LinkedList<Integer>();
}
public void addEdge(int v, int w)
{
adj[v].add(w); //adding an edge to the adjacency list (edges are bidirectional in this example)
}
public List<Integer> DFS(int n)
{
boolean nodes[] = new boolean[V];
List<Integer> ll=new ArrayList<>();
ll.add(0);
Stack<Integer> stack = new Stack<>();
stack.push(n); //push root node to the stack
int a = 0;
while(!stack.empty())
{
n = stack.peek(); //extract the top element of the stack
stack.pop(); //remove the top element from the stack
if(nodes[n] == false)
{
nodes[n] = true;
}
for (int i = 0; i < adj[n].size(); i++) //iterate through the linked list and then propagate to the next few nodes
{
a = adj[n].get(i);
if (!nodes[a]) //only push those nodes to the stack which aren't in it already
{
stack.push(a);
ll.add(a);
//push the top element to the stack
}
}
}
return ll;
}
}
//Dijkstra's Algo
public static class Dijkstra {
// Member variables of this class
private int dist[];
private Set<Integer> settled;
private PriorityQueue<Node> pq;
// Number of vertices
private int V;
List<List<Node> > adj;
// Constructor of this class
public Dijkstra(int V)
{
// This keyword refers to current object itself
this.V = V;
dist = new int[V];
settled = new HashSet<Integer>();
pq = new PriorityQueue<Node>(V, new Node());
}
// Method 1
// Dijkstra's Algorithm
public void dijkstra(List<List<Node>> adj2, int src)
{
this.adj = adj2;
for (int i = 0; i < V; i++)
dist[i] = Integer.MAX_VALUE;
// Add source node to the priority queue
pq.add(new Node(src, 0));
// Distance to the source is 0
dist[src] = 0;
while (settled.size() != V) {
// Terminating condition check when
// the priority queue is empty, return
if (pq.isEmpty())
return;
// Removing the minimum distance node
// from the priority queue
int u = pq.remove().node;
// Adding the node whose distance is
// finalized
if (settled.contains(u))
// Continue keyword skips exwcution for
// following check
continue;
// We don't have to call e_Neighbors(u)
// if u is already present in the settled set.
settled.add(u);
e_Neighbours(u);
}
}
// Method 2
// To process all the neighbours
// of the passed node
private void e_Neighbours(int u)
{
int edgeDistance = -1;
int newDistance = -1;
// All the neighbors of v
for (int i = 0; i < adj.get(u).size(); i++) {
Node v = adj.get(u).get(i);
// If current node hasn't already been processed
if (!settled.contains(v.node)) {
edgeDistance = v.cost;
newDistance = dist[u] + edgeDistance;
// If new distance is cheaper in cost
if (newDistance < dist[v.node])
dist[v.node] = newDistance;
// Add the current node to the queue
pq.add(new Node(v.node, dist[v.node]));
}
}
}
}
//Necessary Supporting F/n for Dijkstra's Algo
public static class Node implements Comparator<Node> {
// Member variables of this class
public int node;
public int cost;
// Constructors of this class
// Constructor 1
public Node() {}
// Constructor 2
public Node(int node, int cost)
{
// This keyword refers to current instance itself
this.node = node;
this.cost = cost;
}
// Method 1
@Override public int compare(Node node1, Node node2)
{
if (node1.cost < node2.cost)
return -1;
if (node1.cost > node2.cost)
return 1;
return 0;
}
}
//BinarySearch to find exact or closest element
public static class BinarySearch{
public static int k=0;
public static int binarySearch(long arr[], int first, int last, long key){
k = (first + last)/2;
while( first <= last ){
if ( arr[k] < key ){
first = k + 1;
}else if (arr[k] == key ){
break;
}else{
last = k - 1;
}
k = (first + last)/2;
}
return k;
}
}
//GCD
public static int gcd(int a,int b)
{
int min=Math.min(a, b);
int max=Math.max(a, b);
int mod=max%min;
if(mod==0)
return min;
else
return gcd(mod,min);
}
//highest Power of 2 less than or equal to the no.
public static int highestPowerof2(int n)
{
int p = (int)(Math.log(n) /
Math.log(2));
return (int)power(2,p);
}
public static void main(String[] args) {
try {
FastReader fr=new FastReader();
QuickSort qs=new QuickSort();
StringBuilder sb=new StringBuilder();
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
BinarySearch bs=new BinarySearch();
int t=fr.nextInt();
while(t-->0)
{
int n=fr.nextInt();
int q=fr.nextInt();
int nmm=1005;
long pre[][]=new long[nmm][nmm];
for(int i=0;i<n;i++) {
int h = fr.nextInt();
int w = fr.nextInt();
pre[h][w] += h*w;
}
for(int i=1;i<nmm;i++)
{
for(int j=1;j<nmm;j++)
{
pre[i][j]+=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1];
}
}
long sum=0;
int h1=0,h2=0,w1=0,w2=0;
for(int i=0;i<q;i++)
{
h1=fr.nextInt();
w1=fr.nextInt();
h2=fr.nextInt();
w2=fr.nextInt();
h2--;
w2--;
sum=pre[h2][w2]-pre[h2][w1]-pre[h1][w2]+pre[h1][w1];
sb.append(sum+"\n");
}
}
output.write(sb+"");
output.close();
}
catch(Exception e)
{
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
06dc557527d122b677c9c51542669e3d
|
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.util.Scanner;
public class Solution{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while(t-- > 0){
int n = scanner.nextInt();
int q = scanner.nextInt();
int max = 1005;
long[][] area = new long[max][max];
long[][] prefix = new long[max][max];
for(int i = 0 ; i < n ; i++){
int l = scanner.nextInt();
int b = scanner.nextInt();
area[l][b] += (long) l * b;
}
for(int i = 1 ; i < max ; i++){
for(int j = 1; j < max ; j++){
prefix[i][j] = area[i][j] + prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1];
}
}
while(q-- > 0){
int ls = scanner.nextInt();
int ws = scanner.nextInt();
int lb = scanner.nextInt();
int wb = scanner.nextInt();
long ans = prefix[lb - 1][wb - 1] - prefix[lb - 1][ws] - prefix[ls][wb - 1] + prefix[ls][ws];
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
595274028072082f1718fc8da20ac52d
|
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.sort;
public class Codeforces {
public static void main(String[] args) {
FastReader fastReader = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int tt = fastReader.nextInt();
while (tt-- > 0) {
int N = fastReader.nextInt(), Q = fastReader.nextInt();
long[][] preSum = new long[1001][1001];
for (int i = 0; i < N; i++) {
int x = fastReader.nextInt(), y = fastReader.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 = fastReader.nextInt(), y1 = fastReader.nextInt(), x2 = fastReader.nextInt(), y2 = fastReader.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();
}
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final long LMAX = 9223372036854775807L;
static Random __r = new Random();
// math util
static int minof(int a, int b, int c) {
return min(a, min(b, c));
}
static int minof(int... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return min(x[0], x[1]);
if (x.length == 3)
return min(x[0], min(x[1], x[2]));
int min = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] < min)
min = x[i];
return min;
}
static long minof(long a, long b, long c) {
return min(a, min(b, c));
}
static long minof(long... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return min(x[0], x[1]);
if (x.length == 3)
return min(x[0], min(x[1], x[2]));
long min = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] < min)
min = x[i];
return min;
}
static int maxof(int a, int b, int c) {
return max(a, max(b, c));
}
static int maxof(int... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return max(x[0], x[1]);
if (x.length == 3)
return max(x[0], max(x[1], x[2]));
int max = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] > max)
max = x[i];
return max;
}
static long maxof(long a, long b, long c) {
return max(a, max(b, c));
}
static long maxof(long... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return max(x[0], x[1]);
if (x.length == 3)
return max(x[0], max(x[1], x[2]));
long max = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] > max)
max = x[i];
return max;
}
static int powi(int a, int b) {
if (a == 0)
return 0;
int ans = 1;
while (b > 0) {
if ((b & 1) > 0)
ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
static long powl(long a, int b) {
if (a == 0)
return 0;
long ans = 1;
while (b > 0) {
if ((b & 1) > 0)
ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
static int fli(double d) {
return (int) d;
}
static int cei(double d) {
return (int) ceil(d);
}
static long fll(double d) {
return (long) d;
}
static long cel(double d) {
return (long) ceil(d);
}
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static int[] exgcd(int a, int b) {
if (b == 0)
return new int[] { 1, 0 };
int[] y = exgcd(b, a % b);
return new int[] { y[1], y[0] - y[1] * (a / b) };
}
static long[] exgcd(long a, long b) {
if (b == 0)
return new long[] { 1, 0 };
long[] y = exgcd(b, a % b);
return new long[] { y[1], y[0] - y[1] * (a / b) };
}
static int randInt(int min, int max) {
return __r.nextInt(max - min + 1) + min;
}
static long mix(long x) {
x += 0x9e3779b97f4a7c15L;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebL;
return x ^ (x >> 31);
}
public static boolean[] findPrimes(int limit) {
assert limit >= 2;
final boolean[] nonPrimes = new boolean[limit];
nonPrimes[0] = true;
nonPrimes[1] = true;
int sqrt = (int) Math.sqrt(limit);
for (int i = 2; i <= sqrt; i++) {
if (nonPrimes[i])
continue;
for (int j = i; j < limit; j += i) {
if (!nonPrimes[j] && i != j)
nonPrimes[j] = true;
}
}
return nonPrimes;
}
// array util
static void reverse(int[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
int swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(long[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
long swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(double[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
double swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(char[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
char swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void shuffle(int[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
int swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(long[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
long swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(double[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
double swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void rsort(int[] a) {
shuffle(a);
sort(a);
}
static void rsort(long[] a) {
shuffle(a);
sort(a);
}
static void rsort(double[] a) {
shuffle(a);
sort(a);
}
static int[] copy(int[] a) {
int[] ans = new int[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
static long[] copy(long[] a) {
long[] ans = new long[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
static double[] copy(double[] a) {
double[] ans = new double[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
static char[] copy(char[] a) {
char[] ans = new char[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
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[] ria(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = Integer.parseInt(next());
return a;
}
long nextLong() {
return Long.parseLong(next());
}
long[] rla(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = Long.parseLong(next());
return a;
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
936b62975445efaca72a316a6dcb815f
|
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
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Ide
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- > 0) {
String [] s1 = br.readLine().split(" ");
int n = Integer.parseInt(s1[0]);
int q = Integer.parseInt(s1[1]);
long [][] prefixSum = new long[1001][1001];
//storing the heights and width of the rectangles
for(int i=0;i<n;i++) {
String [] s = br.readLine().split(" ");
int h = Integer.parseInt(s[0]);
int w = Integer.parseInt(s[1]);
prefixSum[h][w] += h*w;
}
//actually counting the 2D prefix sums
for(int i=1;i<1001;i++) {
for(int j=1;j<1001;j++) {
prefixSum[i][j] = prefixSum[i-1][j] + prefixSum[i][j-1] - prefixSum[i-1][j-1] + prefixSum[i][j];
//System.out.print(prefixSum[i][j] + " ");
}
//System.out.println();
}
while(q-- > 0) {
String [] queris = br.readLine().split(" ");
int a = Integer.parseInt(queris[0]);
int b = Integer.parseInt(queris[1]);
int c = Integer.parseInt(queris[2]);
int d = Integer.parseInt(queris[3]);
int p = a+1;
int q1 = b+1;
int r = c-1;
int s = d-1;
long ans = prefixSum[r][s] - prefixSum[r][q1-1] - prefixSum[p-1][s] + prefixSum[p-1][q1-1];
if(ans < 0) System.out.println(0);
else 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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
bf518bee6896d71dbb41c69682a191d5
|
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
|
//codeforces
//package someAlgorithms;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.io.File;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
String[] strNums = br.readLine().split(" ");
int t=Integer.parseInt(strNums[0]);
// int ot=t;
// FileWriter fw = new FileWriter("C:\\Users\\Lenovo\\Desktop\\output.txt");
// fw.write("");
// fw.close();
while(t-->0) {
//todo
String[] strNums1 = br.readLine().split(" ");
long n=Long.parseLong(strNums1[0]);
long q=Long.parseLong(strNums1[1]);
// long l1=Long.parseLong(strNums1[2]);
// long l2=Long.parseLong(strNums1[3]);
// long d=Long.parseLong(strNums1[4]);
// String str = br.readLine();
// String str2 = br.readLine();
// Long[] arr = new Long[(int)n];
// StringTokenizer tk = new StringTokenizer(br.readLine().trim());
// for(int i=0;i<n;i++) {
// arr[i]=Long.parseLong(tk.nextToken());
// }
long[][] pref = new long[(int)1001][(int)1001];
for(int i=0;i<n;i++) {
String[] strNums2 = br.readLine().split(" ");
long a=Long.parseLong(strNums2[0]);
long b=Long.parseLong(strNums2[1]);
pref[(int)a][(int)b]+=(long)a*b;
}
long sum = 0;
for(int i=0;i<1001;i++) {
sum+=pref[0][i];
pref[0][i]=sum;
}
sum=0;
for(int i=0;i<1001;i++) {
sum+=pref[i][0];
pref[i][0]=sum;
}
for(int i=1;i<1001;i++) {
for(int j=1;j<1001;j++) {
// sum+=pref[i][j];
pref[i][j]=pref[i][j-1]+pref[i-1][j]-pref[i-1][j-1]+pref[i][j];
}
}
// System.out.println(pref[1000][999]+pref[999][1000]-pref[999][999]+);
for(int i=0;i<q;i++) {
String[] strNums3 = br.readLine().split(" ");
long h1=Long.parseLong(strNums3[0]);
long w1=Long.parseLong(strNums3[1]);
long h2=Long.parseLong(strNums3[2]);
long w2=Long.parseLong(strNums3[3]);
long ans=0;
if(h2>=h1 && w2>=w1) {
ans+=pref[(int)h2-1][(int)w2-1]-pref[(int)h1][(int)w2-1]-pref[(int)h2-1][(int)w1]+pref[(int)h1][(int)w1];
}
System.out.println(ans);
}
}
}
}
/*
bw.write(n+"");
bw.newLine();
bw.flush();
System.out.println("Case #"+(ot-t)+":"+" YES");
FileWriter myWriter = new FileWriter("C:\\Users\\Lenovo\\Desktop\\output.txt",true);
myWriter.write("Case #"+(ot-t)+": "+"YES"+"\n");
myWriter.close();
*/
//class Pair{
// public long a=0;
// public long b=0;
// public Pair(long val,long id){
// this.a=val;
// this.b=id;
// }
//
//}
//class Pair{
// public long val;
// public long id;
// public Pair(long val,long id){
// this.val=val;
// this.id=id;
// }
//
//}
//class Node{
// public int val;
//// public Node left=null;
//// public Node right=null;
// ArrayList<Node> children = new ArrayList<>();
//
//}
//class Comp implements Comparator<Pair>{
// public int compare(Pair p1,Pair p2) {
// if(p1.val<p2.val) {
// return -1;
// }
// if(p1.val>p2.val){
// return 1;
// }
// else return 0; //MUST WRITE THIS RETURN 0 FOR EQUAL CASE SINCE GIVE RUNTIME ERROR IN SOME COMPLIERS
// }
//}
//if take gcd of whole array the take default gcd=0 and keep doing gcd of 2 elements of array!
//public static int gcd(int a, int b){
// if(b==0){
// return a;
// }
// return gcd(b,a%b);
//}
//
//ArrayList<Long>[] adjlist = new ArrayList[(int)n+1]; //array of arraylist
//for(int i=0;i<n;i++) {
// String[] strNums2 = br.readLine().split(" ");
// long a=Long.parseLong(strNums2[0]);
// long b=Long.parseLong(strNums2[1]);
//
// adjlist[(int)a].add(b);
// adjlist[(int)b].add(a);
//}
//int[][] vis = new int[(int)n+1][(int)n+1];
//OR can make list of list :-
//List<List<Integer>> adjlist = new ArrayList<>();
//for(int i=0;i<n;i++){
// adjlist.add(new ArrayList<>());
//}
//OR 1-D vis array
//int[] vis = new int[(int)n+1];
/*
Long[] arr = new Long[(int)n];
StringTokenizer tk = new StringTokenizer(br.readLine().trim());
for(int i=0;i<n;i++) {
arr[i]=Long.parseLong(tk.nextToken());
}
Long[][] arr = new Long[(int)n][(int)m];
for(int i=0;i<n;i++) {
String[] strNums2 = br.readLine().split(" ");
for(int j=0;j<m;j++) {
arr[i][j]=Long.parseLong(strNums2[j]);
}
}
4
4 4 3 2
4 4 4 3
Main m = new Main(); //no need since pair class main class ne niche banao
Pair p = m.new Pair(i,i+1);
li.add(p);
*/
//double num = 3.9634;
//double onum=num;
//num = (double)((int)(num*100))/100;
//double down = (num);
//float up =(float) down; //if take up as double then will get large value when add (3.96+0.01)!!
//if(down<onum) {
// up=(float)down+(float)0.01;
//// System.out.println(((float)3.96+(float)0.01));
//// System.out.println(3.96+0.01);//here both double, so output double , so here get large output other than 3.97!!
//}
////in c++ 3.96+0.01 is by default 3.97 but in java need to type cast to float to get this output!!
//System.out.println(down +" "+up);
/*
#include <iostream>
#include <string>
#include<vector>
#include<queue>
#include<utility>
#include<limits.h>
#include <unordered_set>
#include<algorithm>
using namespace std;
*/
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
74b22f7af12a79941070fe99024affa5
|
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.util.*;
import java.io.*;
public class Main {
static StringBuilder sb;
static dsu dsu;
static long fact[];
static int mod = (int) (1e9 + 7);
static void print(int[]arr){
for(int i=0;i<arr.length;i++)System.out.print(arr[i]+" ");
System.out.println();
}
static void print(long[]arr){
for(int i=0;i<arr.length;i++)System.out.print(arr[i]+" ");
System.out.println();
}
static void print(ArrayList<Integer>ls){
System.out.println(ls);
}
static void print(int t){
System.out.println(t);
}
static void print(long t){
System.out.println(t);
}
static void build(int node, int start, int end,int[]tree,int[]arr) {
if (start == end) {
tree[node] = arr[start];
} else {
int mid = (start + end) / 2;
int left = node * 2;
int right = node * 2 + 1;
build(left, start, mid,tree,arr);
build(right, mid + 1, end,tree,arr);
tree[node] = Math.max(tree[left], tree[right]);
}
}
static int query(int node, int start, int end, int l, int r,int[]tree,int[]arr) {
if (end < l || r < start)return 0;
if (start == end) {
return tree[node];
} else if (l <= start && end <= r) {
return tree[node];
} else {
int mid = (start + end) / 2;
int left = query(node * 2, start, mid, l, r,tree,arr);
int right = query(node * 2 + 1, mid + 1, end, l, r,tree,arr);
return (left+ right);
}
}
static void update(int node, int start, int end, int pos, int val,int[]tree,int[]arr) {
if (start == end) {
arr[start] += val;
tree[node] += val;
} else {
int mid = (start + end) / 2;
if ( start <= pos && pos <= mid) {
update(node * 2, start, mid, pos, val,tree,arr);
} else {
update(node * 2 + 1, mid + 1, end, pos, val,tree,arr);
}
tree[node] = (tree[node * 2]+tree[node * 2 + 1]);
}
}
static void swap(int[]arr,int i,int j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
static void solve() {
int n=i();
int q=i();
ArrayList<Long>[]arr=new ArrayList[1001];
for(int i=0;i<=1000;i++)arr[i]=new ArrayList<>();
for(int i=0;i<n;i++){
int h=i();
long w=l();
arr[h].add(w);
}
// h[i][j] denotes sum of areas of rec upto h<=i and w <=j
long[][]h=new long[1001][1001];
for(int i=0;i<1001;i++){
for(int j=0;j<1001;j++){
if(i==0) {
for(long t:arr[i]){
if(t<=j){
h[i][j]+=(t*(long)i);
}
}
}
else{
h[i][j]=h[i-1][j];
for(long t:arr[i]){
if(t<=j){
h[i][j]+=(t*(long)i);
}
}
}
}
}
for(int i=0;i<q;i++){
int l=i();
int w1=i();
int r=i();
int w2=i();
long ans=0;
ans+=(h[r-1][(int)w2-1]-h[r-1][(int)w1]);
if(l>0)ans-=(h[l][(int)w2-1]-h[l][(int)w1]);
sb.append(ans+"\n");
}
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
// fact=new long[(int)1e6+10];
// fact[0]=fact[1]=1;
// for(int i=2;i<fact.length;i++)
// { fact[i]=((long)(i%mod)*(long)(fact[i-1]%mod))%mod; }
int cnt=1;
while (test-- > 0) {
solve();
cnt++;
}
System.out.println(sb);
}
//**************NCR%P******************
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long res = fact[n] % mod;
// System.out.println(res);
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
// System.out.println(res);
return res;
}
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) ;
y--;
}
x = (x * x);
y = y / 2;
}
return res;
}
//**************END******************
// *************Disjoint set
// union*********//
static class dsu {
int parent[];
dsu(int n) {
parent = new int[n];
for (int i = 0; i < n; i++)
parent[i] = i;
}
int find(int a) {
if (parent[a] ==a)
return a;
else {
int x = find(parent[a]);
parent[a] = x;
return x;
}
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
parent[b] = a;
}
}
//**************PRIME FACTORIZE **********************************//
static TreeMap<Integer, Integer> prime(long n) {
TreeMap<Integer, Integer> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
int nt = 0;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i, nt);
}
}
if (n != 1)
h.put((int) n, 1);
return h;
}
//****CLASS PAIR ************************************************
static class Pair implements Comparable<Pair> {
long h;
long w;
Pair( long h,long w) {
this.h = h;
this.w = w;
}
public int compareTo(Pair o) {
return (int) (this.h - o.h);
}
}
//****CLASS PAIR **************************************************
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private 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 Int() {
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 String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
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 printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static long[] sort(long[] a2) {
int n = a2.length;
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static char[] sort(char[] a2) {
int n = a2.length;
ArrayList<Character> l = new ArrayList<>();
for (char i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static long pow(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 != 0) {
res = (res * x);// % modulus;
y--;
}
x = (x * x);// % modulus;
y = y / 2;
}
return res;
}
//GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
// ******LOWEST COMMON MULTIPLE
// *********************************************
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
//INPUT PATTERN********************************************************
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
public static int[] readArrayi(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
dc10ffaf4c012369a25af0b00047707a
|
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class E {
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) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
long[] getArr(int n) {
long[] res = new long[n];
for(int i = 0; i < n; i++){
res[i] = Long.parseLong(next());
}
return res;
}
}
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int j = fs.nextInt();
while(j-- > 0){
int rec = fs.nextInt();
int que = fs.nextInt();
long[][] count = new long[1002][1002];
for(int i = 0; i < rec; i++){
int h = fs.nextInt();
int w = fs.nextInt();
update(h,w,count);
}
// System.out.println("Completed at here");
for(int i = 0; i < que; i++){
int h1 = fs.nextInt();
int w1 = fs.nextInt();
int h2 = fs.nextInt() -1;
int w2 = fs.nextInt() -1;
long val1 = get(h2,w2,count);
long val2 = get(h2,w1,count);
long val3 = get(h1,w2,count);
long val4 = get(h1,w1,count);
System.out.println(val1 - val2 -val3 + val4);
}
}
}
public static void update(int h, int w, long[][] count){
long val = (long)h * (long)w;
for(int i = h; i <= 1001; i += (i &-i) ){
for(int j = w; j <= 1001; j += (j & -j)){
count[i][j] += val;
}
}
}
public static long get(int h, int w, long[][] count){
long res = 0l;
for(int i = h; i > 0; i -= (i &-i) ){
for(int j = w; j > 0; j -= (j & -j)){
res += count[i][j];
}
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
aaeb976ef08da55599d29cc8cc24a67a
|
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.concurrent.ThreadLocalRandom;
/*
3 2 1 4
3 1 4 2 //1
3 4 2 1 //2
4 2 1 3 //3
000
001
010
011
100
101
110
111
1 2 3 4 5 6
1 3 6 10 15 21
*/
public class CF {
private static void sport(int[][] a, int[][] q) {
int n = a.length;
long[][] val = new long[1003][1003];
for (int[] ints : a) {
int h = ints[0];
int w = ints[1];
val[h][w] += w;
}
for (int i = 0; i < val.length; i++) {
for (int j = 1; j < val[0].length; j++) {
val[i][j] += val[i][j - 1];
}
}
for (int[] ints : q) {
long ans = 0;
int hs = ints[0];
int ws = ints[1];
int hb = ints[2];
int wb = ints[3];
for (int i = hs + 1; i < hb; i++) {
long curr = val[i][wb-1] - val[i][ws];
ans += (long) ((long) curr * i);
}
System.out.println(ans);
}
}
static void shuffleArray(int[] ar) {
// If running on Java 6 or older, use `new Random()` on RHS here
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
int q = sc.nextInt();
int[][] a = new int[n][2];
for (int j = 0; j < n; j++) {
a[j] = sc.readArrayInt(2);
}
int[][] req = new int[q][4];
for (int j = 0; j < q; j++) {
req[j] = sc.readArrayInt(4);
}
sport(a, req);
}
}
// 1 2 3 4 5 6 | 2
static void swap(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
static class BIT {
// The size of the array holding the Fenwick tree values
final int N;
// This array contains the Fenwick tree ranges
private long[] tree;
// Create an empty Fenwick Tree with 'sz' parameter zero based.
public BIT(int sz) {
tree = new long[(N = sz + 1)];
}
// Construct a Fenwick tree with an initial set of values.
// The 'values' array MUST BE ONE BASED meaning values[0]
// does not get used, O(n) construction.
public BIT(long[] values) {
if (values == null) {
throw new IllegalArgumentException("Values array cannot be null!");
}
N = values.length;
values[0] = 0L;
// Make a clone of the values array since we manipulate
// the array in place destroying all its original content.
tree = values.clone();
for (int i = 1; i < N; i++) {
int parent = i + lsb(i);
if (parent < N) {
tree[parent] += tree[i];
}
}
}
// Returns the value of the least significant bit (LSB)
// lsb(108) = lsb(0b1101100) = 0b100 = 4
// lsb(104) = lsb(0b1101000) = 0b1000 = 8
// lsb(96) = lsb(0b1100000) = 0b100000 = 32
// lsb(64) = lsb(0b1000000) = 0b1000000 = 64
private static int lsb(int i) {
// Isolates the lowest one bit value
return i & -i;
// An alternative method is to use the Java's built in method
// return Integer.lowestOneBit(i);
}
// Computes the prefix sum from [1, i], O(log(n))
private long prefixSum(int i) {
long sum = 0L;
while (i != 0) {
sum += tree[i];
i &= ~lsb(i); // Equivalently, i -= lsb(i);
}
return sum;
}
// Returns the sum of the interval [left, right], O(log(n))
public long sum(int left, int right) {
if (right < left) {
throw new IllegalArgumentException("Make sure right >= left");
}
return prefixSum(right) - prefixSum(left - 1);
}
// Get the value at index i
public long get(int i) {
return sum(i, i);
}
// Add 'v' to index 'i', O(log(n))
public void add(int i, long v) {
while (i < N) {
tree[i] += v;
i += lsb(i);
}
}
// Set index i to be equal to v, O(log(n))
public void set(int i, long v) {
add(i, v - sum(i, i));
}
@Override
public String toString() {
return java.util.Arrays.toString(tree);
}
}
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());
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
int[] readArrayInt(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());
}
double nextDouble() {
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
74b733329a93b261a9c8f5cc5604e943
|
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.util.*;
import java.io.*;
public class Main {
static int[] parent, size;
public static void main(String[] args) throws IOException {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int q = sc.nextInt();
long[][] arr = new long[1001][1001];
while (n-- > 0) {
int x = sc.nextInt();
int y = sc.nextInt();
arr[x][y] += x * y;
}
for (int i = 1; i < arr.length; i++) {
for (int j = 1; j < arr[0].length; j++) {
arr[i][j] += arr[i - 1][j] + arr[i][j - 1] - arr[i - 1][j - 1];
}
}
while (q-- > 0) {
int h1 = sc.nextInt();
int w1 = sc.nextInt();
int h2 = sc.nextInt();
int w2 = sc.nextInt();
if (h2 - h1 == 1 || w2 - w1 == 1)
pw.println(0);
else
pw.println(query(h1 + 1, w1 + 1, h2 - 1, w2 - 1, arr));
}
}
pw.close();
}
static long query(int h1, int w1, int h2, int w2, long[][] arr) {
return (arr[h2][w2] - arr[h1 - 1][w2] - arr[h2][w1 - 1] + arr[h1 - 1][w1 - 1]);
}
static long getNum(long n) {
return (n * (n - 1)) / 2;
}
static long get(int[] a, int[] b) {
int[] ans = new int[a.length];
for (int i = 0; i < ans.length; i++) {
if (a[i] == b[i])
ans[i] = a[i];
else
ans[i] = 3 - (a[i] + b[i]);
}
return HashArr(ans);
}
static int find(int a) {
if (a == parent[a])
return a;
return parent[a] = find(parent[a]);
}
static void union(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
if (size[a] < size[b])
parent[a] = b;
else
parent[b] = a;
}
static long HashArr(int[] arr) {
int p = 7;
long hash = 0;
long power = 1;
for (int i = 0; i < arr.length; i++) {
hash = (hash + (arr[i] + 1) * power);
power = (power * p);
}
return hash;
}
static long[] HashStr(char[] arr) {
long[] hash = new long[arr.length];
int p = 31;
int m = 1000000007;
long hashValue = 0;
long power = 1;
for (int i = 0; i < arr.length; i++) {
hashValue = (hashValue + (arr[i] - 'a' + 1) * power) % m;
power = (power * p) % m;
hash[i] = hashValue;
}
return hash;
}
static int log2(int n) {
return (int) (Math.log(n) / Math.log(2));
}
static int[] sieve() {
int n = (int) 1e5;
int[] arr = new int[n];
for (int i = 2; i < arr.length; i++) {
for (int j = i; j < arr.length; j += i) {
if (arr[j] == 0)
arr[j] = i;
}
}
return arr;
}
static void shuffle(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
int tmp = arr[i];
arr[i] = arr[r];
arr[r] = tmp;
}
}
static void sort(int[] arr) {
shuffle(arr);
Arrays.sort(arr);
}
static long getSum(int[] arr) {
long sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
static int getMin(int[] arr) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
min = Math.min(min, arr[i]);
}
return min;
}
static int getMax(int[] arr) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
max = Math.max(max, arr[i]);
}
return max;
}
static boolean isEqual(int[] a, int[] b) {
if (a.length != b.length)
return false;
for (int i = 0; i < b.length; i++) {
if (a[i] != b[i])
return false;
}
return true;
}
static void reverse(int[] arr, int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
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 int gcd(int x, int y) {
if (x == 0)
return y;
return gcd(y % x, x);
}
static HashMap<Integer, Integer> Hash(int[] arr) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i : arr) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
return map;
}
static HashMap<Character, Integer> Hash(char[] arr) {
HashMap<Character, Integer> map = new HashMap<>();
for (char i : arr) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
return map;
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
public static long combination(long n, long r) {
return factorial(n) / (factorial(n - r) * factorial(r));
}
static long factorial(Long n) {
if (n == 0)
return 1;
return (n % mod) * (factorial(n - 1) % mod) % mod;
}
static boolean isPalindrome(char[] str, int i, int j) {
while (i < j) {
if (str[i] != str[j])
return false;
i++;
j--;
}
return true;
}
public static int setBit(int mask, int idx) {
return mask | (1 << idx);
}
public static boolean checkBit(int mask, int idx) {
return (mask & (1 << idx)) != 0;
}
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 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 int[] NextIntArray(int n) throws IOException {
int[] arr = new int[n + 1];
for (int i = 1; i < arr.length; i++) {
arr[i] = nextInt();
}
return arr;
}
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();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
public static String toString(int[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + " ");
}
return sb.toString().trim();
}
public static String toString(int[] arr, String separator) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + separator);
}
return sb.toString().trim();
}
public static String toString(long[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + " ");
}
return sb.toString().trim();
}
public static String toString(ArrayList arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.size(); i++) {
sb.append(arr.get(i) + " ");
}
return sb.toString().trim();
}
public static String toString(int[][] arr) {
StringBuilder sb = new StringBuilder();
for (int[] i : arr) {
sb.append(toString(i) + "\n");
}
return sb.toString();
}
public static String toString(boolean[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + " ");
}
return sb.toString().trim();
}
public static String toString(char[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]);
}
return sb.toString().trim();
}
public static <T> String toString(T[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + " ");
}
return sb.toString().trim();
}
static int inf = Integer.MAX_VALUE;
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
a7743f9b0abc31fa0c772386907dbef4
|
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
|
// package FirstPackage;
import java.util.*;
import java.lang.Math;
import java.io.* ;
public class Account {
public static class Pair<Object1 ,Object2> {
Object1 key ;
Object2 val ;
Pair(Object1 key ,Object2 val) {
this.key = key ;
this.val = val ;
}
Pair() {}
}
// static int mod = 998244353 ;
static int mod = (int)(1e9+7) ;
public static void main(String[] args) {
// Scanner sc = new Scanner(System.in) ;
FastReader sc = new FastReader() ;
StringBuilder res = new StringBuilder() ;
// int t = 1 ;
int t = sc.nextInt() ;
while(t-- > 0) {
int n = sc.nextInt() ;
int q = sc.nextInt() ;
int hmax = -1 ,wmax = -1 ;
int[][] arr = new int[n][2] ;
int[][] qrs = new int[q][4] ;
for(int i=0 ;i<n ;i++) {
arr[i][0] = sc.nextInt() ;
arr[i][1] = sc.nextInt() ;
hmax = Math.max(hmax, arr[i][0]) ;
wmax = Math.max(wmax, arr[i][1]) ;
}for(int i=0 ;i<q ;i++) {
for(int j=0 ;j<4 ;j++) {
qrs[i][j] = sc.nextInt() ;
if(j%2 == 0)
hmax = Math.max(hmax, qrs[i][j]) ;
else
wmax = Math.max(wmax, qrs[i][j]) ;
}
}
long[][] count = new long[hmax+1][wmax+1] ;
for(int i=0 ;i<n ;i++) {
int h = arr[i][0] ,w = arr[i][1] ;
count[h][w] += (long)h * w ;
}for(int i=1 ;i<=hmax ;i++) {
for(int j=0 ;j<=wmax ;j++) {
count[i][j] += count[i-1][j] ;
}
}for(int j=1 ;j<=wmax ;j++) {
for(int i=0 ;i<=hmax ;i++) {
count[i][j] += count[i][j-1] ;
}
}for(int i=0 ;i<q ;i++) {
int hs = qrs[i][0] ,ws = qrs[i][1] ,hb = qrs[i][2] ,wb = qrs[i][3] ;
long totArea = count[hb-1][wb-1] - count[hs][wb-1] - count[hb-1][ws] + count[hs][ws] ;
res.append(totArea+"\n") ;
}
}
System.out.println(res);
}
/*
* 1 1 1 2 2 3 4 4 4 5
* 100
*
*
*/
public static long lcmOfArray(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 = lcmOfArray(arr, idx+1);
return (a*b/gcd(a,b));
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public static long fact(int n) {
if(n == 0 || n == 1) {
return 1 ;
}
// int mod = (int)(1e9+7) ;
long fac = 1 ;
while(n > 1) {
fac = mul(fac ,n--) ;
fac = (fac * n--)%mod ;
}
return fac ;
}
// _xor from 1 to n
public static int xor(int n) {
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
return 0;
}
// mod operations :
public static long add(long a, long b) {
return (a+b)%mod;
}
public static long sub(long a, long b) {
return ((a-b)%mod+mod)%mod;
}
public static long mul(long a, long b) {
return (a*b)%mod;
}
/*
* Can instantiate SGTNode object like
* (i) root = new SGTNode() ;
* root.buildTree(nums) ;
* or
* (ii) root = new SGTNode(nums) ;
*
* where nums is an array
*/
public static class SGTNode {
int val ,low ,high ;
SGTNode left ,right ;
public SGTNode() {}
public SGTNode(int[] arr){
this.buildTree(arr) ;
}
public SGTNode(int val ,int low ,int high) {
this.val = val ;
this.low = low ;
this.high = high ;
}
public SGTNode(int val ,int low ,int high ,SGTNode left ,SGTNode right) {
this.val = val ;
this.low = low ;
this.high = high ;
this.left = left ;
this.right = right ;
}
public void buildTree(int[] arr) { // O(n)
SGTNode root = buildTreeHelper(this ,arr ,0 ,arr.length-1) ;
// return root ;
}
public SGTNode buildTreeHelper(SGTNode root ,int[] arr ,int low ,int high) {
if(high == low) {
// return new SGTNode(arr[low] ,low ,high) ;
root.val = arr[low] ;
root.low = low ;
root.high = high ;
return root ;
}
int mid = low + (high - low) / 2 ;
root.left = buildTreeHelper(new SGTNode() ,arr ,low ,mid) ;
root.right = buildTreeHelper(new SGTNode() ,arr ,mid+1 ,high) ;
root.val = root.left.val + root.right.val ; // Computable
root.low = low ;
root.high = high ;
return root ;
// return new SGTNode(left.val+right.val ,low ,high ,left ,right) ;
}
public void update(int ind ,int val) { // O(logn)
updateHelper(this ,ind ,val) ;
}
public void updateHelper(SGTNode root ,int ind ,int val) {
if(root.low == root.high) {
root.val = val ;
return ;
}
int mid = root.low + (root.high - root.low) / 2 ;
if(ind <= mid) {
updateHelper(root.left ,ind ,val) ;
}else {
updateHelper(root.right ,ind ,val) ;
}
root.val = root.left.val + root.right.val ; // Computable
}
public int sumRange(int low ,int high) { // O(logn)
return sumRangeHelper(this ,low ,high) ;
}
public int sumRangeHelper(SGTNode root ,int low ,int high) {
if(root.low == low && high == root.high) { // complete overlap
return root.val ;
}
// partial overlap
int mid = root.low + (root.high - root.low) / 2 ;
if(high <= mid) {
return sumRangeHelper(root.left ,low ,high) ;
}if(low >= mid+1) {
return sumRangeHelper(root.right ,low ,high) ;
}
int left = sumRangeHelper(root.left ,low ,mid) ;
int right = sumRangeHelper(root.right ,mid+1 ,high) ;
return left + right ;
}
}
public 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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
00440f86222627ba42654b8cf6b88e99
|
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
|
/*
Author: Spidey2182
#: 1722E
*/
import java.util.*;
public class CountingRectangles implements Runnable {
static ContestScanner sc = new ContestScanner();
static ContestPrinter out = new ContestPrinter();
public static void main(String[] args) {
new Thread(null, new CountingRectangles(), "main", 1 << 28).start();
}
public void run() {
int n, h, w, hs, hb, ws, wb;
int testCases = sc.nextInt();
for (int testCase = 1; testCase <= testCases; testCase++) {
n = sc.nextInt();
int queries = sc.nextInt();
int[][] inp = sc.nextIntMatrix(n, 2);
long[][] dp = new long[1001][1001];
for (int[] x : inp)
dp[x[0]][x[1]] += (long) x[0] * x[1];
for (int i = 1; i < dp.length; i++)
for (int j = 1; j < dp[i].length; j++)
dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
while (queries-- > 0) {
hs = sc.nextInt();
ws = sc.nextInt();
hb = sc.nextInt() - 1;
wb = sc.nextInt() - 1;
out.println(dp[hb][wb] - dp[hb][ws] - dp[hs][wb] + dp[hs][ws]);
}
}
out.flush();
out.close();
}
static final int MOD = (int) 1e9 + 7;
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
}
//Credits: https://github.com/NASU41/AtCoderLibraryForJava/tree/master/ContestIO
class ContestScanner {
private final java.io.InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public ContestScanner(java.io.InputStream in) {
this.in = in;
}
public ContestScanner(java.io.File file) throws java.io.FileNotFoundException {
this(new java.io.BufferedInputStream(new java.io.FileInputStream(file)));
}
public ContestScanner() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (java.io.IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) return buffer[ptr++];
else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b))
);
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b))
);
}
}
}
}
throw new ArithmeticException(
String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit)
);
}
n = n * 10 + digit;
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(int length) {
long[] array = new long[length];
for (int i = 0; i < length; i++) array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
long[] array = new long[length];
for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length) {
int[] array = new int[length];
for (int i = 0; i < length; i++) array[i] = this.nextInt();
return array;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
int[] array = new int[length];
for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length) {
double[] array = new double[length];
for (int i = 0; i < length; i++) array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
double[] array = new double[length];
for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width) {
long[][] mat = new long[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width) {
int[][] mat = new int[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width) {
double[][] mat = new double[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width) {
char[][] mat = new char[height][width];
for (int h = 0; h < height; h++) {
String s = this.next();
for (int w = 0; w < width; w++) {
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
class ContestPrinter extends java.io.PrintWriter {
public ContestPrinter(java.io.PrintStream stream) {
super(stream);
}
public ContestPrinter(java.io.File file) throws java.io.FileNotFoundException {
super(new java.io.PrintStream(file));
}
public ContestPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printArray(int[] array, String separator) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n - 1]));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n - 1]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
public <T> void printArray(T[] array, String separator) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public <T> void printArray(T[] array) {
this.printArray(array, " ");
}
public <T> void printArray(T[] array, String separator, java.util.function.UnaryOperator<T> map) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(map.apply(array[i]));
super.print(separator);
}
super.println(map.apply(array[n - 1]));
}
public <T> void printArray(T[] array, java.util.function.UnaryOperator<T> map) {
this.printArray(array, " ", map);
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
f212bf85350a970fd2c195873f1565ff
|
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
|
// 17-05 //
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class D {
static int N = 1000;
static long hw[][];
public static void main(String[] args) {
Scanner sc = new Scanner();
int t = sc.nextInt();
PrintWriter out = new PrintWriter(System.out);
while (t-- > 0) {
hw = new long[N + 1][N + 1];
int n = sc.nextInt();
int q = sc.nextInt();
for (int i = 0; i < n; i++) {
int h = sc.nextInt();
int w = sc.nextInt();
hw[h][w] += (long) w;
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
hw[i][j] += hw[i][j - 1];
}
}
while (q-- > 0) {
int hs = sc.nextInt();
int ws = sc.nextInt();
int hb = sc.nextInt();
int wb = sc.nextInt();
long res = 0L;
for (int i = hs + 1; i < hb; i++) {
long sum = hw[i][wb - 1] - hw[i][ws];
res += i * sum;
}
out.println(res);
}
}
out.flush();
out.close();
}
static class pp implements Comparable<pp> {
int a;
int b;
pp(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(pp o) {
return this.a - o.a;
}
}
static class Scanner {
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());
}
String str = "";
String nextLine() {
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
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 sort(int[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n), temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static final int M = 1_000_000_007;
static final int inf = Integer.MAX_VALUE;
static final int ninf = inf + 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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
05f3a9c3a88ba1f28214128faffd3201
|
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class Main {
static long ans;
public static void main(String arg[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
while(T --> 0) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int n = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
long[][] pref = new long[1001][1001];
long[][] dp = new long[1001][1001];
for(int i = 1; i <= n; i++) {
st = new StringTokenizer(br.readLine(), " ");
int h = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
dp[h][w] += h * w;
}
for(int i = 1; i < 1001; i++) {
for(int j = 1; j < 1001; j++) {
pref[i][j] = pref[i-1][j] + pref[i][j-1] - pref[i-1][j-1] + dp[i][j];
}
}
for(int i = 0; i < q; i++) {
st = new StringTokenizer(br.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());
ans = 0;
ans = pref[hb-1][wb-1] - pref[hb-1][ws] - pref[hs][wb-1] + pref[hs][ws];
sb.append(ans).append('\n');
}
}
System.out.println(sb);
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
e8da1925875e175d76c6f7d1a2b62629
|
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.*;
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 {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t>0){
int n = sc.nextInt();
int q = sc.nextInt();
long[][] arr = new long[1001][1001];
long[][] pref = new long[1001][1001];
for(int i=0;i<n;i++){
int h = sc.nextInt();
int w =sc.nextInt();
arr[h][w]+=(h*w);
}
for(int i=1;i<1001;i++){
for(int j=1;j<1001;j++){
pref[i][j] = arr[i][j]+pref[i][j-1]+pref[i-1][j]-pref[i-1][j-1];
}
}
for(int i=0;i<q;i++){
int hs = sc.nextInt();
int ws = sc.nextInt();
int hb = sc.nextInt();
int wb = sc.nextInt();
long ans = pref[hb-1][wb-1]+pref[hs][ws]-pref[hb-1][ws]-pref[hs][wb-1];
System.out.println(ans);
}
t--;
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
77fd6684af4f9c7bc95b32b070ad92f5
|
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
|
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.*;
import java.lang.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.io.*;
public class CodeForces {
static private final String INPUT = "input.txt";
static private final String OUTPUT = "output.txt";
static BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer ST;
static PrintWriter out = new PrintWriter(System.out);
static DecimalFormat df = new DecimalFormat("0.00");
final static int MAX = Integer.MAX_VALUE, MIN = Integer.MIN_VALUE, mod = (int) (1e9 + 7);
final static long LMAX = Long.MAX_VALUE, LMIN = Long.MIN_VALUE;
final static long INF = (long) 1e18, Neg_INF = (long) -1e18;
static Random rand = new Random();
// ======================= MAIN ==================================
public static void main(String[] args) throws IOException {
long time = System.currentTimeMillis();
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
// ==== start ====
input();
preprocess();
int t = 1;
t = readInt();
while (t-- > 0) {
solve();
}
out.flush();
// ==== end ====
if (!oj)
System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
}
private static void solve() throws IOException {
int n = readInt(), q = readInt();
BIT bit = new BIT(1005, 1005);
for (int i = 0; i < n; i++) {
int x = readInt(), y = readInt();
bit.update(x, y, x * y);
}
while (q-- > 0) {
int a = readInt(), b = readInt(), c = readInt(), d = readInt();
out.println(bit.query(a + 1, b + 1, c - 1, d - 1));
}
}
private static void preprocess() throws IOException {
}
// cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces
// javac CodeForces.java && java CodeForces
// change Stack size -> java -Xss16M CodeForces.java
// ==================== CUSTOM CLASSES ================================
static class Pair implements Comparable<Pair> {
int first, second;
Pair(int f, int s) {
first = f;
second = s;
}
public int compareTo(Pair o) {
if (this.first == o.first)
return this.second - o.second;
return this.first - o.first;
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
Pair other = (Pair) (obj);
if (this.first != other.first)
return false;
if (this.second != other.second)
return false;
return true;
}
@Override
public int hashCode() {
return this.first ^ this.second;
}
@Override
public String toString() {
return this.first + " " + this.second;
}
}
static class DequeNode {
DequeNode prev, next;
int val;
DequeNode(int val) {
this.val = val;
}
DequeNode(int val, DequeNode prev, DequeNode next) {
this.val = val;
this.prev = prev;
this.next = next;
}
}
// ======================= FOR INPUT ==================================
private static void input() {
FileInputStream instream = null;
PrintStream outstream = null;
try {
instream = new FileInputStream(INPUT);
outstream = new PrintStream(new FileOutputStream(OUTPUT));
System.setIn(instream);
System.setOut(outstream);
} catch (Exception e) {
System.err.println("Error Occurred.");
}
BR = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
static String next() throws IOException {
while (ST == null || !ST.hasMoreTokens())
ST = new StringTokenizer(readLine());
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 char readCharacter() throws IOException {
return next().charAt(0);
}
static String readString() throws IOException {
return next();
}
static String readLine() throws IOException {
return BR.readLine().trim();
}
static int[] readIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = readInt();
return arr;
}
static int[][] read2DIntArray(int n, int m) throws IOException {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
arr[i] = readIntArray(m);
return arr;
}
static List<Integer> readIntList(int n) throws IOException {
List<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(readInt());
return list;
}
static long[] readLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = readLong();
return arr;
}
static long[][] read2DLongArray(int n, int m) throws IOException {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++)
arr[i] = readLongArray(m);
return arr;
}
static List<Long> readLongList(int n) throws IOException {
List<Long> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(readLong());
return list;
}
static char[] readCharArray() throws IOException {
return readString().toCharArray();
}
static char[][] readMatrix(int n, int m) throws IOException {
char[][] mat = new char[n][m];
for (int i = 0; i < n; i++)
mat[i] = readCharArray();
return mat;
}
// ========================= FOR OUTPUT ==================================
private static void printIList(List<Integer> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printLList(List<Long> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printIArray(int[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DIArray(int[][] arr) {
for (int i = 0; i < arr.length; i++)
printIArray(arr[i]);
}
private static void printLArray(long[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DLArray(long[][] arr) {
for (int i = 0; i < arr.length; i++)
printLArray(arr[i]);
}
private static void yes() {
out.println("YES");
}
private static void no() {
out.println("NO");
}
// ====================== TO CHECK IF STRING IS NUMBER ========================
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static boolean isLong(String s) {
try {
Long.parseLong(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
// ==================== FASTER SORT ================================
private static void sort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void sort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
// ==================== MATHEMATICAL FUNCTIONS ===========================
private static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static int mod_pow(long a, long b, int mod) {
if (b == 0)
return 1;
int temp = mod_pow(a, b >> 1, mod);
temp %= mod;
temp = (int) ((1L * temp * temp) % mod);
if ((b & 1) == 1)
temp = (int) ((1L * temp * a) % mod);
return temp;
}
private static long multiply(long a, long b) {
return (((a % mod) * (b % mod)) % mod);
}
private static long divide(long a, long b) {
return multiply(a, mod_pow(b, mod - 2, mod));
}
private static boolean isPrime(long n) {
for (long i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
private static long nCr(long n, long r) {
if (n - r > r)
r = n - r;
long ans = 1L;
for (long i = r + 1; i <= n; i++)
ans *= i;
for (long i = 2; i <= n - r; i++)
ans /= i;
return ans;
}
private static List<Integer> factors(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 1; 1L * i * i <= n; i++)
if (n % i == 0) {
list.add(i);
if (i != n / i)
list.add(n / i);
}
return list;
}
private static List<Long> factors(long n) {
List<Long> list = new ArrayList<>();
for (long i = 1; i * i <= n; i++)
if (n % i == 0) {
list.add(i);
if (i != n / i)
list.add(n / i);
}
return list;
}
// ==================== Primes using Seive =====================
private static List<Integer> getPrimes(int n) {
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
for (int i = 2; 1L * i * i <= n; i++)
if (prime[i])
for (int j = i * i; j <= n; j += i)
prime[j] = false;
// return prime;
List<Integer> list = new ArrayList<>();
for (int i = 2; i <= n; i++)
if (prime[i])
list.add(i);
return list;
}
private static int[] SeivePrime(int n) {
int[] primes = new int[n];
for (int i = 0; i < n; i++)
primes[i] = i;
for (int i = 2; 1L * i * i < n; i++) {
if (primes[i] != i)
continue;
for (int j = i * i; j < n; j += i)
if (primes[j] == j)
primes[j] = i;
}
return primes;
}
// ==================== STRING FUNCTIONS ================================
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
// check if a is subsequence of b
private static boolean isSubsequence(String a, String b) {
int idx = 0;
for (int i = 0; i < b.length() && idx < a.length(); i++)
if (a.charAt(idx) == b.charAt(i))
idx++;
return idx == a.length();
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
private static String sortString(String str) {
int[] arr = new int[256];
for (char ch : str.toCharArray())
arr[ch]++;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 256; i++)
while (arr[i]-- > 0)
sb.append((char) i);
return sb.toString();
}
// ==================== LIS & LNDS ================================
private static int LIS(int arr[], int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find1(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find1(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) >= val) {
ret = mid;
j = mid - 1;
} else {
i = mid + 1;
}
}
return ret;
}
private static int LNDS(int[] arr, int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find2(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find2(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) <= val) {
i = mid + 1;
} else {
ret = mid;
j = mid - 1;
}
}
return ret;
}
// =============== Lower Bound & Upper Bound ===========
// less than or equal
private static int lower_bound(List<Integer> list, int val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(List<Long> list, long val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(int[] arr, int val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(long[] arr, long val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
// greater than or equal
private static int upper_bound(List<Integer> list, int val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(List<Long> list, long val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(int[] arr, int val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(long[] arr, long val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
// ==================== UNION FIND =====================
private static int find(int x, int[] parent) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x], parent);
}
private static boolean union(int x, int y, int[] parent, int[] rank) {
int lx = find(x, parent), ly = find(y, parent);
if (lx == ly)
return false;
if (rank[lx] > rank[ly])
parent[ly] = lx;
else if (rank[lx] < rank[ly])
parent[lx] = ly;
else {
parent[lx] = ly;
rank[ly]++;
}
return true;
}
// ==================== TRIE ================================
static class Trie {
class Node {
Node[] children;
boolean isEnd;
Node() {
children = new Node[26];
}
}
Node root;
Trie() {
root = new Node();
}
boolean insert(String word) {
Node curr = root;
boolean ans = true;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
curr.children[ch - 'a'] = new Node();
curr = curr.children[ch - 'a'];
if (curr.isEnd)
ans = false;
}
curr.isEnd = true;
return ans;
}
boolean find(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
return false;
curr = curr.children[ch - 'a'];
}
return curr.isEnd;
}
}
// ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ==================
public static class SegmentTree {
int n;
long[] arr, tree, lazy;
SegmentTree(long arr[]) {
this.arr = arr;
this.n = arr.length;
this.tree = new long[(n << 2)];
this.lazy = new long[(n << 2)];
build(1, 0, n - 1);
}
void build(int id, int start, int end) {
if (start == end)
tree[id] = arr[start];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
build(left, start, mid);
build(right, mid + 1, end);
tree[id] = tree[left] + tree[right];
}
}
void update(int l, int r, long val) {
update(1, 0, n - 1, l, r, val);
}
void update(int id, int start, int end, int l, int r, long val) {
distribute(id, start, end);
if (end < l || r < start)
return;
if (start == end)
tree[id] += val;
else if (l <= start && end <= r) {
lazy[id] += val;
distribute(id, start, end);
} else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
update(left, start, mid, l, r, val);
update(right, mid + 1, end, l, r, val);
tree[id] = tree[left] + tree[right];
}
}
long query(int l, int r) {
return query(1, 0, n - 1, l, r);
}
long query(int id, int start, int end, int l, int r) {
if (end < l || r < start)
return 0L;
distribute(id, start, end);
if (start == end)
return tree[id];
else if (l <= start && end <= r)
return tree[id];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r);
}
}
void distribute(int id, int start, int end) {
if (start == end)
tree[id] += lazy[id];
else {
tree[id] += lazy[id] * (end - start + 1);
lazy[(id << 1)] += lazy[id];
lazy[(id << 1) + 1] += lazy[id];
}
lazy[id] = 0;
}
}
// ==================== FENWICK TREE ================================
static class FT {
int n;
int[] arr;
int[] tree;
FT(int[] arr, int n) {
this.arr = arr;
this.n = n;
this.tree = new int[n + 1];
for (int i = 1; i <= n; i++) {
update(i, arr[i - 1]);
}
}
FT(int n) {
this.n = n;
this.tree = new int[n + 1];
}
// 1 based indexing
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += idx & -idx;
}
}
// 1 based indexing
int query(int l, int r) {
return getSum(r) - getSum(l - 1);
}
int getSum(int idx) {
int ans = 0;
while (idx > 0) {
ans += tree[idx];
idx -= idx & -idx;
}
return ans;
}
}
// ==================== BINARY INDEX TREE ================================
static class BIT {
long[][] tree;
int n, m;
BIT(int n, int m) {
this.n = n;
this.m = m;
tree = new long[n + 1][m + 1];
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= m; j++) {
// update(i, j, mat[i - 1][j - 1]);
// }
// }
}
void update(int x, int y, int val) {
while (x <= n) {
int t = y;
while (t <= m) {
tree[x][t] += val;
t += t & -t;
}
x += x & -x;
}
}
long query(int x1, int y1, int x2, int y2) {
return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1);
}
long getSum(int x, int y) {
long ans = 0L;
while (x > 0) {
int t = y;
while (t > 0) {
ans += tree[x][t];
t -= t & -t;
}
x -= x & -x;
}
return 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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
29bfc522074bb770375e947f8715431d
|
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.BufferedReader;
import java.io.*;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.*;
public class Yoo
{
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;
}
}
public static void main(String[] args)
{
FastReader sc=new FastReader();
int i,j=0;
int t = sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int q = sc.nextInt();
long a[][]=new long[1003][1003];
for(i=0;i<n;i++)
{
int v1 = sc.nextInt();
int v2 = sc.nextInt();
a[v1][v2]+= v1*v2;
}
long dp[][]=new long[1003][1003];
for(i=1;i<1003;i++)
{
dp[i][1]=a[i][1]+dp[i-1][1];
}
for(i=1;i<1003;i++)
{
dp[1][i]=a[1][i]+dp[1][i-1];
}
/*for(i=1;i<5;i++)
{
for(j=1;j<5;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println();*/
for(i=2;i<1003;i++)
{
for(j=2;j<1003;j++)
{
dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+a[i][j];
}
}
/*for(i=1;i<6;i++)
{
for(j=1;j<6;j++)
{
System.out.print(dp[i][j]+" ");
}
System.out.println();
}*/
for(i=0;i<q;i++)
{
int h1 = sc.nextInt();
int w1 = sc.nextInt();
int h2 = sc.nextInt();
int w2 = sc.nextInt();
//long ans = dp[h2-1][w2-1]-dp[h1+1][w1+1];
//long ans = dp[h2][w2] - dp[h2][w1-1] - dp[h1-1][w2] + dp[h1-1][w1-1];
long ans = (dp[h2-1][w2-1]-dp[h2-1][w1]) -(dp[h1][w2-1]-dp[h1][w1]);
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
c59d8ed1ad512febd3da40791d56e317
|
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.*;
import java.util.*;
public class Counting_Rectangles {
static FastScanner fs;
static FastWriter fw;
static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null;
private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}};
private static final int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
private static final int iMax = Integer.MAX_VALUE, iMin = Integer.MIN_VALUE;
private static final long lMax = Long.MAX_VALUE, lMin = Long.MIN_VALUE;
private static final int mod1 = (int) (1e9 + 7);
private static final int mod2 = 998244353;
public static void main(String[] args) throws IOException {
fs = new FastScanner();
fw = new FastWriter();
int t = 1;
t = fs.nextInt();
while (t-- > 0) {
solve();
}
fw.out.close();
}
private static void solve() {
int n = fs.nextInt(), q = fs.nextInt();
long[][] matrix = new long[1005][1005];
for (int i = 0; i < n; i++) {
int h = fs.nextInt(), w = fs.nextInt();
matrix[h][w] += ((long) h * w);
}
long[][] prefix_sum = new long[1005][1005];
for (int i = 1; i <= 1000; i++) {
for (int j = 1; j <= 1000; j++) {
prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] -
prefix_sum[i - 1][j - 1] + matrix[i][j];
}
}
while (q-- > 0) {
int h1 = fs.nextInt(), w1 = fs.nextInt();
int h2 = fs.nextInt(), w2 = fs.nextInt();
if (h2 - h1 == 1 || w2 - w1 == 1) {
fw.out.println(0);
continue;
}
long ans = prefix_sum[h2 - 1][w2 - 1] - prefix_sum[h1][w2 - 1] -
prefix_sum[h2 - 1][w1] + prefix_sum[h1][w1];
fw.out.println(ans);
}
}
private static class UnionFind {
private final int[] parent;
private final int[] rank;
UnionFind(int n) {
parent = new int[n + 5];
rank = new int[n + 5];
for (int i = 0; i <= n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
private int find(int i) {
if (parent[i] == i)
return i;
return parent[i] = find(parent[i]);
}
private void union(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
if (rank[a] < rank[b]) {
int temp = a;
a = b;
b = temp;
}
parent[b] = a;
if (rank[a] == rank[b])
rank[a]++;
}
}
}
private static class SCC {
private final int n;
private final List<List<Integer>> adjList;
SCC(int _n, List<List<Integer>> _adjList) {
n = _n;
adjList = _adjList;
}
private List<List<Integer>> getSCC() {
List<List<Integer>> ans = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
boolean[] vis = new boolean[n];
for (int i = 0; i < n; i++) {
if (!vis[i]) dfs(i, adjList, vis, stack);
}
vis = new boolean[n];
List<List<Integer>> rev_adjList = rev_graph(n, adjList);
while (!stack.isEmpty()) {
int curr = stack.pop();
if (!vis[curr]) {
List<Integer> scc_list = new ArrayList<>();
dfs2(curr, rev_adjList, vis, scc_list);
ans.add(scc_list);
}
}
return ans;
}
private void dfs(int curr, List<List<Integer>> adjList, boolean[] vis, Stack<Integer> stack) {
vis[curr] = true;
for (int x : adjList.get(curr)) {
if (!vis[x]) dfs(x, adjList, vis, stack);
}
stack.add(curr);
}
private void dfs2(int curr, List<List<Integer>> adjList, boolean[] vis, List<Integer> scc_list) {
vis[curr] = true;
scc_list.add(curr);
for (int x : adjList.get(curr)) {
if (!vis[x]) dfs2(x, adjList, vis, scc_list);
}
}
}
private static List<List<Integer>> rev_graph(int n, List<List<Integer>> adjList) {
List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < n; i++) ans.add(new ArrayList<>());
for (int i = 0; i < n; i++) {
for (int x : adjList.get(i)) {
ans.get(x).add(i);
}
}
return ans;
}
private static class Calc_nCr {
private final long[] fact;
private final long[] invfact;
private final int p;
Calc_nCr(int n, int prime) {
fact = new long[n + 5];
invfact = new long[n + 5];
p = prime;
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = (i * fact[i - 1]) % p;
}
invfact[n] = pow_with_mod(fact[n], p - 2, p);
for (int i = n - 1; i >= 0; i--) {
invfact[i] = (invfact[i + 1] * (i + 1)) % p;
}
}
private long nCr(int n, int r) {
if (r > n || n < 0 || r < 0) return 0;
return (((fact[n] * invfact[r]) % p) * invfact[n - r]) % p;
}
}
private static int random_between_two_numbers(int l, int r) {
Random ran = new Random();
return ran.nextInt(r - l) + l;
}
private static long gcd(long a, long b) {
return (b == 0 ? a : gcd(b, a % b));
}
private static long lcm(long a, long b) {
return ((a * b) / gcd(a, b));
}
private static long pow(long a, long b) {
long result = 1;
while (b > 0) {
if ((b & 1L) == 1) {
result = (result * a);
}
a = (a * a);
b >>= 1;
}
return result;
}
private static long pow_with_mod(long a, long b, int mod) {
long result = 1;
while (b > 0) {
if ((b & 1L) == 1) {
result = (result * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long ceilDiv(long a, long b) {
return ((a + b - 1) / b);
}
private static long getMin(long... args) {
long min = lMax;
for (long arg : args)
min = Math.min(min, arg);
return min;
}
private static long getMax(long... args) {
long max = lMin;
for (long arg : args)
max = Math.max(max, arg);
return max;
}
private static boolean isPalindrome(String s, int l, int r) {
int i = l, j = r;
while (j - i >= 1) {
if (s.charAt(i) != s.charAt(j))
return false;
i++;
j--;
}
return true;
}
private static List<Integer> primes(int n) {
boolean[] primeArr = new boolean[n + 5];
Arrays.fill(primeArr, true);
for (int i = 2; (i * i) <= n; i++) {
if (primeArr[i]) {
for (int j = i * i; j <= n; j += i) {
primeArr[j] = false;
}
}
}
List<Integer> primeList = new ArrayList<>();
for (int i = 2; i <= n; i++) {
if (primeArr[i])
primeList.add(i);
}
return primeList;
}
private static int noOfSetBits(long x) {
int cnt = 0;
while (x != 0) {
x = x & (x - 1);
cnt++;
}
return cnt;
}
private static int sumOfDigits(long num) {
int cnt = 0;
while (num > 0) {
cnt += (num % 10);
num /= 10;
}
return cnt;
}
private static int noOfDigits(long num) {
int cnt = 0;
while (num > 0) {
cnt++;
num /= 10;
}
return cnt;
}
private static boolean isPerfectSquare(long num) {
long sqrt = (long) Math.sqrt(num);
return ((sqrt * sqrt) == num);
}
private static class Pair<U, V> {
private final U first;
private final V second;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return first.equals(pair.first) && second.equals(pair.second);
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
private Pair(U ff, V ss) {
this.first = ff;
this.second = ss;
}
}
private static void randomizeIntArr(int[] arr, int n) {
Random r = new Random();
for (int i = (n - 1); i > 0; i--) {
int j = r.nextInt(i + 1);
swapInIntArr(arr, i, j);
}
}
private static void randomizeLongArr(long[] arr, int n) {
Random r = new Random();
for (int i = (n - 1); i > 0; i--) {
int j = r.nextInt(i + 1);
swapInLongArr(arr, i, j);
}
}
private static void swapInIntArr(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
private static void swapInLongArr(long[] arr, int a, int b) {
long temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
private static int[] readIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = fs.nextInt();
return arr;
}
private static long[] readLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = fs.nextLong();
return arr;
}
private static List<Integer> readIntList(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(fs.nextInt());
return list;
}
private static List<Long> readLongList(int n) {
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(fs.nextLong());
return list;
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner() throws IOException {
if (checkOnlineJudge)
this.br = new BufferedReader(new FileReader("src/input.txt"));
else
this.br = new BufferedReader(new InputStreamReader(System.in));
this.st = new StringTokenizer("");
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException err) {
err.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
if (st.hasMoreTokens()) {
return st.nextToken("").trim();
}
try {
return br.readLine().trim();
} catch (IOException err) {
err.printStackTrace();
}
return "";
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
private static class FastWriter {
PrintWriter out;
FastWriter() throws IOException {
if (checkOnlineJudge)
out = new PrintWriter(new FileWriter("src/output.txt"));
else
out = new PrintWriter(System.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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
e304276adba76dcb4b1cd5aab8f96e91
|
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.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class c{
static int []f=new int [2000001];
//static int []f1;
//static int []f2;
//static int []size1;
//static int []size2;
//static int []a=new int [500001];
static int max=Integer.MAX_VALUE;
public static void main(String []args) {
MyScanner s=new MyScanner();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int t=s.nextInt();
while(t-->0) {
int n=s.nextInt();
int q=s.nextInt();
long [][]f=new long[1001][1001];
for(int i=0;i<n;i++) {
int a=s.nextInt();
int b=s.nextInt();
f[a][b]+=a*b;
}
long [][]sum=new long[1002][1002];
for(int i=1;i<1002;i++) {
for(int j=1;j<1002;j++)
sum[i][j]=f[i-1][j-1]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
}
while(q-->0) {
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
int d=s.nextInt();
long k=sum[c][d]-sum[a+1][d]-sum[c][b+1]+sum[a+1][b+1];
k=Math.max(0, k);
System.out.println(k);
}
}
}
public static void swap(char []a,int j) {
char x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
public static boolean is(int j) {
for(int i=2;i<=(int )Math.sqrt(j);i++) {
if(j%i==0)return false;
}
return true;
}
public static String addStrings(String num1, String num2) {
StringBuilder s=new StringBuilder();
int i=num1.length()-1;
int j=num2.length()-1;
int res=0;
while(i>=0||j>=0||res!=0){
if(i>=0)res+=num1.charAt(i)-'0';
if(j>=0)res+=num2.charAt(j)-'0';
s.append(res%10);
res/=10;
i--;j--;
}
return s.reverse().toString();
}
public static int find (int []father,int x) {
if(x!=father[x])
x=find(father,father[x]);
return father[x];
}
public static void union(int []father,int x,int y,int []size) {
x=find(father,x);
y=find(father,y);
if(x==y)
return ;
if(size[x]<size[y]) {
int tem=x;
x=y;
y=tem;
}
father[y]=x;
size[x]+=size[y];
return ;
}
public static void shufu(int []f) {
for(int i=0;i<f.length;i++) {
int k=(int)(Math.random()*(f.length));
int t=f[k];
f[k]=f[0];
f[0]=t;
}
}
public static int gcd(int x,int y) {
return y==0?x:gcd(y,x%y);
}
/*
public static void buildertree(int k,int l,int r) {
if(l==r)
{
f[k]=a[l];
return ;
}
int m=l+r>>1;
buildertree(k+k,l,m);
buildertree(k+k+1,m+1,r);
f[k]=
}
public static void update(int u,int l,int r,int x,int c)
{
if(l==x && r==x)
{
f[u]=c;
return;
}
int mid=l+r>>1;
if(x<=mid)update(u<<1,l,mid,x,c);
else if(x>=mid+1)update(u<<1|1,mid+1,r,x,c);
f[u]=Math.max(f[u+u], f[u+u+1]);
}
public static int query(int k,int l,int r,int x,int y) {
if(x==l&&y==r) {
return f[k];
}
int m=l+r>>1;
if(y<=m) {
return query(k+k,l,m,x,y);
}
else if(x>m)return query(k+k+1,m+1,r,x,y);
else {
int i=query(k+k,l,m,x,m),j=query(k+k+1,m+1,r,m+1,y);
return Math.max(j, Math.max(i+j, i));
}
}
public static void calc(int k,int l,int r,int x,int z) {
f[k]+=z;
if(l==r) {
return ;
}
int m=l+r>>1;
if(x<=m)
calc(k+k,l,m,x,z);
else calc(k+k+1,m+1,r,x,z);
}
*/
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", "");
cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname;
final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in");
br = new BufferedReader(new InputStreamReader(fin.exists()
? new FileInputStream(fin) : System.in));
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
public String next() {
try {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
a7824ef43b98b19bafd3e3fb037c713a
|
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.*;
import java.util.*;
public class E {
static PrintWriter out;
static Kioken sc;
static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null;
public static void main(String[] args) throws FileNotFoundException {
if (checkOnlineJudge) {
out = new PrintWriter("E:/CF_V2/output.txt");
sc = new Kioken(new File("E:/CF_V2/input.txt"));
} else {
out = new PrintWriter((System.out));
sc = new Kioken();
}
int tt = 1;
tt = sc.nextInt();
while (tt-- > 0) {
solve();
}
out.flush();
out.close();
}
public static void solve() {
int n = sc.nextInt(), q = sc.nextInt();
BIT bit = new BIT(1001, 1001);
// long[][] arr = new long[1001][1001];
for(int i = 0; i < n; i++){
int x = sc.nextInt();
int y = sc.nextInt();
bit.update(x, y, (long)x*y);
}
// calculate 2-d prefix Sum
// out.println(bit.query(1, 1, 999, 999));
for(int i = 0; i < q; i++){
int hs = sc.nextInt(), ws = sc.nextInt(), hq = sc.nextInt(), wq = sc.nextInt();
long ans = bit.query(hs+1, ws+1, hq-1, wq-1);
out.println(ans);
}
}
public static long gcd(long a, long b) {
while (b != 0) {
long rem = a % b;
a = b;
b = rem;
}
return a;
}
static long MOD = 1000000007;
static void reverseSort(int[] arr){List<Integer> list = new ArrayList<>();for (int i=0; i<arr.length; i++){list.add(arr[i]);}Collections.sort(list, Collections.reverseOrder());for (int i = 0; i < arr.length; i++){arr[i] = list.get(i);}}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
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 Kioken {
// FileInputStream br = new FileInputStream("input.txt");
BufferedReader br;
StringTokenizer st;
Kioken(File filename) {
try {
FileReader fr = new FileReader(filename);
br = new BufferedReader(fr);
st = new StringTokenizer("");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
Kioken() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean hasNext() {
String next = null;
try {
next = br.readLine();
} catch (Exception e) {
}
if (next == null || next.length() == 0) {
return false;
}
st = new StringTokenizer(next);
return true;
}
public int[] readArrayInt(int n){
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = nextInt();
}
return arr;
}
public long[] readArrayLong(int n){
long[] arr = new long[n];
for(int i = 0; i < n; i++){
arr[i] = nextLong();
}
return arr;
}
}
static class BIT {
long[][] tree;
int n, m;
BIT(int n, int m) {
this.n = n;
this.m = m;
tree = new long[n + 1][m + 1];
// for (int i = 1; i < n; i++) {
// for (int j = 1; j < m; j++) {
// update(i, j, mat[i][j]);
// }
// }
}
void update(int x, int y, long val) {
while (x <= n) {
int t = y;
while (t <= m) {
tree[x][t] += val;
t += t & -t;
}
x += x & -x;
}
}
long query(int x1, int y1, int x2, int y2) {
return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1);
}
long getSum(int x, int y) {
long ans = 0L;
while (x > 0) {
int t = y;
while (t > 0) {
ans += tree[x][t];
t -= t & -t;
}
x -= x & -x;
}
return 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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
4c349198ac5d5cad0858020f9df5ace0
|
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 static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
public class E_Counting_Rectangles {
static long mod = Long.MAX_VALUE;
public static void main(String[] args) {
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
FastReader f = new FastReader();
int t = f.nextInt();
while(t-- > 0){
solve(f, out);
}
out.close();
}
public static void solve(FastReader f, PrintWriter out) {
int n = f.nextInt();
int q = f.nextInt();
long grid[][] = new long[1001][1001];
for(int i = 0; i < n; i++) {
int h = f.nextInt();
int w = f.nextInt();
grid[h][w] += (long)h*w;
}
long dp[][] = new long[1001][1001];
for(int i = 1; i < 1001; i++) {
for(int j = 1; j < 1001; j++) {
dp[i][j] += dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1] + grid[i][j];
}
}
while(q-- > 0) {
int hs = f.nextInt();
int ws = f.nextInt();
int hb = f.nextInt();
int wb = f.nextInt();
long ans = dp[hb-1][wb-1] - dp[hs][wb-1] - dp[hb-1][ws] + dp[hs][ws];
out.println(ans);
}
}
// Sort an array
public static void sort(int arr[]) {
ArrayList<Integer> al = new ArrayList<>();
for(int i: arr) {
al.add(i);
}
Collections.sort(al);
for(int i = 0; i < arr.length; i++) {
arr[i] = al.get(i);
}
}
// Find all divisors of n
public static void allDivisors(int n) {
for(int i = 1; i*i <= n; i++) {
if(n%i == 0) {
System.out.println(i + " ");
if(i != n/i) {
System.out.println(n/i + " ");
}
}
}
}
// Check if n is prime or not
public static boolean isPrime(int n) {
if(n < 1) return false;
if(n == 2 || n == 3) return true;
if(n % 2 == 0 || n % 3 == 0) return false;
for(int i = 5; i*i <= n; i += 6) {
if(n % i == 0 || n % (i+2) == 0) {
return false;
}
}
return true;
}
// Find gcd of a and b
public static long gcd(long a, long b) {
long dividend = a > b ? a : b;
long divisor = a < b ? a : b;
while(divisor > 0) {
long reminder = dividend % divisor;
dividend = divisor;
divisor = reminder;
}
return dividend;
}
// Find lcm of a and b
public static long lcm(long a, long b) {
long lcm = gcd(a, b);
long hcf = (a * b) / lcm;
return hcf;
}
// Find factorial in O(n) time
public static long fact(int n) {
long res = 1;
for(int i = 2; i <= n; i++) {
res *= res * i;
}
return res;
}
// Find power in O(logb) time
public static long power(long a, long b) {
long res = 1;
while(b > 0) {
if((b&1) == 1) {
res = (res * a)%mod;
}
a = (a * a)%mod;
b >>= 1;
}
return res;
}
// Find nCr
public static long nCr(int n, int r) {
if(r < 0 || r > n) {
return 0;
}
long ans = fact(n) / (fact(r) * fact(n-r));
return ans;
}
// Find nPr
public static long nPr(int n, int r) {
if(r < 0 || r > n) {
return 0;
}
long ans = fact(n) / fact(r);
return ans;
}
// sort all characters of a string
public static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
// User defined class for fast I/O
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();
}
boolean hasNext() {
if (st != null && st.hasMoreTokens()) {
return true;
}
String tmp;
try {
br.mark(1000);
tmp = br.readLine();
if (tmp == null) {
return false;
}
br.reset();
} catch (IOException e) {
return false;
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
boolean nextBoolean() {
return Boolean.parseBoolean(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextArray(int n) {
int[] a = new int[n];
for(int i=0; i<n; i++) {
a[i] = nextInt();
}
return a;
}
}
}
/**
Dec Char Dec Char Dec Char Dec Char
--------- --------- --------- ----------
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH (start of heading) 33 ! 65 A 97 a
2 STX (start of text) 34 " 66 B 98 b
3 ETX (end of text) 35 # 67 C 99 c
4 EOT (end of transmission) 36 $ 68 D 100 d
5 ENQ (enquiry) 37 % 69 E 101 e
6 ACK (acknowledge) 38 & 70 F 102 f
7 BEL (bell) 39 ' 71 G 103 g
8 BS (backspace) 40 ( 72 H 104 h
9 TAB (horizontal tab) 41 ) 73 I 105 i
10 LF (NL line feed, new line) 42 * 74 J 106 j
11 VT (vertical tab) 43 + 75 K 107 k
12 FF (NP form feed, new page) 44 , 76 L 108 l
13 CR (carriage return) 45 - 77 M 109 m
14 SO (shift out) 46 . 78 N 110 n
15 SI (shift in) 47 / 79 O 111 o
16 DLE (data link escape) 48 0 80 P 112 p
17 DC1 (device control 1) 49 1 81 Q 113 q
18 DC2 (device control 2) 50 2 82 R 114 r
19 DC3 (device control 3) 51 3 83 S 115 s
20 DC4 (device control 4) 52 4 84 T 116 t
21 NAK (negative acknowledge) 53 5 85 U 117 u
22 SYN (synchronous idle) 54 6 86 V 118 v
23 ETB (end of trans. block) 55 7 87 W 119 w
24 CAN (cancel) 56 8 88 X 120 x
25 EM (end of medium) 57 9 89 Y 121 y
26 SUB (substitute) 58 : 90 Z 122 z
27 ESC (escape) 59 ; 91 [ 123 {
28 FS (file separator) 60 < 92 \ 124 |
29 GS (group separator) 61 = 93 ] 125 }
30 RS (record separator) 62 > 94 ^ 126 ~
31 US (unit separator) 63 ? 95 _ 127 DEL
*/
// (a/b)%mod == (a * moduloInverse(b)) % mod;
// moduloInverse(b) = power(b, mod-2);
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
ce606f4aa6150879e946f351a5c336b6
|
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.*;
import java.math.*;
import java.util.*;
// @author : Dinosparton
public class test {
static class Pair{
long x;
int y;
Pair(long x,int y){
this.x = x;
this.y = y;
}
}
static class Compare {
void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x!=p2.x) {
return (int)(p1.x - p2.x);
}
else {
return (int)(p1.y - p2.y);
}
}
});
// for (int i = 0; i < n; i++) {
// System.out.print(arr[i].x + " " + arr[i].y + " ");
// }
// System.out.println();
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner()
{
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[]) throws Exception {
Scanner sc = new Scanner();
StringBuilder res = new StringBuilder();
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt();
int q = sc.nextInt();
long map[][] = new long[1001][1001];
for(int i=0;i<n;i++) {
int h = sc.nextInt();
int w = sc.nextInt();
map[h][w]+=w;
}
for(int i=1;i<=1000;i++){
for(int j=1;j<=1000;j++) {
map[i][j] += map[i][j-1];
}
}
// for(int i=0;i<=n;i++) {
// for(int j=0;j<=n;j++) {
// System.out.print(map[i][j]+" ");
// }
// System.out.println();
// }
while(q-->0) {
long cnt = 0;
int h1 = sc.nextInt();
int w1 = sc.nextInt();
int h2 = sc.nextInt();
int w2 = sc.nextInt();
for(int i=h1+1;i<h2;i++) {
cnt += (1L)*(i*(map[i][w2-1] - map[i][w1]));
}
res.append(cnt+"\n");
}
}
System.out.println(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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
0ffccf9749b713dbdaee48cb434c1ece
|
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
|
/*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class SolutionE {
static int MAX = 1005;
static long[][] arr = new long[MAX][MAX];
public static void main(String[] args) throws Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 1; t <= test; t++) {
solve(t);
}
out.close();
}
private static void solve(int t) {
int n = sc.nextInt();
int q = sc.nextInt();
for (int i = 0; i < MAX; i++) {
Arrays.fill(arr[i], 0);
}
for (int i = 0; i < n; i++) {
int h = sc.nextInt();
int w = sc.nextInt();
arr[h][w] += (long) h * w;
}
for (int i = 1; i < MAX; i++) {
for (int j = 1; j < MAX; j++) {
arr[i][j] += arr[i - 1][j] + arr[i][j - 1] - arr[i - 1][j - 1];
}
}
for (int i = 0; i < q; i++) {
int ha = sc.nextInt();
int wa = sc.nextInt();
int hb = sc.nextInt();
int wb = sc.nextInt();
out.println(arr[hb - 1][wb - 1] - arr[hb - 1][wa] - arr[ha][wb - 1] + arr[ha][wa]);
}
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.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 lastMonthOfVacation)
{
lastMonthOfVacation.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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
3bd294889344b4d32b98104524abc2e4
|
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.util.*;
import java.io.*;
public class F{
static FastScanner fs = null;
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
while(t-->0){
int n = fs.nextInt();
int q = fs.nextInt();
long ar[][] = new long[1005][1005];
long dp[][] = new long[1005][1005];
for(int i=0;i<n;i++){
int u = fs.nextInt();
int v = fs.nextInt();
dp[u][v]+=(long)((long)u*(long)v);
ar[u][v]+=(long)((long)u*(long)v);
}
for(int i = 0;i<=1002;i++){
for(int j = 1;j<=1002;j++){
dp[i][j]+=dp[i][j-1];
}
}
for(int i = 0;i<=1002;i++){
for(int j = 1;j<=1002;j++){
dp[j][i]+=dp[j-1][i];
}
}
while (q-->0) {
int hs = fs.nextInt();
int ws = fs.nextInt();
int hb = fs.nextInt();
int wb = fs.nextInt();
long ans = (dp[hb-1][wb-1]-dp[hb-1][ws]-dp[hs][wb-1]+dp[hs][ws]);
if(ans<(long)0)
ans = (long)0;
if((wb-ws)==1 || (hb-hs)==1)
ans = (long)0;
out.println(ans);
}
// out.println();
}
out.close();
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int 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());
}
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());
}
double nextDouble() {
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
90abe61b1506f737254abbf509f06acf
|
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
|
///WizardAP - 当你休息的时候,很多人比你付出更多的努力!不放弃 !
/// Time : 2022-10-12, Wed, 16:30
import java.io.*;
import java.util.*;
import static java.lang.Double.parseDouble;
import static java.lang.System.in;
import static java.lang.System.out;
public class Main {
static final int MOD = (int) 1e9 + 7;
public static void main(String[] args) throws Exception {
FastIO fs = new FastIO();
//Scanner fs = new Scanner(System.in);
int t = fs.nextInt();
while(t-->0)
{
int n =fs.nextInt();
int q= fs.nextInt();
long[][] a= new long[1005][1005];
for (int i = 0;i<n;i++)
{
int c =fs.nextInt();
int k = fs.nextInt();
a[c][k]+=(long)c*k;
}
for (int i =1;i<=1000;i++)
for (int j = 1;j<=1000;j++)
a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1];
while(q-->0)
{
int hs =fs.nextInt();
int ws = fs.nextInt();
int hb = fs.nextInt();
int wb= fs.nextInt();
System.out.println(a[hb-1][wb-1] - a[hb-1][ws] - a[hs][wb-1] + a[hs][ws]);
}
}
fs.close();
}
//BeginCodeSnip{FastIO}
static class FastIO extends PrintWriter {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar;
private int numChars;
// standard input
public FastIO() {
this(in, System.out);
}
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
private int nextByte() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1) {
return -1; // end of file
}
}
return buf[curChar++];
}
// to read in entire lines, replace c <= ' '
// with a function that checks whether c is a line break
public String next() {
int c;
do {
c = nextByte();
} while (c <= ' ');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > ' ');
return res.toString();
}
public int nextInt() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public long nextLong() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public double nextDouble() {
return parseDouble(next());
}
}
//EndCodeSnip
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
2e21d6a413548876e3629ae1d12b35fd
|
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.*;
import java.util.StringTokenizer;
public class Counting_Rectangles{
static long [][]prefix;
public static void main(String[]args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int q = sc.nextInt();
long [][] arr = new long[1001][1001];
for(int i = 0;i<n;i++)
{
int a = sc.nextInt();
int b = sc.nextInt();
arr[a][b] += a*b;
}
prefix = prefix(arr);
for(int i = 0;i<q;i++){
pw.println( calc(sc.nextInt()+1,sc.nextInt()+1,sc.nextInt()-1,sc.nextInt()-1));
}
}
pw.flush();
}
public static long [][] prefix(long [][]arr){
long [] [] prefix = new long[1001][1001];
prefix[0][0] = arr[0][0];
for(int i = 1;i<1001;i++)
prefix[0][i] = prefix[0][i-1]+arr[0][i];
for(int i = 1;i<1001;i++)
prefix[i][0] = prefix[i-1][0]+arr[i-1][0];
for(int i = 1;i<1001;i++){
for(int j =1 ;j<1001;j++){
prefix[i][j] = prefix[i][j-1]+prefix[i-1][j]-prefix[i-1][j-1]+arr[i][j];
}
}
return prefix;
}
public static long sum(int i,int j){
if(i < 0 || j < 0 || i >= 1001 || j>= 1001)
return 0;
else
return prefix[i][j];
}
public static long calc(int hs,int ws,int hb,int wb){
return sum(hb,wb)-sum(hb,ws-1)-sum(hs-1,wb)+sum(hs-1,ws-1);
}
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 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 boolean ready() throws IOException {
return br.ready();
}
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 long[]nextlongArray(int n) throws IOException{
long [] a = new long[n];
for(int i = 0;i<n;i++){
a[i] = 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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
7c6c299141aad39de88e2ca4079e0be7
|
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.*;
import java.util.*;
public class Main {
static int T;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(br.readLine());
T = Integer.parseInt(st.nextToken());
for (int t = 0; t < T; t++) {
st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int Q = Integer.parseInt(st.nextToken());
long[][] dp = new long[1002][1002];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
long h = Long.parseLong(st.nextToken());
long w = Long.parseLong(st.nextToken());
dp[(int)h][(int)w] += h * w;
}
for (int i = 1; i <= 1000; i++) {
for (int j = 1; j <= 1000; j++) {
dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
}
}
for(int i = 0; i<Q; i++){
st = new StringTokenizer(br.readLine());
long h_s = Long.parseLong(st.nextToken());
long w_s = Long.parseLong(st.nextToken());
long h_b = Long.parseLong(st.nextToken());
long w_b = Long.parseLong(st.nextToken());
long ans = dp[(int)h_s][(int)w_s] + dp[(int)h_b - 1][(int)w_b - 1] - (dp[(int)h_b - 1][(int)w_s] + dp[(int)h_s][(int)w_b - 1]);
pw.println(ans);
}
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
3579c830c9b6fea4b99f9e82e6d616bc
|
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.util.*;
import java.lang.*;
import java.io.*;
public final 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 {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws java.lang.Exception{
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0) {
int n =sc.nextInt(), q=sc.nextInt();
long[][] pre = new long[1001][1001];
for(int i=0;i<n;i++) {
int h=sc.nextInt(),w=sc.nextInt();
pre[h][w]+=(long)h*w;
}
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];
}
}
for(int i=0;i<q;i++) {
int hs=sc.nextInt(),ws=sc.nextInt(),hb=sc.nextInt(),wb=sc.nextInt();
System.out.println(
pre[hb-1][wb-1]-pre[hb-1][ws]-pre[hs][wb-1]+pre[hs][ws]);
}
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
ea9cb63506712b0249f3a089f82d4ce9
|
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 javax.print.attribute.IntegerSyntax;
import java.util.*;
import java.io.*;
public class Solution
{
private static class FastIO {
private static class FastReader
{
BufferedReader br;
StringTokenizer st;
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;
}
}
private static PrintWriter out = new PrintWriter(System.out);
private static FastReader in = new FastReader();
public void print(String s) {out.print(s);}
public void println(String s) {out.println(s);}
public void println() {
println("");
}
public void print(int i) {out.print(i);}
public void print(long i) {out.print(i);}
public void print(char i) {out.print(i);}
public void print(double i) {out.print(i);}
public void println(int i) {out.println(i);}
public void println(long i) {out.println(i);}
public void println(char i) {out.println(i);}
public void println(double i) {out.println(i);}
public void printIntArrayWithoutSpaces(int[] a) {
for(int i : a) {
out.print(i);
}
out.println();
}
public void printIntArrayWithSpaces(int[] a) {
for(int i : a) {
out.print(i + " ");
}
out.println();
}
public void printIntArrayNewLine(int[] a) {
for(int i : a) {
out.println(i);
}
}
public int[] getIntArray(int n) {
int[] res = new int[n];
for(int i = 0; i < n; i++) {
res[i] = in.nextInt();
}
return res;
}
public List<Integer> getIntList(int n) {
List<Integer> list = new ArrayList<>();
for(int i = 0; i < n; i++) {
list.add(in.nextInt());
}
return list;
}
public static void printKickstartCase(int i) {
out.print("Case #" + i + ": ");
}
public String next() {return in.next();}
int nextInt() { return in.nextInt(); }
char nextChar() {return in.next().charAt(0);}
long nextLong() { return in.nextLong(); }
double nextDouble() { return in.nextDouble(); }
String nextLine() {return in.nextLine();}
public void close() {
out.flush();
out.close();
}
}
private static final FastIO io = new FastIO();
private static class MathUtil {
public static final int MOD = 1000000007;
public static int gcd(int a, int b) {
if(b == 0) {
return a;
}
return gcd(b, a % b);
}
public static int gcd(int[] a) {
if(a.length == 0) {
return 0;
}
int res = a[0];
for(int i = 1; i < a.length; i++) {
res = gcd(res, a[i]);
}
return res;
}
public static int gcd(List<Integer> a) {
if(a.size() == 0) {
return 0;
}
int res = a.get(0);
for(int i = 1; i < a.size(); i++) {
res = gcd(res, a.get(i));
}
return res;
}
public static int modular_mult(int a, int b, int M) {
long res = (long)a * b;
return (int)(res % M);
}
public static int modular_mult(int a, int b) {
return modular_mult(a, b, MOD);
}
public static int modular_add(int a, int b, int M) {
long res = (long)a + b;
return (int)(res % M);
}
public static int modular_add(int a, int b) {
return modular_add(a, b, MOD);
}
public static int modular_sub(int a, int b, int M) {
long res = ((long)a - b) + M;
return (int)(res % M);
}
public static int modular_sub(int a, int b) {
return modular_sub(a, b, MOD);
}
//public static int modular_div(int a, int b, int M) {}
//public static int modular_div(int a, int b) {return modular_div(a, b, MOD);}
public static int pow(int a, int b, int M) {
int res = 1;
while (b > 0) {
if ((b & 1) == 1) {
res = modular_mult(res, a, M);
}
a = modular_mult(a, a, M);
b = b >> 1;
}
return res;
}
public static int pow(int a, int b) {
return pow(a, b, MOD);
}
/*public static int fact(int i, int M) {
}
public static int fact(int i) {
}
public static void preComputeFact(int i) {
}
public static int mod_mult_inverse(int den, int mod) {
}
public static void C(int n, int r) {
}*/
}
private static class ArrayUtil {
@FunctionalInterface
private static interface NumberPairComparator {
boolean test(int a, int b);
}
public static int[] nextGreaterOrSmallerRight(int[] a, NumberPairComparator npc) {
int n = a.length;
int[] res = new int[n];
Stack<Integer> stack = new Stack<>();
for(int i = 0; i < n; i++) {
int cur = a[i];
while(!stack.isEmpty() && npc.test(a[stack.peek()], cur)) {
res[stack.pop()] = i;
}
stack.push(i);
}
while(!stack.isEmpty()) {
res[stack.pop()] = n;
}
return res;
}
public static int[] nextGreaterOrSmallerLeft(int[] a, NumberPairComparator npc) {
int n = a.length;
int[] res = new int[n];
Stack<Integer> stack = new Stack<>();
for(int i = n - 1; i >= 0; i--) {
int cur = a[i];
while(!stack.isEmpty() && npc.test(a[stack.peek()], cur)) {
res[stack.pop()] = i;
}
stack.push(i);
}
while(!stack.isEmpty()) {
res[stack.pop()] = n;
}
return res;
}
public static Map<Integer, Integer> getFreqMap(int[] a) {
Map<Integer, Integer> map = new HashMap<>();
for(int i : a) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
return map;
}
public static long arraySum(int[] a) {
long sum = 0;
for(int i : a) {
sum += i;
}
return sum;
}
private static int maxIndex(int[] a) {
int max = Integer.MIN_VALUE;
int max_index = -1;
for(int i = 0; i < a.length; i++) {
if(a[i] > max) {
max = a[i];
max_index = i;
}
}
return max_index;
}
}
private static final int M = 1000000007;
private static final String yes = "YES";
private static final String no = "NO";
private static int MAX = M / 100;
private static final int MIN = -MAX;
private static final int UNVISITED = -1;
private static class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "x: " + x + " y: " + y;
}
}
private static List<List<Integer>> getAdj(int n, int e, boolean directed, boolean shift) {
List<List<Integer>> res = new ArrayList<>();
for(int i = 0; i < n; i++) {
res.add(new ArrayList<>());
}
for(int i = 0; i < e; i++) {
int u = io.nextInt();
int v = io.nextInt();
if(shift) {
u --;
v --;
}
res.get(u).add(v);
if(!directed) {
res.get(v).add(u);
}
}
return res;
}
private static class TreeUtil {
private static List<List<Integer>> getTreeInputWithOffset(int n) {
List<List<Integer>> res = new ArrayList<>();
for(int i = 0; i < n; i++) {
res.add(new ArrayList<>());
}
for(int i = 0; i < n - 1; i++) {
int u = io.nextInt() - 1;
int v = io.nextInt() - 1;
res.get(u).add(v);
res.get(v).add(u);
}
return res;
}
private static int subtreeSizeDfs(int root, int parent, List<List<Integer>> tree, List<Integer> res) {
int sum = 0;
for(int child : tree.get(root)) {
if(child == parent) {
continue;
}
sum += subtreeSizeDfs(child, root, tree, res);
}
res.set(root, 1 + sum);
return 1 + sum;
}
private static List<Integer> getSubtreeSizes(List<List<Integer>> tree, int root) {
List<Integer> res = new ArrayList<>();
for(int i = 0; i < tree.size(); i++) {
res.add(0);
}
subtreeSizeDfs(root, -1, tree, res);
return res;
}
}
private static final long e3 = 1_000;
private static final long e6 = 1_000_000;
private static final long e9 = 1_000_000_000;
private static final long e12 = e9 * e3;
private static final long e15 = e9 * e6;
private static final long e18 = e9 * e9;
String mod = "%";
private static int f(int node, int parent, List<List<Integer>> tree, List<Integer> subtreeSizes) {
tree.get(node).remove(Integer.valueOf(parent));
int numChildren = tree.get(node).size();
if(numChildren == 0) {
return 0;
}
if(numChildren == 1) {
return subtreeSizes.get(tree.get(node).get(0)) - 1;
}
int left = tree.get(node).get(0);
int right = tree.get(node).get(1);
int leftAns = subtreeSizes.get(left) - 1 + f(right, node, tree, subtreeSizes);
int rightAns = subtreeSizes.get(right) - 1 + f(left, node, tree, subtreeSizes);
return Math.max(leftAns, rightAns);
}
public static void main(String[] args) {
int testcases = io.nextInt();
// int testcases = 1;
for(int zqt = 0; zqt < testcases; zqt ++) {
int n = io.nextInt();
int q = io.nextInt();
int[][] arr = new int[1001][1001];
for (int i = 0; i < n; i++) {
int h = io.nextInt();
int w = io.nextInt();
arr[h][w] += 1;
}
long[][] prefix = new long[1001][1001];
for (int i = 1; i < 1001; i++) {
for (int j = 1; j < 1001; j++) {
long cur = (long)arr[i][j] * i * j;
prefix[i][j] = cur + prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1];
}
}
for(int i = 0; i < q; i++) {
int h1 = io.nextInt();
int w1 = io.nextInt();
int h2 = io.nextInt();
int w2 = io.nextInt();
if(h2 == h1 + 1 || w2 == w1 + 1) {
io.println(0);
continue;
}
int x1 = h1 + 1;
int y1 = w1 + 1;
int x2 = h2 - 1;
int y2 = w2 - 1;
long res = prefix[x2][y2] - prefix[x2][y1 - 1] - prefix[x1 - 1][y2] + prefix[x1 - 1][y1 - 1];
io.println(res);
}
}
io.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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
67cd08c9216c3c2ef307bc60450babf8
|
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 javax.print.DocFlavor;
import java.util.*;
import java.io.*;
public class Main {
static String ss, io[];
static int test = 1, n, m, N = 100010;
static long[][] a = new long[1001][1001], ps = new long[1001][1001];
static void solve() throws IOException{
n = ni(); m = ni();
for (int i = 0;i <= 1000;i++){
for (int j = 0;j <= 1000;j++){
ps[i][j] = a[i][j] = 0;
}
}
for (int i = 0;i < n;i++){
int x = ni(), y = ni();
a[x][y] += (long) x * y;
}
for (int i = 1;i <= 1000;i++){
for (int j = 1;j <= 1000;j++){
ps[i][j] = a[i][j]+ps[i-1][j]+ps[i][j-1]-ps[i-1][j-1];
}
}
for (int i = 0;i < m;i++){
int x1 = ni(), y1 = ni(), x2 = ni(), y2 = ni();
out.println(ps[x2-1][y2-1]-ps[x1][y2-1]-ps[x2-1][y1]+ps[x1][y1]);
}
}
public static void main(String[] args) throws Exception {
test = ni(in.readLine());
while (test-- > 0){
solve();
}
out.flush();
}
static int ni() throws IOException{input.nextToken();return (int) input.nval;}
static long nl() throws IOException{input.nextToken();return (long) input.nval;}
static int ni(String x) {return Integer.parseInt(x);}
static long nl(String x) {return Long.parseLong(x);}
static int max(int a, int b) {return a > b ? a : b;}
static int min(int a, int b) {return a < b ? a : b;}
static int abs(int a) {return a > 0?a:-a;}
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer input = new StreamTokenizer(in);
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
76d5ca9af8527153aa8d427f134964ea
|
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
|
//package com.rajan.codeforces.contests.contest817;
import java.io.*;
public class ProblemE {
private static int MAX = 1001;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int tt = Integer.parseInt(reader.readLine());
while (tt-- > 0) {
int[] input = parseInt(reader.readLine(), 2);
int n = input[0], q = input[1];
long[][] grid = new long[MAX][MAX];
for (int i = 0; i < n; i++) {
input = parseInt(reader.readLine(), 2);
int hi = input[0], wi = input[1];
grid[hi][wi] += hi * 1L * wi;
}
long[][] rowPrefix = new long[MAX][MAX];
long[][] colPrefix = new long[MAX][MAX];
long[][] squareSum = new long[MAX][MAX];
for (int i = 0; i < MAX; i++)
for (int j = 0; j < MAX; j++) {
if (j == 0)
rowPrefix[i][j] = grid[i][j];
else
rowPrefix[i][j] += rowPrefix[i][j - 1] + grid[i][j];
}
for (int j = 0; j < MAX; j++)
for (int i = 0; i < MAX; i++) {
if (i == 0)
colPrefix[i][j] = grid[i][j];
else
colPrefix[i][j] += colPrefix[i - 1][j] + grid[i][j];
}
for (int i = 0; i < MAX; i++)
for (int j = 0; j < MAX; j++) {
if (i == 0 && j == 0)
squareSum[i][j] = grid[i][j];
else if (i == 0)
squareSum[i][j] = grid[i][j - 1] + grid[i][j];
else if (j == 0)
squareSum[i][j] = colPrefix[i - 1][j] + grid[i][j];
else
squareSum[i][j] = squareSum[i - 1][j - 1] + rowPrefix[i][j - 1] + colPrefix[i - 1][j] + grid[i][j];
}
for (int i = 0; i < q; i++) {
input = parseInt(reader.readLine(), 4);
int hs = input[0], ws = input[1], hb = input[2], wb = input[3];
long ans = squareSum[hb - 1][wb - 1] - squareSum[hb - 1][ws] - squareSum[hs][wb - 1] + squareSum[hs][ws];
writer.write(ans + "\n");
}
}
writer.flush();
}
private static int[] parseInt(String str, int n) {
int[] ans = new int[n];
int idx = 0;
for (int k = 0; k < str.length(); ) {
int j = k;
int sum = 0;
while (j < str.length() && str.charAt(j) != ' ') {
if (str.charAt(j) == '-') {
j++;
continue;
}
sum = sum * 10 + str.charAt(j) - '0';
j++;
}
ans[idx++] = (str.charAt(k) == '-' ? -1 : 1) * sum;
k = j + 1;
}
return 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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
0205d42182b8f19ad70875d3151c31da
|
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class File {
public static void main(String args[]) {
FastScanner fs = new FastScanner();
int testcases = fs.nextInt();
while(testcases-- > 0) {
final int nmm = 1005;
long[][] pre = new long[nmm][nmm];
int n = fs.nextInt();
int q = fs.nextInt();
for(int i=0;i<n;i++) {
int h = fs.nextInt();
int w = fs.nextInt();
pre[h][w] += h*w;
}
for(int i=1;i<nmm;i++) {
for(int j=1;j<nmm;j++) {
pre[i][j] += pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1];
}
}
while(q-- > 0) {
int hs = fs.nextInt(),ws = fs.nextInt();
int hb = fs.nextInt(),wb = fs.nextInt();
hb--;
wb--;
long ans = pre[hb][wb]-pre[hs][wb]-pre[hb][ws]+pre[hs][ws];
System.out.println(ans);
}
}
}
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());
}
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());
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
cb949f63924a54a3d78af613a85f7a17
|
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.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class E_Counting_Rectangles {
public static void s() {
int n = sc.nextInt(), q = sc.nextInt();
long[][] rmap = new long[1002][1002];
for(int i=0; i<n; i++) {
long h = sc.nextInt();
long w = sc.nextInt();
rmap[(int)h][(int)w]+=h*w;
}
for(int i=rmap.length-1; i>=0; i--) {
for(int j=rmap.length-1; j>=0; j--) {
if(i == rmap.length-1 && j ==rmap.length-1) continue;
else if(i == rmap.length-1) rmap[i][j]+=rmap[i][j+1];
else if(j == rmap.length-1) rmap[i][j]+=rmap[i+1][j];
else rmap[i][j]+=rmap[i+1][j]+rmap[i][j+1]-rmap[i+1][j+1];
}
}
// for(int i=1; i<=10; i++) {
// for(int j=1; j<=10; j++) {
// p.writes(rmap[i][j]);
// }
// p.writeln();
// }
while(q-- != 0) {
int hs = sc.nextInt(), ws = sc.nextInt();
int hb = sc.nextInt(), wb = sc.nextInt();
long ans = (rmap[hs+1][ws+1] - rmap[hb][ws+1]) + (rmap[hb][wb] -rmap[hs+1][wb]);
p.writeln(ans);
}
}
public static void main(String[] args) {
int t = 1;
t = sc.nextInt();
while (t-- != 0) {
s();
}
p.print();
}
public static boolean debug = false;
static void debug(String st) {
if(debug) p.writeln(st);
}
static final Integer MOD = (int) 1e9 + 7;
static final FastReader sc = new FastReader();
static final Print p = new Print();
static class Functions {
static void sort(int... a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
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 int max(int... a) {
int max = Integer.MIN_VALUE;
for (int val : a) max = Math.max(val, max);
return max;
}
static int min(int... a) {
int min = Integer.MAX_VALUE;
for (int val : a) min = Math.min(val, min);
return min;
}
static long min(long... a) {
long min = Long.MAX_VALUE;
for (long val : a) min = Math.min(val, min);
return min;
}
static long max(long... a) {
long max = Long.MIN_VALUE;
for (long val : a) max = Math.max(val, max);
return max;
}
static long sum(long... a) {
long sum = 0;
for (long val : a) sum += val;
return sum;
}
static int sum(int... a) {
int sum = 0;
for (int val : a) sum += val;
return sum;
}
public static long mod_add(long a, long b) {
return (a % MOD + b % MOD + MOD) % MOD;
}
public static long pow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1) != 0)
res = mod_mul(res, a);
a = mod_mul(a, a);
b >>= 1;
}
return res;
}
public static long mod_mul(long a, long b) {
long res = 0;
a %= MOD;
while (b > 0) {
if ((b & 1) > 0) {
res = mod_add(res, a);
}
a = (2 * a) % MOD;
b >>= 1;
}
return res;
}
public static long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
public static long factorial(long n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res = (i % MOD * res % MOD) % MOD;
}
return res;
}
public static int count(int[] arr, int x) {
int count = 0;
for (int val : arr) if (val == x) count++;
return count;
}
public static ArrayList<Integer> generatePrimes(int n) {
boolean[] primes = new boolean[n];
for (int i = 2; i < primes.length; i++) primes[i] = true;
for (int i = 2; i < primes.length; i++) {
if (primes[i]) {
for (int j = i * i; j < primes.length; j += i) {
primes[j] = false;
}
}
}
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < primes.length; i++) {
if (primes[i]) arr.add(i);
}
return arr;
}
}
static class Print {
StringBuffer strb = new StringBuffer();
public void write(Object str) {
strb.append(str);
}
public void writes(Object str) {
char c = ' ';
strb.append(str).append(c);
}
public void writeln(Object str) {
char c = '\n';
strb.append(str).append(c);
}
public void writeln() {
char c = '\n';
strb.append(c);
}
public void yes() {
char c = '\n';
writeln("YES");
}
public void no() {
writeln("NO");
}
public void writes(int... arr) {
for (int val : arr) {
write(val);
write(' ');
}
}
public void writes(long... arr) {
for (long val : arr) {
write(val);
write(' ');
}
}
public void writeln(int... arr) {
for (int val : arr) {
writeln(val);
}
}
public void print() {
System.out.print(strb);
}
public void println() {
System.out.println(strb);
}
}
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());
}
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;
}
double[] readArrayDouble(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
String nextLine() {
String str = new String();
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
649b817145ebe835ae5d3e5d30b0d5d0
|
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
|
/*
بسم الله الرحمن الرحيم
/$$$$$ /$$$$$$ /$$ /$$ /$$$$$$
|__ $$ /$$__ $$ |$$ |$$ /$$__ $$
| $$| $$ \ $$| $$|$$| $$ \ $$
| $$| $$$$$$$$| $$ / $$/| $$$$$$$$
/ $$ | $$| $$__ $$ \ $$ $$/ | $$__ $$
| $$ | $$| $$ | $$ \ $$$/ | $$ | $$
| $$$$$$/| $$ | $$ \ $/ | $$ | $$
\______/ |__/ |__/ \_/ |__/ |__/
/$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$
| $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$
| $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$
| $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/
| $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$
| $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$
| $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$
|__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/
TREESET.HIGHER() METHOD GIVES THE LEAST STRICTLY GREATER VALUE THAN THE PARAMETER;
IF NO SUCH ELEMENT EXISTS IT RETURN NULL
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class E817 {
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
try {
// FAST SORT TO PASS---->arr = Arrays.stream(arr).boxed().sorted().mapToInt($->$).toArray();
// Scanner sc=new Scanner(System.in);
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n=sc.nextInt();
int q=sc.nextInt();
long[][] dp=new long[1001][1001];
for(int i=0;i<n;i++){
long h=sc.nextLong();
long w=sc.nextLong();
dp[(int)h][(int)w]+=h*w;
}
for(int i=1;i<=1000;i++){
for(int j=1;j<=1000;j++){
dp[i][j]+=(dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]);
}
}
// for(int i=0;i<10;i++){
// for(int j=0;j<10;j++)System.out.print(dp[i][j]+" ");
// System.out.println();
// }
while(q-->0){
int hl=sc.nextInt()+1;
int wl=sc.nextInt()+1;
int hr=sc.nextInt()-1;
int wr=sc.nextInt()-1;
long ans=dp[hr][wr]-dp[hl-1][wr]-dp[hr][wl-1]+dp[hl-1][wl-1];
System.out.println(ans);
}
/*int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}*/
/*long[] arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextLong();
}*/
//System.out.println(gcd_int(4, 6));
}
} catch (Exception e) {
return;
}
}
public static int lowerbound(int[] ar,int k)
{
int s=0;
int e=ar.length;
while (s !=e)
{
int mid = s+e>>1;
if (ar[mid] <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.length)
{
return -1;
}
return s;
}
public static class pair {
int ff;
int ss;
pair(int ff, int ss) {
this.ff = ff;
this.ss = ss;
}
}
static int BS(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] == element) {
return low;
} else if (arr[high] == element) {
return high;
}
return -1;
}
static int lower_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] >= element) {
return low;
} else if (arr[high] >= element) {
return high;
}
return -1;
}
static int upper_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] <= element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] > element) {
return low;
} else if (arr[high] > element) {
return high;
}
return -1;
}
public static int upperbound(long[] arr, int k) {
int s = 0;
int e = arr.length;
while (s != e) {
int mid = s + e >> 1;
if (arr[mid] <= k) {
s = mid + 1;
} else {
e = mid;
}
}
if (s == arr.length) {
return -1;
}
return s;
}
public static long pow(long x,long y,long mod){
if(x==0)return 0l;
if(y==0)return 1l;
//(x^y)%mod
if(y%2l==1l){
return ((x%mod)*(pow(x,y-1l,mod)%mod))%mod;
}
return pow(((x%mod)*(x%mod))%mod,y/2l,mod);
}
public static long gcd_long(long a, long b) {
// a/b,a-> dividant b-> divisor
if (b == 0)
return a;
return gcd_long(b, a % b);
}
public static int gcd_int(int a, int b) {
// a/b,a-> dividant b-> divisor
if (b == 0)
return a;
return gcd_int(b, a % b);
}
public static int lcm(int a, int b) {
int gcd = gcd_int(a, b);
return (a * b) / gcd;
}
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());
}
double nextDouble(){
return Double.valueOf(Integer.parseInt(next()));
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
Long nextLong() {
return Long.parseLong(next());
}
}
public static boolean contains(String main, String Substring) {
boolean flag=false;
if(main==null && main.trim().equals("")) {
return flag;
}
if(Substring==null) {
return flag;
}
char fullstring[]=main.toCharArray();
char sub[]=Substring.toCharArray();
int counter=0;
if(sub.length==0) {
flag=true;
return flag;
}
for(int i=0;i<fullstring.length;i++) {
if(fullstring[i]==sub[counter]) {
counter++;
} else {
counter=0;
}
if(counter==sub.length) {
flag=true;
return flag;
}
}
return flag;
}
}
/*
* public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){
* return true; } if(n<1 || m<1 || k<0){ return false; } boolean
* tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; }
* return false; }
*/
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
6bb5f538a63dd50b345d3e8f911ea239
|
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
|
//package random;
import java.util.*;
import java.io.*;
public class CF {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int q = Integer.parseInt(br.readLine());
while(q-->0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());
long[][] arr = new long[1001][1001];
for (int i=0; i<n; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
arr[a][b]+=((long)a*b);
}
for (int i=1; i<1001; i++) {
for (int j=1; j<1001; j++) {
arr[i][j]+=arr[i][j-1];
arr[i][j]+=arr[i-1][j];
arr[i][j]-=arr[i-1][j-1];
}
}
while(t-->0) {
st = new StringTokenizer(br.readLine());
int h_s = Integer.parseInt(st.nextToken());
int w_s = Integer.parseInt(st.nextToken());
int h_b = Integer.parseInt(st.nextToken());
int w_b = Integer.parseInt(st.nextToken());
System.out.println(arr[h_b-1][w_b-1]-arr[h_b-1][w_s]-arr[h_s][w_b-1]+arr[h_s][w_s]);
}
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
2e3628a99c413bc04a568dffd151552c
|
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.*;
import java.lang.reflect.Array;
import java.util.*;
public class Submit {
public static void main(String[] args) {
solve();
}
private static void solve() {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
// write any code from here//
int n = sc.nextInt();
int q = sc.nextInt();
int h[] = new int[n];
int w[] = new int[n];
long arr[][] = new long[1001][1001]; // 2d prefix sum array
int H = 1000;
int W = 1000;
for (int i = 0; i < n; i++) {
h[i] = sc.nextInt();
w[i] = sc.nextInt();
arr[h[i]][w[i]] += h[i] * w[i];
}
// 2-D prefixSum
// rows
for (int i = 0; i <= H; i++) {
long sum = 0;
for (int j = 0; j <= W; j++) {
sum += arr[i][j];
arr[i][j] = sum;
}
}
// cols
for (int j = 0; j <= W; j++) {
long sum = 0;
for (int i = 0; i <= H; i++) {
sum += arr[i][j];
arr[i][j] = sum;
}
}
while (q-- > 0) {
int hs = sc.nextInt()+1;
int ws = sc.nextInt()+1;
int hb = sc.nextInt()-1;
int wb = sc.nextInt()-1;
long area = arr[hb][wb] - arr[hs - 1][wb] - arr[hb][ws - 1] + arr[hs - 1][ws - 1];
System.out.println(area);
}
}
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
0944d01b31a570cd3295e95db3af6086
|
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.*;
import java.util.*;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
public class Solution{
public 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;
}
}
public static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static class Pair{
int s;
int end;
public Pair(int a, int e) {
end = e;
s = a;
}
}
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) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
public static void main(String[] args) throws Exception
{
FastReader scn = new FastReader();
OutputStream outputStream = System.out;
OutputWriter output = new OutputWriter(outputStream);
int t = scn.nextInt();
long mod = 1000000007L;
// String[] pal = {"0000", "0110","0220","0330","0440","0550","1001","1111","1221","1331","1441","1551","2002","2112","2222","2332"};
while(t>0) {
// int n = scn.nextInt();
// long max =0, min= Integer.MAX_VALUE;
// long[] a = new long[n];
// for(int i=0;i<n;i++) {
// a[i]= scn.nextLong();
// max = Math.max(max,a[i]);
// min = Math.min(min, a[i]);
// }
int n = scn.nextInt();
int q = scn.nextInt();
long[][] a = new long[n][2];
long[][] m1 = new long[1001][1001];
for(int i=0;i<n;i++) {
a[i][0] = scn.nextLong();
a[i][1] = scn.nextLong();
m1[(int) a[i][0]][(int)(a[i][1])]++;
}
for(long key = 1;key<=1000;key++) {
for(int i=1;i<=1000;i++) {
m1[(int) key][i] = (long)(m1[(int) key][i-1])+(long)(i)*m1[(int) key][i];
}
}
while(q>0) {
long hs = scn.nextLong();
long ws = scn.nextLong();
long hb = scn.nextLong();
long wb = scn.nextLong();
long ans = 0;
for(long key=1;key<=1000;key++) {
if(key>hs && key<hb) {
ans=ans+key*(long)(m1[(int) key][(int)(wb)-1]-m1[(int) key][(int)(ws)]);
}
}
output.println(ans);
q--;
}
t--;
}
output.close();
}
public static int checkL(char[][] a, int r, int c, boolean[][] mark) {
int cnt=0;
for(int i=Math.max(r-1,0);i<=Math.min(r+2,a.length-1);i++) {
for(int j= Math.max(c-1,0);j<=Math.min(c+2,a[0].length-1);j++) {
if(a[i][j]=='*') {
cnt++;
}
}
}
char ch11 = a[r][c];
char ch12 = a[r][c+1];
char ch21 = a[r+1][c];
char ch22 = a[r+1][c+1];
int n = a.length;
int m = a[0].length;
if(cnt==4) {
if(ch22!='*' && r+2<n && c+2<m && a[r+2][c+2]=='*') {
cnt--;
}else if(ch21!='*' && r+2<n && c-1>=0 && a[r+2][c-1]=='*') {
cnt--;
}else if(ch11!='*' && r-1>=0 && c-1>=0 && a[r-1][c-1]=='*') {
cnt--;
}else if(ch12!='*' && r-1>=0 && c+2<m && a[r-1][c+2]=='*') {
cnt--;
}
}
if(cnt==0) {
return 1;
}
if(cnt>3) {
return 4;
}
if(cnt<3) {
return 2;
}
if((ch11=='*' && ch12=='*' && ch22=='*' && ch21!='*')|| (ch11=='*' && ch21=='*' && ch22=='*' && ch12!='*') || (ch12=='*'&&ch22=='*'&&ch21=='*'&&ch11!='*')||(ch11=='*'&&ch12=='*'&&ch21=='*'&&ch22!='*')) {
for(int i=r;i<r+2;i++) {
for(int j=c;j<c+2;j++) {
if(a[i][j]=='*') {
mark[i][j] = true;
}
}
}
return 1;
}
return 1;
}
public static void addTime(int h, int m, int ah, int am) {
int rm = m+am;
int rh = (h+ah+(rm/60))%24;
rm = rm%60;
}
public static long power(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
3ff8ca99afb25e7fb8362edfdabbd11a
|
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.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
// graph, dfs,bfs, get connected components,iscycle, isbipartite, dfs on trees
public class Solution{
static class Graph{
public static class Vertex{
HashMap<Integer,Long> nb= new HashMap<>(); // for neighbours of each vertex
}
public static HashMap<Integer,Vertex> vt; // for vertices(all)
public Graph(){
vt= new HashMap<>();
}
public static int numVer(){
return vt.size();
}
public static boolean contVer(int ver){
return vt.containsKey(ver);
}
public static void addVer(int ver){
Vertex v= new Vertex();
vt.put(ver,v);
}
public static void addEdge(int ver1, int ver2, long weight){
if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){
return;
}
Vertex v1= vt.get(ver1);
Vertex v2= vt.get(ver2);
v1.nb.put(ver2,(long)weight); // if previously there is an edge, then this replaces that edge
v2.nb.put(ver1,(long)weight);
}
public static void delEdge(int ver1, int ver2){
if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){
return;
}
vt.get(ver1).nb.remove(ver2);
vt.get(ver2).nb.remove(ver1);
}
public static void delVer(int ver){
if(!vt.containsKey(ver)){
return;
}
Vertex v1= vt.get(ver);
ArrayList<Integer> arr= new ArrayList<>(v1.nb.keySet());
for (int i = 0; i <arr.size() ; i++) {
int s= arr.get(i);
vt.get(s).nb.remove(ver);
}
vt.remove(ver);
}
static boolean done[];
static int parent[];
static ArrayList<Integer>vals= new ArrayList<>();
public static boolean isCycle(int i){
Stack<Integer>stk= new Stack<>();
stk.push(i);
while(!stk.isEmpty()){
int x= stk.pop();
vals.add(x);
// System.out.print("current="+x+" stackinit="+stk);
if(!done[x]){
done[x]=true;
}
else if(done[x] ){
return true;
}
ArrayList<Integer>ar= new ArrayList<>(vt.get(x).nb.keySet());
for (int j = 0; j <ar.size() ; j++) {
if(parent[x]!=ar.get(j)){
parent[ar.get(j)]=x;
stk.push(ar.get(j));
}
}
// System.out.println(" stackfin="+stk);
}
return false;
}
static int[]level;
static int[] curr;
static int[] fin;
public static void dfs(int src){
done[src]=true;
level[src]=0;
Queue<Integer>q= new LinkedList<>();
q.add(src);
while(!q.isEmpty()){
int x= q.poll();
ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet());
for (int i = 0; i <arr.size() ; i++) {
int v= arr.get(i);
if(!done[v]){
level[v]=level[x]+1;
done[v]=true;
q.offer(v);
}
}
}
}
static int oc[];
static int ec[];
public static void dfs1(int src){
Queue<Integer>q= new LinkedList<>();
q.add(src);
done[src]= true;
// int count=0;
while(!q.isEmpty()){
int x= q.poll();
// System.out.println("x="+x);
int even= ec[x];
int odd= oc[x];
if(level[x]%2==0){
int val= (curr[x]+even)%2;
if(val!=fin[x]){
// System.out.println("bc");
even++;
vals.add(x);
}
}
else{
int val= (curr[x]+odd)%2;
if(val!=fin[x]){
// System.out.println("bc");
odd++;
vals.add(x);
}
}
ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet());
// System.out.println(arr);
for (int i = 0; i <arr.size() ; i++) {
int v= arr.get(i);
if(!done[v]){
done[v]=true;
oc[v]=odd;
ec[v]=even;
q.add(v);
}
}
}
}
static long popu[];
static long happy[];
static long count[]; // total people crossing that pos
static long sum[]; // total sum of happy people including that.
HashMap<Integer, ArrayList<Integer>>map= new HashMap<>();
public static void bfs(int x,ArrayList<Long>curr){
}
public static void bfs1(int x){
done[x]=true;
// long total= popu[x];
long smile= 0;
ArrayList<Integer>nbrs= new ArrayList<>(vt.get(x).nb.keySet());
for (int i = 0; i <nbrs.size() ; i++) {
int r= nbrs.get(i);
if(!done[r]){
bfs1(r);
// total+=count[r];
smile+=happy[r];
}
}
// count[x]=total;
sum[x]=smile;
}
}
static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
static class Pair implements Comparable<Pair> {
int val;
int pos;
public Pair(int val, int pos) {
this.pos = pos;
this.val = val;
}
@Override
public String toString() {
return val + " " + pos;
}
@Override
public int compareTo(Pair o) {
if(this.pos >o.pos){
return 1;
}
else if(this.pos==o.pos){
return 0;
}
else{
return -1;
}
}}
static class Pair1 implements Comparable<Pair1> {
int val;
int pos;
public Pair1(int val, int pos) {
this.pos = pos;
this.val = val;
}
@Override
public String toString() {
return val + " " + pos;
}
@Override
public int compareTo(Pair1 o) {
if(this.val >o.val){
return 1;
}
else if(this.val==o.val){
return 0;
}
else{
return -1;
}
}
}
static ArrayList<Pair> findSubArrays(long[] arr, int n)
{
// create an empty map
HashMap<Long,ArrayList<Integer>> map = new HashMap<>();
// create an empty vector of pairs to store
// subarray starting and ending index
ArrayList<Pair> out = new ArrayList<>();
// Maintains sum of elements so far
long sum = 0;
for (int i = 0; i < n; i++)
{
// add current element to sum
sum += arr[i];
// if sum is 0, we found a subarray starting
// from index 0 and ending at index i
if (sum == 0)
out.add(new Pair(0, i));
ArrayList<Integer> al = new ArrayList<>();
// If sum already exists in the map there exists
// at-least one subarray ending at index i with
// 0 sum
if (map.containsKey(sum))
{
// map[sum] stores starting index of all subarrays
al = map.get(sum);
for (int it = 0; it < al.size(); it++)
{
out.add(new Pair(al.get(it) + 1, i));
}
}
al.add(i);
map.put(sum, al);
}
return out;
}
// After writing solution, quick scan for:
// array out of bounds
// special cases e.g. n=1?
//
// Big numbers arithmetic bugs:
// int overflow
// sorting, or taking max, or negative after MOD
static long mod= (long)(1e8);
static long dp[][][][];
static BufferedWriter out;
public static void main(String[] args) throws IOException {
Reader.init(System.in);
out = new BufferedWriter(new OutputStreamWriter(System.out));
int t= Reader.nextInt();
for (int tt = 0; tt <t ; tt++) {
int n= Reader.nextInt();
int q= Reader.nextInt();
long matrix[][]= new long[1000+1][1000+1];
for (int i = 0; i <n ; i++) {
int x1= Reader.nextInt();
int x2= Reader.nextInt();
matrix[x1][x2]+= (long) x1 *x2;
; }
// for (int i = 0; i <5 ; i++) {
// for (int j = 0; j <5 ; j++) {
// System.out.print(matrix[i][j]+" ");
// }
// System.out.println();
// }
// System.out.println();
// System.out.println();
long psums[][]= new long[1000+1][1000+1];
for (int i = 1; i <=1000 ; i++) {
for (int j = 1; j <=1000 ; j++) {
psums[i][j]= matrix[i][j]+psums[i-1][j] + psums[i][j-1] - psums[i-1][j-1];
}
}
//
// for (int i = 0; i <5 ; i++) {
// for (int j = 0; j <5 ; j++) {
// System.out.print(psums[i][j]+" ");
// }
// System.out.println();
// }
for (int i = 0; i <q ; i++) {
int x1= Reader.nextInt()+1;
int y1= Reader.nextInt()+1;
int x2= Reader.nextInt()-1;
int y2= Reader.nextInt()-1;
long ans= psums[x2][y2] -psums[x1-1][y2] -psums[x2][y1-1] + psums[x1-1][y1-1];
out.append(ans+"\n");
}
}
out.flush();
out.close();
}
public static String convert(String s,int n){
if(s.length()==n){
return s;
}
else{
int x= s.length();
int v=n-x;
String str="";
StringBuilder s2= new StringBuilder();
for (int i = 0; i <v ; i++) {
s2.append('0');
}
StringBuilder s1= new StringBuilder(s);
s2.append(s1);
// str+=s;
String q= s2.toString();
return q;
}
}
public static int lower(ArrayList<Integer>arr,int key){
int low = 0;
int high = arr.size()-1;
while(low < high){
int mid = low + (high - low)/2;
if(arr.get(mid) >= key){
high = mid;
}
else{
low = mid+1;
}
}
return low;
}
public static int upper(ArrayList<Integer>arr,int key){
int low = 0;
int high = arr.size()-1;
while(low < high){
int mid = low + (high - low+1)/2;
if(arr.get(mid) <= key){
low = mid;
}
else{
high = mid-1;
}
}
return low;
}
static long modExp(long a, long b, long mod) {
//System.out.println("a is " + a + " and b is " + b);
if (a==1) return 1;
long ans = 1;
while (b!=0) {
if (b%2==1) {
ans = (ans*a)%mod;
}
a = (a*a)%mod;
b/=2;
}
return ans;
}
public static long modmul(long a, long b, long mod) {
return b == 0 ? 0 : ((modmul(a, b >> 1, mod) << 1) % mod + a * (b & 1)) % mod;
}
static long sum(long n){
// System.out.println("lol="+ (n*(n-1))/2);
return (n*(n+1))/2;
}
public static ArrayList<Integer> Sieve(int n) {
boolean arr[]= new boolean [n+1];
Arrays.fill(arr,true);
arr[0]=false;
arr[1]=false;
for (int i = 2; i*i <=n ; i++) {
if(arr[i]){
for (int j = 2; j <=n/i ; j++) {
int u= i*j;
arr[u]=false;
}}
}
ArrayList<Integer> ans= new ArrayList<>();
for (int i = 0; i <n+1 ; i++) {
if(arr[i]){
ans.add(i);
}
}
return ans;
}
static long power( long x, long y, long p)
{
long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0)
{
if((y & 1)==1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static long ceil_div(long a, long b){
return (a+b-1)/b;
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b)
{
return (a*b)/gcd(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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
70d19654a080641fd5b03d3819174ceb
|
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
|
//Some of the methods are copied from GeeksforGeeks Website
import java.util.*;
import java.lang.*;
import java.io.*;
@SuppressWarnings("unchecked")
public class E_Counting_Rectangles
{
//static Scanner sc=new Scanner(System.in);
//static Reader sc=new Reader();
static FastReader sc=new FastReader(System.in);
static long mod = (long)(1e9)+ 7;
static int max_num=(int)1e5+5;
static List<Integer> gr[];
public static void main (String[] args) throws java.lang.Exception
{
try{
/* out.println("Case #"+tt+": "+ans );
gr=new ArrayList[n];
for(int i=0;i<n;i++) gr[i]=new ArrayList<>();
while(l<=r)
{
int m=l+(r-l)/2;
if(val(m))
{
ans=m;
l=m+1;
}
else
r=m-1;
}
Collections.sort(al,Collections.reverseOrder());
StringBuilder sb=new StringBuilder(""); sb.append(cur); sb=sb.reverse(); String rev=sb.toString();
map.put(a[i],map.getOrDefault(a[i],0)+1);
map.putIfAbsent(x,new ArrayList<>());
long n=sc.nextLong();
String s=sc.next();
char a[]=s.toCharArray();
*/
int t = sc.nextInt();
for(int tt=1;tt<=t;tt++)
{
int n=sc.nextInt();
int q=sc.nextInt();
int max=1005;
long a[][]=new long[max][max];
long p[][]=new long[max][max];
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<max;i++)
{
for(int j=1;j<max;j++)
{
p[i][j]=p[i-1][j]+p[i][j-1]-p[i-1][j-1]+a[i][j];
}
}
while(q-->0)
{
int hs=sc.nextInt();
int ws=sc.nextInt();
int hb=sc.nextInt();
int wb=sc.nextInt();
long ans=p[hb-1][wb-1]-p[hb-1][ws]-p[hs][wb-1]+p[hs][ws];
out.println(ans);
}
}
out.flush();
out.close();
}
catch(Exception e)
{}
}
/*
...SOLUTION ENDS HERE...........SOLUTION ENDS HERE...
*/
static void flag(boolean flag)
{
out.println(flag ? "YES" : "NO");
out.flush();
}
/*
Map<Long,Long> map=new HashMap<>();
for(int i=0;i<n;i++)
{
if(!map.containsKey(a[i]))
map.put(a[i],1);
else
map.replace(a[i],map.get(a[i])+1);
}
Set<Map.Entry<Long,Long>> hmap=map.entrySet();
for(Map.Entry<Long,Long> data : hmap)
{
}
Iterator<Integer> itr = set.iterator();
while(itr.hasNext())
{
int val=itr.next();
}
*/
// static class Pair
// {
// int x,y;
// Pair(int x,int y)
// {
// this.x=x;
// this.y=y;
// }
// }
// Arrays.sort(p, new Comparator<Pair>()
// {
// @Override
// public int compare(Pair o1,Pair o2)
// {
// if(o1.x>o2.x) return 1;
// else if(o1.x==o2.x)
// {
// if(o1.y>o2.y) return 1;
// else return -1;
// }
// else return -1;
// }});
static void print(int a[])
{
int n=a.length;
for(int i=0;i<n;i++)
{
out.print(a[i]+" ");
}
out.println();
out.flush();
}
static void print(long a[])
{
int n=a.length;
for(int i=0;i<n;i++)
{
out.print(a[i]+" ");
}
out.println();
out.flush();
}
static void print_int(ArrayList<Integer> al)
{
int si=al.size();
for(int i=0;i<si;i++)
{
out.print(al.get(i)+" ");
}
out.println();
out.flush();
}
static void print_long(ArrayList<Long> al)
{
int si=al.size();
for(int i=0;i<si;i++)
{
out.print(al.get(i)+" ");
}
out.println();
out.flush();
}
static void pn(int x)
{
out.println(x);
out.flush();
}
static void pn(long x)
{
out.println(x);
out.flush();
}
static void pn(String x)
{
out.println(x);
out.flush();
}
static int LowerBound(int a[], int x)
{
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(int a[], int x)
{// x is the key or target value
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
static void DFS(ArrayList<Integer> graph[],boolean[] visited, int u)
{
visited[u]=true;
int v=0;
for(int i=0;i<graph[u].size();i++)
{
v=graph[u].get(i);
if(!visited[v])
DFS(graph,visited,v);
}
}
static boolean[] prime(int num)
{
boolean[] bool = new boolean[num];
for (int i = 0; i< bool.length; i++) {
bool[i] = true;
}
for (int i = 2; i< Math.sqrt(num); i++) {
if(bool[i] == true) {
for(int j = (i*i); j<num; j = j+i) {
bool[j] = false;
}
}
}
if(num >= 0) {
bool[0] = false;
bool[1] = false;
}
return bool;
}
static long nCr(long a,long b,long mod)
{
return (((fact[(int)a] * modInverse(fact[(int)b],mod))%mod * modInverse(fact[(int)(a - b)],mod))%mod + mod)%mod;
}
static long fact[]=new long[max_num];
static void fact_fill()
{
fact[0]=1l;
for(int i=1;i<max_num;i++)
{
fact[i]=(fact[i-1]*(long)i);
if(fact[i]>=mod)
fact[i]%=mod;
}
}
static long modInverse(long a, long m)
{
return power(a, m - 2, m);
}
static long power(long x, long y, long m)
{
if (y == 0)
return 1;
long p = power(x, y / 2, m) % m;
p = (long)((p * (long)p) % m);
if (y % 2 == 0)
return p;
else
return (long)((x * (long)p) % m);
}
static long sum_array(int a[])
{
int n=a.length;
long sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum;
}
static long sum_array(long a[])
{
int n=a.length;
long sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum;
}
static void sort(int[] a)
{
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
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 void reverse_array(int a[])
{
int n=a.length;
int i,t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
static void reverse_array(long a[])
{
int n=a.length;
int i; long t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static class FastReader{
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan());
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public 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;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
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 (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
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
{
if (din == null)
return;
din.close();
}
}
static PrintWriter out=new PrintWriter(System.out);
static int int_max=Integer.MAX_VALUE;
static int int_min=Integer.MIN_VALUE;
static long long_max=Long.MAX_VALUE;
static long long_min=Long.MIN_VALUE;
}
// Thank You !
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
b72a64b245a19123bde779b65cfa15b3
|
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
|
/*
KEEP CALM AND जय श्री राम
* A B C D are easy just dont give up , you can do it!
* FIRST AND VERY IMP -> READ AND UNDERSTAND THE QUESTION VERY CAREFULLY.
* WARNING -> DON'T CODE BULLSHIT , ALWAYS CHECK THE LOGIC ON MULTIPLE TESTCASES AND EDGECASES BEFORE.
* SECOND -> TRY TO FIND RELEVENT PATTERN SMARTLY.
* WARNING -> IF YOU THINK YOUR SOLUION IS JUST DUMB DONT SUBMIT IT BEFORE RECHECKING ON YOUR END.
try try till you die; :):):)
*/
import java.util.*;
import java.io.*;
import java.util.stream.*;
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while (t-- > 0) {
long n = scn.nextLong();
long q = scn.nextLong();
long[][] a = new long[1001][1001];
long[][] pref = new long[1001][1001];
for(int i = 0 ; i < n ; i++){
int h = scn.nextInt();
int w = scn.nextInt();
a[h][w] += (long)h * w;
}
for(int i = 1 ; i <= 1000 ; i++){
for(int j = 1 ; j <= 1000 ; j++){
pref[i][j] = pref[i-1][j]+pref[i][j-1]-pref[i-1][j-1]+a[i][j];
}
}
for(int i = 0 ; i < q ; i++){
int hs = scn.nextInt();
int ws = scn.nextInt();
int hb = scn.nextInt();
int wb = scn.nextInt();
System.out.println(pref[hb-1][wb-1]-pref[hb-1][ws]-pref[hs][wb-1]+pref[hs][ws]);
}
}
}
public static void print(int k){
System.out.println(k);
}
public static void printString(String str){
System.out.println(str);
}
public static void printCharArray(char[] arr){
for(int i = 0 ; i < arr.length ; i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
// int ko char array m karne ke lia char[] arr = (n+"").toCharArray(); likhneka
public static <K, V> K getKey(Map<K, V> map, V value) {
for (Map.Entry<K, V> entry : map.entrySet()) {
if (value.equals(entry.getValue())) {
return entry.getKey();
}
}
return null;
}
public static void printArray(int[] arr) {
for (int i = 0 ; i < arr.length ; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void swap(int[] arr) {
for (int i = 0 ; i < arr.length ; i = i + 2) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
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;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int highestPowerof2(int n) {
int res = 0;
for (int i = n; i >= 1; i--) {
if ((i & (i - 1)) == 0) {
res = i;
break;
}
}
return res;
}
public static int countElement(int[] arr, int k) {
int cc = 0;
for (int i = 0 ; i < arr.length ; i++ ) {
if (arr[i] == k) {
cc++;
}
}
return cc;
}
public static long integerfrmbinary(String str) {
long j = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '1') {
j = j + (long)Math.pow(2, str.length() - 1 - i);
}
}
return (long) j;
}
static int maxInArray(int[] arr) {
int max = arr[0];
for (int i = 0 ; i < arr.length ; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
static void printN(){
System.out.println("NO");
}
static void printY(){
System.out.println("YES");
}
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());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for (int v : a) {
c0[(v & 0xff) + 1]++;
c1[(v >>> 8 & 0xff) + 1]++;
c2[(v >>> 16 & 0xff) + 1]++;
c3[(v >>> 24 ^ 0x80) + 1]++;
}
for (int i = 0; i < 0xff; i++) {
c0[i + 1] += c0[i];
c1[i + 1] += c1[i];
c2[i + 1] += c2[i];
c3[i + 1] += c3[i];
}
int[] t = new int[n];
for (int v : a)t[c0[v & 0xff]++] = v;
for (int v : t)a[c1[v >>> 8 & 0xff]++] = v;
for (int v : a)t[c2[v >>> 16 & 0xff]++] = v;
for (int v : t)a[c3[v >>> 24 ^ 0x80]++] = v;
return a;
}
static int[] EvenOddArragement(int nums[])
{
int i1 = 0, i2 = nums.length - 1;
while (i1 < i2) {
while (nums[i1] % 2 == 0 && i1 < i2) {
i1++;
}
while (nums[i2] % 2 != 0 && i2 > i1) {
i2--;
}
int temp = nums[i1];
nums[i1] = nums[i2];
nums[i2] = temp;
}
return nums;
}
static int gcd(int a, int b) {
while (b != 0) {
int t = a;
a = b;
b = t % b;
}
return a;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int DigitSum(int n)
{
int r = 0, sum = 0;
while (n >= 0)
{
r = n % 10;
sum = sum + r;
n = n / 10;
}
return sum;
}
static boolean checkPerfectSquare(int number)
{
double sqrt = Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static boolean isPowerOfTwo(int n)
{
if (n == 0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static boolean isPrime2(int n)
{
if (n <= 1)
{
return false;
}
if (n == 2)
{
return true;
}
if (n % 2 == 0)
{
return false;
}
for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
static String minLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for (int i = 0; i < n; i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[0];
}
static String maxLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for (int i = 0; i < n; i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[arr.length - 1];
}
static class P implements Comparable<P> {
int i, j;
public P(int i, int j) {
this.i = i;
this.j = j;
}
public int compareTo(P o) {
return Integer.compare(i, o.i);
}
}
static class pair {
int i, j;
pair(int x, int y) {
i = x;
j = y;
}
}
static int binary_search(int a[], int value)
{
int start = 0;
int end = a.length - 1;
int mid = start + (end - start) / 2;
while (start <= end)
{
if (a[mid] == value)
{
return mid;
}
if (a[mid] > value)
{
end = mid - 1;
}
else
{
start = mid + 1;
}
mid = start + (end - start) / 2;
}
return -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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
ddad434243eedd82818b469989fa0435
|
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.*;
import java.util.*;
public class CodeForces {
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
int testCases=Integer.parseInt(br.readLine());
while(testCases-->0){
String input[]=br.readLine().split(" ");
int n=Integer.parseInt(input[0]);
int q=Integer.parseInt(input[1]);
long dp[][]=new long[1001][1001];
for(int i=0;i<n;i++) {
input=br.readLine().split(" ");
int h=Integer.parseInt(input[0]);
int w=Integer.parseInt(input[1]);
dp[h][w]+=h*w;
}
for(int i=1;i<=1000;i++) {
for(int j=1;j<=1000;j++) {
dp[i][j]+=dp[i-1][j];
}
}
for(int i=1;i<=1000;i++) {
for(int j=1;j<=1000;j++) {
dp[i][j]+=dp[i][j-1];
}
}
for(int i=1;i<=q;i++) {
input=br.readLine().split(" ");
int h1=Integer.parseInt(input[0]);
int w1=Integer.parseInt(input[1]);
int h2=Integer.parseInt(input[2]);
int w2=Integer.parseInt(input[3]);
long ans=(dp[h2-1][w2-1]-dp[h2-1][w1]) -(dp[h1][w2-1]-dp[h1][w1]);
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
564b03611979ce8ac9b64f362d5f2425
|
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.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
public static void main(String[]args) throws IOException {
new Main().run();
}
void run() throws IOException{
new solve().setIO(System.in, System.out).run();
}
public class solve extends ioTask{
int t,n,i,j,len,h,m,k,x,y;
int a,b,c,d;
long ans[][]=new long[1005][1005];
public void run() throws IOException {
t=in.in();
while(t-->0){
for(i=1;i<=1000;i++) {
for(j=1;j<=1000;j++) {
ans[i][j]=0;
}
}
n=in.in();
m=in.in();
for(i=0;i<n;i++) {
x=in.in();
y=in.in();
ans[x][y]+=x*y;
}
for(int i=1;i<=1000;i++) {
for(int j=1;j<=1000;j++) {
ans[i][j]+=ans[i][j-1];
}
}
for(int i=1;i<=1000;i++) {
for(int j=1;j<=1000;j++) {
ans[i][j]+=ans[i-1][j];
}
}
while(m-->0)
{
a=in.in();
b=in.in();
c=in.in()-1;
d=in.in()-1;
out.println(ans[a][b]+ans[c][d]-ans[a][d]-ans[c][b]);
}
}
out.close();
}
}
class In{
private StringTokenizer in=new StringTokenizer("");
private InputStream is;
private BufferedReader bf;
public In(File file) throws IOException {
is=new FileInputStream(file);
init();
}
public In(InputStream is) throws IOException
{
this.is=is;
init();
}
private void init() throws IOException {
bf=new BufferedReader(new InputStreamReader(is));
}
boolean hasNext() throws IOException {
return in.hasMoreTokens()||bf.ready();
}
String ins() throws IOException {
while(!in.hasMoreTokens()) {
in=new StringTokenizer(bf.readLine());
}
return in.nextToken();
}
int in() throws IOException {
return Integer.parseInt(ins());
}
long inl() throws IOException {
return Long.parseLong(ins());
}
double ind() throws IOException {
return Double.parseDouble(ins());
}
}
class Out{
PrintWriter out;
private OutputStream os;
private void init() {
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));
}
public Out(File file) throws IOException {
os=new FileOutputStream(file);
init();
}
public Out(OutputStream os) throws IOException
{
this.os=os;
init();
}
}
class graph{
int[]to,nxt,head,x;
int cnt;
void init(int n) {
cnt=1;
for(int i=1;i<=n;i++)
{
head[i]=0;
}
}
public graph(int n,int m) {
to=new int[m+1];
nxt=new int[m+1];
head=new int[n+1];
x=new int[m+1];
cnt=1;
}
void add(int u,int v,int x) {
to[cnt]=v;
nxt[cnt]=head[u];
this.x[cnt]=x;
head[u]=cnt++;
}
}
abstract class ioTask{
In in;
PrintWriter out;
public ioTask setIO(File in,File out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(File in,OutputStream out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(InputStream in,OutputStream out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(InputStream in,File out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
void run()throws IOException{
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
03351323775fbb63e18e84d4e62983c3
|
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.*;
import java.util.*;
public class Main {
static FastReader scanner = new FastReader();
public static void solve() {
int TARGET = 1001;
int n = scanner.nextInt();
int q = scanner.nextInt();
long[][] arr = new long[TARGET][TARGET];
for(int i = 0; i < n; i++) {
int h = scanner.nextInt();
int w = scanner.nextInt();
arr[h][w]++;
}
for(int i = 1; i < TARGET; i++) {
for(int j = 1; j < TARGET; j++) {
arr[i][j] = arr[i][j] * i * j + arr[i - 1][j] + arr[i][j - 1] - arr[i - 1][j - 1];
}
}
for(int i = 0; i < q; i++) {
int hs = scanner.nextInt();
int ws = scanner.nextInt();
int hb = scanner.nextInt() - 1;
int wb = scanner.nextInt() - 1;
System.out.println(arr[hb][wb] + arr[hs][ws] -arr[hb][ws] - arr[hs][wb]);
}
}
public static void main(String[] args) throws Exception {
// int n = scanner.nextInt();
// while (n-- > 0) {
// solve();
// }
int n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
solve();
}
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
0f26a41a4712b534c1a9910666f5ec4b
|
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.*;
import java.util.*;
public class Main {
final static int mod = 998244353;
// final static int mod = (int)1e9+7;
static boolean[] prime = new boolean[1];
static int[][] dir1 = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
static int[][] dir2 = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
final static int inf = 0x3f3f3f3f;
static void yes() throws Exception {
print("YES");
}
static void no() throws Exception {
print("NO");
}
static {
for (int i = 2; i < prime.length; i++)
prime[i] = true;
for (int i = 2; i < prime.length; i++) {
if (prime[i]) {
for (int k = 2; i * k < prime.length; k++) {
prime[i * k] = false;
}
}
}
}
static class DSU {
int[] fa;
DSU(int n) {
fa = new int[n];
for (int i = 0; i < n; i++)
fa[i] = i;
}
int find(int t) {
if (t != fa[t])
fa[t] = find(fa[t]);
return fa[t];
}
void join(int x, int y) {
x = find(x);
y = find(y);
fa[x] = y;
}
}
static List<Integer>[] lists;
static void init(int n) {
lists = new List[n];
for(int i = 0; i< n;i++){
lists[i] = new ArrayList<>();
}
}
static class LCA{
int[] dep;
int[][] fa;
int[] log;
boolean[] v;
public LCA(int n){
dep = new int[n+5];
log = new int[n+5];
fa = new int[n+5][31];
v = new boolean[n+5];
for (int i = 2; i <= n; ++i) {
log[i] = log[i/2] + 1;
}
dfs(1,0);
}
private void dfs(int cur, int pre){
if(v[cur]) return;
v[cur] = true;
dep[cur] = dep[pre]+1;
fa[cur][0] = pre;
for (int i = 1; i <= log[dep[cur]]; ++i) {
fa[cur][i] = fa[fa[cur][i - 1]][i - 1];
}
for(int i : lists[cur]){
dfs(i,cur);
}
}
private int lca(int a, int b){
if(dep[a] > dep[b]){
int t = a;
a = b;
b = t;
}
while (dep[a] != dep[b]){
b = fa[b][log[dep[b] - dep[a]]];
}
if(a == b) return a;
for (int k = log[dep[a]]; k >= 0; k--) {
if (fa[a][k] != fa[b][k]) {
a = fa[a][k]; b = fa[b][k];
}
}
return fa[a][0];
}
}
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static int get() throws Exception {
String ss = bf.readLine();
if (ss.contains(" "))
ss = ss.substring(0, ss.indexOf(" "));
return Integer.parseInt(ss);
}
static long getx() throws Exception {
String ss = bf.readLine();
if (ss.contains(" "))
ss = ss.substring(0, ss.indexOf(" "));
return Long.parseLong(ss);
}
static int[] getint() throws Exception {
String[] s = bf.readLine().split(" ");
int[] a = new int[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(s[i]);
}
return a;
}
static long[] getlong() throws Exception {
String[] s = bf.readLine().split(" ");
long[] a = new long[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Long.parseLong(s[i]);
}
return a;
}
static String getstr() throws Exception {
return bf.readLine();
}
static void println() throws Exception {
bw.write("\n");
}
static void print(int a) throws Exception {
bw.write(a + "\n");
}
static void print(long a) throws Exception {
bw.write(a + "\n");
}
static void print(String a) throws Exception {
bw.write(a + "\n");
}
static void print(int[] a) throws Exception {
for (int i : a) {
bw.write(i + " ");
}
println();
}
static void print(long[] a) throws Exception {
for (long i : a) {
bw.write(i + " ");
}
println();
}
static void print(int[][] a) throws Exception {
for (int i[] : a)
print(i);
}
static void print(long[][] a) throws Exception {
for (long[] i : a)
print(i);
}
static void print(char[] a) throws Exception {
for (char i : a) {
bw.write(i + "");
}
println();
}
static long pow(long a, long b) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans *= a;
}
a *= a;
b >>= 1;
}
return ans;
}
static int powmod(long a, long b, int mod) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return (int) ans;
}
static void sort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static void sort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static void resort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[n - 1 - i];
}
static void resort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[n - 1 - i];
}
static int max(int a, int b) {
return Math.max(a, b);
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long max(long a, long b) {
return Math.max(a, b);
}
static long min(long a, long b) {
return Math.min(a, b);
}
static int max(int[] a) {
int max = a[0];
for (int i : a)
max = max(max, i);
return max;
}
static int min(int[] a) {
int min = a[0];
for (int i : a)
min = min(min, i);
return min;
}
static long max(long[] a) {
long max = a[0];
for (long i : a)
max = max(max, i);
return max;
}
static long min(long[] a) {
long min = a[0];
for (long i : a)
min = min(min, i);
return min;
}
static int abs(int a) {
return Math.abs(a);
}
static long abs(long a) {
return Math.abs(a);
}
static int[] getarr(List<Integer> list) {
int n = list.size();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = list.get(i);
return a;
}
public static void main(String[] args) throws Exception {
int T = 1;
T = get();
while (T-- > 0) {
int[] p= getint();
int n = p[0], q = p[1];
int[][] h = new int[n][2];
for(int i = 0; i< n;i++) h[i] = getint();
int g[][] = new int[1001][1001];
long s[][] = new long[1001][1001];
for(int i = 0;i < n;i++){
int x = h[i][0], y = h[i][1];
g[x][y]++;
}
for(int i = 1;i <= 1000;i++){
for(int j = 1;j <= 1000;j++){
s[i][j] = s[i][j-1];
if(g[i][j] > 0) s[i][j] += j*g[i][j];
}
}
for(int i = 0;i < q;i++){
p = getint();
long x = 0;
int a = p[0], b = p[1], c = p[2], d = p[3];
for(int j = a+1;j < c;j++){
x += j*(s[j][d-1]-s[j][b]);
}
print(x);
}
}
bw.flush();
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
c30b109848ab8bdeaed38b325d2b5489
|
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
|
// package c1722;
//
// Codeforces Round #817 (Div. 4) 2022-08-30 07:50
// E. Counting Rectangles
// https://codeforces.com/contest/1722/problem/E
// time limit per test 6 seconds; memory limit per test 256 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// 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 a rectangle of height h_s and
// width w_s while also a rectangle of height h_b and width w_b. In other words, print \sum h_i *
// w_i for i such that h_s < h_i < h_b and w_s < w_i < w_b.
//
// Also note that you 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++).
//
// Input
//
// The first line of the input contains an integer t (1 <= t <= 100)-- the number of test cases.
//
// The first line of each test case two integers n, q (1 <= n <= 10^5; 1 <= q <= 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 <= h_i, w_i <= 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 <= h_s < h_b,\ w_s < w_b
// <= 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.
//
// Output
//
// For each test case, output q lines, the i-th line containing the answer to the i-th query.
//
// Example
/*
input:
3
2 1
2 3
3 2
1 1 3 4
5 5
1 1
2 2
3 3
4 4
5 5
3 3 6 6
2 1 4 5
1 1 2 10
1 1 100 100
1 1 3 3
3 1
999 999
999 999
999 998
1 1 1000 1000
output:
6
41
9
0
54
4
2993004
*/
// Note
//
// https://espresso.codeforces.com/7b5085a1e5de63f1f939687ca60666a581423515.png
//
// 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 x 1 rectangle inside of it and fit into a 3 x 4 rectangle.
//
// Only the 2 x 3 rectangle works, because 1 < 2 (comparing heights) and 1 < 3 (comparing widths),
// so the 1 x 1 rectangle fits inside, and 2 < 3 (comparing heights) and 3 < 4 (comparing widths),
// so it fits inside the 3 x 4 rectangle. The 3 x 2 rectangle is too tall to fit in a 3 x 4
// rectangle. The total area is 2 * 3 = 6.
//
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
import java.util.Random;
import java.util.StringTokenizer;
public class C1722E {
static final int MOD = 998244353;
static final Random RAND = new Random();
static long[] solve(int[][] rects, int[][] queries) {
int n = rects.length;
int q = queries.length;
long[][] areas = new long[1001][1001];
for (int i = 0; i < n; i++) {
int h = rects[i][0];
int w = rects[i][1];
areas[h][w] += h * w;
}
// System.out.println(Utils.trace(areas, true));
long[][] dp = new long[1001][1001];
for (int i = 1; i < 1001; i++) {
for (int j = 1; j < 1001; j++) {
dp[i][j] = areas[i][j] + dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1];
}
}
// System.out.println(Utils.trace(dp, true));
long[] ans = new long[q];
for (int i = 0; i < q; i++) {
int hs = queries[i][0];
int ws = queries[i][1];
int hb = queries[i][2];
int wb = queries[i][3];
if (hb < hs + 2 || wb < ws + 2) {
ans[i] = 0;
} else {
ans[i] = dp[hb-1][wb-1] - dp[hs][wb-1] - dp[hb-1][ws] + dp[hs][ws];
}
}
return ans;
}
static boolean test = false;
static void doTest() {
if (!test) {
return;
}
long t0 = System.currentTimeMillis();
System.out.format("%d msec\n", System.currentTimeMillis() - t0);
System.exit(0);
}
public static void main(String[] args) {
doTest();
MyScanner in = new MyScanner();
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[][] queries = new int[q][4];
for (int i = 0; i < q; i++) {
queries[i][0] = in.nextInt();
queries[i][1] = in.nextInt();
queries[i][2] = in.nextInt();
queries[i][3] = in.nextInt();
}
long[] ans = solve(rects, queries);
output(ans);
}
}
static void output(long[] a) {
StringBuilder sb = new StringBuilder();
for (long v : a) {
sb.append(v);
sb.append('\n');
if (sb.length() > 4000) {
System.out.print(sb.toString());
sb.setLength(0);
}
}
System.out.print(sb.toString());
}
static void myAssert(boolean cond) {
if (!cond) {
throw new RuntimeException("Unexpected");
}
}
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", "");
cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname;
final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in");
br = new BufferedReader(new InputStreamReader(fin.exists()
? new FileInputStream(fin) : System.in));
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
public String next() {
try {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
c94ceb36e3bd0e4eeaa42ddcdde3ee69
|
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.*;
import java.util.*;
public class E {
static IOHandler sc = new IOHandler();
static long [] bit;
public static void main(String[] args) {
// TODO Auto-generated method stub
int testCases = sc.nextInt();
for (int i = 1; i <= testCases; ++i) {
solve(i);
}
}
private static void solve(int t) {
int n = sc.nextInt();
int q = sc.nextInt();
int [][] arr = new int [n][2];
bit = new long [1001];
for (int i = 0; i < n; ++i) {
arr[i][0] = sc.nextInt();
arr[i][1] = sc.nextInt();
}
Arrays.sort(arr, (a, b) -> a[0] - b[0]);
long [][] dp = new long [1001][1001];
int area;
long [] result;
int idx = 0;
for (int [] a : arr) {
dp[a[0]][a[1]] += a[0] * a[1];
}
for (int i = 1; i <= 1000; ++i) {
for (int j = 1; j <= 1000; ++j) {
dp[i][j] += dp[i][j - 1];
}
}
for (int i = 1; i <= 1000; ++i) {
for (int j = 1; j <= 1000; ++j) {
dp[i][j] += dp[i - 1][j];
}
}
long [] res = new long [q];
int hs, ws, hb,wb;
long rs, ls;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < q; ++i) {
hs = sc.nextInt();
ws = sc.nextInt();
hb = sc.nextInt();
wb = sc.nextInt();
rs = dp[hb - 1][wb - 1] - dp[hb - 1][ws];
ls = dp[hs][wb - 1] - dp[hs][ws];
sb.append(rs - ls);
sb.append("\n");
}
sb.setLength(sb.length() - 1);
System.out.println(sb);
}
private static long getBitVal(int idx) {
long result = 0;
for (int i = idx; i >= 0 ; i -= (i & -i)) {
result += bit[i];
}
return result;
}
private static class IOHandler {
BufferedReader br;
StringTokenizer st;
public IOHandler() {
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 [] readArray(int n) {
int [] res = new int [n];
for (int i = 0; i < n; ++i)
res[i] = nextInt();
return res;
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
55c91874eb960a7bbd37fd8c258e48c1
|
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.*;
import java.util.*;
public class Main {
static Reader r = new Reader();
static StringBuilder sb = new StringBuilder();
static long cnt[][];
public static void main(String args[]) throws IOException {
int t = r.readInt();
while(t-->0){
solve();
}
System.out.println(sb);
}
static void solve() throws IOException{
int n = r.readInt(), q = r.readInt();
cnt = new long[1001][1001];
for(int i=0;i<n;i++){
int h = r.readInt(), w = r.readInt();
cnt[h][w]+=h*w;
}
for(int i=2;i<1001;i++){
for(int j=1;j<1001;j++){
cnt[i][j] += cnt[i-1][j];
}
}
for(int j=2;j<1001;j++){
for(int i=1;i<1001;i++){
cnt[i][j] += cnt[i][j-1];
}
}
for(int i=0;i<q;i++){
int hs = r.readInt(), ws = r.readInt(), hb = r.readInt(), wb = r.readInt();
sb.append(cnt[hb-1][wb-1]-cnt[hb-1][ws]-cnt[hs][wb-1]+cnt[hs][ws]).append("\n");
}
}
}
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[5000]; // line length
int cnt = 0, c;
while((c=read())!=-1){
if(c=='\n'){
if(cnt!=0) break;
else continue;
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int readInt() throws IOException {
int ret = 0;
byte c = read();
while(c <= ' '){ c = read();}
boolean neg = (c == '-');
if(neg) c = read();
do{
ret = (ret<<3) + (ret<<1) + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return neg ? -ret : ret;
}
public long readLong() throws IOException {
long ret = 0;
byte c = read();
while(c <= ' '){ c = read();}
boolean neg = (c == '-');
if(neg) c = read();
do{
ret = (ret<<3) + (ret<<1) + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return neg ? -ret : ret;
}
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 {
if(din==null) return;
din.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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
2e9621e906e0b85262a8930188226d65
|
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.*;
import java.lang.*;
import java.util.*;
public class ComdeFormces {
static int dp[][];
static boolean mnans;
static int x[]= {-1,0,0,1};
static int y[]= {0,-1,1,0};
static int ans[];
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
FastReader sc=new FastReader();
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
// OutputStream out = new BufferedOutputStream ( System.out );
int t=sc.nextInt();
int tc=1;
while(t--!=0) {
int n=sc.nextInt();
int q=sc.nextInt();
int a[][]=new int[n][2];
long cnt[][]=new long[1001][10001];
for(int i=0;i<n;i++) {
a[i][0]=sc.nextInt();
a[i][1]=sc.nextInt();
cnt[a[i][0]][a[i][1]]+=a[i][1];
}
for(int i=1;i<=1000;i++) {
long sm=0;
for(int j=1;j<=1000;j++) {
sm+=cnt[i][j];
cnt[i][j]=sm;
}
}
for(int i=0;i<q;i++) {
int hi=sc.nextInt();
int wi=sc.nextInt();
int hj=sc.nextInt();
int wj=sc.nextInt();
long ans=0;
for(int k=hi+1;k<=hj-1;k++) {
long tp=k;
ans+=tp*(cnt[k][wj-1]-cnt[k][wi]);
}
log.write(ans+"\n");
}
}
log.flush();
}
static int[] manacher_odd(String ss) {
int n = ss.length();
ss = "$" + ss + "^";
char s[]=ss.toCharArray();
int p[]=new int[n+2];
int l = 1, r = 1;
for(int i = 1; i <= n; i++) {
p[i] = Math.max(0, Math.min(r - i, p[l + (r - i)]));
while(s[i - p[i]] == s[i + p[i]]) {
p[i]++;
}
if(i + p[i] > r) {
l = i - p[i]; r = i + p[i];
}
}
return p;
}
static int mod=998244853;
static int[] lps(int a[],String s) {
int i=1;
int j=0;
a[0]=0;
while(i<s.length()) {
if(s.charAt(i)==s.charAt(j)) {
a[i]=j+1;
i++;
j++;
}
else {
if(j!=0) {
j=a[j-1];
}
else {
a[i]=0;
i++;
}
}
}
return a;
}
static int[] zed(char a[]) {
int z[]=new int[a.length];
int l=0;
int r=0;
for(int i=0;i<a.length;i++) {
if(i>r) {
l=r=i;
while(r<a.length && a[r]==a[r-l])r++;
z[i]=r-l;
r--;
}
else {
int k1=i-l;
if(z[k1]<r-i+1) {
z[i]=z[k1];
}
else {
l=i;
while(r<a.length && a[r]==a[r-l])r++;
z[i]=r-l;
r--;
}
}
}
return z;
}
public static class pair2{
int a,b,c,d;
public pair2(int a,int b,int c,int d) {
this.a=a;
this.b=b;
this.c=c;
this.d=d;
}
}
static boolean dfs(ArrayList<ArrayList<Integer>> ar,int src,int pr,HashSet<Integer> hs) {
int cnt=0;
boolean an=false;
for(int k:ar.get(src)) {
if(k==pr)continue;
boolean p=dfs(ar,k,src,hs);
an|=p;
if(p)cnt++;
}
if(cnt>1)mnans=false;
if(hs.contains(src))an=true;
return an;
}
static int find(int el,int p[]) {
if(p[el]<0)return el;
return p[el]=find(p[el],p);
}
static boolean union(int a,int b,int p[]) {
int p1=find(a,p);
int p2=find(b,p);
if(p1>=0 && p1==p2)return false;
else {
if(p[p1]<p[p2]) {
p[p1]+=p[p2];
p[p2]=p1;
}
else {
p[p2]+=p[p1];
p[p1]=p2;
}
return true;
}
}
public static void build(int a[][],int b[]) {
for(int i=0;i<b.length;i++) {
a[i][0]=b[i];
}
int jmp=2;
while(jmp<=b.length) {
for(int j=0;j<b.length;j++) {
int ind=(int)(Math.log(jmp/2)/Math.log(2));
int ind2=(int)(Math.log(jmp)/Math.log(2));
if(j+jmp-1<b.length) {
a[j][ind2]=Math.max(a[j][ind],a[j+(jmp/2)][ind]);
}
}
jmp*=2;
}
// for(int i=0;i<a.length;i++) {
// for(int j=0;j<33;j++) {
// System.out.print(a[i][j]+" ");
// }
// System.out.println();
// }
}
public static void build2(int a[][],int b[]) {
for(int i=0;i<b.length;i++) {
a[i][0]=b[i];
}
int jmp=2;
while(jmp<=b.length) {
for(int j=0;j<b.length;j++) {
int ind=(int)(Math.log(jmp/2)/Math.log(2));
int ind2=(int)(Math.log(jmp)/Math.log(2));
if(j+jmp-1<b.length) {
a[j][ind2]=Math.min(a[j][ind],a[j+(jmp/2)][ind]);
}
}
jmp*=2;
}
// for(int i=0;i<a.length;i++) {
// for(int j=0;j<33;j++) {
// System.out.print(a[i][j]+" ");
// }
// System.out.println();
// }
}
public static int serst(int a[][],int i,int j) {
int len=j-i+1;
int hp=1;
int tlen=len>>=1;
// System.out.println(tlen);
while(tlen!=0) {
tlen>>=1;
hp<<=1;
}
// System.out.println(hp);
int ind=(int)(Math.log(hp)/Math.log(2));
int i2=j+1-hp;
return Math.max(a[i][ind], a[i2][ind]);
}
public static int serst2(int a[][],int i,int j) {
int len=j-i+1;
int hp=1;
int tlen=len>>=1;
// System.out.println(tlen);
while(tlen!=0) {
tlen>>=1;
hp<<=1;
}
// System.out.println(hp);
int ind=(int)(Math.log(hp)/Math.log(2));
int i2=j+1-hp;
return Math.min(a[i][ind], a[i2][ind]);
}
static void update(long f[],long upd,int ind) {
int vl=ind;
while(vl<f.length) {
f[vl]+=upd;
int tp=~vl;
tp++;
tp&=vl;
vl+=tp;
}
}
static long ser(long f[],int ind) {
int vl=ind;
long sm=0;
while(vl!=0) {
sm+=f[vl];
int tp=~vl;
tp++;
tp&=vl;
vl-=tp;
}
return sm;
}
public static void radixSort(int a[]) {
int n=a.length;
int res[]=new int[n];
int p=1;
for(int i=0;i<=8;i++) {
int cnt[]=new int[10];
for(int j=0;j<n;j++) {
a[j]=res[j];
cnt[(a[j]/p)%10]++;
}
for(int j=1;j<=9;j++) {
cnt[j]+=cnt[j-1];
}
for(int j=n-1;j>=0;j--) {
res[cnt[(a[j]/p)%10]-1]=a[j];
cnt[(a[j]/p)%10]--;
}
p*=10;
}
}
static int bits(long n) {
int ans=0;
while(n!=0) {
if((n&1)==1)ans++;
n>>=1;
}
return ans;
}
public static int kadane(int a[]) {
int sum=0,mx=Integer.MIN_VALUE;
for(int i=0;i<a.length;i++) {
sum+=a[i];
mx=Math.max(mx, sum);
if(sum<0) sum=0;
}
return mx;
}
public static int m=(int)(1e9+7);
public static int mul(int a, int b) {
return ((a%m)*(b%m))%m;
}
public static long mul(long a, long b) {
return ((a%m)*(b%m))%m;
}
public static int add(int a, int b) {
return ((a%mod)+(b%mod))%mod;
}
public static long add(long a, long b) {
return ((a%m)+(b%m))%m;
}
//debug
public static <E> void p(E[][] a,String s) {
System.out.println(s);
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[0].length;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void p(int[] a,String s) {
System.out.print(s+"=");
for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");
System.out.println();
}
public static void p(long[] a,String s) {
System.out.print(s+"=");
for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");
System.out.println();
}
public static <E> void p(E a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(ArrayList<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(LinkedList<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(HashSet<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(Stack<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(Queue<E> a,String s){
System.out.println(s+"="+a);
}
//utils
static ArrayList<Integer> divisors(int n){
ArrayList<Integer> ar=new ArrayList<>();
for (int i=2; i<=Math.sqrt(n); i++){
if (n%i == 0){
if (n/i == i) {
ar.add(i);
}
else {
ar.add(i);
ar.add(n/i);
}
}
}
return ar;
}
static ArrayList<Integer> prime(int n){
ArrayList<Integer> ar=new ArrayList<>();
int cnt=0;
boolean pr=false;
while(n%2==0) {
ar.add(2);
n/=2;
}
for(int i=3;i*i<=n;i+=2) {
pr=false;
while(n%i==0) {
n/=i;
ar.add(i);
pr=true;
}
}
if(n>2) ar.add(n);
return ar;
}
static long gcd(long a,long b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static int gcd(int a,int b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static long factmod(long n,long mod) {
if(n==0)return 0;
long ans=1;
long temp=1;
while(temp<=n) {
ans=((ans%mod)*((temp)%mod))%mod;
temp++;
}
return ans%mod;
}
static int ncr(int n, int r){
if(r>n-r)r=n-r;
int ans=1;
for(int i=0;i<r;i++){
ans*=(n-i);
ans/=(i+1);
}
return ans;
}
public static class trip{
int a;
int b;
int c;
public trip(int a,int b,int c) {
this.a=a;
this.b=b;
this.c=c;
}
// public int compareTo(trip q) {
// return this.b-q.b;
// }
}
static void mergesort(int[] a,int start,int end) {
if(start>=end)return ;
int mid=start+(end-start)/2;
mergesort(a,start,mid);
mergesort(a,mid+1,end);
merge(a,start,mid,end);
}
static void merge(int[] a, int start,int mid,int end) {
int ptr1=start;
int ptr2=mid+1;
int b[]=new int[end-start+1];
int i=0;
while(ptr1<=mid && ptr2<=end) {
if(a[ptr1]<=a[ptr2]) {
b[i]=a[ptr1];
ptr1++;
i++;
}
else {
b[i]=a[ptr2];
ptr2++;
i++;
}
}
while(ptr1<=mid) {
b[i]=a[ptr1];
ptr1++;
i++;
}
while(ptr2<=end) {
b[i]=a[ptr2];
ptr2++;
i++;
}
for(int j=start;j<=end;j++) {
a[j]=b[j-start];
}
}
public static class FastReader {
BufferedReader b;
StringTokenizer s;
public FastReader() {
b=new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(s==null ||!s.hasMoreElements()) {
try {
s=new StringTokenizer(b.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return s.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str="";
try {
str=b.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
return str;
}
boolean hasNext() {
if (s != null && s.hasMoreTokens()) {
return true;
}
String tmp;
try {
b.mark(1000);
tmp = b.readLine();
if (tmp == null) {
return false;
}
b.reset();
} catch (IOException e) {
return false;
}
return true;
}
}
public static class pair{
int a;
int b;
public pair(int a,int b) {
this.a=a;
this.b=b;
}
// public int compareTo(pair b) {
// return this.a-b.a;
//
// }
// // public int compareToo(pair b) {
// return this.b-b.b;
// }
@Override
public String toString() {
return "{"+this.a+" "+this.b+"}";
}
}
static long pow(long a, long pw) {
long temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
public static int md=998244353;
static long mpow(long a, long pw) {
long temp;
if(pw==0)return 1;
temp=pow(a,pw/2)%md;
if(pw%2==0)return mul(temp,temp);
return mul(a,mul(temp,temp));
}
static int pow(int a, int pw) {
int temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
a7bba71fc406b348e4a5dfddcbf194e1
|
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.*;
import java.util.*;
public class R817E {
public static void main(String[] args) {
JS scan = new JS();
int t = scan.nextInt();
while(t-->0){
int n = scan.nextInt();
int q = scan.nextInt();
long[][] freq = new long[1001][1001];
for(int i = 0;i<n;i++){
int h = scan.nextInt();
int w = scan.nextInt();
freq[h][w]++;
}
long[][] prefix = new long[1001][1001];
for(int i = 0;i<1001;i++){
for(int j = 1;j<1001;j++){
prefix[i][j] = prefix[i][j-1];
prefix[i][j] += freq[i][j] * i * j;
}
}
for(int i = 0;i<q;i++){
int hs = scan.nextInt(), ws = scan.nextInt();
int hb = scan.nextInt(), wb = scan.nextInt();
long ans = 0;
for(int j = hs+1;j<hb;j++){
ans+=prefix[j][wb-1] - prefix[j][ws];
}
System.out.println(ans);
}
}
}
static class JS {
public int BS = 1 << 16;
public char NC = (char) 0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public JS() {
in = new BufferedInputStream(System.in, BS);
}
public JS(String s) throws FileNotFoundException {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
public char nextChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public long nextLong() {
num = 1;
boolean neg = false;
if (c == NC) c = nextChar();
for (; (c < '0' || c > '9'); c = nextChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = nextChar()) {
res = (res << 3) + (res << 1) + c - '0';
num *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / num;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = nextChar();
while (c > 32) {
res.append(c);
c = nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = nextChar();
while (c != '\n') {
res.append(c);
c = nextChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = nextChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
71b493e3975eb853f0f300bb258c32c7
|
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
|
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.*;
import java.lang.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.io.*;
public class CodeForces {
static private final String INPUT = "input.txt";
static private final String OUTPUT = "output.txt";
static BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer ST;
static PrintWriter out = new PrintWriter(System.out);
static DecimalFormat df = new DecimalFormat("0.00");
final static int MAX = Integer.MAX_VALUE, MIN = Integer.MIN_VALUE, mod = (int) (1e9 + 7);
final static long LMAX = Long.MAX_VALUE, LMIN = Long.MIN_VALUE;
final static long INF = (long) 1e18, Neg_INF = (long) -1e18;
static Random rand = new Random();
// ======================= MAIN ==================================
public static void main(String[] args) throws IOException {
long time = System.currentTimeMillis();
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
// ==== start ====
input();
preprocess();
int t = 1;
t = readInt();
while (t-- > 0) {
solve();
}
out.flush();
// ==== end ====
if (!oj)
System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
}
private static void solve() throws IOException {
int n = readInt(), q = readInt();
BIT bit = new BIT(1005, 1005);
for (int i = 0; i < n; i++) {
int x = readInt(), y = readInt();
bit.update(x, y, x * y);
}
while (q-- > 0) {
int a = readInt(), b = readInt(), c = readInt(), d = readInt();
out.println(bit.query(a + 1, b + 1, c - 1, d - 1));
}
}
private static void preprocess() throws IOException {
}
// cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces
// javac CodeForces.java && java CodeForces
// change Stack size -> java -Xss16M CodeForces.java
// ==================== CUSTOM CLASSES ================================
static class Pair implements Comparable<Pair> {
int first, second;
Pair(int f, int s) {
first = f;
second = s;
}
public int compareTo(Pair o) {
if (this.first == o.first)
return this.second - o.second;
return this.first - o.first;
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
Pair other = (Pair) (obj);
if (this.first != other.first)
return false;
if (this.second != other.second)
return false;
return true;
}
@Override
public int hashCode() {
return this.first ^ this.second;
}
@Override
public String toString() {
return this.first + " " + this.second;
}
}
static class DequeNode {
DequeNode prev, next;
int val;
DequeNode(int val) {
this.val = val;
}
DequeNode(int val, DequeNode prev, DequeNode next) {
this.val = val;
this.prev = prev;
this.next = next;
}
}
// ======================= FOR INPUT ==================================
private static void input() {
FileInputStream instream = null;
PrintStream outstream = null;
try {
instream = new FileInputStream(INPUT);
outstream = new PrintStream(new FileOutputStream(OUTPUT));
System.setIn(instream);
System.setOut(outstream);
} catch (Exception e) {
System.err.println("Error Occurred.");
}
BR = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
static String next() throws IOException {
while (ST == null || !ST.hasMoreTokens())
ST = new StringTokenizer(readLine());
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 char readCharacter() throws IOException {
return next().charAt(0);
}
static String readString() throws IOException {
return next();
}
static String readLine() throws IOException {
return BR.readLine().trim();
}
static int[] readIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = readInt();
return arr;
}
static int[][] read2DIntArray(int n, int m) throws IOException {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
arr[i] = readIntArray(m);
return arr;
}
static List<Integer> readIntList(int n) throws IOException {
List<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(readInt());
return list;
}
static long[] readLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = readLong();
return arr;
}
static long[][] read2DLongArray(int n, int m) throws IOException {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++)
arr[i] = readLongArray(m);
return arr;
}
static List<Long> readLongList(int n) throws IOException {
List<Long> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(readLong());
return list;
}
static char[] readCharArray() throws IOException {
return readString().toCharArray();
}
static char[][] readMatrix(int n, int m) throws IOException {
char[][] mat = new char[n][m];
for (int i = 0; i < n; i++)
mat[i] = readCharArray();
return mat;
}
// ========================= FOR OUTPUT ==================================
private static void printIList(List<Integer> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printLList(List<Long> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printIArray(int[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DIArray(int[][] arr) {
for (int i = 0; i < arr.length; i++)
printIArray(arr[i]);
}
private static void printLArray(long[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DLArray(long[][] arr) {
for (int i = 0; i < arr.length; i++)
printLArray(arr[i]);
}
private static void yes() {
out.println("YES");
}
private static void no() {
out.println("NO");
}
// ====================== TO CHECK IF STRING IS NUMBER ========================
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static boolean isLong(String s) {
try {
Long.parseLong(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
// ==================== FASTER SORT ================================
private static void sort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void sort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
// ==================== MATHEMATICAL FUNCTIONS ===========================
private static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static int mod_pow(long a, long b, int mod) {
if (b == 0)
return 1;
int temp = mod_pow(a, b >> 1, mod);
temp %= mod;
temp = (int) ((1L * temp * temp) % mod);
if ((b & 1) == 1)
temp = (int) ((1L * temp * a) % mod);
return temp;
}
private static long multiply(long a, long b) {
return (((a % mod) * (b % mod)) % mod);
}
private static long divide(long a, long b) {
return multiply(a, mod_pow(b, mod - 2, mod));
}
private static boolean isPrime(long n) {
for (long i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
private static long nCr(long n, long r) {
if (n - r > r)
r = n - r;
long ans = 1L;
for (long i = r + 1; i <= n; i++)
ans *= i;
for (long i = 2; i <= n - r; i++)
ans /= i;
return ans;
}
private static List<Integer> factors(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 1; 1L * i * i <= n; i++)
if (n % i == 0) {
list.add(i);
if (i != n / i)
list.add(n / i);
}
return list;
}
private static List<Long> factors(long n) {
List<Long> list = new ArrayList<>();
for (long i = 1; i * i <= n; i++)
if (n % i == 0) {
list.add(i);
if (i != n / i)
list.add(n / i);
}
return list;
}
// ==================== Primes using Seive =====================
private static List<Integer> getPrimes(int n) {
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
for (int i = 2; 1L * i * i <= n; i++)
if (prime[i])
for (int j = i * i; j <= n; j += i)
prime[j] = false;
// return prime;
List<Integer> list = new ArrayList<>();
for (int i = 2; i <= n; i++)
if (prime[i])
list.add(i);
return list;
}
private static int[] SeivePrime(int n) {
int[] primes = new int[n];
for (int i = 0; i < n; i++)
primes[i] = i;
for (int i = 2; 1L * i * i < n; i++) {
if (primes[i] != i)
continue;
for (int j = i * i; j < n; j += i)
if (primes[j] == j)
primes[j] = i;
}
return primes;
}
// ==================== STRING FUNCTIONS ================================
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
// check if a is subsequence of b
private static boolean isSubsequence(String a, String b) {
int idx = 0;
for (int i = 0; i < b.length() && idx < a.length(); i++)
if (a.charAt(idx) == b.charAt(i))
idx++;
return idx == a.length();
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
private static String sortString(String str) {
int[] arr = new int[256];
for (char ch : str.toCharArray())
arr[ch]++;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 256; i++)
while (arr[i]-- > 0)
sb.append((char) i);
return sb.toString();
}
// ==================== LIS & LNDS ================================
private static int LIS(int arr[], int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find1(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find1(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) >= val) {
ret = mid;
j = mid - 1;
} else {
i = mid + 1;
}
}
return ret;
}
private static int LNDS(int[] arr, int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find2(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find2(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) <= val) {
i = mid + 1;
} else {
ret = mid;
j = mid - 1;
}
}
return ret;
}
// =============== Lower Bound & Upper Bound ===========
// less than or equal
private static int lower_bound(List<Integer> list, int val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(List<Long> list, long val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(int[] arr, int val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(long[] arr, long val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
// greater than or equal
private static int upper_bound(List<Integer> list, int val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(List<Long> list, long val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(int[] arr, int val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(long[] arr, long val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
// ==================== UNION FIND =====================
private static int find(int x, int[] parent) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x], parent);
}
private static boolean union(int x, int y, int[] parent, int[] rank) {
int lx = find(x, parent), ly = find(y, parent);
if (lx == ly)
return false;
if (rank[lx] > rank[ly])
parent[ly] = lx;
else if (rank[lx] < rank[ly])
parent[lx] = ly;
else {
parent[lx] = ly;
rank[ly]++;
}
return true;
}
// ==================== TRIE ================================
static class Trie {
class Node {
Node[] children;
boolean isEnd;
Node() {
children = new Node[26];
}
}
Node root;
Trie() {
root = new Node();
}
boolean insert(String word) {
Node curr = root;
boolean ans = true;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
curr.children[ch - 'a'] = new Node();
curr = curr.children[ch - 'a'];
if (curr.isEnd)
ans = false;
}
curr.isEnd = true;
return ans;
}
boolean find(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
return false;
curr = curr.children[ch - 'a'];
}
return curr.isEnd;
}
}
// ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ==================
public static class SegmentTree {
int n;
long[] arr, tree, lazy;
SegmentTree(long arr[]) {
this.arr = arr;
this.n = arr.length;
this.tree = new long[(n << 2)];
this.lazy = new long[(n << 2)];
build(1, 0, n - 1);
}
void build(int id, int start, int end) {
if (start == end)
tree[id] = arr[start];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
build(left, start, mid);
build(right, mid + 1, end);
tree[id] = tree[left] + tree[right];
}
}
void update(int l, int r, long val) {
update(1, 0, n - 1, l, r, val);
}
void update(int id, int start, int end, int l, int r, long val) {
distribute(id, start, end);
if (end < l || r < start)
return;
if (start == end)
tree[id] += val;
else if (l <= start && end <= r) {
lazy[id] += val;
distribute(id, start, end);
} else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
update(left, start, mid, l, r, val);
update(right, mid + 1, end, l, r, val);
tree[id] = tree[left] + tree[right];
}
}
long query(int l, int r) {
return query(1, 0, n - 1, l, r);
}
long query(int id, int start, int end, int l, int r) {
if (end < l || r < start)
return 0L;
distribute(id, start, end);
if (start == end)
return tree[id];
else if (l <= start && end <= r)
return tree[id];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r);
}
}
void distribute(int id, int start, int end) {
if (start == end)
tree[id] += lazy[id];
else {
tree[id] += lazy[id] * (end - start + 1);
lazy[(id << 1)] += lazy[id];
lazy[(id << 1) + 1] += lazy[id];
}
lazy[id] = 0;
}
}
// ==================== FENWICK TREE ================================
static class FT {
int n;
int[] arr;
int[] tree;
FT(int[] arr, int n) {
this.arr = arr;
this.n = n;
this.tree = new int[n + 1];
for (int i = 1; i <= n; i++) {
update(i, arr[i - 1]);
}
}
FT(int n) {
this.n = n;
this.tree = new int[n + 1];
}
// 1 based indexing
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += idx & -idx;
}
}
// 1 based indexing
int query(int l, int r) {
return getSum(r) - getSum(l - 1);
}
int getSum(int idx) {
int ans = 0;
while (idx > 0) {
ans += tree[idx];
idx -= idx & -idx;
}
return ans;
}
}
// ==================== BINARY INDEX TREE ================================
static class BIT {
long[][] tree;
int n, m;
BIT(int n, int m) {
this.n = n;
this.m = m;
tree = new long[n + 1][m + 1];
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= m; j++) {
// update(i, j, mat[i - 1][j - 1]);
// }
// }
}
void update(int x, int y, int val) {
while (x <= n) {
int t = y;
while (t <= m) {
tree[x][t] += val;
t += t & -t;
}
x += x & -x;
}
}
long query(int x1, int y1, int x2, int y2) {
return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1);
}
long getSum(int x, int y) {
long ans = 0L;
while (x > 0) {
int t = y;
while (t > 0) {
ans += tree[x][t];
t -= t & -t;
}
x -= x & -x;
}
return 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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 11
|
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 < h_b,\ w_s < 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
|
caf49feb6999be0b2f345d0d95257143
|
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.*;
import java.util.*;
public class CountingRectangles {
private static final int START_TEST_CASE = 1;
private static final int H_MAX = 1000;
private static final int W_MAX = 1000;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int Q = io.nextInt();
long[][] A = new long[H_MAX + 1][W_MAX + 1];
for (int i = 0; i < N; ++i) {
final int H = io.nextInt();
final int W = io.nextInt();
A[H][W] += H * W;
}
SubmatrixSum sums = new SubmatrixSum(A);
for (int q = 0; q < Q; ++q) {
final int HS = io.nextInt();
final int WS = io.nextInt();
final int HB = io.nextInt();
final int WB = io.nextInt();
io.println(sums.sumRange(HS + 1, WS + 1, HB, WB));
}
}
/**
* Performs static 2D range sums. Note that arguments are inclusive-lower
* exclusive-upper for function sumRange(int r1, int c1, int r2, int c2).
*/
public static class SubmatrixSum {
private long[][] sum;
private int N, M;
public SubmatrixSum(int[][] matrix) {
this(
Arrays.stream(matrix).map(
row -> Arrays.stream(row).asLongStream().toArray()
).toArray(long[][]::new)
);
}
public SubmatrixSum(long[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) {
return;
}
N = matrix.length;
M = matrix[0].length;
sum = new long[N + 1][M + 1];
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + matrix[i][j];
}
}
}
public long sumRange(int r1, int c1, int r2, int c2) {
if (sum == null) {
return 0;
}
return sum[r2][c2] - sum[r2][c1] - sum[r1][c2] + sum[r1][c1];
}
public long sumRange(Range r) {
return sumRange(r.r1, r.c1, r.r2, r.c2);
}
public static class Range {
public int r1, c1, r2, c2;
public Range(int r1, int c1, int r2, int c2) {
this.r1 = r1;
this.c1 = c1;
this.r2 = r2;
this.c2 = c2;
}
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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) {
print(objects);
writer.println();
}
public void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.flush();
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
2dff6ea4625df783c7aade39a3a86c67
|
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.*;
import java.util.*;
public class CountingRectangles {
private static final int START_TEST_CASE = 1;
private static final int H_MAX = 1000;
private static final int W_MAX = 1000;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int Q = io.nextInt();
long[][] A = new long[H_MAX + 1][W_MAX + 1];
for (int i = 0; i < N; ++i) {
final int H = io.nextInt();
final int W = io.nextInt();
A[H][W] += H * W;
}
SubmatrixSum sums = new SubmatrixSum(A);
for (int q = 0; q < Q; ++q) {
final int HS = io.nextInt();
final int WS = io.nextInt();
final int HB = io.nextInt();
final int WB = io.nextInt();
io.println(sums.sumRange(HS + 1, WS + 1, HB, WB));
}
}
/**
* Performs static 2D range sums.
* Note that arguments are inclusive-lower exclusive-upper for function sumRange(int r1, int c1, int r2, int c2).
*/
public static class SubmatrixSum {
private long[][] sum;
private int N, M;
public SubmatrixSum(int[][] matrix) {
this(
Arrays.stream(matrix).map(
row -> Arrays.stream(row).asLongStream().toArray()
).toArray(long[][]::new)
);
}
public SubmatrixSum(long[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) {
return;
}
N = matrix.length;
M = matrix[0].length;
sum = new long[N + 1][M + 1];
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + matrix[i][j];
}
}
}
public long sumRange(int r1, int c1, int r2, int c2) {
if (sum == null) {
return 0;
}
return sum[r2][c2] - sum[r2][c1] - sum[r1][c2] + sum[r1][c1];
}
public long sumRange(Range r) {
return sumRange(r.r1, r.c1, r.r2, r.c2);
}
public static class Range {
public int r1, c1, r2, c2;
public Range(int r1, int c1, int r2, int c2) {
this.r1 = r1;
this.c1 = c1;
this.r2 = r2;
this.c2 = c2;
}
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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) {
print(objects);
writer.println();
}
public void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.flush();
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
7b394ccca13db5f4618d439c81d22dc9
|
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.*;
import java.util.*;
public class CountingRectangles {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int Q = io.nextInt();
IntQuadTree qt = new IntQuadTree(1, 1000, 1, 1000);
for (int i = 0; i < N; ++i) {
final int H = io.nextInt();
final int W = io.nextInt();
long prev = qt.get(H, H, W, W);
qt.set(H, W, prev + 1L * H * W);
}
for (int q = 0; q < Q; ++q) {
final int HS = io.nextInt();
final int WS = io.nextInt();
final int HB = io.nextInt();
final int WB = io.nextInt();
io.println(qt.get(HS + 1, HB - 1, WS + 1, WB - 1));
}
}
/**
* IntQuadTree is basically a 2D segment tree with int keys for computing range sums.
* It is faster than QuadTree with long keys.
*
* For function get(int xlo, int xhi, int ylo, int yhi):
* - Note the argument order.
* - Bounds are all inclusive.
*/
public static class IntQuadTree {
public int xl, xr, yl, yr;
public long val;
private IntQuadTree topLeft;
private IntQuadTree topRight;
private IntQuadTree botLeft;
private IntQuadTree botRight;
public IntQuadTree(int xlo, int xhi, int ylo, int yhi) {
this.xl = xlo;
this.xr = xhi;
this.yl = ylo;
this.yr = yhi;
}
public void set(int x, int y, long v) {
if (x == xl && x == xr && y == yl && y == yr) {
val = v;
return;
}
int xm = (xl + xr) >> 1;
int ym = (yl + yr) >> 1;
if (x <= xm) {
if (y <= ym) {
if (botLeft == null) {
botLeft = new IntQuadTree(xl, xm, yl, ym);
}
botLeft.set(x, y, v);
} else {
if (topLeft == null) {
topLeft = new IntQuadTree(xl, xm, ym + 1, yr);
}
topLeft.set(x, y, v);
}
} else {
if (y <= ym) {
if (botRight == null) {
botRight = new IntQuadTree(xm + 1, xr, yl, ym);
}
botRight.set(x, y, v);
} else {
if (topRight == null) {
topRight = new IntQuadTree(xm + 1, xr, ym + 1, yr);
}
topRight.set(x, y, v);
}
}
val = valueOf(topLeft) + valueOf(topRight) + valueOf(botLeft) + valueOf(botRight);
}
public long get(int xlo, int xhi, int ylo, int yhi) {
if (xlo > xr || xhi < xl || ylo > yr || yhi < yl) {
return defaultValue();
}
if (xl >= xlo && xr <= xhi && yl >= ylo && yr <= yhi) {
return val;
}
return tryGet(topLeft, xlo, xhi, ylo, yhi) + tryGet(topRight, xlo, xhi, ylo, yhi) + tryGet(botLeft, xlo, xhi, ylo, yhi) + tryGet(botRight, xlo, xhi, ylo, yhi);
}
private static long defaultValue() {
return 0;
}
private static long valueOf(IntQuadTree qt) {
if (qt == null) {
return defaultValue();
}
return qt.val;
}
private static long tryGet(IntQuadTree qt, int xlo, int xhi, int ylo, int yhi) {
if (qt == null) {
return defaultValue();
}
return qt.get(xlo, xhi, ylo, yhi);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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) {
print(objects);
writer.println();
}
public void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.flush();
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
1f0b328436fb88b0b7cdde9eeaef6372
|
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.*;
import java.util.*;
public class CountingRectangles {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int Q = io.nextInt();
IntQuadTree qt = new IntQuadTree(0, 1000, 0, 1000, null);
for (int i = 0; i < N; ++i) {
final int H = io.nextInt();
final int W = io.nextInt();
long prev = qt.getLeaf(H, W).val;
qt.set(H, W, prev + 1L * H * W);
}
for (int q = 0; q < Q; ++q) {
final int HS = io.nextInt();
final int WS = io.nextInt();
final int HB = io.nextInt();
final int WB = io.nextInt();
io.println(qt.get(HS + 1, HB - 1, WS + 1, WB - 1));
}
}
/**
* IntQuadTree is basically a 2D segment tree with int keys for computing range sums.
* It is faster than QuadTree with long keys.
* Note the argument order of function get(int xlo, int xhi, int ylo, int yhi).
*/
public static class IntQuadTree {
public int xl, xr, yl, yr;
public long val;
private IntQuadTree parent;
private IntQuadTree topLeft;
private IntQuadTree topRight;
private IntQuadTree botLeft;
private IntQuadTree botRight;
public IntQuadTree(int xlo, int xhi, int ylo, int yhi, IntQuadTree p) {
this.xl = xlo;
this.xr = xhi;
this.yl = ylo;
this.yr = yhi;
this.parent = p;
}
public IntQuadTree getLeaf(int x, int y) {
if (x == xl && x == xr && y == yl && y == yr) {
return this;
}
int xm = (xl + xr) >> 1;
int ym = (yl + yr) >> 1;
if (x <= xm) {
if (y <= ym) {
if (botLeft == null) {
botLeft = new IntQuadTree(xl, xm, yl, ym, this);
}
return botLeft.getLeaf(x, y);
} else {
if (topLeft == null) {
topLeft = new IntQuadTree(xl, xm, ym + 1, yr, this);
}
return topLeft.getLeaf(x, y);
}
} else {
if (y <= ym) {
if (botRight == null) {
botRight = new IntQuadTree(xm + 1, xr, yl, ym, this);
}
return botRight.getLeaf(x, y);
} else {
if (topRight == null) {
topRight = new IntQuadTree(xm + 1, xr, ym + 1, yr, this);
}
return topRight.getLeaf(x, y);
}
}
}
public void set(int x, int y, long v) {
IntQuadTree qt = getLeaf(x, y);
qt.val = v;
qt = qt.parent;
while (qt != null) {
qt.val = valueOf(qt.topLeft) + valueOf(qt.topRight) + valueOf(qt.botLeft) + valueOf(qt.botRight);
qt = qt.parent;
}
}
public long get(int xlo, int xhi, int ylo, int yhi) {
if (xlo > xr || xhi < xl || ylo > yr || yhi < yl) {
return defaultValue();
}
if (xl >= xlo && xr <= xhi && yl >= ylo && yr <= yhi) {
return val;
}
return tryGet(topLeft, xlo, xhi, ylo, yhi) + tryGet(topRight, xlo, xhi, ylo, yhi) + tryGet(botLeft, xlo, xhi, ylo, yhi) + tryGet(botRight, xlo, xhi, ylo, yhi);
}
private static long defaultValue() {
return 0;
}
private static long valueOf(IntQuadTree qt) {
if (qt == null) {
return defaultValue();
}
return qt.val;
}
private static long tryGet(IntQuadTree qt, int xlo, int xhi, int ylo, int yhi) {
if (qt == null) {
return defaultValue();
}
return qt.get(xlo, xhi, ylo, yhi);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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) {
print(objects);
writer.println();
}
public void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.flush();
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
fe55d30f1a759bc39c68a800ecd2f76c
|
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.*;
import java.util.*;
public class CountingRectangles {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int Q = io.nextInt();
IntQuadTree qt = new IntQuadTree(0, 1023, 0, 1023, null);
for (int i = 0; i < N; ++i) {
final int H = io.nextInt();
final int W = io.nextInt();
long prev = qt.getLeaf(H, W).val;
qt.set(H, W, prev + 1L * H * W);
}
for (int q = 0; q < Q; ++q) {
final int HS = io.nextInt();
final int WS = io.nextInt();
final int HB = io.nextInt();
final int WB = io.nextInt();
io.println(qt.get(HS + 1, HB - 1, WS + 1, WB - 1));
}
}
/**
* IntQuadTree is basically a 2D segment tree with int keys for computing range sums.
* It is faster than QuadTree with long keys.
* Note the argument order of function get(int xlo, int xhi, int ylo, int yhi).
*/
public static class IntQuadTree {
public int xl, xr, yl, yr;
public long val;
private IntQuadTree parent;
private IntQuadTree topLeft;
private IntQuadTree topRight;
private IntQuadTree botLeft;
private IntQuadTree botRight;
public IntQuadTree(int xlo, int xhi, int ylo, int yhi, IntQuadTree p) {
this.xl = xlo;
this.xr = xhi;
this.yl = ylo;
this.yr = yhi;
this.parent = p;
}
public IntQuadTree getLeaf(int x, int y) {
if (x == xl && x == xr && y == yl && y == yr) {
return this;
}
int xm = (xl + xr) >> 1;
int ym = (yl + yr) >> 1;
if (x <= xm) {
if (y <= ym) {
if (botLeft == null) {
botLeft = new IntQuadTree(xl, xm, yl, ym, this);
}
return botLeft.getLeaf(x, y);
} else {
if (topLeft == null) {
topLeft = new IntQuadTree(xl, xm, ym + 1, yr, this);
}
return topLeft.getLeaf(x, y);
}
} else {
if (y <= ym) {
if (botRight == null) {
botRight = new IntQuadTree(xm + 1, xr, yl, ym, this);
}
return botRight.getLeaf(x, y);
} else {
if (topRight == null) {
topRight = new IntQuadTree(xm + 1, xr, ym + 1, yr, this);
}
return topRight.getLeaf(x, y);
}
}
}
public void set(int x, int y, long v) {
IntQuadTree qt = getLeaf(x, y);
qt.val = v;
qt = qt.parent;
while (qt != null) {
qt.val = valueOf(qt.topLeft) + valueOf(qt.topRight) + valueOf(qt.botLeft) + valueOf(qt.botRight);
qt = qt.parent;
}
}
public long get(int xlo, int xhi, int ylo, int yhi) {
if (xlo > xr || xhi < xl || ylo > yr || yhi < yl) {
return defaultValue();
}
if (xl >= xlo && xr <= xhi && yl >= ylo && yr <= yhi) {
return val;
}
return tryGet(topLeft, xlo, xhi, ylo, yhi) + tryGet(topRight, xlo, xhi, ylo, yhi) + tryGet(botLeft, xlo, xhi, ylo, yhi) + tryGet(botRight, xlo, xhi, ylo, yhi);
}
private static long defaultValue() {
return 0;
}
private static long valueOf(IntQuadTree qt) {
if (qt == null) {
return defaultValue();
}
return qt.val;
}
private static long tryGet(IntQuadTree qt, int xlo, int xhi, int ylo, int yhi) {
if (qt == null) {
return defaultValue();
}
return qt.get(xlo, xhi, ylo, yhi);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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) {
print(objects);
writer.println();
}
public void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.flush();
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
dcefa4fc53eca82fdd7a9a7605aa0f47
|
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.*;
import java.util.*;
public class CountingRectangles {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int Q = io.nextInt();
QuadTree qt = new QuadTree(0, 1000, 0, 1000, null);
for (int i = 0; i < N; ++i) {
final int H = io.nextInt();
final int W = io.nextInt();
long prev = qt.getLeaf(H, W).val;
qt.set(H, W, prev + 1L * H * W);
}
for (int q = 0; q < Q; ++q) {
final int HS = io.nextInt();
final int WS = io.nextInt();
final int HB = io.nextInt();
final int WB = io.nextInt();
io.println(qt.get(HS + 1, HB - 1, WS + 1, WB - 1));
}
}
/*
* QuadTree is basically a 2D segment tree for compute range sums.
*/
public static class QuadTree {
public int xl, xr, yl, yr;
public long val;
private QuadTree parent;
private QuadTree topLeft;
private QuadTree topRight;
private QuadTree botLeft;
private QuadTree botRight;
public QuadTree(int xlo, int xhi, int ylo, int yhi, QuadTree p) {
this.xl = xlo;
this.xr = xhi;
this.yl = ylo;
this.yr = yhi;
this.parent = p;
}
public QuadTree getLeaf(int x, int y) {
if (x == xl && x == xr && y == yl && y == yr) {
return this;
}
int xm = (xl + xr) >> 1;
int ym = (yl + yr) >> 1;
if (x <= xm) {
if (y <= ym) {
if (botLeft == null) {
botLeft = new QuadTree(xl, xm, yl, ym, this);
}
return botLeft.getLeaf(x, y);
} else {
if (topLeft == null) {
topLeft = new QuadTree(xl, xm, ym + 1, yr, this);
}
return topLeft.getLeaf(x, y);
}
} else {
if (y <= ym) {
if (botRight == null) {
botRight = new QuadTree(xm + 1, xr, yl, ym, this);
}
return botRight.getLeaf(x, y);
} else {
if (topRight == null) {
topRight = new QuadTree(xm + 1, xr, ym + 1, yr, this);
}
return topRight.getLeaf(x, y);
}
}
}
public void set(int x, int y, long v) {
QuadTree qt = getLeaf(x, y);
qt.val = v;
qt = qt.parent;
while (qt != null) {
qt.val = valueOf(qt.topLeft) + valueOf(qt.topRight) + valueOf(qt.botLeft) + valueOf(qt.botRight);
qt = qt.parent;
}
}
public long get(int xlo, int xhi, int ylo, int yhi) {
if (xlo > xhi || ylo > yhi) {
return defaultValue();
}
if (xlo > xr || xhi < xl || ylo > yr || yhi < yl) {
return defaultValue();
}
if (xl >= xlo && xr <= xhi && yl >= ylo && yr <= yhi) {
return val;
}
return tryGet(topLeft, xlo, xhi, ylo, yhi) + tryGet(topRight, xlo, xhi, ylo, yhi) + tryGet(botLeft, xlo, xhi, ylo, yhi) + tryGet(botRight, xlo, xhi, ylo, yhi);
}
private static long defaultValue() {
return 0;
}
private static long valueOf(QuadTree qt) {
if (qt == null) {
return defaultValue();
}
return qt.val;
}
private static long tryGet(QuadTree qt, int xlo, int xhi, int ylo, int yhi) {
if (qt == null) {
return defaultValue();
}
return qt.get(xlo, xhi, ylo, yhi);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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) {
print(objects);
writer.println();
}
public void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.flush();
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
f5c55b03f0c03a638bab39a132f60fe7
|
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.*;
import java.util.*;
public class CountingRectangles {
private static final int START_TEST_CASE = 1;
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
final int Q = io.nextInt();
QuadTree qt = new QuadTree(0, 1000, 0, 1000, null);
for (int i = 0; i < N; ++i) {
final int H = io.nextInt();
final int W = io.nextInt();
long prev = qt.getLeaf(H, W).val;
qt.set(H, W, prev + 1L * H * W);
}
for (int q = 0; q < Q; ++q) {
final int HS = io.nextInt();
final int WS = io.nextInt();
final int HB = io.nextInt();
final int WB = io.nextInt();
io.println(qt.get(HS + 1, HB - 1, WS + 1, WB - 1));
}
}
/*
* QuadTree is basically a 2D segment tree for compute range sums.
*/
public static class QuadTree {
public int xl, xr, yl, yr;
public long val;
private QuadTree parent;
private QuadTree topLeft;
private QuadTree topRight;
private QuadTree botLeft;
private QuadTree botRight;
public QuadTree(int xlo, int xhi, int ylo, int yhi, QuadTree p) {
this.xl = xlo;
this.xr = xhi;
this.yl = ylo;
this.yr = yhi;
this.parent = p;
}
public QuadTree getLeaf(int x, int y) {
if (x == xl && x == xr && y == yl && y == yr) {
return this;
}
int xm = (xl + xr) >> 1;
int ym = (yl + yr) >> 1;
if (x <= xm) {
if (y <= ym) {
if (botLeft == null) {
botLeft = new QuadTree(xl, xm, yl, ym, this);
}
return botLeft.getLeaf(x, y);
} else {
if (topLeft == null) {
topLeft = new QuadTree(xl, xm, ym + 1, yr, this);
}
return topLeft.getLeaf(x, y);
}
} else {
if (y <= ym) {
if (botRight == null) {
botRight = new QuadTree(xm + 1, xr, yl, ym, this);
}
return botRight.getLeaf(x, y);
} else {
if (topRight == null) {
topRight = new QuadTree(xm + 1, xr, ym + 1, yr, this);
}
return topRight.getLeaf(x, y);
}
}
}
public void set(int x, int y, long v) {
QuadTree qt = getLeaf(x, y);
qt.val = v;
qt = qt.parent;
while (qt != null) {
qt.val = valueOf(qt.topLeft) + valueOf(qt.topRight) + valueOf(qt.botLeft) + valueOf(qt.botRight);
qt = qt.parent;
}
}
public long get(int xlo, int xhi, int ylo, int yhi) {
// if (xlo > xhi || ylo > yhi) {
// return defaultValue();
// }
if (xlo > xr || xhi < xl || ylo > yr || yhi < yl) {
return defaultValue();
}
if (xl >= xlo && xr <= xhi && yl >= ylo && yr <= yhi) {
return val;
}
return tryGet(topLeft, xlo, xhi, ylo, yhi) + tryGet(topRight, xlo, xhi, ylo, yhi) + tryGet(botLeft, xlo, xhi, ylo, yhi) + tryGet(botRight, xlo, xhi, ylo, yhi);
}
private static long defaultValue() {
return 0;
}
private static long valueOf(QuadTree qt) {
if (qt == null) {
return defaultValue();
}
return qt.val;
}
private static long tryGet(QuadTree qt, int xlo, int xhi, int ylo, int yhi) {
if (qt == null) {
return defaultValue();
}
return qt.get(xlo, xhi, ylo, yhi);
}
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int 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 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;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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) {
print(objects);
writer.println();
}
public void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.flush();
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
8e013206a5c8369c56f1e0c73155f52e
|
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.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
private static final int N = (int) (1e3 + 10);
static int n, q;
static long[][] a = new long[N][N];
static long[][] s = new long[N][N];
static String s1, s2, s3;
public static void main(String[] args) {
int _t = sc.nextInt();
while (_t -- > 0) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
a[i][j] = s[i][j] = 0;
}
}
n = sc.nextInt();
q = sc.nextInt();
int x, y;
for (int i = 1; i <= n; i++) {
x = sc.nextInt();
y = sc.nextInt();
a[x][y] += x * y;
}
for (int i = 1; i <= 1e3; i++) {
for (int j = 1; j <= 1e3; j++) {
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
}
}
int hs, ws, hb, wb;
while (q-- > 0){
hs = sc.nextInt();
ws = sc.nextInt();
hb = sc.nextInt();
wb = sc.nextInt();
// long res = s[y1 - 1][y2 - 1] - s[y1 - 1][x2] - s[x1][y2 - 1] + s[x1][y1];
long res = s[hb-1][wb-1]-s[hb-1][ws]-s[hs][wb-1]+s[hs][ws];
System.out.println(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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
f36f107457f6fccccd41c3f40fc32671
|
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.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();
int q = scn.nextInt();
long[][] dp1 = new long[1001][1001];
for(int i = 0; i < n; i++){
int h =scn.nextInt();
int w = scn.nextInt();
dp1[h][w] += 1L * h*w;
}
long[][] dp = new long[1001][1001];
for(int i = 1; i <= 1000; i++){
for(int j =1; j <= 1000; j++){
dp[i][j] = dp1[i][j] + dp[i][j-1] - dp[i-1][j-1] + dp[i-1][j] ;
}
}
for(int i =0; i < q; i++){
int h1 = scn.nextInt();
int w1 = scn.nextInt();
int h2 = scn.nextInt();
int w2 = scn.nextInt();
System.out.println((dp[h2-1][w2-1] - dp[h2-1][w1] - dp[h1][w2-1] + dp[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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
8305014b2b1e76c8d05879d258bb755a
|
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.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();
int q = scn.nextInt();
long[][] dp1 = new long[1001][1001];
for(int i = 0; i < n; i++){
int h =scn.nextInt();
int w = scn.nextInt();
dp1[h][w] += 1L * h*w;
}
long[][] dp = new long[1001][1001];
for(int i = 1; i <= 1000; i++){
for(int j =1; j <= 1000; j++){
dp[i][j] = dp1[i][j] + dp[i][j-1] - dp[i-1][j-1] + dp[i-1][j] ;
// System.out.print(dp[i][j] + " ");
}
// System.out.println("\n");
}
// for(int i = 1; i <= 6;i++){
// for(int j = 1; j <= 2;j++){
// System.out.print(dp[i][j] + " ");
// }
// System.out.println();
// }
for(int i =0; i < q; i++){
int h1 = scn.nextInt();
int w1 = scn.nextInt();
int h2 = scn.nextInt();
int w2 = scn.nextInt();
// if(i ==1){
// System.out.println(h1 + " " + w1 + " " + h2 + " " + w2);
// }
System.out.println((dp[h2-1][w2-1] - dp[h2-1][w1] - dp[h1][w2-1] + dp[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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
eeb37329d89f001ad9d6c6e4356410fc
|
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
public class Solution {
static MyScanner str = new MyScanner();
public static void main(String[] args) throws IOException {
int T = i();
while (T-- > 0) {
solve();
}
}
static void solve() throws IOException {
int n = i(), q = i();
long[][] hw = new long[1010][1010];
for (int i = 0; i < n; i++) {
int h = i(), w = i();
hw[h][w] += (long) h * w;
}
for (int i = 1; i < 1010; i++) {
for (int j = 1; j < 1010; j++) {
hw[i][j] += hw[i - 1][j] + hw[i][j - 1] - hw[i - 1][j - 1];
}
}
for (int i = 0; i < q; i++) {
int h1 = i(), w1 = i(), h2 = i(), w2 = i();
long res = hw[h2 - 1][w2 - 1] - hw[h1][w2 - 1] - hw[h2 - 1][w1] + hw[h1][w1];
System.out.println(res);
}
}
public static int i() throws IOException {
return str.nextInt();
}
public static long l() throws IOException {
return str.nextLong();
}
public static double d() throws IOException {
return str.nextDouble();
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
33b639f3217fe8e7b189e03adcc83c73
|
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.util.*;
import java.util.spi.CalendarDataProvider;
import javax.imageio.metadata.IIOInvalidTreeException;
import java.lang.*;
import java.sql.Array;
// import java.lang.invoke.ConstantBootstraps;
// import java.math.BigInteger;
// import java.beans.IndexedPropertyChangeEvent;
import java.io.*;
@SuppressWarnings("unchecked")
public class Main implements Runnable {
static FastReader in;
static PrintWriter out;
static int bit(long n) {
return (n == 0) ? 0 : (1 + bit(n & (n - 1)));
}
static void p(Object o) {
out.print(o);
}
static void pn(Object o) {
out.println(o);
}
static void pni(Object o) {
out.println(o);
out.flush();
}
static String n() throws Exception {
return in.next();
}
static String nln() throws Exception {
return in.nextLine();
}
static int ni() throws Exception {
return Integer.parseInt(in.next());
}
static long nl() throws Exception {
return Long.parseLong(in.next());
}
static double nd() throws Exception {
return Double.parseDouble(in.next());
}
static class FastReader {
static BufferedReader br;
static StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception {
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
throw new Exception(e.toString());
}
return str;
}
}
static long power(long a, long b) {
if (a == 0L)
return 0L;
if (b == 0)
return 1;
long val = power(a, b / 2);
val = val * val;
if ((b % 2) != 0)
val = val * a;
return val;
}
static long power(long a, long b, long mod) {
if (a == 0L)
return 0L;
if (b == 0)
return 1;
long val = power(a, b / 2L, mod) % mod;
val = (val * val) % mod;
if ((b % 2) != 0)
val = (val * a) % mod;
return val;
}
static ArrayList<Long> prime_factors(long n) {
ArrayList<Long> ans = new ArrayList<Long>();
while (n % 2 == 0) {
ans.add(2L);
n /= 2L;
}
for (long i = 3; i * i <= n; i++) {
while (n % i == 0) {
ans.add(i);
n /= i;
}
}
if (n > 2) {
ans.add(n);
}
return ans;
}
static void sort(ArrayList<Long> a) {
Collections.sort(a);
}
static void reverse_sort(ArrayList<Long> a) {
Collections.sort(a, Collections.reverseOrder());
}
static void swap(long[] a, int i, int j) {
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(List<Long> a, int i, int j) {
long temp = a.get(i);
a.set(j, a.get(i));
a.set(j, temp);
}
static void sieve(boolean[] prime) {
int n = prime.length - 1;
Arrays.fill(prime, true);
for (int i = 2; i * i <= n; i++) {
if (prime[i]) {
for (int j = 2 * i; j <= n; j += i) {
prime[j] = false;
}
}
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static HashMap<Long, Integer> map_prime_factors(long n) {
HashMap<Long, Integer> map = new HashMap<>();
while (n % 2 == 0) {
map.put(2L, map.getOrDefault(2L, 0) + 1);
n /= 2L;
}
for (long i = 3; i <= Math.sqrt(n); i++) {
while (n % i == 0) {
map.put(i, map.getOrDefault(i, 0) + 1);
n /= i;
}
}
if (n > 2) {
map.put(n, map.getOrDefault(n, 0) + 1);
}
return map;
}
static List<Long> divisor(long n) {
List<Long> ans = new ArrayList<>();
ans.add(1L);
long count = 0;
for (long i = 2L; i * i <= n; i++) {
if (n % i == 0) {
if (i == n / i)
ans.add(i);
else {
ans.add(i);
ans.add(n / i);
}
}
}
return ans;
}
static void sum_of_divisors(int n) {
int[] dp = new int[n + 1];
for (int i = 1; i <= n; i++) {
dp[i] += i;
for (int j = i + i; j <= n; j += i) {
dp[j] += i;
}
}
}
static void prime_factorization_using_sieve(int n) {
int[] dp = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
for (int i = 2; i <= n; i++) {
dp[i] = Math.min(dp[i], i);// dp[i] stores smallest prime number which divides number i
for (int j = 2 * i; j <= n; j++) {// can calculate prime factorization in O(logn) time by dividing
// val/=dp[val]; till 1 is obtained
dp[j] = Math.min(dp[j], i);
}
}
}
/*
* ----------------------------------------------------Sorting------------------
* ------------------------------------------------
*/
public static void sort(long[] arr, int l, int r) {
if (l >= r)
return;
int mid = (l + r) / 2;
sort(arr, l, mid);
sort(arr, mid + 1, r);
merge(arr, l, mid, r);
}
public static void sort(int[] arr, int l, int r) {
if (l >= r)
return;
int mid = (l + r) / 2;
sort(arr, l, mid);
sort(arr, mid + 1, r);
merge(arr, l, mid, r);
}
static void merge(int[] arr, int l, int mid, int r) {
int[] left = new int[mid - l + 1];
int[] right = new int[r - mid];
for (int i = l; i <= mid; i++) {
left[i - l] = arr[i];
}
for (int i = mid + 1; i <= r; i++) {
right[i - (mid + 1)] = arr[i];
}
int left_start = 0;
int right_start = 0;
int left_length = mid - l + 1;
int right_length = r - mid;
int temp = l;
while (left_start < left_length && right_start < right_length) {
if (left[left_start] < right[right_start]) {
arr[temp] = left[left_start++];
} else {
arr[temp] = right[right_start++];
}
temp++;
}
while (left_start < left_length) {
arr[temp++] = left[left_start++];
}
while (right_start < right_length) {
arr[temp++] = right[right_start++];
}
}
static void merge(long[] arr, int l, int mid, int r) {
long[] left = new long[mid - l + 1];
long[] right = new long[r - mid];
for (int i = l; i <= mid; i++) {
left[i - l] = arr[i];
}
for (int i = mid + 1; i <= r; i++) {
right[i - (mid + 1)] = arr[i];
}
int left_start = 0;
int right_start = 0;
int left_length = mid - l + 1;
int right_length = r - mid;
int temp = l;
while (left_start < left_length && right_start < right_length) {
if (left[left_start] < right[right_start]) {
arr[temp] = left[left_start++];
} else {
arr[temp] = right[right_start++];
}
temp++;
}
while (left_start < left_length) {
arr[temp++] = left[left_start++];
}
while (right_start < right_length) {
arr[temp++] = right[right_start++];
}
}
// static int[] smallest_prime_factor;
// static int count = 1;
// static int[] p = new int[100002];
// static long[] flat_tree = new long[300002];
// static int[] in_time = new int[1000002];
// static int[] out_time = new int[1000002];
// static long[] subtree_gcd = new long[100002];
// static int w = 0;
// static boolean poss = true;
/*
* (a^b^c)%mod
* Using fermats Little theorem
* x^(mod-1)=1(mod)
* so b^c can be written as b^c=x*(mod-1)+y
* then (a^(x*(mod-1)+y))%mod=(a^(x*(mod-1))*a^(y))mod
* the term (a^(x*(mod-1)))%mod=a^(mod-1)*a^(mod-1)
*
*/
// ---------------------------------------------------Segment_Tree----------------------------------------------------------------//
// static class comparator implements Comparator<node> {
// public int compare(node a, node b) {
// return a.a - b.a > 0 ? 1 : -1;
// }
// }
static class Segment_Tree {
private long[] segment_tree;
public Segment_Tree(int n) {
this.segment_tree = new long[4 * n + 1];
}
void build(int index, int left, int right, int[] a) {
if (left == right) {
segment_tree[index] = a[left];
return;
}
int mid = (left + right) / 2;
build(2 * index + 1, left, mid, a);
build(2 * index + 2, mid + 1, right, a);
segment_tree[index] = segment_tree[2 * index + 1] + segment_tree[2 * index + 2];
}
long query(int index, int left, int right, int l, int r) {
if (left > right)
return 0;
if (left >= l && r >= right) {
return segment_tree[index];
}
if (l > right || left > r)
return 0;
int mid = (left + right) / 2;
return query(2 * index + 1, left, mid, l, r) + query(2 * index + 2, mid + 1, right, l, r);
}
void update(int index, int left, int right, int node, int val) {
if (left == right) {
segment_tree[index] += val;
return;
}
int mid = (left + right) / 2;
if (node <= mid)
update(2 * index + 1, left, mid, node, val);
else
update(2 * index + 2, mid + 1, right, node, val);
segment_tree[index] = segment_tree[2 * index + 1] + segment_tree[2 * index + 2];
}
}
static class min_Segment_Tree {
private long[] segment_tree;
public min_Segment_Tree(int n) {
this.segment_tree = new long[4 * n + 1];
}
void build(int index, int left, int right, int[] a) {
if (left == right) {
segment_tree[index] = a[left];
return;
}
int mid = (left + right) / 2;
build(2 * index + 1, left, mid, a);
build(2 * index + 2, mid + 1, right, a);
segment_tree[index] = Math.min(segment_tree[2 * index + 1], segment_tree[2 * index + 2]);
}
long query(int index, int left, int right, int l, int r) {
if (left > right)
return Integer.MAX_VALUE;
if (left >= l && r >= right) {
return segment_tree[index];
}
if (l > right || left > r)
return Integer.MAX_VALUE;
int mid = (left + right) / 2;
return Math.min(query(2 * index + 1, left, mid, l, r), query(2 * index + 2, mid + 1, right, l, r));
}
}
static class max_Segment_Tree {
public long[] segment_tree;
public max_Segment_Tree(int n) {
this.segment_tree = new long[4 * n + 1];
}
void build(int index, int left, int right, int[] a) {
// pn(index+" "+left+" "+right);
if (left == right) {
segment_tree[index] = a[left];
return;
}
int mid = (left + right) / 2;
build(2 * index + 1, left, mid, a);
build(2 * index + 2, mid + 1, right, a);
segment_tree[index] = Math.max(segment_tree[2 * index + 1], segment_tree[2 * index + 2]);
}
long query(int index, int left, int right, int l, int r) {
if (left >= l && r >= right) {
return segment_tree[index];
}
if (l > right || left > r)
return Integer.MIN_VALUE;
int mid = (left + right) / 2;
long max = Math.max(query(2 * index + 1, left, mid, l, r), query(2 * index + 2, mid + 1, right, l, r));
// pn(left+" "+right+" "+max);
return max;
}
}
// // ------------------------------------------------------ DSU
// // --------------------------------------------------------------------//
static class dsu {
private int[] parent;
private int[] rank;
private int[] size;
public dsu(int n) {
this.parent = new int[n + 1];
this.rank = new int[n + 1];
this.size = new int[n + 1];
for (int i = 0; i <= n; i++) {
parent[i] = i;
rank[i] = 1;
size[i] = 1;
}
}
int findParent(int a) {
if (parent[a] == a)
return a;
else
return parent[a] = findParent(parent[a]);
}
void join(int a, int b) {
int parent_a = findParent(a);
int parent_b = findParent(b);
if (parent_a == parent_b)
return;
if (rank[parent_a] > rank[parent_b]) {
parent[parent_b] = parent_a;
size[parent_a] += size[parent_b];
} else if (rank[parent_a] < rank[parent_b]) {
parent[parent_a] = parent_b;
size[parent_b] += size[parent_a];
} else {
parent[parent_a] = parent_b;
size[parent_b] += size[parent_a];
rank[parent_b]++;
}
}
}
// ------------------------------------------------Comparable---------------------------------------------------------------------//
public static class rectangle {
int x1, x3, y1, y3;// lower left and upper rigth coordinates
int x2, y2, x4, y4;// remaining coordinates
/*
* (x4,y4) (x3,y3)
* ____________
* | |
* |____________|
*
* (x1,y1) (x2,y2)
*/
public rectangle(int x1, int y1, int x3, int y3) {
this.x1 = x1;
this.y1 = y1;
this.x3 = x3;
this.y3 = y3;
this.x2 = x3;
this.y2 = y1;
this.x4 = x1;
this.y4 = y3;
}
public long area() {
if (x3 < x1 || y3 < y1)
return 0;
return (long) Math.abs(x1 - x3) * (long) Math.abs(y1 - y3);
}
}
static long intersection(rectangle a, rectangle b) {
if (a.x3 < a.x1 || a.y3 < a.y1 || b.x3 < b.x1 || b.y3 < b.y1)
return 0;
long l1 = ((long) Math.min(a.x3, b.x3) - (long) Math.max(a.x1, b.x1));
long l2 = ((long) Math.min(a.y3, b.y3) - (long) Math.max(a.y1, b.y1));
if (l1 < 0 || l2 < 0)
return 0;
long area = ((long) Math.min(a.x3, b.x3) - (long) Math.max(a.x1, b.x1))
* ((long) Math.min(a.y3, b.y3) - (long) Math.max(a.y1, b.y1));
if (area < 0)
return 0;
return area;
}
static class pair implements Comparable<pair> {
int a;
int b;
int dir;
public pair(int a, int b, int dir) {
this.a = a;
this.b = b;
this.dir = dir;
}
public int compareTo(pair p) {
// if (this.b == Integer.MIN_VALUE || p.b == Integer.MIN_VALUE)
// return (int) (this.index - p.index);
return (int) (this.a - p.a);
}
}
static class pair2 implements Comparable<pair2> {
long a;
int index;
public pair2(long a, int index) {
this.a = a;
this.index = index;
}
public int compareTo(pair2 p) {
return (int) (this.a - p.a);
}
}
static class node implements Comparable<node> {
int l;
int r;
public node(int l, int r) {
this.l = l;
this.r = r;
}
public int compareTo(node a) {
if(this.l==a.l){
return this.r-a.r;
}
return (int) (this.l - a.l);
}
}
static long ans = 0;
static int leaf = 0;
static boolean poss = true;
static long mod = 1000000007L;
static int[] dx = { -1, 0, 0, 1 };
static int[] dy = { 0, -1, 1, 0 };
static boolean cycle;
int count = 0;
public static void main(String[] args) throws Exception {
// new Thread(null,new Main(), "1", 1 << 26).start();
long start = System.nanoTime();
in = new FastReader();
out = new PrintWriter(System.out, false);
int tc = ni();
while (tc-- > 0) {
// int[] a=new int[n];
int n=ni();
int q=ni();
long[][] dp=new long[1003][1003];
for(int i=0;i<n;i++){
int a=ni();
int b=ni();
dp[a][b]+=a*b;
}
for(int i=1;i<=1000;i++){
for(int j=1;j<1001;j++){
dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1];
}
}
for(int i=0;i<q;i++){
int a=ni();
int b=ni();
int c=ni();
int d=ni();
long val=dp[c-1][d-1]-dp[c-1][b]-dp[a][d-1]+dp[a][b];
pn(Math.max(val,0));
}
}
long end = System.nanoTime();
// pn((end-start)*1.0/1000000000);
out.flush();
out.close();
}
static long ncm(long[] fact, long[] fact_inv, int n, int m) {
if (n < m)
return 0L;
long a = fact[n];
long b = fact_inv[n - m];
long c = fact_inv[m];
a = (a * b) % mod;
return (a * c) % mod;
}
public void run() {
try {
in = new FastReader();
out = new PrintWriter(System.out);
int tc = ni();
while (tc-- > 0) {
}
out.flush();
} catch (Exception e) {
}
}
static void dfs(int i, int j,boolean[][] visited) {
// visited[i] = true;
// for (int nei : arr.get(i)) {
// if (nei == p || visited[nei])
// continue;
// dfs(nei, i, visited, arr);
// }
}
static boolean inside(int i, int j, int n, int m) {
if (i >= 0 && j >= 0 && i < n && j < m)
return true;
return false;
}
static int binary_search(int[] a, int val) {
int l = 0;
int r = a.length - 1;
int ans = 0;
while (l <= r) {
int mid = l + (r - l) / 2;
if (a[mid] <= val) {
ans = mid;
l = mid + 1;
} else
r = mid - 1;
}
return ans;
}
static int[] longest_common_prefix(String s) {
int m = s.length();
int[] lcs = new int[m];
int len = 0;
int i = 1;
lcs[0] = 0;
while (i < m) {
if (s.charAt(i) == s.charAt(len)) {
lcs[i++] = ++len;
} else {
if (len == 0) {
lcs[i] = 0;
i++;
} else
len = lcs[len - 1];
}
}
return lcs;
}
static void swap(char[] a, char[] b, int i, int j) {
char temp = a[i];
a[i] = b[j];
b[j] = temp;
}
static void factorial(long[] fact, long[] fact_inv, int n, long mod) {
fact[0] = 1;
for (int i = 1; i < n; i++) {
fact[i] = (i * fact[i - 1]) % mod;
}
for (int i = 0; i < n; i++) {
fact_inv[i] = power(fact[i], mod - 2, mod);// (1/x)%m can be calculated by fermat's little theoram which is
// (x**(m-2))%m when m is prime
}
// (a^(b^c))%m is equal to, let res=(b^c)%(m-1) then (a^res)%m
// https://www.geeksforgeeks.org/find-power-power-mod-prime/?ref=rp
}
static void find(int i, int n, int[] row, int[] col, int[] d1, int[] d2) {
if (i >= n) {
ans++;
return;
}
for (int j = 0; j < n; j++) {
if (col[j] == 0 && d1[i - j + n - 1] == 0 && d2[i + j] == 0) {
col[j] = 1;
d1[i - j + n - 1] = 1;
d2[i + j] = 1;
find(i + 1, n, row, col, d1, d2);
col[j] = 0;
d1[i - j + n - 1] = 0;
d2[i + j] = 0;
}
}
}
static int answer(int l, int r, int[][] dp) {
if (l > r)
return 0;
if (l == r) {
dp[l][r] = 1;
return 1;
}
if (dp[l][r] != -1)
return dp[l][r];
int val = Integer.MIN_VALUE;
int mid = l + (r - l) / 2;
val = 1 + Math.max(answer(l, mid - 1, dp), answer(mid + 1, r, dp));
return dp[l][r] = val;
}
// static long find(String s, int i, int n, long[] dp) {
// pn(i);
// if (i >= n)
// return 1L;
// if (s.charAt(i) == '0')
// return 0;
// if (i == n - 1)
// return 1L;
// if (dp[i] != -1)
// return dp[i];
// if (s.substring(i, i + 2).equals("10") || s.substring(i, i + 2).equals("20"))
// {
// return dp[i] = (find(s, i + 2, n, dp)) % mod;
// }
// if ((s.charAt(i) == '1' || (s.charAt(i) == '2' && s.charAt(i + 1) - '0' <=
// 6))
// && ((i + 2 < n ? (s.charAt(i + 2) != '0' ? true : false) : (i + 2 == n ? true
// : false)))) {
// return dp[i] = (find(s, i + 1, n, dp) + find(s, i + 2, n, dp)) % mod;
// } else
// return dp[i] = (find(s, i + 1, n, dp)) % mod;
static void print(int[] a) {
for (int i = 0; i < a.length; i++)
p(a[i] + " ");
pn("");
}
static long count(long n) {
long count = 0;
while (n != 0) {
count += n % 10;
n /= 10;
}
return count;
}
static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static int LcsOfPrefix(String a, String b) {
int i = 0;
int j = 0;
int count = 0;
while (i < a.length() && j < b.length()) {
if (a.charAt(i) == b.charAt(j)) {
j++;
count++;
}
i++;
}
return a.length() + b.length() - 2 * count;
}
static void reverse(int[] a, int n) {
for (int i = 0; i < n / 2; i++) {
int temp = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = temp;
}
}
static char get_char(int a) {
return (char) (a + 'A');
}
static int find1(int[] a, int val) {
int ans = -1;
int l = 0;
int r = a.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (a[mid] <= val) {
l = mid + 1;
ans = mid;
} else
r = mid - 1;
}
return ans;
}
static int find2(int[] a, int val) {
int l = 0;
int r = a.length - 1;
int ans = -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;
}
// static void dfs(List<List<Integer>> arr, int node, int parent, long[] val) {
// p[node] = parent;
// in_time[node] = count;
// flat_tree[count] = val[node];
// subtree_gcd[node] = val[node];
// count++;
// for (int adj : arr.get(node)) {
// if (adj == parent)
// continue;
// dfs(arr, adj, node, val);
// subtree_gcd[node] = gcd(subtree_gcd[adj], subtree_gcd[node]);
// }
// out_time[node] = count;
// flat_tree[count] = val[node];
// count++;
// }
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
6af6506e904efe6ae1c69116fa036452
|
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.*;
import java.util.*;
public class p3
{
BufferedReader br;
StringTokenizer st;
BufferedWriter bw;
public static void main(String[] args)throws Exception
{
new p3().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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
14218c45266e88710040e2d7021d2208
|
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.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class HarHarShambhu{
static Scanner in=new Scanner();
static long systemTime;
static long mod = 1000000007;
//static ArrayList<ArrayList<Integer>> adj;
static int seive[]=new int[1000001];
static long C[][];
public static void main(String[] args) throws Exception{
int z=in.readInt();
for(int test=1;test<=z;test++) {
//setTime();
solve();
//printTime();
//printMemory();
}
}
static void solve() {
int n=in.readInt();
int q=in.readInt();
long a[][]=new long[n][2];
for(int i=0;i<n;i++) {
a[i][0]=in.readLong();
a[i][1]=in.readLong();
}
long pre[][]=new long[1001][1001];
for(int i=0;i<n;i++) {
pre[(int)a[i][0]][(int)a[i][1]]++;
}
for(int i=0;i<1001;i++) {
for(int j=0;j<1001;j++) {
pre[i][j]=(long)(i*j)*(long)pre[i][j];
if(i>0) {
pre[i][j]+=pre[i-1][j];
}
if(j>0) {
pre[i][j]+=pre[i][j-1];
}
if(i>0&&j>0) {
pre[i][j]-=pre[i-1][j-1];
}
}
}
while(q-->0) {
int b[]=nia(4);
print(pre[b[2]-1][b[3]-1]-pre[b[0]][b[3]-1]-pre[b[2]-1][b[1]]+pre[b[0]][b[1]]);
}
}
static int[] bs1(long a[][],int n,long h1,long h2) {
int l=0,r=n-1;
int left=-1;
int right=-1;
while(l<=r) {
int mid=(l+r)/2;
if(a[mid][0]<h2&&a[mid][0]>h1) {
left=mid;
r=mid-1;
}
else if(a[mid][0]>=h2) {
r=mid-1;
}
else {
l=mid+1;
}
}
l=0;r=n-1;
while(l<=r) {
int mid=(l+r)/2;
if(a[mid][0]<h2&&a[mid][0]>h1) {
right=mid;
l=mid+1;
}
else if(a[mid][0]>=h2) {
r=mid-1;
}
else {
l=mid+1;
}
}
return new int[] {left,right};
}
static int[] bs2(long a[][],int l1,int r1,long h1,long h2) {
int l=l1,r=r1;
int left=-1;
int right=-1;
while(l<=r) {
int mid=(l+r)/2;
if(a[mid][1]<h2&&a[mid][1]>h1) {
left=mid;
r=mid-1;
}
else if(a[mid][1]>=h2) {
r=mid-1;
}
else {
l=mid+1;
}
}
l=l1;r=r1;
while(l<=r) {
int mid=(l+r)/2;
if(a[mid][1]<h2&&a[mid][1]>h1) {
right=mid;
l=mid+1;
}
else if(a[mid][1]>=h2) {
r=mid-1;
}
else {
l=mid+1;
}
}
return new int[] {left,right};
}
static long pow(long n, long m) {
if(m==0)
return 1;
else if(m==1)
return n;
else {
long r=pow(n,m/2);
if(m%2==0)
return (r*r);
else
return (r*r*n);
}
}
static long maxsumsub(ArrayList<Long> al) {
long max=0;
long sum=0;
for(int i=0;i<al.size();i++) {
sum+=al.get(i);
if(sum<0) {
sum=0;
}
max=Math.max(max,sum);
}
long a[]=al.stream().mapToLong(i -> i).toArray();
return max;
}
static long abs(long a) {
return Math.abs(a);
}
static void ncr(int n, int k){
C= new long[n + 1][k + 1];
int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= Math.min(i, k); j++) {
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
}
static boolean isPalin(String s) {
int i=0,j=s.length()-1;
while(i<=j) {
if(s.charAt(i)!=s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
static int knapsack(int W, int wt[],int val[], int n){
int []dp = new int[W + 1];
for (int i = 1; i < n + 1; i++) {
for (int w = W; w >= 0; w--) {
if (wt[i - 1] <= w) {
dp[w] = Math.max(dp[w],dp[w - wt[i - 1]] + val[i - 1]);
}
}
}
return dp[W];
}
static void seive() {
Arrays.fill(seive, 1);
seive[0]=0;
seive[1]=0;
for(int i=2;i*i<1000001;i++) {
if(seive[i]==1) {
for(int j=i*i;j<1000001;j+=i) {
if(seive[j]==1) {
seive[j]=0;
}
}
}
}
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a)
l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++)
a[i]=l.get(i);
}
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 int[] nia(int n){
int[] arr= new int[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static long[] nla(int n){
long[] arr= new long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static long[] nla1(int n){
long[] arr= new long[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readLong();
}
return arr;
}
static int[] nia1(int n){
int[] arr= new int[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readInt();
}
return arr;
}
static Integer[] nIa(int n){
Integer[] arr= new Integer[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static Long[] nLa(int n){
Long[] arr= new Long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static long gcd(long a, long b) {
if (b==0) return a;
return gcd(b, a%b);
}
static void no() {
System.out.println("NO");
}
static void yes() {
System.out.println("YES");
}
static void print(long i) {
System.out.println(i);
}
static void print(Object o) {
System.out.println(o);
}
static void print(int a[]) {
for(int i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long a[]) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(ArrayList<Long> a) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(Object a[]) {
for(Object i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void setTime() {
systemTime = System.currentTimeMillis();
}
static void printTime() {
System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime));
}
static void printMemory() {
System.err.println("Memory consumed: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + "kb");
}
static class Scanner{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String readString() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
double readDouble() {
return Double.parseDouble(readString());
}
int readInt() {
return Integer.parseInt(readString());
}
long readLong() {
return Long.parseLong(readString());
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
c4fe3acebd1fa6e1b9de8411187848e1
|
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.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.Comparator;
import java.util.List;
import java.util.StringTokenizer;
public class E {
static final long mod = (long) 1e9 + 7l;
static List<int[]> [] adj;
static int count;
private static void solve(int t){ // 2d pref sums
int n = fs.nextInt();
int q = fs.nextInt();
int N = 1005;
long rec[][] = new long[N][N];
for (int i = 0; i < n; i++) {
int h = fs.nextInt();
int w = fs.nextInt();
rec[h][w] += h*w;
}
for (int i = 1; i < N; i++) {
for (int j = 1; j < N; j++) {
rec[i][j]+=rec[i][j-1];
}
}
while (q-->0){
int lh = fs.nextInt();
int lw = fs.nextInt();
int uh = fs.nextInt();
int uw = fs.nextInt();
long ans =0;
for (int i = lh+1; i < uh; i++) {
ans+= rec[i][uw-1]-rec[i][lw];
}
out.println(ans);
}
}
private static int[] sortByCollections(int[] arr, boolean reverse) {
ArrayList<Integer> ls = new ArrayList<>(arr.length);
for (int i = 0; i < arr.length; i++) {
ls.add(arr[i]);
}
if(reverse)Collections.sort(ls, Comparator.reverseOrder());
else Collections.sort(ls);
for (int i = 0; i < arr.length; i++) {
arr[i] = ls.get(i);
}
return arr;
}
public static void main(String[] args) {
fs = new FastScanner();
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
int t = fs.nextInt();
for (int i = 1; i <= t; i++) solve(t);
out.close();
// System.err.println( System.currentTimeMillis() - s + "ms" );
}
static boolean DEBUG = true;
static PrintWriter out;
static FastScanner fs;
static void trace(Object... o) {
if (!DEBUG) return;
System.err.println(Arrays.deepToString(o));
}
static void pl(Object o) {
out.println(o);
}
static void p(Object o) {
out.print(o);
}
static long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
static int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
static void sieveOfEratosthenes(int n, int factors[]) {
factors[1] = 1;
for (int p = 2; p * p <= n; p++) {
if (factors[p] == 0) {
factors[p] = p;
for (int i = p * p; i <= n; i += p)
factors[i] = p;
}
}
}
static long mul(long a, long b) {
return a * b % mod;
}
static long fact(int x) {
long ans = 1;
for (int i = 2; i <= x; i++) ans = mul(ans, i);
return ans;
}
static long fastPow(long base, long exp) {
if (exp == 0) return 1;
long half = fastPow(base, exp / 2);
if (exp % 2 == 0) return mul(half, half);
return mul(half, mul(half, base));
}
static long modInv(long x) {
return fastPow(x, mod - 2);
}
static long nCk(int n, int k) {
return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n - k))));
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
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());
}
}
static class _Scanner {
InputStream is;
_Scanner(InputStream is) {
this.is = is;
}
byte[] bb = new byte[1 << 15];
int k, l;
byte getc() throws IOException {
if (k >= l) {
k = 0;
l = is.read(bb);
if (l < 0) return -1;
}
return bb[k++];
}
byte skip() throws IOException {
byte b;
while ((b = getc()) <= 32)
;
return b;
}
int nextInt() throws IOException {
int n = 0;
for (byte b = skip(); b > 32; b = getc())
n = n * 10 + b - '0';
return n;
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
9801978ee699561aef3514639a730cbc
|
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class CodeForces {
static FastScanner fs = new FastScanner();
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
int testCases = fs.nextInt();
for (int u = 0; u < testCases; u++) {
int a = fs.nextInt();
int b = fs.nextInt();
long[][] count = new long[1001][1001];
for (int i = 0; i < a; i++) {
int left = fs.nextInt();
int right = fs.nextInt();
count[left][right] += left * right;
}
long[][] pre = new long[1002][1002];
for (int i = 1; i < 1001; i++) {
for (int j = 1; j < 1001; j++) {
pre[i + 1][j + 1] = pre[i + 1][j] + count[i][j];
}
}
for (int i = 0; i < b; i++) {
int l1= fs.nextInt();
int r1 = fs.nextInt();
int l2 = fs.nextInt();
int r2 = fs.nextInt();
long res = 0;
for (int j = l1 + 1; j < l2; j++) {
res += pre[j + 1][r2] - pre[j + 1][r1 + 1];
}
out.println(res);
}
}
out.close();
}
static int[] t;
static int n;
static void add(int x) {
//因为树状数组t中,储存的为下标,所以最大下标+1应该为n
for (; x <= n; x += x & (-x)) t[x]++;
}
static int sum(int x) {
int sum = 0;
for (; x > 0; x -= x & (-x)) sum += t[x];
return sum;
}
// 并查集
static class UnionFind {
int[] roots;
public UnionFind(int n) {
roots = new int[n];
for (int i = 0; i < n; i++) {
roots[i] = i;
}
}
public int find(int i) {
if (roots[i] == i) {
return i;
}
return roots[i] = find(roots[i]);
}
// 判断 p 和 q 是否在同一个集合中
public boolean isConnected(int p, int q) {
return find(q) == find(p);
}
// 合并 p 和 q 到一个集合中
public void union(int p, int q) {
roots[find(p)] = find(q);
}
}
static final Random random=new Random();
static final int mod=998244353;
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
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 long add(long a, long b) {
return (a+b)%mod;
}
static long sub(long a, long b) {
return ((a-b)%mod+mod)%mod;
}
static long mul(long a, long b) {
return (a*b)%mod;
}
static long exp(long base, long exp) {
if (exp==0) return 1;
long half=exp(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int 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());
}
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());
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
6fee8e5070716072bef9a2b3bf4df9cf
|
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
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
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 {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static long modPow(long var, long num) {
long m = 1;
while (num > 0) {
m = (m * var) %1000000007;
--num;
}
return m;}
public static void main (String[] args) throws java.lang.Exception
{
FastReader s = new FastReader();
BufferedWriter output = new BufferedWriter(
new OutputStreamWriter(System.out));
int t=s.nextInt();
for(int o=0;o<t;o++)
{
int n=s.nextInt();
int q=s.nextInt();
long[][] arr= new long[1001][1001];
for(int i=0;i<n;i++)
{
int l=s.nextInt();
int w=s.nextInt();
arr[l][w]=arr[l][w]+(long)l*(long)w;
}
for(int i=1;i<1001;i++)
{
for(int j=1;j<1001;j++)
{
arr[i][j]=arr[i][j-1]+arr[i-1][j]-arr[i-1][j-1]+arr[i][j];
}
}
for(int i=0;i<q;i++)
{
int l1=s.nextInt();
int w1=s.nextInt();
int l2=s.nextInt();
int w2=s.nextInt();
long ans= arr[l2-1][w2-1]-arr[l1][w2-1]-arr[l2-1][w1]+arr[l1][w1];
output.write(String.valueOf(ans)+"\n");
}
//output.write("hello");
}
output.flush();
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
28552b901985918567af6d43a1d1bcf8
|
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.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Solution {
static PrintWriter pw;
static FastScanner s;
public static void main(String[] args) throws Exception {
pw=new PrintWriter(System.out);
s=new FastScanner(System.in);
long t=s.nextLong();
while(t-->0) {
long rect=s.nextLong();
long queries=s.nextLong();
long area[][]=new long[1001][1001];
while(rect-->0) {
int h=s.nextInt();
int w=s.nextInt();
area[h][w]+=(long)h*(long)w;
}
long prefix[][]=new long[1001][1001];
for(int i=1;i<1001;i++)
for(int j=1;j<1001;j++)
prefix[i][j]=prefix[i-1][j]+prefix[i][j-1]-prefix[i-1][j-1]+area[i][j];
while(queries-->0) {
int a=s.nextInt();
int b=s.nextInt();
int A=s.nextInt();
int B=s.nextInt();
pw.println(prefix[A-1][B-1]-prefix[A-1][b]-prefix[a][B-1]+prefix[a][b]);
}
}
pw.flush();
}
//public static void main(String[] args) throws Exception {
// pw=new PrintWriter(System.out);
// s=new FastScanner(System.in);
// long t=s.nextLong();
// while(t-->0) {
// long rect=s.nextLong();
// long queries=s.nextLong();
// ArrayList<Rectangle> rectangles=new ArrayList<Rectangle>();
// while(rect-->0) {
// rectangles.add(new Rectangle(s.nextLong(), s.nextLong()));
// }
// while(queries-->0) {
// Rectangle small=new Rectangle(s.nextLong(),s.nextLong());
// Rectangle large=new Rectangle(s.nextLong(),s.nextLong());
// long area=0;
// for(int i=0;i<rectangles.size();i++) {
// Rectangle curr=rectangles.get(i);
// boolean flag=(curr.len<large.len)&&
// (curr.wid<large.wid && curr.len>small.len
// && curr.wid>small.wid);
// if(flag)
// area+=curr.len*curr.wid;
// }
// pw.print(area);
// pw.println();
//
// }
//
// }
//
//pw.flush();
//
//
//}
}
class Rectangle{
long len;
long wid;
public Rectangle(long len, long wid) {
super();
this.len = len;
this.wid = wid;
}
@Override
public String toString() {
return "[ " + len + "," + wid + "]";
}
}
class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public FastScanner(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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
120ef74bf7c104fee4a9bb57fa02aff0
|
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
//
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();
}
public int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static long[][]arr=new long[1001][1001];
static MyScanner s=new MyScanner();
public static void process(){
int n=s.nextInt(),q=s.nextInt();
for(int i=0;i<1001;i++)Arrays.fill(arr[i],0);
for(int i=0;i<n;i++){
int a=s.nextInt(),b=s.nextInt();
arr[a][b]+=(long)a*b;
}
for(int i=1;i<1001;i++){
for(int j=1;j<1001;j++){
arr[i][j]+=(arr[i-1][j]+arr[i][j-1]-arr[i-1][j-1]);
}
}
for(int i=0;i<q;i++){
int h1=s.nextInt(),w1=s.nextInt(),h2=s.nextInt(),w2=s.nextInt();
System.out.println(arr[h2-1][w2-1]+arr[h1][w1]-arr[h2-1][w1]-arr[h1][w2-1]);
}
}
public static void main(String[] args) {
int num=s.nextInt();
for(int k=0;k<num;k++){
process();
}
}
}
|
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
3160f962e3d80f264d9250bd75a5bfba
|
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
|
//package codeforce.div4.r817;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import static java.lang.System.out;
import static java.util.stream.Collectors.joining;
/**
* @author pribic (Priyank Doshi)
* @see <a href="https://codeforces.com/contest/1722/problem/E" target="_top">https://codeforces.com/contest/1722/problem/E</a>
* @since 01/09/22 9:31 AM
*/
public class E {
static FastScanner sc = new FastScanner(System.in);
public static void main(String[] args) {
try (PrintWriter out = new PrintWriter(System.out)) {
int T = sc.nextInt();
for (int tt = 1; tt <= T; tt++) {
int n = sc.nextInt();
int q = sc.nextInt();
long[][] rectangles = new long[1001][1001];
for (int i = 0; i < n; i++) {
int h = sc.nextInt();
int w = sc.nextInt();
rectangles[h][w] += (long) h * w;
}
//precompute
for (int i = 1; i < rectangles.length; i++) {
for (int j = 1; j < rectangles.length; j++) {
rectangles[i][j] += rectangles[i][j - 1] + rectangles[i - 1][j] - rectangles[i - 1][j - 1];
}
}
//read queries
for (int i = 0; i < q; i++) {
int hs = sc.nextInt();
int ws = sc.nextInt();
int hb = sc.nextInt();
int wb = sc.nextInt();
long totalArea = rectangles[hb - 1][wb - 1]
- rectangles[hb - 1][ws]
- rectangles[hs][wb - 1]
+ rectangles[hs][ws];
System.out.println(totalArea);
}
}
}
}
/*
3*3
0 1 2 3 4
0 | | | | | |
1 | | | | | |
2 | | | | 6| |
3 | | |6 | | |
*/
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f), 32768);
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
5c429187a33668d7378e7691303bbb9c
|
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.util.Arrays;
import java.util.InputMismatchException;
/**
*
* 二位前缀和
* */
public class E {
public static void main(String[] args) {
//Scanner sc = new Scanner(System.in);
RealFastReader r = new RealFastReader(System.in);
int t = r.ni();
StringBuilder sb = new StringBuilder();
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];
sb.append(ans).append("\n");
}
}
System.out.println(sb);
}
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 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 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 < h_b,\ w_s < 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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.