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

CF Gym 100463A树状数组求逆序数

时间:2015-07-21 18:32:03      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

题意:给你一个序列,和标准序列连线,求交点数。

题解:就是求逆序对个数,用数组数组优化就行了。

做八数码的时候,求逆序数搜过怎么求,但是练习时忘了。。。于是当场想怎么实现,结果搞了1个小时才搞出来,还被WA了个LongLong,基础还不够扎实

#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cstring>

using namespace std;

//#define local
typedef long long  ll;
const int maxn = 1000000+100;
#define lowbit(x) ((x)&-(x))
ll C[maxn];
ll n,a,b;
void add(ll x,ll d)
{
    while(x <= n) {
        C[x]+=d; x+=lowbit(x);
    }
}

ll sum(ll x)
{
    ll ret = 0;
    while(x > 0){
        ret += C[x]; x-=lowbit(x);
    }
    return ret;
}

ll Count()
{
    ll res = 0;
    ll ele = b;
    for(int i = 0; i < n; i++){
        ll pre = sum(ele+1);
        res += ele - pre;
        add(ele+1,1);
        ele = (ele+a)%n;
    }
    return res;
}

int main()
{
#ifdef local
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
#endif // local
    int cas = 0;
    while(~scanf("%I64d%I64d%I64d",&n,&a,&b)&&n){
        memset(C,0,sizeof(C));
        printf("Case %d: %I64d\n",++cas,Count());
    }

    return 0;
}

 

CF Gym 100463A树状数组求逆序数

标签:

原文地址:http://www.cnblogs.com/jerryRey/p/4665264.html

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