G-Segmentation The 15th Wenyuan Zhixing Cup Programming Competition of Guangdong University of Technology in 2021 (simultaneous competition)

G-Segmentation The 15th Wenyuan Zhixing Cup Programming Competition of Guangdong University of Technology in 2021 (simultaneous competition)

In a two-dimensional plane,
there are n line parallel to the y-axis, the x-coordinate is their x [I],
m of straight lines parallel to the x-axis y [I], their y coordinates is y [i].
Seek The total area of ​​all these straight lines that may form a rectangle is modulo 1000000007.
Ensure that all straight lines do not completely overlap.

Total area = the distance between any two x-lines and * the sum of the distance between any two y-lines

First sort all x-rays from smallest to largest.
Then calculate the sum of the distances from each line to all previous lines.
The sum of the distances of each line is the sum of the distances of any two x-lines.
The y line is the same, and
finally multiply it

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll x[100100];
ll y[100100];
ll dx[100100];
ll dy[100100];
ll mod=1000000007;
int main(){
    
    
    ios::sync_with_stdio(0);
    ll n,m;
    ll ans;
    ll sumx=0,sumy=0;
    cin>>n>>m;
    for(ll i=0;i<n;i++){
    
    
        cin>>x[i];
    }
    for(ll i=0;i<m;i++){
    
    
        cin>>y[i];
    }
    sort(x,x+n);
    sort(y,y+m);
    dx[0]=0;
    for(ll i=1;i<n;i++){
    
    
        dx[i]=(x[i]-x[i-1])*i+dx[i-1];
        dx[i]%=mod;
        sumx+=dx[i];
        sumx%=mod;
    }
    dy[0]=0;
    for(ll i=1;i<m;i++){
    
    
        dy[i]=(y[i]-y[i-1])*i+dy[i-1];
        dy[i]%=mod;
        sumy+=dy[i];
        sumy%=mod;
    }
    ans=(sumx*sumy)%mod;
    cout<<ans;
    return 0;
}

Guess you like

Origin blog.csdn.net/lanxing147/article/details/115300514