当前位置: 首页 > news >正文

E. Counting Rectangles(二维前缀和)

原题链接

You have nn rectangles, the ii-th rectangle has height hihi and width wiwi.

You are asked qq queries of the form hs ws hb wbhs ws hb wb.

For each query output, the total area of rectangles you own that can fit a rectangle of height hshs and width wsws while also fitting in a rectangle of height hbhb and width wbwb. In other words, print ∑hi⋅wi∑hi⋅wi for ii such that hs<hi<hbhs<hi<hb and ws<wi<wbws<wi<wb.

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++).

Input

The first line of the input contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases.

The first line of each test case two integers n,qn,q (1≤n≤1051≤n≤105; 1≤q≤1051≤q≤105) — the number of rectangles you own and the number of queries.

Then nn lines follow, each containing two integers hi,wihi,wi (1≤hi,wi≤10001≤hi,wi≤1000) — the height and width of the ii-th rectangle.

Then qq lines follow, each containing four integers hs,ws,hb,wbhs,ws,hb,wb (1≤hs<hb, ws<wb≤10001≤hs<hb, ws<wb≤1000) — the description of each query.

The sum of qq over all test cases does not exceed 105105, and the sum of nn over all test cases does not exceed 105105.

Output

For each test case, output qq lines, the ii-th line containing the answer to the ii-th query.

Example

input

Copy

 

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

Copy

6
41
9
0
54
4
2993004

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×11×1 rectangle inside of it and fit into a 3×43×4 rectangle.

Only the 2×32×3 rectangle works, because 1<21<2 (comparing heights) and 1<31<3 (comparing widths), so the 1×11×1 rectangle fits inside, and 2<32<3 (comparing heights) and 3<43<4 (comparing widths), so it fits inside the 3×43×4 rectangle. The 3×23×2 rectangle is too tall to fit in a 3×43×4 rectangle. The total area is 2⋅3=62⋅3=6.

思路:

1,根据范围,不可能暴力,

2,只看输入的h,w,联想到二维前缀和,注意规范操作

代码:

int a[1020][1020];
void solve(){//二维前缀和
    // int x;read(x);cout<<x<<'\n';
    int n,q;
    memset(a,0,sizeof(a));
    read(n);read(q);
    int ans=0;
    for(int i=1;i<=n;++i){
        int x,y;
        read(x);read(y);
        a[x][y]+=x*y;
    }
    for(int i=1;i<=1000;++i){
        for(int j=1;j<=1000;++j){
            a[i][j]=a[i][j]+a[i-1][j]+a[i][j-1]-a[i-1][j-1];
        }
    }
    while(q--){
        int x1,y1,x2,y2;
        read(x1);read(y1);read(x2);read(y2);
        cout<<a[x2-1][y2-1]-a[x1][y2-1]-a[x2-1][y1]+a[x1][y1]<<'\n';
    }
}

相关文章:

  • 流量操作与后门
  • RADIUS 本地服务器还能用吗?
  • springboot+mybatisplus+postgis实现几何点和线串增删改查分页
  • linux内核移植流程
  • canvas 正在慢慢吃掉你的内存...
  • 【无标题】11111
  • go pprof 的使用
  • 类和对象 中
  • LeetCode变位词组
  • locust压测实例
  • 8.6 轻量化网络设计概述
  • 【C#】萌狼学习C#那年写的笔记汇总
  • 20个js工具函数助力高效开发
  • 软件领域中面向对象的设计模式
  • 01用户登录,登出,token等框架说明
  • #Java异常处理
  • .pyc 想到的一些问题
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • Android开源项目规范总结
  • java8 Stream Pipelines 浅析
  • overflow: hidden IE7无效
  • Python爬虫--- 1.3 BS4库的解析器
  • python学习笔记 - ThreadLocal
  • Vue2 SSR 的优化之旅
  • 服务器从安装到部署全过程(二)
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 如何使用 JavaScript 解析 URL
  • 世界上最简单的无等待算法(getAndIncrement)
  • 微服务框架lagom
  • 微信小程序--------语音识别(前端自己也能玩)
  • 我是如何设计 Upload 上传组件的
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • - 转 Ext2.0 form使用实例
  • 白色的风信子
  • 宾利慕尚创始人典藏版国内首秀,2025年前实现全系车型电动化 | 2019上海车展 ...
  • ​卜东波研究员:高观点下的少儿计算思维
  • ​批处理文件中的errorlevel用法
  • ​软考-高级-系统架构设计师教程(清华第2版)【第1章-绪论-思维导图】​
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (附源码)ssm高校运动会管理系统 毕业设计 020419
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)...
  • (十五)Flask覆写wsgi_app函数实现自定义中间件
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (转)大型网站架构演变和知识体系
  • .bat批处理(一):@echo off
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .Net Core与存储过程(一)
  • .NET Micro Framework 4.2 beta 源码探析
  • .NET Standard 的管理策略
  • .Net(C#)自定义WinForm控件之小结篇
  • .NET/C# 阻止屏幕关闭,阻止系统进入睡眠状态
  • .NetCore实践篇:分布式监控Zipkin持久化之殇