CodeForces 469B. Chat Online(数学)

题目链接: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;
}
时间: 2024-07-29 13:59:46

CodeForces 469B. Chat Online(数学)的相关文章

CodeForces 534C Polycarpus&#39; Dice (数学)

题意:第一行给两个数,n 和 A,n 表示有n 个骰子,A表示 n 个骰子掷出的数的和.第二行给出n个数,表示第n个骰子所能掷出的最大的数,这些骰子都有问题, 可能或多或少的掷不出几个数,输出n个骰子掷不出的数的个数. 析:我们只要考虑两个极端就好,考由其他骰子投出的最大值和最小值,还有自身在最大值和最小值,作一个数学运算就OK了.公式如下: 骰子的最大值-能投的最大值+能投的最小值-1. 代码如下: #include <cstdio> #include <string> #inc

Codeforces 468C Hack it!(数学)

题目链接:Codeforces 468C Hack it! 题目大意:给据题目定义,找到l,r,使得solve(l,r) % a = 0. 解题思路:f(x + 1e18) = f(x) + 1,所以有solve(x + 1, x+1e18) = solve(x, x+1e18-1) + 1,假定x为0,我们求出solve(0, 1e18) % a = k,那么a - k,即为区间需要移动的步长.solve(1e18) % a = 4518 1e17 % a #include <cstdio>

Codeforces 520E. Pluses everywhere 数学

520E - Pluses everywhere/521C - Pluses everywhere Idea: Endagorion Preparation: gchebanov, DPR-pavlin Consider some way of placing all the pluses, and a single digit di (digits in the string are numbered starting from 0 from left to right). This digi

Codeforces 688D Remainders Game 数学

题意:Pari选了两个数x,k,已知k和n个数c[i],也能"知道"x%c[i],问是否x%k的值是否唯一?n,k,c[i]<=1e6 假如存在x1,x2满足:x1和x2同余c[i](i=1..n),但是x1%k!=x2%k 则此时不能确定x%k的值,答案为no即(x1-x2)%c[i]==0 因为lcm(c[1]..c[n])|(x1-x2) k不整除(x1-x2) k也不整除lcm(c[1]..c[n]) 该条件为必要条件.证明充分性很简单 构造x1=2*lcm,x2=lcm

Codeforces 17A Noldbach problem(数学)

题意 求n以内等于两个连续素数的和加上1的数的个数 n不大于1000 #include<cstdio> #include<cmath> #include<algorithm> using namespace std; const int N = 1000; int n, k, ans; bool isPrime (int a) { for (int i = 2; i <= sqrt (a); ++i) if (a % i == 0) return 0; retur

codeforces 688D - Remainders Game 数学相关

题意:给你x%ci=bi(x未知),是否能确定x%k的值(k已知) 分析:只要保证k能整除ci的最小公倍数即可,由于太大,所以通过暴力分解因子的办法来判断 #include <cstdio> #include <iostream> #include <ctime> #include <vector> #include <cmath> #include <map> #include <set> #include <st

Codeforces 911D. Inversion Counting (数学、思维)

题目链接:Inversion Counting 题意: 定义数列{ai|i=1,2,...,n}的逆序对如下:对于所有的1≤j<i≤n,若ai<aj,则<i,j>为一个逆序对.于是,对于一个数列a[1..n],给定m次操作.对于每一次操作,给定l,r(1≤l<r≤n),将序列a[l..r]倒置.求倒置后的逆序对的数量的奇偶性. 题解: 假设现在我们有一个序列并翻转这个序列[l,r]区间里面的数.假设某个数的k值是指在这个值后面小于这个数的数的个数,其实我们可以发现对于[1,l

Codeforces 932E Team Work 数学

Team Work 发现网上没有我这种写法.. i ^ k我们可以理解为对于每个子集我们k个for套在一起数有多少个. 那么我们问题就变成了 任意可重复位置的k个物品属于多少个子集. 然后我们枚举k个物品所占位置的个数 i , 然后需要计算有多少种方案能把k个不同物品放入i个桶中. 这个东西可以用dp[ i ][ j ] 表示 i 个物品放入 j 个桶中的方案数. dp[ i ][ j ] = dp[ i - 1 ][ j ] * j + dp[ i - 1 ][ j - 1 ] * j 然后就

codeforces469B

Chat Online CodeForces - 469B 问题描述 你和你的朋友经常在网上聊天.你的朋友作息规律每天只会在p个时间段[ai,bi]在线.你作息混乱,假设你在t时刻起床,那么你会在q个时间段[t+ci,t+di]在线.现在,你有可能会在[l,r]的这个时间段中的任意一个时刻起床,假设你在t时刻起床,请问有多少个这样的t使得你有机会和你的朋友聊天?只要你们在线的时间有哪怕一刻重合,你就可以和你的朋友聊天输入格式 第一行4个整数p,q,l,r.(1<=p,q<=50;0<=l