Four-tuples

链接:https://www.nowcoder.com/acm/contest/123/F
来源:牛客网
 

题目描述

Given   ,please count the number of four-tuples such thatand  

The answer should modulo109+7 before output.

输入描述:

The input consists of several test cases. The first
line gives the number of test cases,T(1≤T≤106).

For each test case, the input contains one line
with 8 integers,.

输出描述:

For each test case, output one line containing one
integer, representing the answer.

示例1

输入

复制

1
1 1 2 2 3 3 4 4

输出

复制

1
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define bug(x) printf("sum:%lld\n",x)

const int mod=1e9+7;


ll L[5],R[5],H[5];
/*
当年欠的债,终于理解了
*/

int main(){
    //freopen("123.txt","r",stdin);
    //freopen("4.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--){
        rep(i,1,5)scanf("%lld %lld",&L[i],&R[i]);
        rep(i,1,5)H[i]=R[i]-L[i]+1;
        ll ans=1;
        rep(i,1,5)ans=ans*H[i]%mod;

        ll low,hig,sum=1;
        //a
        low=max(L[1],L[2]),hig=min(R[1],R[2]);
        ans=(ans-max(0ll,hig-low+1)%mod*H[3]%mod*H[4]%mod+mod)%mod;
        //b
        low=max(L[2],L[3]),hig=min(R[2],R[3]);
        ans=(ans-max(0ll,hig-low+1)%mod*H[1]%mod*H[4]%mod+mod)%mod;
        //c
        low=max(L[3],L[4]),hig=min(R[3],R[4]);
        ans=(ans-max(0ll,hig-low+1)%mod*H[1]%mod*H[2]%mod+mod)%mod;
        //d
        low=max(L[4],L[1]),hig=min(R[4],R[1]);
        ans=(ans-max(0ll,hig-low+1)%mod*H[2]%mod*H[3]%mod+mod)%mod;

        //ab
        low=max(L[1],max(L[2],L[3])),hig=min(R[1],min(R[2],R[3]));
        ans=(ans+max(0ll,(hig-low+1))*H[4]%mod)%mod;
        //ac
        low=max(L[1],L[2]),hig=min(R[1],R[2]);
        sum=max(0ll,(hig-low+1))%mod;
        low=max(L[3],L[4]),hig=min(R[3],R[4]);
        sum=sum*max(0ll,(hig-low+1))%mod;
        ans=(ans+sum)%mod;
        //ad
        low=max(L[1],max(L[2],L[4])),hig=min(R[1],min(R[2],R[4]));
        ans=(ans+max(0ll,(hig-low+1))*H[3]%mod)%mod;
        //bc
        low=max(L[2],max(L[3],L[4])),hig=min(R[2],min(R[3],R[4]));
        ans=(ans+max(0ll,(hig-low+1))*H[1]%mod)%mod;
        //bd
        low=max(L[1],L[4]),hig=min(R[1],R[4]);
        sum=max(0ll,(hig-low+1))%mod;
        low=max(L[2],L[3]),hig=min(R[2],R[3]);
        sum=sum*max(0ll,(hig-low+1))%mod;
        ans=(ans+sum)%mod;
        //cd
        low=max(L[3],max(L[4],L[1])),hig=min(R[3],min(R[4],R[1]));
        ans=(ans+max(0ll,(hig-low+1))*H[2]%mod)%mod;

        low=max(max(L[1],L[2]),max(L[3],L[4])),hig=min(min(R[1],R[2]),min(R[3],R[4]));
        for(int i=1;i<=3;i++)   ans=(ans-max(0ll,(hig-low+1))%mod+mod)%mod;


        //rep(i,1,5)printf("%lld %lld%c",L[i],R[i],i==4?'\n':' ');
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/81711817