链接:https://www.nowcoder.com/acm/contest/123/F
来源:牛客网
题目描述
Given ,please count the number of four-tuples such that and
The answer should modulo10 9+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 2 2 3 3 4 4
输出
1
题意:
给定四个区间(li,ri)(li,ri)(闭区间),求一个四元组(x1,x2,x3,x4)(x1,x2,x3,x4),满足xixi在区间(li,ri)(li,ri)内,且任意两个相邻的xixi不能相等。
题解:
容斥原理。就是麻烦。
ans=所有都不相等-两个相等+三个相等+两对两个相等-四个相等。
A=(区间1与区间2的共有长度)*(区间3的长度)*(区间4的长度)
别人的代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mod = 1e9 + 7;
long long l1,l2,l3,l4,r1,r2,r3,r4;
int main()
{
//freopen("1.in","r",stdin);
//freopen("3.txt","w",stdout);
long long t;
scanf("%lld",&t);
while(t--)
{
scanf("%lld %lld %lld %lld %lld %lld %lld %lld",&l1,&r1,&l2,&r2,&l3,&r3,&l4,&r4);
long long ans = (r1 - l1 + 1) * (r2 - l2 + 1)%mod;
ans = ans * (r3 - l3 + 1)%mod;
ans = ans * (r4 - l4 + 1)%mod;
long long l,r,temp,rr,ll;
// 1 == 2
l = max(l1,l2);r = min(r1,r2);
if(r >= l)
ans=((ans-(r-l+1)*(r3-l3+1)%mod*(r4-l4+1)%mod)%mod+mod)%mod;
//2 == 3
l = max(l3,l2);r = min(r3,r2);
if(r >= l)
ans = ((ans - (r - l + 1) * (r1 - l1 + 1) % mod * (r4 - l4 + 1)%mod)%mod + mod) % mod;
//3 == 4
l = max(l3,l4);r = min(r3,r4);
if(r >= l)
ans = ((ans - (r - l + 1) * (r1 - l1 + 1) % mod * (r2 - l2 + 1)%mod)%mod + mod) % mod;
// 4 == 1
l = max(l1,l4);r = min(r1,r4);
if(r >= l)
ans = ((ans - (r - l + 1) * (r3 - l3 + 1) % mod * (r2 - l2 + 1)%mod)%mod + mod) % mod;
//1 == 2 == 3
r = min(min(r1,r2),r3),l = max(max(l1,l2),l3);
if(r >= l)
ans = (ans + (r - l + 1) * (r4 - l4 + 1)%mod)%mod;
//2 == 3 == 4
r = min(min(r4,r2),r3),l = max(max(l4,l2),l3);
if(r >= l)
ans = (ans + (r - l + 1) * (r1 - l1 + 1)%mod)%mod;
//1 == 3 == 4
r = min(min(r1,r4),r3),l = max(max(l1,l4),l3);
if(r >= l)
ans = (ans + (r - l + 1) * (r2 - l2 + 1)%mod)%mod;
//1 == 2 == 4
r = min(min(r1,r2),r4),l = max(max(l1,l2),l4);
if(r >= l)
ans = (ans + (r - l + 1) * (r3 - l3 + 1)%mod)%mod;
//1 ==2 3 == 4
r = min(r1,r2),rr = min(r3,r4),l = max(l1,l2),ll = max(l3,l4);
if(r >= l && rr >= ll)
ans = (ans + (r - l + 1) * (rr - ll + 1)%mod)%mod;
//2 == 3 4 == 1
r = min(r3,r2),rr = min(r4,r1),l = max(l2,l3),ll = max(l4,l1);
if(r >= l && rr >= ll)
ans = (ans + (r - l + 1) * (rr - ll + 1)%mod)%mod;
l = max(max(max(l1,l2),l3),l4),r = min(min(min(r1,r2),r3),r4);
if(r >= l)
ans = ((ans - (3 * (r - l + 1))%mod)%mod + mod)%mod;
printf("%lld\n",ans);
}
return 0;
}