ACM刷題之ZOJ————Let's Chat

Let's Chat

Time Limit: 1 Second      Memory Limit: 65536 KB

ACM (ACMers' Chatting Messenger) is a famous instant messaging software developed by Marjar Technology Company. To attract more users, Edward, the boss of Marjar Company, has recently added a new feature to the software. The new feature can be described as follows:

If two users, A and B, have been sending messages to each other on the last m consecutive days, the "friendship point" between them will be increased by 1 point.

More formally, if user A sent messages to user B on each day between the (i - m + 1)-th day and the i-th day (both inclusive), and user B also sent messages to user A on each day between the (i - m + 1)-th day and the i-th day (also both inclusive), the "friendship point" between A and B will be increased by 1 at the end of the i-th day.

Given the chatting logs of two users A and B during n consecutive days, what's the number of the friendship points between them at the end of the n-th day (given that the initial friendship point between them is 0)?

Input

There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 10), indicating the number of test cases. For each test case:

The first line contains 4 integers n (1 ≤ n ≤ 109), m (1 ≤ m ≤ n), x and y (1 ≤ xy ≤ 100). The meanings of n and m are described above, while x indicates the number of chatting logs about the messages sent by A to B, and y indicates the number of chatting logs about the messages sent by B to A.

For the following x lines, the i-th line contains 2 integers lai and rai (1 ≤ lai ≤ rai ≤ n), indicating that A sent messages to B on each day between the lai-th day and the rai-th day (both inclusive).

For the following y lines, the i-th line contains 2 integers lbi and rbi (1 ≤ lbi ≤ rbi ≤ n), indicating that B sent messages to A on each day between the lbi-th day and the rbi-th day (both inclusive).

It is guaranteed that for all 1 ≤ i < xrai + 1 < lai + 1 and for all 1 ≤ i < yrbi + 1 < lbi + 1.

Output

For each test case, output one line containing one integer, indicating the number of friendship points between A and B at the end of the n-th day.

Sample Input

2
10 3 3 2
1 3
5 8
10 10
1 8
10 10
5 3 1 1
1 2
4 5

Sample Output

3
0

Hint

For the first test case, user A and user B send messages to each other on the 1st, 2nd, 3rd, 5th, 6th, 7th, 8th and 10th day. As m = 3, the friendship points between them will be increased by 1 at the end of the 3rd, 7th and 8th day. So the answer is 3.



簡單暴力枚舉即可。

不需要考慮給的數據有重疊的情況。


下面是ac代碼:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int N=2e5+7;

struct aaa{
	int ll,rr,t;
};

bool cmp(const aaa &a, const aaa &b){
	return a.ll<b.ll;
}

aaa a[1000];
aaa b[1000];

int main()
{
	//freopen("f:/input.txt", "r", stdin);
	int zu,i,j,k,n,m,x,y,l,r,sum; 
	cin>>zu;
	while(zu--)
	{
		CLR(a,0);
		CLR(b,0);
		
		scanf("%d%d%d%d",&n,&m,&x,&y);
		for(i=0;i<x;i++){
			scanf("%d%d",&a[i].ll,&a[i].rr);
			if(a[i].rr-a[i].ll+1>=m) a[i].t=1;
		}
		
		for(i=0;i<y;i++){
			scanf("%d%d",&b[i].ll,&b[i].rr);
			if(b[i].rr-b[i].ll+1>=m) b[i].t=1;
		}
		
		sort(a,a+x,cmp);
		sort(b,b+y,cmp);
		sum=0;
		/*
		for(i=0,j=0;i<x;i++){
			if(a[i].t==1){
				while(b[j].t==0&&j!=y){
					++j;
				}
				if(j==y) break;
				
				l=max(a[i].ll, b[j].ll);
				r=min(a[i].rr, b[j].rr);
				if(r>n){
					r=n;
				}
				
				
				if(r-l+2-m>0) sum+=(r-l+2-m);
				else if(r==b[j].rr){
					++j;
					if(j==y) break;
					--i;
					continue;
				}
			}
		}
		*/
		
		for(i=0;i<x;i++){
			for(j=0;j<y;j++){
				l=max(a[i].ll, b[j].ll);
				r=min(a[i].rr, b[j].rr);
				if(r>n){
					r=n;
				}
				
				
				if(r-l+2-m>0) sum+=(r-l+2-m);
			}
		}
		
		printf("%d\n", sum);
		
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章