CodeForces 469B Chat Online

CodeForces 469B   Chat Online

Time Limit: 1000 MS      Memory Limit: 262144 KB      64bit IO Format: %I64d & %I64u


Description

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, between a2 and b2, ..., between ap and bp (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 time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).

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 between l and r (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 integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000).

It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j.

Output

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

Sample Input

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<stdio.h>
#include<string.h>
int main(){
	int i,j,k,p,q,l,r,a,b,c,d,num,flag,s[2222],z[2222];
	while(scanf("%d%d%d%d",&p,&q,&l,&r)!=EOF){
		num=0;flag=1;
		memset(s,0,sizeof(s));
		memset(z,0,sizeof(z));
		for(i=0;i<p;i++){
			scanf("%d%d",&a,&b);
			for(j=a;j<=b;j++){
				s[j]=1;
			}
		}
		for(i=0;i<q;i++){
			scanf("%d%d",&c,&d);
			for(j=l;j<=r;j++){
				for(k=c+j;k<=d+j;k++){
					if(s[k]==1&&z[j]!=1){
						z[j]=1;
						num++;
						break;
					}
				}
			}
		}
		
		printf("%d\n",num);
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值