码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 469B. Chat Online(数学)

时间:2014-09-21 20:47:11      阅读:425      评论:0      收藏:0      [点我收藏+]

标签:codeforces   数学   

题目链接:http://codeforces.com/problemset/problem/469/B


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, 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 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 <cstdio>
#include <cstring>
#define MAXN 2047

int 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;
}


代码二如下:

#include <cstdio>
#include <cstring>
#define MAXN 1017
int 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(数学)

标签:codeforces   数学   

原文地址:http://blog.csdn.net/u012860063/article/details/39453783

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!