zoukankan      html  css  js  c++  java
  • 【BZOJ】3295: [Cqoi2011]动态逆序对

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3295


    mamaya,弱鸡xrdog终于会写树套树啦....

    将树状数组中每一个节点看成一棵平衡树,支持$RANK$操作,每次删除一个数之前,支持查询位置在当前数之前权值比他大的有多少个,查询位置在当前数之后权值比他小的有多少个,再支持删除操作。

    复杂度${O(nlogn^{2})}$,空间复杂度${O(nlogn)}$


     1 #include<iostream>
     2 #include<ext/pb_ds/assoc_container.hpp>  
     3 #include<cstdio>
     4 #include<algorithm>
     5 #include<vector>
     6 #include<cstdlib>
     7 #include<cmath>
     8 #include<cstring>
     9 #include<set>
    10 using namespace std;
    11 using namespace __gnu_pbds;  
    12 #define maxn 1001000
    13 #define llg long long
    14 #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
    15 llg n,m,d[maxn],ans,wz[maxn],v;
    16 tree<llg,null_type,less<llg>,rb_tree_tag,tree_order_statistics_node_update>c[maxn];  
    17 
    18 //tree<llg,null_mapped_type,less<llg>,rb_tree_tag,tree_order_statistics_node_update>c[maxn];  //-----如果您的编译器版本较低(BZOJ)
    19 llg cc[maxn];
    20 inline llg lowbit(llg x){return x&-x;}
    21 
    22 void add(llg x,llg val)  {while (x<=n) {c[x].insert(val);x+=lowbit(x);}}
    23 
    24 void del(llg x,llg val)  {while (x<=n) c[x].erase(val),x+=lowbit(x);}
    25 
    26 void add_(llg x,llg val) {while (x<=n) cc[x]+=val,x+=lowbit(x);}
    27 
    28 llg sum_(llg x) {llg tot=0; while (x>0) tot+=cc[x],x-=lowbit(x); return tot;}
    29 
    30 llg sum(llg x,llg val)
    31 {
    32     llg tot=0;
    33     while (x>0)
    34     {
    35         tot+=c[x].size();
    36         llg xiao=c[x].order_of_key(val+1);
    37         tot-=xiao;
    38         x-=lowbit(x);
    39 }return tot;
    40 }
    41 
    42 llg summ(llg x,llg val)
    43 {
    44     llg tot=0;
    45     while (x>0)
    46     {
    47         tot+=c[x].order_of_key(val+1);
    48         x-=lowbit(x);
    49     }
    50     return tot;
    51 }
    52 
    53 void init()
    54 {
    55     cin>>n>>m;
    56     for (llg i=1;i<=n;i++) 
    57     {
    58         scanf("%lld",&d[i]);
    59         wz[d[i]]=i;
    60         ans+=sum_(n)-sum_(d[i]);
    61         add_(d[i],1);
    62         add(i,d[i]);
    63     }
    64 
    65 }
    66 
    67 void work()
    68 {
    69     llg x,da,xiao;
    70     while (m--)
    71     {
    72         printf("%lld
    ",ans);
    73         scanf("%lld",&v);
    74         x=wz[v];
    75         da=sum(x,v);
    76         xiao=summ(n,v)-summ(x,v);
    77         ans-=da+xiao;
    78         del(x,v);
    79     }
    80 }
    81 
    82 int main()
    83 {
    84     yyj("bzoj3295");
    85     init();
    86     work();
    87     return 0;
    88 }
    本文作者:xrdog 作者博客:http://www.cnblogs.com/Dragon-Light/ 转载请注明出处,侵权必究,保留最终解释权!
  • 相关阅读:
    NET 中反射的用法
    Prism 框架解读之一系列
    委托(Delegate)
    Python NameError:name ‘xrange’ is not defined
    Python import commands ImportError: No module named 'commands'
    Python import commands ImportError: No module named 'commands'
    Python3 TypeError: initial_value must be str or None, not bytes
    Python3 TypeError: initial_value must be str or None, not bytes
    Python import urllib2 ImportError: No module named 'urllib2'
    Python import urllib2 ImportError: No module named 'urllib2'
  • 原文地址:https://www.cnblogs.com/Dragon-Light/p/6471964.html
Copyright © 2011-2022 走看看