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 1≤i<j≤n and 1≤k<l≤m, find the area of the rectangle formed by the lines x=xi, x=xj, y=yk and y=yl, and print the sum of these areas modulo 109+7.
Constraints
- 2≤n,m≤105
- −109≤x1<…<xn≤109
- −109≤y1<…<ym≤109
- 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
3 3 1 3 4 1 3 6
Sample Output 1
60
The following figure illustrates this input:
The total area of the nine rectangles A, B, ..., I shown in the following figure, is 60.
Sample Input 2
6 5 -790013317 -192321079 95834122 418379342 586260100 802780784 -253230108 193944314 363756450 712662868 735867677
Sample Output 2
835067060
思路:每次固定两个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;
}