码迷,mamicode.com
首页 > 编程语言 > 详细

poj--2299(树状数组+离散化)

时间:2018-12-12 00:35:20      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:图片   while   cst   display   www.   closed   +=   play   isp   

一、离散化:

https://www.cnblogs.com/2018zxy/p/10104393.html

 

二、逆序数

 

AC代码:

技术分享图片
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 510000;
struct Node{
    LL data;
    int val;
}cur[maxn];
LL a[maxn];
bool cmp(Node A,Node B)
{
    return A.data<B.data;
}
int lowbit(int x)
{
    return x&(-x);
}
void update(int x)
{
    while(x<maxn-10)
    {
        a[x]++;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=a[x];
        x-=lowbit(x);
    }
    return ans;
}
int main(void)
{
    int n,i;
    while(~scanf("%d",&n)&&n)
    {
        memset(a,0,sizeof(a));
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&cur[i].data);cur[i].val=i;
        }
        sort(cur+1,cur+1+n,cmp);
        LL ans=0;
        for(i=1;i<=n;i++)
        {
            update(cur[i].val);
            ans+=i-sum(cur[i].val);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
View Code

 

技术分享图片
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 510000;
struct Node{
    LL data;
    int val;
}cur[maxn];
LL a[maxn];
bool cmp(Node A,Node B)
{
    return A.data<B.data;
}
int lowbit(int x)
{
    return x&(-x);
}
void update(int x)
{
    while(x<maxn-10)
    {
        a[x]++;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=a[x];
        x-=lowbit(x);
    }
    return ans;
}
int main(void)
{
    int n,i;
    while(~scanf("%d",&n)&&n)
    {
        memset(a,0,sizeof(a));
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&cur[i].data);cur[i].val=i;
        }
        sort(cur+1,cur+1+n,cmp);
        LL ans=0;
        for(i=n;i>=1;i--)
        {
            ans+=sum(cur[i].val);
            update(cur[i].val);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
View Code

poj--2299(树状数组+离散化)

标签:图片   while   cst   display   www.   closed   +=   play   isp   

原文地址:https://www.cnblogs.com/2018zxy/p/10104279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!