zoukankan      html  css  js  c++  java
  • [CQOI2011]动态逆序对

    嘟嘟嘟
    双倍经验


    这道题只要想明白了就和(cdq)分治的板儿没什么区别了,然而如果想不明白就会像我一样磨叽了一晚上。


    删数不好办,于是离线倒序改成加数。
    考虑加上一个数(a_i)形成的逆序对:1.在他前面且比他大的。2.在他后面且比他小的。
    因为数字是动态添加的,所以上述的数必须是在他之前添加的!
    如果给每一个数三个属性:(x, tim, val),分别表示这个数的位置,添加时间和大小。那么上面的两条就可以形式化的写成:
        $ x_j < x_i, tim_j < tim_i,val_j > val_i( &emsp;&emsp;&emsp;&emsp;)x_j > x_i, tim_j < tim_i, val_j < val_i( 这不就是陌上花开吗! 于是我们按)x(排序,然后归并排序)tim(,并用树状数组维护)val$。
    需要注意的是成立条件有两条,因此我们在分治的每一层要归并两次,第一次统计右边的数形成的逆序对个数,第二次统计左边的数形成的逆序对个数。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define rg register
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 1e5 + 5;
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    int n, m, pos[maxn];
    struct Node
    {
      int x, tim, val;
    }a[maxn], t[maxn];
    
    int c[maxn];
    int lowbit(int x)
    {
      return x & -x;
    }
    void erase(int pos)
    {
      for(; pos <= n; pos += lowbit(pos))
        if(c[pos]) c[pos] = 0;
        else break;
    }
    void add(int pos, int d)
    {
      for(; pos <= n; pos += lowbit(pos)) c[pos] += d;
    }
    int query(int pos)
    {
      int ret = 0;
      for(; pos; pos -= lowbit(pos)) ret += c[pos];
      return ret;
    }
    
    ll ans[maxn];
    void cdqSolve(int L, int R)
    {
      if(L == R) return;
      int mid = (L + R) >> 1, id1 = L, id2 = mid + 1;
      cdqSolve(L, mid); cdqSolve(mid + 1, R);
      for(int i = L; i <= R; ++i)
        {
          if(id2 > R || (id1 <= mid && a[id1].tim <= a[id2].tim))
        {
          t[i] = a[id1++];
          add(t[i].val, 1);
        }
          else
        {
          t[i] = a[id2++];
          ans[t[i].tim] += (id1 - L) - query(t[i].val);
        }
        }
      for(int i = L; i <= mid; ++i) erase(a[i].val);
      id1 = L; id2 = mid + 1;
      for(int i = L; i <= R; ++i)
        {
          if(id2 > R || (id1 <= mid && a[id1].tim <= a[id2].tim))
        {
          t[i] = a[id1++];
          ans[t[i].tim] += query(t[i].val - 1);
        }
          else
        {
          t[i] = a[id2++];
          add(t[i].val, 1);
        }
        }
      for(int i = mid + 1; i <= R; ++i) erase(a[i].val);
      for(int i = L; i <= R; ++i) a[i] = t[i];
    }
    
    int main()
    {
      n = read(); m = read();
      for(int i = 1; i <= n; ++i)
        {
          a[i].val = read(), a[i].x = i, a[i].tim = 0;
          pos[a[i].val] = i;
        }
      int T = m;
      for(int i = 1; i <= m; ++i)
        {
          int x = read();
          a[pos[x]].tim = T--;
        }
      cdqSolve(1, n);
      for(int i = 1; i <= m; ++i) ans[i] = ans[i - 1] + ans[i];
      for(int i = m; i; --i) write(ans[i]), enter;
      return 0;
    }
    
  • 相关阅读:
    ​《数据库系统概念》5-连接、视图和事务
    ​《数据库系统概念》4-DDL、集合运算、嵌套子查询
    ​《数据库系统概念》3-主键、关系运算
    ​《数据库系统概念》2-存储、事务等的简介
    ​《数据库系统概念》1-数据抽象、模型及SQL
    Web API与JWT认证
    巨杉Tech | 十分钟快速搭建 Wordpress 博客系统
    巨杉内核笔记(一)| SequoiaDB 会话(session)简介
    SequoiaDB巨杉数据库入门:快速搭建流媒体服务器
    微服务?数据库?它们之间到底是啥关系?
  • 原文地址:https://www.cnblogs.com/mrclr/p/10043157.html
Copyright © 2011-2022 走看看