AtCoder:井井井 / ###(数学)

本文介绍了一种算法,用于计算由平行于坐标轴的线段所形成的矩形的总面积。通过固定每一对平行于y轴的线段,枚举所有平行于x轴的线段组合,最终求得所有矩形面积之和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

D - 井井井 / ###


Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement

On a two-dimensional plane, there are m lines drawn parallel to the x axis, and n lines drawn parallel to the y axis. Among the lines parallel to the x axis, the i-th from the bottom is represented by y=yi. Similarly, among the lines parallel to the y axis, the i-th from the left is represented by x=xi.

For every rectangle that is formed by these lines, find its area, and print the total area modulo 109+7.

That is, for every quadruple (i,j,k,l) satisfying 1i<jn and 1k<lm, find the area of the rectangle formed by the lines x=xix=xjy=yk and y=yl, and print the sum of these areas modulo 109+7.

Constraints

  • 2n,m105
  • −109x1<<xn109
  • −109y1<<ym109
  • xi and yi are integers.

Input

Input is given from Standard Input in the following format:

n m
x1 x2  xn
y1 y2  ym

Output

Print the total area of the rectangles, modulo 109+7.


Sample Input 1

Copy
3 3
1 3 4
1 3 6

Sample Output 1

Copy
60

The following figure illustrates this input:

sample1-1

The total area of the nine rectangles A, B, ..., I shown in the following figure, is 60.

sample1-2


Sample Input 2

Copy
6 5
-790013317 -192321079 95834122 418379342 586260100 802780784
-253230108 193944314 363756450 712662868 735867677

Sample Output 2

Copy
835067060
题意:给定n条平行于y轴的线x[1~n],和m条平行于x轴的线y[1~n],计算x[i],x[j]和y[k],y[l]组成的矩形面积之和,1<=x<y<=n,1<=k<l<=m。

思路:每次固定两个y线,枚举所有x线二元组,发现x线和y线可以分开计算,即

# include <bits/stdc++.h>
using namespace std;
 
typedef long long LL;
const LL mod = 1e9+7;
LL x[100003], y[100003];
int main()
{
    int n, m;
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; ++i)
        scanf("%lld",&x[i]);
    for(int i=0; i<m; ++i)
        scanf("%lld",&y[i]);
    LL W = 0, H = 0;
    for(int l=0, r=n-1; l<r; ++l,--r)//考虑每个小区间对答案的贡献。
        W = (W + (x[r]-x[l])*(r-l))%mod;
    for(int l=0, r=m-1; l<r; ++l,--r)
        H = (H + (y[r]-y[l])*(r-l))%mod;
    printf("%lld\n",(W*H)%mod);
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值