codeforces #371div2 (ABC)

A. Meeting of Old Friends
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Today an outstanding event is going to happen in the forest — hedgehog Filya will come to his old fried Sonya!

Sonya is an owl and she sleeps during the day and stay awake from minute l1 to minute r1 inclusive. Also, during the minute k she prinks and is unavailable for Filya.

Filya works a lot and he plans to visit Sonya from minute l2 to minute r2 inclusive.

Calculate the number of minutes they will be able to spend together.

Input

The only line of the input contains integers l1r1l2r2 and k (1 ≤ l1, r1, l2, r2, k ≤ 1018l1 ≤ r1l2 ≤ r2), providing the segments of time for Sonya and Filya and the moment of time when Sonya prinks.

Output

Print one integer — the number of minutes Sonya and Filya will be able to spend together.

Examples
input
1 10 9 20 1
output
2
input
1 100 50 200 75
output
50
Note

In the first sample, they will be together during minutes 9 and 10.

In the second sample, they will be together from minute 50 to minute 74 and from minute 76 to minute 100.


 


B. Filya and Homework
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.

Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.

Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line containsn integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array.

Output

If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).

Examples
input
5
1 3 3 2 1
output
YES
input
5
1 2 3 4 5
output
NO
Note

In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements.


 

C. Sonya and Queries
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type:

  1.  +  ai — add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer.
  2.  -  ai — delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset.
  3. ? s — count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern,0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left.

For example, if the pattern is s = 010, than integers 92221250 and 414 match the pattern, while integers 311025 and 1030 do not.

Input

The first line of the input contains an integer t (1 ≤ t ≤ 100 000) — the number of operation Sonya has to perform.

Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci — the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≤ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18.

It's guaranteed that there will be at least one query of type '?'.

It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it.

Output

For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time.

Examples
input
12
+ 1
+ 241
? 1
+ 361
- 241
? 0101
+ 101
? 101
- 101
? 101
+ 4000
? 0
output
2
1
2
1
1
input
4
+ 200
+ 200
- 200
? 0
output
1
Note

Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input.

  1. 1 and 241.
  2. 361.
  3. 101 and 361.
  4. 361.
  5. 4000.

 

A:给两个区间 (l1,r1) (l2,r2) 和一个数k,求区间交集,如果k在交集内,ans--;注意判断没有交集的情况输出0(被hack了,贼伤)

代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f
typedef long long ll;
using namespace std;

int main()
{
    ll l1,l2,r1,r2,k;
    while(cin>>l1>>r1>>l2>>r2>>k){
        ll minn = min(r1,r2);
        ll maxn = max(l1,l2);
        ll ans = minn-maxn+1;
        if(maxn > minn){
            cout<<0<<endl;
            continue;
        }
        if(k >= maxn && k <= minn)
            ans--;
        cout<<ans<<endl;
    }
    return 0;
}


B:给定n个数字,可以选择其中的一些数 +x 或者 -x,每个数只能操作一次。问是否经过操作之后可以使得这些数全部相等。

思路:先将数列去重排序,判断元素个数。如果  len > 3 ok = 0;len <= 2 ok = 1;len = 3,判断 两边的数和中间的数的差值是否相等。

代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define N 100000+5
typedef long long ll;
using namespace std;

int a[N];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    while(cin>>n){
        int ok = 1;
        for(int i = 1;i <= n;++ i)
            cin>>a[i];

        sort(a+1,a+n+1);
        int len = unique(a+1,a+n+1)-a;
        len--;
        if(len > 3)
            ok = 0;
        else{
            if(len == 3){
                if(abs(a[1]-a[2]) != abs(a[2]-a[3]))
                    ok = 0;
            }
        }
        if(ok)  cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}



C:给定n个操作 op 以及操作的数字 res。op =  + 表示集合内的 res+1。op =  - 表示集合内的 res-1;op = ? 时给出一个字符串,问集合内有多少个元素与该字符串匹配(从右到左)。0表示匹配位置偶数,1表示匹配位置为奇数,长度不够的补0; 比如匹配串 : 0101,数字  323,3,23和匹配串匹配。

思路:考虑到0也是偶数,那么我们处理的时候不需要考虑两个串的长度问题了。将集合中的每个元素预处理为01串,利用 map 存储从左端开始的第一个为奇数也就是1的位置,比如预处理得到 0101,我们用 map 存储 101,及M["101"]++即可。

代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define N 100005
typedef long long ll;
using namespace std;

map <string,int> M;

int fuc(string s,int flag){
    string temp;
    temp.resize(25);

    int cur = -1,len = 0;
    for(int i = 0;i < s.size();++ i){
        if((s[i]-'0')%2 == 1){
            cur = i;
            break;
        }
    }
    if(cur == -1)
        temp = "0";
    else{
        for(int i = cur;i < s.size();++ i)
            temp[len++] = (s[i]-'0')%2 == 1 ? '1' : '0';
    }
    if(flag == 1)
        M[temp]++;
    if(flag == 0)
        M[temp]--;
    if(flag == 2)
        return M[temp];
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int n;
    while(cin>>n){
        M.clear();
        string op,s;
        for(int i = 1;i <= n;++ i){
            cin>>op>>s;
            if(op[0] == '+')
                fuc(s,1);
            if(op[0] == '-')
                fuc(s,0);
            if(op[0] == '?'){
                int ans = fuc(s,2);
                cout<<ans<<endl;
            }
        }
    }
    return 0;
}




原文地址:https://www.cnblogs.com/Jstyle-continue/p/6351926.html