Luo Gu P2286 [HNOI2004] pet adoption field

Luo Gu P2286 [HNOI2004] pet adoption field

Description

  • Fanfan opened a pet adoption field. Adoption field offers two services: the adoption of abandoned pet owners and let the new owner to adopt these pets.

    Each adopters wants to adopt the satisfaction of their pets, according to the requirements Fanfan adopter via a special formula of his own invention, the adopter obtain desired characteristics of the pet adoption value a (a is a positive integers, a <2 ^ 31), and he adopted for each field in a pet characteristic value. In this way he will be able to easily handle the entire process of pet adoption, pet adoption field there will always be two things happens: too much or want to adopt a pet pet abandoned people, and pets too.

    When too many abandoned pets, adoption if the arrival of a person, the adopters wish to adopt a pet's characteristics is, then it will be to adopt a pet adoption is not currently in a characteristic value closest to a pet. (Two pets of any characteristic values ​​can not be the same, any hope of two adopted pet's adoption of the characteristics of value can not be the same) if you have two pets meet the requirements, namely the existence of their two pets characteristic values ​​of ab and a + b, then the value of the characteristics of adopters will adopt ab pet bird.

    Adoption pet owners too, if the arrival of an adopted pet, the adoption of which it is able to adopt it? It is possible to adopt adopters, a value that is desirable adopter pet adoption pet characteristic features closest to the value, if the pet characteristic value a, the presence of two persons they wish to adopt the characteristics of the pet adoption values ​​of ab and a + b, then the value of the characteristics of the adopter ab successful adoption of the pet.

    A adoptees adopted a characteristic value of a pet, which itself wish to adopt a pet's characteristics is b, then the dissatisfaction that adopters of abs (ab).

    You get a year, the adopter and circumstances of the arrival of the adoption pet adoption, please calculate the sum you are not satisfied with the degree of adopters of all pet adoption. When the initial year, the adoption of which neither pets nor adopters.

Input

  • The first line a positive integer n, n <= 80000, represents the total number of pet adoptions and adopter of the year came to the field. The next n lines, according to the order of arrival time of the year came to describe the situation in the field of adoption and pet adoption persons. Each row has two positive integers a, b, where a = 0 represents a pet, a = 1 represents adoption is, b represents pet features characteristic value or a desired value by adoption of pet adoption. (At the same time stay in the adoption of, or all pets or all adopters, the number of these pets and adoption is not more than 10,000)

Output

  • Only a positive integer representing the year the results of all adoptions are not satisfied with the extent of the adopter's pet sum mod 1000000 future.

Sample Input

5                  

0 2                      

0 4                         

1 3

1 2

1 5

Sample Output

3

注:abs(3-2) + abs(2-4)=3,

最后一个领养者没有宠物可以领养。

answer:

  • This is the first application Treap topic I wrote, the more classical, requires repeated scrutiny.
  • This question is intended to simulate the idea is to press questions, very simple. But looking from a recent weight weights is to find the weight of the precursor and successor ah! It is possible to open two Treap. A master record, a record pet. At every pet owner to find Treap the predecessor and successor in the best one, and then delete it; to every pet to find a successor and predecessor in the best one in the master Treap, and then delete it. If you come to a master, pet Treap is empty, it will be added to the master in the master Treap; pets empathy. Such problems can be solved.
  • However, the observation that only need to open an Treap like. Treap pet because it is not empty is the owner Treap is empty, in other words, any time there is always a Treap is empty. So we can record the current Treap Treap or pet owners Treap, and then update, delete or add to.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define N 80005
#define mod 1000000
#define LL long long
using namespace std;

struct T {LL val, dat, l, r, cnt;} t[N];
LL n, tim1, tim2, ans, tot, root, inf = 2147483648 + 1;

LL New(LL val)
{
    t[++tot].val = val, t[tot].dat = rand();
    t[tot].cnt = 1;
    return tot;
}

void zig(LL &y)
{
    LL x = t[y].l;
    t[y].l = t[x].r, t[x].r = y, y = x;
}

void zag(LL &x)
{
    LL y = t[x].r;
    t[x].r = t[y].l, t[y].l = x, x = y;
}

void insert(LL &p, LL val)
{
    if(!p) {p = New(val); return;}
    if(val == t[p].val) {t[p].cnt++; return;}
    if(val < t[p].val)
    {
        insert(t[p].l, val);
        if(t[t[p].l].dat > t[p].dat) zig(p);
    }
    else
    {
        insert(t[p].r, val);
        if(t[t[p].r].dat > t[p].dat) zag(p); 
    }
}

LL nextPre(LL val, LL tag)
{
    LL ans = tag == 0 ? 2 : 1, p = root;
    while(p)
    {
        if(val == t[p].val)
        {
            if(!tag && t[p].l)
            {
                p = t[p].l;
                while(t[p].r) p = t[p].r;
                ans = p; break;
            }
            else if(!tag) break;
            if(tag && t[p].r)
            {
                p = t[p].r;
                while(t[p].l) p = t[p].l;
                ans = p; break;
            }
            else if(tag) break;
        }
        if(!tag && t[p].val < val && t[p].val > t[ans].val) ans = p;
        if(tag && t[p].val > val && t[p].val < t[ans].val) ans = p;
        p = val < t[p].val ? t[p].l : t[p].r;
    }
    return t[ans].val;
}

void erase(LL &p, LL val)
{
    if(!p) return;
    if(val == t[p].val)
    {
        if(t[p].cnt > 1) {t[p].cnt--; return;}
        if(t[p].l || t[p].r)
        {
            if(!t[p].r || t[t[p].l].dat > t[t[p].r].dat)
                zig(p), erase(t[p].r, val);
            else
                zag(p), erase(t[p].l, val);
        }
        else p = 0;
        return;
    }
    val < t[p].val ? erase(t[p].l, val) : erase(t[p].r, val);
}

int main()
{
    New(inf), New(-inf), t[1].l = 2, root = 1;
    cin >> n;
    for(LL i = 1; i <= n; i++)
    {
        LL a, b;
        scanf("%lld%lld", &a, &b);
        if(!a)
        {
            tim1++; 
            if(!tim2) insert(root, b);
            else
            {
                LL v1 = nextPre(b, 0), v2 = nextPre(b, 1);
                if(abs(v1 - b) < abs(v2 - b))
                {
                    ans += abs(v1 - b), ans %= mod;
                    erase(root, v1);
                }
                else if(abs(v1 - b) == abs(v2 - b))
                {
                    ans += abs(v1 - b), ans %= mod;
                    erase(root, v1);
                }
                else
                {
                    ans += abs(v2 - b), ans %= mod;
                    erase(root, v2);
                }
                tim1--, tim2--;
            }
        }
        else
        {
            tim2++;
            if(!tim1) insert(root, b);
            else
            {
                LL v1 = nextPre(b, 0), v2 = nextPre(b, 1);
                if(abs(v1 - b) < abs(v2 - b))
                {
                    ans += abs(v1 - b), ans %= mod;
                    erase(root, v1);
                }
                else if(abs(v1 - b) == abs(v2 - b))
                {
                    ans += abs(v1 - b), ans %= mod;
                    erase(root, v1);
                }
                else
                {
                    ans += abs(v2 - b), ans %= mod;
                    erase(root, v2);
                }
                tim1--, tim2--;
            }
        }
    }
    cout << ans;
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11267153.html