Link: http://codeforces.com/problemset/problem/469/ B
B. Chat onlinetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard 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 timeA1 andB1,A2 andB2,...,APAndBP(All borders random SIVE ). 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 timeC1 andD1,C2 andD2,...,CQAndDQ(All borders random SIVE). But if he gets up at timeT, These segments will be shiftedT. 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 timeLAndR(Both borders random SIVE ). 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 IntegersP,?Q,?L,?R(1? ≤ ??P,?Q? ≤? 50; 0? ≤?L? ≤?R? ≤? 1000 ).
Each of the nextPLines contains two space-separated IntegersAI,?BI(0? ≤?AI? <?BI? ≤? 1000). Each of the nextQLines contains two space-separated IntegersCJ,?DJ(0? ≤?CJ? <?DJ? ≤? 1000 ).
It's guaranteed thatBI? <?AI? +? 1 andDJ? <?CJ? +? 1 For all validIAndJ.
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 42 30 1
Output
3
Input
2 3 0 2015 1723 261 47 1115 17
Output
20
Code 1 is as follows:
#include <cstdio>#include <cstring>#define MAXN 2047int main(){ int p, q, l, r; int vis[MAXN]; int a[MAXN], b[MAXN], c[MAXN], d[MAXN]; while(~scanf("%d%d%d%d",&p,&q,&l,&r)) { memset(vis,0,sizeof(vis)); for(int i = 0; i < p; i++) { scanf("%d%d",&a[i],&b[i]); for(int j = a[i]; j <= b[i]; j++) { vis[j] = 1; } } for(int i = 0; i < q; i++) { scanf("%d%d",&c[i],&d[i]); } int k = 0; int flag; for(int i = l; i <= r; i++) { flag = 0; for(int j = 0; j < q; j++) { for(int h = c[j]+i; h <= d[j]+i; h++) { if(vis[h]) { k++; flag = 1; break; } } if(flag) break; } } printf("%d\n",k); } return 0;}
Code 2 is as follows:
#include <cstdio>#include <cstring>#define MAXN 1017int main(){ int p, q, l, r; int a[MAXN], b[MAXN], c[MAXN], d[MAXN]; while(~scanf("%d%d%d%d",&p,&q,&l,&r)) { for(int i = 0; i < p; i++) { scanf("%d%d",&a[i],&b[i]); } for(int i = 0; i < q; i++) { scanf("%d%d",&c[i],&d[i]); } int k = 0; int flag; for(int i = l; i <= r; i++) { flag = 0; for(int j = 0; j < q; j++) { int t1 = c[j]+i; int t2 = d[j]+i; for(int h = 0; h < p; h++) { // if((t1>=a[h]&&t1<=b[h]) || (t2>=a[h]&&t2<=b[h])) for(int f = t1; f <= t2; f++) { if((f>=a[h]&&f<=b[h]) || (f>=a[h]&&f<=b[h])) { k++; // printf("ss::%d\n",i); flag = 1; break; } } if(flag) break; } if(flag) break; } } printf("%d\n",k); } return 0;}
Codeforces 469b. Chat online (Mathematics)