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?
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 a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
1 1 0 4 2 3 0 1
3
2 3 0 20 15 17 23 26 1 4 7 11 15 17
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; }