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

[Luogu] 火柴排队

时间:2018-04-06 16:46:31      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:names   www.   span   ref   main   name   lowbit   tchar   log   

https://www.luogu.org/problemnew/show/P1966

离散化

树状数组求逆序对个数

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 10;
const int Mod = 99999997;

struct Node {
    int num, wher;
} G_1[N], G_2[N];
int n;
int Map1[N], Map2[N], imp[N], work[N];
int Tree[N];
long long Answer;

#define gc getchar()

inline int read() {
    int x = 0; char c = gc;
    while(c < 0 || c > 9) c = gc;
    while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = gc;
    return x;
}

inline bool cmp(Node a, Node b) {return a.num < b.num;}
inline int Lowbit(int x) {return x & (-x);}
inline int Ask(int x) {int ret = 0; while(x) {ret += Tree[x]; x -= Lowbit(x);} return ret;}
void Add(int x) {while(x <= n) {Tree[x] ++; x += Lowbit(x);}}

int main () {
    n = read();
    for(int i = 1; i <= n; i ++) G_1[i].num = read(), G_1[i].wher = i;
    for(int i = 1; i <= n; i ++) G_2[i].num = read(), G_2[i].wher = i;
    sort(G_2 + 1, G_2 + n + 1, cmp);
    sort(G_1 + 1, G_1 + n + 1, cmp);
    for(int i = 1; i <= n; i ++) Map1[G_1[i].wher] = i;
    for(int i = 1; i <= n; i ++) Map2[G_2[i].wher] = i;
    for(int i = 1; i <= n; i ++) imp[Map2[i]] = i;
    for(int i = 1; i <= n; i ++) work[i] = imp[Map1[i]];
    for(int i = 1; i <= n; i ++) {
        int A = Ask(work[i]);
        Add(work[i]);
        Answer += (i - 1 - A);
        while(Answer >= Mod) Answer -= Mod;
    }
    cout << Answer;
    return 0;
}

 

[Luogu] 火柴排队

标签:names   www.   span   ref   main   name   lowbit   tchar   log   

原文地址:https://www.cnblogs.com/shandongs1/p/8727945.html

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