CodeForces 540E Infinite Inversions (线段树/逆序列/离散化)

There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, …}. We performed n swap operations with this sequence. A swap(a, b) is an operation of swapping the elements of the sequence on positions a and b. Your task is to find the number of inversions in the resulting sequence, i.e. the number of such index pairs (i, j), that i < j and pi > pj.

Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of swap operations applied to the sequence.

Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109, ai ≠ bi) — the arguments of the swap operation.

Output
Print a single integer — the number of inversions in the resulting sequence.

Examples
Input
2
4 2
1 4
Output
4
Input
3
1 6
3 4
2 5
Output
15
Note
In the first sample the sequence is being modified as follows: . It has 4 inversions formed by index pairs (1, 4), (2, 3), (2, 4) and (3, 4).

很繁琐的一道题,求无穷范围内的一些数字的逆序数。
首先离散化,并将点与区间一同记录下来,列如 5 10 12 16
离散化后为 1-4,5-5,6-9,10-10,11-11,12-12,13-15,16-16
在对其进行求逆序列,以下有两种办法:
1.从最小的开始放,放入后求前面的和,即为前面比它小的数的和,总数减去小的和,即为比它大的数的区间和,又由于每个数字其实都可看作数字段,区间和*=当前区间长度,ans+=区间和,依次进行。
2.首先构建好加法树状数组,权值为区间长度,从最小的开始删除,删除后求其前面的区间和,前面的都是比它大的,区间和*=当前区间长度,ans+=区间和,依次进行。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

long long tree[4001000];
struct
{
   int x,y;
}question[500005];

int in[500005];

struct ccc
{
   int minn,maxx,member;
}inf[1000005],tteemmpp;

int cmp(struct ccc q,struct ccc w)
{
    return q.minn<w.minn;
}

int main()
{
    int i,j,n,num,k;long long ansss;

    scanf("%d",&n);

    for(i=1;i<=n;i++)
    {
        scanf("%d%d",&question[i].x,&question[i].y);
        in[i]=question[i].x;
        in[i+n]=question[i].y;
    }

    sort(in+1,in+1+2*n);
    int m=unique(in+1,in+1+2*n)-in;
    m--;//=1,=m


    num=1;

    map<int,int>mapp;

    for(i=1;i<=m;i++)
    {
        if(in[i]==in[i-1]+1)
        {
            inf[num].maxx=in[i];
            inf[num].minn=in[i];

            mapp[in[i]]=num;
            num++;
        }
        else
        {
            inf[num].maxx=in[i]-1;
            inf[num].minn=in[i-1]+1;
            num++;

            inf[num].maxx=in[i];
            inf[num].minn=in[i];

            mapp[in[i]]=num;num++;
        }

    }

    num--;//=1 =num



    for(i=1;i<=n;i++)
    {
        tteemmpp=inf[mapp[question[i].x]];
        inf[mapp[question[i].x]]=inf[mapp[question[i].y]];
        inf[mapp[question[i].y]]=tteemmpp;

    }

    for(i=1;i<=num;i++)
        inf[i].member=i;


    sort(inf+1,inf+1+num,cmp);

    ansss=0;long long tempp,tot=0;
    for(i=1;i<=num;i++)
    {
        tempp=0;
        k=inf[i].member-1;
        while(k>0)
        {
          tempp+=(long long)tree[k];
          k-=((-k)&k);
        }

        ansss+=(tot-tempp)*((long long)inf[i].maxx-(long long)inf[i].minn+1);
        k=inf[i].member;

        while(k<=num)
        {
          tree[k]+=(long long)inf[i].maxx-(long long)inf[i].minn+1;
          k+=((-k)&k);
        }

        tot=inf[i].maxx;

    }

    printf("%lld",ansss);//die

    return 0;
}
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

long long tree[4001000];
struct
{
   int x,y;
}question[301000];

int in[501000];

struct ccc
{
   int minn,maxx,member;
}inf[1001000],tteemmpp;

int cmp(struct ccc q,struct ccc w)
{
    return q.minn<w.minn;
}

int main()
{
    int i,j,n,num,k;long long ansss;

    scanf("%d",&n);

    for(i=1;i<=n;i++)
    {
        scanf("%d%d",&question[i].x,&question[i].y);
        in[i]=question[i].x;
        in[i+n]=question[i].y;
    }

    sort(in+1,in+1+2*n);
    int m=unique(in+1,in+1+2*n)-in;
    m--;//=1,=m


    num=1;

    map<int,int>mapp;

    for(i=1;i<=m;i++)
    {
        if(in[i]==in[i-1]+1)
        {
            inf[num].maxx=in[i];
            inf[num].minn=in[i];

            mapp[in[i]]=num;
            num++;
        }
        else
        {
            inf[num].maxx=in[i]-1;
            inf[num].minn=in[i-1]+1;
            num++;

            inf[num].maxx=in[i];
            inf[num].minn=in[i];

            mapp[in[i]]=num;num++;
        }

    }

    num--;//=1 =num



    for(i=1;i<=n;i++)
    {
        tteemmpp=inf[mapp[question[i].x]];
        inf[mapp[question[i].x]]=inf[mapp[question[i].y]];
        inf[mapp[question[i].y]]=tteemmpp;

    }

    for(i=1;i<=num;i++)
        inf[i].member=i;

    for(i=1;i<=num;i++)
    {
        k=i;

        while(k<=num)
        {
          tree[k]+=(long long)inf[i].maxx-(long long)inf[i].minn+1;
          k+=((-k)&k);
        }
    }

    sort(inf+1,inf+1+num,cmp);

    ansss=0;
    for(i=1;i<=num;i++)
    {
        k=inf[i].member;

        while(k<=num)
        {
          tree[k]-=inf[i].maxx-inf[i].minn+1;
          k+=((-k)&k);
        }

        k=inf[i].member;
        while(k>0)
        {
          ansss+=tree[k]*(long long)(inf[i].maxx-inf[i].minn+1);
          k-=((-k)&k);
        }

    }

    printf("%lld",ansss);

    return 0;
}

你可能感兴趣的:(CodeForces 540E Infinite Inversions (线段树/逆序列/离散化))