CodeForces - 785E - Anton and Permutation 分块+暴力+二分

题目链接

题意:有一个初始为 1 , 2 , . . . , n 1,2,...,n 12...n 的排列,现在有 q q q 次操作,第 i i i 次操作为 l i , r i l_i,r_i liri ,表示交换两个位置上的数,每次操作完询问当前这个排列中有多少逆序对数。

思路:由于每次只修改两个位置,相当于只需要计算这两个位置对于答案的贡献,那么我们可以对这个排列进行分块,一共分成 t t t 块: [ 1 , n ] , [ n + 1 , 2 ∗ n ] , . . . , [ ( t − 1 ) ∗ n + 1 , n ] [1,\sqrt{n}],[\sqrt{n}+1,2*\sqrt{n}],...,[(t-1)*\sqrt{n}+1,n] [1n ][n +12n ]...[(t1)n +1n]。保证每次操作后块内元素要按照值有序。
那么对于每次操作,先单独考虑两个位置互相是否为逆序对。然后只需要讨论两个位置之间的逆序对数变化情况即可。
若两个位置属于同一块,那么可以暴力枚举其中两个位置之间的逆序对数 x x x ,以及交换后的逆序对数 y y y ,则操作之后的答案为 a n s − x + y ans-x+y ansx+y
若两个位置不属于同一块,则先对两个位置所处的块中暴力枚举位于两个位置之间的逆序对数变化情况,随后再枚举两个位置之间的所有块,对于每个块,由于块内元素的位置与要交换的两个位置的大小关系已知,只要二分找到值的大小分别满足交换前和交换后为逆序对的个数即可。
每次操作完对更新的两个块进行排序维护。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define NUM 200010
#define debug true
#define lowbit(x) ((-x) & x)
#define ffor(i, d, u) for (int i = (d); i <= (u); ++i)
#define _ffor(i, u, d) for (int i = (u); i >= (d); --i)
#define mst(array, Num, Kind, Count) memset(array, Num, sizeof(Kind) * (Count))
const int P = 1e9 + 7;
template <typename T>
void read(T &x)
{
    x = 0;char c;T t = 1;
    while (((c = getchar()) < '0' || c > '9') && c != '-');
    if (c == '-'){t = -1;c = getchar();}
    do(x *= 10) += (c - '0');while ((c = getchar()) >= '0' && c <= '9');
    x *= t;
}
template <typename T>
void write(T x)
{
    int len = 0;char c[21];
    if (x < 0)putchar('-'), x *= (-1);
    do{++len;c[len] = (x % 10) + '0';} while (x /= 10);
    _ffor(i, len, 1) putchar(c[i]);
}
int n, q, tot = 0;
struct ELEMENT
{
    int pos, value;
    bool operator < (const ELEMENT &x) const
    {
        return value < x.value;
    }
    bool operator < (const int &x) const
    {
        return value < x;
    }
};
struct BLOCK
{
    int l, r, len;
    vector<ELEMENT> elem;
} block[505];
int pos_block[NUM], ori[NUM];
ll ans = 0;
inline void solve(const int &t, const int &lpos, const int &rpos, const int &pos, const bool &flag)
{
    ffor(i, 0, block[t].len)
    {
        if (block[t].elem[i].pos <= lpos || block[t].elem[i].pos >= rpos)
            continue;
        if ((flag ^ (block[t].elem[i].pos > pos)) && (flag ^ (block[t].elem[i].value < ori[pos])))
            --ans;
        if ((flag ^ (block[t].elem[i].pos > pos)) && (flag ^ (block[t].elem[i].value > ori[pos])))
            ++ans;
    }
}
void AC()
{
    read(n), read(q);
    int i = 1, m = (int)sqrt(n - 1) + 1;
    ffor(i, 1, n)
        ori[i] = i;
    while (i <= n)
    {
        ++tot, block[tot].l = i;
        int j = i;
        while (j <= n && j < i + m)
        {
            block[tot].elem.push_back(ELEMENT{j, j});
            pos_block[j] = tot;
            ++j;
        }
        block[tot].r = j - 1;
        block[tot].len = block[tot].r - block[tot].l;
        i = j;
    }
    int lpos, rpos;
    int v, u;
    while (q--)
    {
        read(lpos), read(rpos);
        if (lpos == rpos)
        {
            write(ans), putchar('\n');
            continue;
        }
        if (lpos > rpos)
            swap(lpos, rpos);
        if (ori[lpos] > ori[rpos])
        	--ans;
        else
        	++ans;
        if (pos_block[lpos] == pos_block[rpos])
        {
            int t = pos_block[lpos];
            solve(t, lpos, rpos, lpos, false), solve(t, lpos, rpos, rpos, true);
            for (int i = 0; i <= block[pos_block[lpos]].len; ++i)
                if (block[pos_block[lpos]].elem[i].pos == lpos)
                    v = i;
            for (int i = 0; i <= block[pos_block[rpos]].len; ++i)
                if (block[pos_block[rpos]].elem[i].pos == rpos)
                    u = i;
            swap(block[pos_block[lpos]].elem[v].pos, block[pos_block[rpos]].elem[u].pos);
            swap(ori[lpos], ori[rpos]);
        }
        else
        {
            int t1 = pos_block[lpos], t2 = pos_block[rpos];
            solve(t1, lpos, rpos, lpos, false), solve(t2, lpos, rpos, rpos, true);
            solve(t2, lpos, rpos, lpos, false), solve(t1, lpos, rpos, rpos, true);
            ffor(t, pos_block[lpos] + 1, pos_block[rpos] - 1)//遍历两个位置之间的块
            {
                ans += (block[t].elem.end() - lower_bound(block[t].elem.begin(), block[t].elem.end(), ori[lpos]));
                ans += (lower_bound(block[t].elem.begin(), block[t].elem.end(), ori[rpos]) - block[t].elem.end());
                ans -= (lower_bound(block[t].elem.begin(), block[t].elem.end(), ori[lpos]) - block[t].elem.end());
                ans -= (block[t].elem.end() - lower_bound(block[t].elem.begin(), block[t].elem.end(), ori[rpos]));
            }
            swap(ori[lpos], ori[rpos]);
            for (int i = 0; i <= block[pos_block[lpos]].len; ++i)
                if (block[pos_block[lpos]].elem[i].pos == lpos)
                    v = i;
            for (int i = 0; i <= block[pos_block[rpos]].len; ++i)
                if (block[pos_block[rpos]].elem[i].pos == rpos)
                    u = i;
            swap(block[pos_block[lpos]].elem[v].value, block[pos_block[rpos]].elem[u].value);
            sort(block[pos_block[lpos]].elem.begin(), block[pos_block[lpos]].elem.end());
            sort(block[pos_block[rpos]].elem.begin(), block[pos_block[rpos]].elem.end());
        }
        write(ans), putchar('\n');
    }
}
int main()
{
    AC();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值