码迷,mamicode.com
首页 > 其他好文 > 详细

火柴排队

时间:2019-09-22 14:35:07      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:mes   targe   title   include   clu   sum   lowbit   bool   oid   

题面

树状数组求逆序对

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100010;
const int maxm = 99999997;
struct MyStruct
{
    int data;
    int loc;
}a[maxn],b[maxn];
int e[maxn], n, c[maxn];
int inline readint()
{
    int x = 0;
    char c = getchar();
    while (c<‘0‘ || c>‘9‘) c = getchar();
    while (c >= ‘0‘&&c <= ‘9‘)
    {
        x = x * 10 + c - ‘0‘;
        c = getchar();
    }
    return x;
}
int lowbit(int x)
{
    return x&-x;
}
void add(int x,int t)
{
    while (x <= n)
    {
        e[x] += t;
        e[x] %= maxm;
        x += lowbit(x);
    }
}
int sum(int x)
{
    int s = 0;
    while(x)
    {
        s += e[x];
        s %= maxm;
        x -= lowbit(x);
    }
    return s;
}
bool cmp(MyStruct x, MyStruct y)
{
    return x.data < y.data;
}
int main()
{
    n = readint();
    for (int i = 1; i <= n; i++)
    {
        a[i].data = readint();
        a[i].loc = i;
    }
    for (int i = 1; i <= n; i++)
    {
        b[i].data = readint();
        b[i].loc = i;
    }
    sort(a + 1, a + n + 1, cmp);
    sort(b + 1, b + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    {
        c[a[i].loc] = b[i].loc;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        add(c[i], 1);
        ans += i - sum(c[i]);
        ans %= maxm;
    }
    printf("%d", ans);
    return 0;
}

  

火柴排队

标签:mes   targe   title   include   clu   sum   lowbit   bool   oid   

原文地址:https://www.cnblogs.com/ainiyuling/p/11567292.html

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