HDU 5193 Go to movies Ⅱ(块状数组套树状数组)

题意:一群人站一排,身高各不同,有加入人以及删除人操作,求全部人的逆序数。

用块状数组去维护下下标,每个块里用个树状数组去维护下大小,这里有个关键的问题就是因为有加入和删除操作,那么一个块里的东西可能会变没有或者超出范围,不光内存开不下,效率也会非常低。所以就需要重构。假设一个块的大小为s,那么在加入或删除的那个块里就直接暴力移动元素以及计算之前大于他之后小于他的,对于其他块来说直接利用树状数组计算,复杂度是o(s+s/plogn),为了均摊,取s= 根号(plogn),有m次修改复杂度为o(m根号(plogn))。这里的原因其实跟区间第k大也这样分块来做的道理是一样的。

重构的时间也设为s,因为重构一次需要p*logn的时间。所以需要重构的次数为m/s,复杂度也为m/s*plogn = o(m根号(plogn))。

这样的话,我一个块里的数据大小只需要设2倍的根号(plogn)就可以了。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned long long
#define eps 1e-8
#define NMAX 1000000000
#define MOD 1000000
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.end())
template<class T>
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}
template<class T> inline T Max(T a, T b){ return a > b ? a : b;}
template<class T> inline T Min(T a, T b){ return a < b ? a : b;}
const int maxn = 20000;
struct Blocklist
{
    int c[maxn+5],top,data[550*2];
    void init()
    {
        top = 0;
        memset(c,0,sizeof(c));
    }
}B[70];
int a[maxn<<1+10],T[maxn+5];
int p,kuai,nct;
ll inv;
inline int lowbit(int x)
{
    return x&(-x);
}

void add(int *A, int x, int d)
{
    while(x <= maxn)
    {
        A[x] += d;
        x += lowbit(x);
    }
}

int sum(int *A, int x)
{
    int ret = 0;
    while(x)
    {
        ret += A[x];
        x -= lowbit(x);
    }
    return ret;
}

inline void build(bool flag)
{
    if(!flag)
    {
        int k = 0;
        for(int i = 0; i < nct; i++)
            for(int j = 0; j < B[i].top; j++)
                a[k++] = B[i].data[j];
        p = k;
    }
    kuai = (int)sqrt(p*15);
    nct = p/kuai;
    if(nct*kuai < p) nct++;
    for(int i = 0; i < nct; i++)
        B[i].init();
    for(int i = 0; i < p; i++)
    {
        int x = i/kuai;
        B[x].data[B[x].top++] = a[i];
        add(B[x].c,a[i],1);
    }
}

inline void update(int x, int h)
{
    int pos,ji = 0;
    for(int i = 0; i < nct; i++)//find the block position
    {
        ji += B[i].top;
        if(ji >= x)
        {
            pos = i;
            ji -= B[i].top;
            break;
        }
    }
    for(int i = 0; i < B[pos].top; i++)
    {
        if(ji+i < x)
        {
            if(B[pos].data[i] > h) inv++;
        }
        else if(B[pos].data[i] < h) inv++;
    }
    for(int i = B[pos].top-1; i >= x-ji; i--)
        B[pos].data[i+1] = B[pos].data[i];
    B[pos].top++;
    B[pos].data[x-ji] = h;
    add(B[pos].c,h,1);
    for(int i = 0; i < pos; i++)
        inv += (ll)sum(B[i].c,maxn)-(ll)sum(B[i].c,h);
    for(int i = pos+1; i < nct; i++)
        inv += (ll)sum(B[i].c,h-1);
}

inline void eraseit(int x)
{
    int pos,ji = 0;
    for(int i = 0; i < nct; i++)
    {
        ji += B[i].top;
        if(ji >= x)
        {
            ji -= B[i].top;
            pos = i;
            break;
        }
    }
    int pp = x-ji-1,big = B[pos].data[pp];
    for(int i = 0; i < B[pos].top; i++)
    {
        if(i < pp)
        {
            if(B[pos].data[i] > big) inv--;
        }
        else if(B[pos].data[i] < big) inv--;
    }
    for(int i = pp+1; i < B[pos].top; i++)
        B[pos].data[i-1] = B[pos].data[i];
    add(B[pos].c,big,-1);
    B[pos].top--;
    for(int i = 0; i < pos; i++)
        inv -= (ll)sum(B[i].c,maxn)-(ll)sum(B[i].c,big);
    for(int i = pos+1; i < nct; i++)
        inv -= (ll)sum(B[i].c,big-1);
}

int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o.txt","w",stdout);
#endif
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        p = n;
        for(int i = 0; i < n; i++)
            scanf("%d",&a[i]);
        build(true);
        inv = 0;
        memset(T,0,sizeof(T));
        for(int i = 0; i < p; i++)
        {
            inv += (ll)sum(T,maxn) - (ll)sum(T,a[i]);
            add(T,a[i],1);
        }
        int w = 0, ha = (int)sqrt(n*14);
        while(m--)
        {
            int flag,x,y;
            w++;
            scanf("%d",&flag);
            if(flag == 0)
            {
                scanf("%d%d",&x,&y);
                update(x,y);
                p++;
            }
            else
            {
                scanf("%d",&x);
                eraseit(x);
                p--;
            }
            printf("%I64d\n",inv);
            if(w == ha)
            {
                if(p != 0) build(false);
                w = 0;
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值