HDOJ4768(二分区间)

Flyer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2795    Accepted Submission(s): 1051


Problem Description
The new semester begins! Different kinds of student societies are all trying to advertise themselves, by giving flyers to the students for introducing the society. However, due to the fund shortage, the flyers of a society can only be distributed to a part of the students. There are too many, too many students in our university, labeled from 1 to 2^32. And there are totally N student societies, where the i-th society will deliver flyers to the students with label A_i, A_i+C_i,A_i+2*C_i,…A_i+k*C_i (A_i+k*C_i<=B_i, A_i+(k+1)*C_i>B_i). We call a student "unlucky" if he/she gets odd pieces of flyers. Unfortunately, not everyone is lucky. Yet, no worries; there is at most one student who is unlucky. Could you help us find out who the unfortunate dude (if any) is? So that we can comfort him by treating him to a big meal!
 
Input
There are multiple test cases. For each test case, the first line contains a number N (0 < N <= 20000) indicating the number of societies. Then for each of the following N lines, there are three non-negative integers A_i, B_i, C_i (smaller than 2^31, A_i <= B_i) as stated above. Your program should proceed to the end of the file.
 
Output
For each test case, if there is no unlucky student, print "DC Qiang is unhappy." (excluding the quotation mark), in a single line. Otherwise print two integers, i.e., the label of the unlucky student and the number of flyers he/she gets, in a single line.
 
Sample Input
2
1 10 1
2 10 1
4
5 20 7
6 14 3
5 9 1
7 21 12
 
Sample Output
1 1
8 1
 
思路:二分枚举区间(l,r],mid=(l+r)/2。若(l,mid]出现的数字次数之和为奇数,由奇数+偶数为奇数可得,答案必在(l,mid]内。否则,在(mid,r]区间内。
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 20005;
const LL INF = 0x7fffffffffffffff;
struct Node{
    LL a, b, c;
}soc[MAXN];
int n;
bool test(LL x)
{
    LL s = 0;
    for(int i = 0; i < n; i++)
    {
        LL l = min(x, soc[i].b);
        if(l >= soc[i].a)
        {
            s += ((l - soc[i].a) / soc[i].c + 1);
        }
    }
    return s & 1;
}
int main()
{
    while(scanf("%d", &n) != EOF)
    {
        LL mn = INF, mx = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%I64d %I64d %I64d", &soc[i].a, &soc[i].b, &soc[i].c);
            mn = min(mn, soc[i].a);
            mx = max(mx, soc[i].b);
        }
        LL left = mn, right = mx;
        if(!test(right))
        {
            printf("DC Qiang is unhappy.
");
            continue;
        }
        while(right > left)
        {
            LL mid = (left + right) >> 1;
            if(test(mid))
            {
                right = mid;
            }
            else
            {
                left = mid + 1;
            }
        }
        LL res = 0;
        for(int i = 0; i < n; i++)
        {
            if(right > soc[i].b || right < soc[i].a) continue;
            if((right - soc[i].a) % soc[i].c == 0)
            {
                res++;
            }
        }
        printf("%I64d %I64d
", right, res);
    }
    return 0;
}

异或运算也能过。

#include <cstdio>
using namespace std;
typedef long long LL;
const int MAXN = 20005;
struct Node{
    LL a, b, c;
}soc[MAXN];
int n;
int main()
{
    while(scanf("%d", &n) != EOF)
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%I64d %I64d %I64d", &soc[i].a, &soc[i].b, &soc[i].c);
        }
        LL x = 0;
        for(int i = 0; i < n; i++)
        {
            for(LL l = soc[i].a; l <= soc[i].b; l += soc[i].c)
            {
                x ^= l;
            }
        }
        if(x != 0)
        {
            LL res = 0;
            for(int i = 0; i < n; i++)
            {
                if(x > soc[i].b || x < soc[i].a)    continue;
                if((x - soc[i].a) % soc[i].c == 0)
                {
                    res++;
                }
            }
            printf("%I64d %I64d
", x, res);
        }
        else
        {
            printf("DC Qiang is unhappy.
");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/program-ccc/p/5812316.html