http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3961
照题意模拟,
代码也很简单
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
struct node
{
int x,y;
};
int main()
{
int T;
int n,m,a,b;
node s[110],ss[110];
cin>>T;
while(T--)
{
long long int res=0;
cin>>n>>m>>a>>b;
for(int i=0;i<a;i++)
{
cin>>s[i].x>>s[i].y;
}
for(int i=0;i<b;i++)
{
cin>>ss[i].x>>ss[i].y;
}
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
int xx=max(s[i].x,ss[j].x);
int yy=min(s[i].y,ss[j].y);
if(xx>=yy)
continue;
else if(yy-xx+1<m)
continue;
else
{
res+=yy-xx-m+2;
}
}
}
if(res)
cout<<res<<endl;
else
cout<<0<<endl;
}
return 0;
}