F-Four-tuples
####题目描述
Given
l
1
l_1
l1,
r
1
r_1
r1,
l
2
l_2
l2,
r
2
r_2
r2,
l
3
l_3
l3,
r
3
r_3
r3,
l
4
l_4
l4,
r
4
r_4
r4,please count the number of four-tuples (
x
1
x_1
x1,
x
2
x_2
x2,
x
3
x_3
x3,
x
4
x_4
x4) such that
l
i
l_i
li
≤
≤
≤
x
i
x_i
xi
≤
≤
≤
r
i
r_i
ri and
x
1
x_1
x1
≠
≠
̸=
x
2
x_2
x2,
x
2
x_2
x2
≠
≠
̸=
x
3
x_3
x3,
x
3
x_3
x3
≠
≠
̸=
x
4
x_4
x4,
x
4
x_4
x4
≠
≠
̸=
x
1
x_1
x1.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≤
1
0
6
10^6
106).
For each test case, the input contains one line with 8 integers
l
1
l_1
l1,
r
1
r_1
r1,
l
2
l_2
l2,
r
2
r_2
r2,
l
3
l_3
l3,
r
3
r_3
r3,
l
4
l_4
l4,
r
4
r_4
r4.(1
≤
≤
≤
l
i
l_i
li
≤
≤
≤
r
i
r_i
ri
≤
≤
≤
1
0
9
10^9
109)
####输出描述:
For each test case, output one line containing one integer, representing the answer.
####示例1
输入
1
1 1 2 2 3 3 4 4
输出
1
####题解:
首先看这个题,容易想到是容斥。设事件A为
x
1
x_1
x1
≠
≠
̸=
x
2
x_2
x2 ,事件B为
x
2
x_2
x2
≠
≠
̸=
x
3
x_3
x3 ,事件C为
x
3
x_3
x3
≠
≠
̸=
x
4
x_4
x4,事件D为
x
4
x_4
x4
≠
≠
̸=
x
1
x_1
x1
问题要求:
∣
A
∩
B
∩
C
∩
D
∣
|A∩B∩C∩D|
∣A∩B∩C∩D∣,可以转换成
∣
U
∣
−
∣
A
′
∪
B
′
∪
C
′
∪
D
′
∣
|U|-|A'∪B'∪C'∪D'|
∣U∣−∣A′∪B′∪C′∪D′∣,即求
∣
A
′
∪
B
′
∪
C
′
∪
D
′
∣
|A'∪B'∪C'∪D'|
∣A′∪B′∪C′∪D′∣ 就行,容斥定理:
因为相等的传递性,
x
1
x_1
x1=
x
2
x_2
x2,
x
2
x_2
x2=
x
3
x_3
x3,
x
3
x_3
x3=
x
4
x_4
x4和
x
1
x_1
x1=
x
2
x_2
x2,
x
2
x_2
x2=
x
3
x_3
x3,
x
4
x_4
x4=
x
1
x_1
x1和
x
1
x_1
x1=
x
2
x_2
x2,
x
3
x_3
x3=
x
4
x_4
x4,
x
4
x_4
x4=
x
1
x_1
x1和
x
2
x_2
x2=
x
3
x_3
x3,
x
3
x_3
x3=
x
4
x_4
x4,
x
4
x_4
x4=
x
1
x_1
x1和
x
1
x_1
x1=
x
2
x_2
x2,
x
2
x_2
x2=
x
3
x_3
x3,
x
3
x_3
x3=
x
4
x_4
x4,
x
4
x_4
x4=
x
1
x_1
x1是一样的结果,所以只要减去三倍
∣
A
∩
B
∩
C
∣
|A∩B∩C|
∣A∩B∩C∣就可以了。
####代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;
const int maxn=4;
const int mod=1e9+7;
LL L[maxn],R[maxn];
LL work2(int a,int b) //a==b
{
LL mL=max(L[a],L[b]);
LL mR=min(R[a],R[b]);
if(mR<mL) return 0;
return (mR-mL+1)%mod;
}
LL work3(int a,int b,int c) //a==b,c==d,e>=0
{
LL mL1=max(L[a],max(L[b],L[c]));
LL mR1=min(R[a],min(R[b],R[c]));
if(mL1>mR1) return 0;
return (mR1-mL1+1)%mod;
}
LL work4(int a,int b,int c,int d)
{
LL cnt=1;
LL mL=max(L[d],max(L[a],max(L[b],L[c])));
LL mR=min(R[d],min(R[a],min(R[b],R[c])));
if(mL>mR) return 0;
return (mR-mL+1)*3%mod;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
for(int i=0; i<maxn; ++i)
scanf("%lld %lld",&L[i],&R[i]);
LL ans=1;
for(int i=0; i<maxn; ++i)
{
ans=(ans*(R[i]-L[i]+1))%mod;
}
ans=(ans-work2(0,1)*(R[2]-L[2]+1)%mod*(R[3]-L[3]+1)%mod+mod)%mod;//0=1
ans=(ans-work2(1,2)*(R[0]-L[0]+1)%mod*(R[3]-L[3]+1)%mod+mod)%mod;//1=2
ans=(ans-work2(2,3)*(R[0]-L[0]+1)%mod*(R[1]-L[1]+1)%mod+mod)%mod;//2=3
ans=(ans-work2(3,0)*(R[2]-L[2]+1)%mod*(R[1]-L[1]+1)%mod+mod)%mod;//3=0
ans=(ans+work3(0,1,2)*(R[3]-L[3]+1)%mod)%mod;//0==1,1==2
ans=(ans+work2(0,1)*work2(2,3)%mod)%mod;//0==1,2==3
ans=(ans+work3(0,1,3)*(R[2]-L[2]+1)%mod)%mod;//0==1,3==0
ans=(ans+work3(1,2,3)*(R[0]-L[0]+1)%mod)%mod;//1==2,2==3
ans=(ans+work2(1,2)*work2(3,0)%mod)%mod;//1==2,3==0
ans=(ans+work3(2,3,0)*(R[1]-L[1]+1)%mod)%mod;//2==3,3==0
ans=(ans-work4(0,1,2,3)+mod)%mod;//0==1,1==2,2==3 ->3==0
printf("%lld\n",ans);
}
return 0;
}