题目:
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 mconsecutive 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)?
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 ≤x, y ≤ 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 la, i and ra, i (1 ≤la, i ≤ ra, i ≤ n), indicating that A sent messages to B on each day between thela, i-th day and the ra, i-th day (both inclusive).
For the following y lines, the i-th line contains 2 integers lb, i and rb, i (1 ≤lb, i ≤ rb, i ≤ n), indicating that B sent messages to A on each day between thelb, i-th day and the rb, i-th day (both inclusive).
It is guaranteed that for all 1 ≤ i < x, ra, i + 1 < la, i + 1 and for all 1 ≤ i <y, rb, i + 1 < lb, i + 1.
<h4< dd="">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.
<h4< dd="">2 10 3 3 2 1 3 5 8 10 10 1 8 10 10 5 3 1 1 1 2 4 5<h4< dd="">
3 0<h4< dd="">
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.
思路:
以前做过用数组求公共区间的问题,一开始想用数组,但是n太大了,数组装不了,所以我选用了链表,但还是超过内存,超时的。
后来在网上看到一种做法用二维数组来存储区间,通过最大左边界和最小右边界的差来判断公共区间的大小。
参考代码:
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int T,n,m,x,y;
scanf("%d",&T);
while(T --)
{
int AB[101][2],BA[101][2];
int score = 0;
scanf("%d %d %d %d",&n,&m,&x,&y);
for(int i = 0;i < x;i ++)
{
scanf("%d %d",&AB[i][0],&AB[i][1]);
}
for(int i = 0;i < y;i ++)
{
scanf("%d %d",&BA[i][0],&BA[i][1]);
}
for(int i = 0;i < x;i ++)
{
if(AB[i][1] - AB[i][0] < m-1)
continue;
for(int j = 0;j < y;j ++)
{
if(BA[j][1]-BA[j][0] < m-1)
continue;
int l = max(AB[i][0],BA[j][0]);
int r = min(AB[i][1],BA[j][1]);
int len = r - l + 1;
if(len >= m)
score += len - m + 1;
}
}
printf("%d\n",score);
}
return 0;
}