CodeForces 469B Chat Online

本文介绍了一个算法问题,涉及计算两个朋友在线聊天的时间段。通过输入各自不同时段的在线时间,算法确定在特定范围内有多少个整数时间点适合双方进行在线聊天。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Chat Online
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little X and Little Z are good friends. They always chat online. But both of them have schedules.

Little Z has fixed schedule. He always online at any moment of time between a1 and b1, betweena2 andb2, ..., betweenap andbp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time0, he will be online at any moment of time between c1 andd1, betweenc2 andd2, ..., betweencq anddq (all borders inclusive). But if he gets up at timet, these segments will be shifted byt. They become [ci + t, di + t] (for alli).

If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time betweenl andr (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?

Input

The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000).

Each of the next p lines contains two space-separated integersai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integerscj, dj (0 ≤ cj < dj ≤ 1000).

It's guaranteed that bi < ai + 1 anddj < cj + 1 for all validi and j.

Output

Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.

Sample test(s)
Input
1 1 0 4
2 3
0 1
Output
3
Input
2 3 0 20
15 17
23 26
1 4
7 11
15 17
Output
20

打表

#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int main(){
    int p,q,l,r,i,j,t,sum,flag,online,a,b;
    int c[60],d[60],time[2010];
    while(cin>>p>>q>>l>>r){
        sum=0;
        memset(time,0,sizeof(time));
        for(i=0;i<p;i++){
            cin>>a>>b;
            for(j=a;j<=b;j++){
                time[j]=1;
            }
        }
        for(i=0;i<q;i++){
            cin>>c[i]>>d[i];
        }
        for(i=l;i<=r;i++){
            flag=0;
            for(j=0;j<q;j++){
                for(online=c[j]+i;online<=d[j]+i;online++){
                    if(time[online]){
                        sum++;
                        flag=1;
                        break;
                    }
                }
                if(flag){
                    break;
                }
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值