莫队维护逆序对,区间左右增减要分类讨论。

记得离散化。

 /**************************************************************
Problem: 3289
User: idy002
Language: C++
Result: Accepted
Time:5480 ms
Memory:3164 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 50010
#define lowbit(i) ((i)&(-(i)))
using namespace std; typedef long long lng; int n, m;
int disc[maxn];
int lx[maxn], rx[maxn], mccno[maxn], stot;
int w[maxn];
lng cnt[maxn], cnttot, cur_ans;
lng ans[maxn]; struct Qu {
int l, r, id;
bool operator<( const Qu & b ) const {
return mccno[l]<mccno[b.l] || (mccno[l]==mccno[b.l] && r<b.r );
}
};
Qu qu[maxn]; lng qu_pres( int r ) {
lng rt = ;
for( int i=r; i; i-=lowbit(i) )
rt += cnt[i];
return rt;
}
void up_val( int pos, int delta ) {
for( int i=pos; i<=n; i+=lowbit(i) )
cnt[i] += delta;
cnttot += delta;
}
void push_back( int pos ) {
cur_ans += cnttot - qu_pres( pos );
up_val( pos, + );
}
void pop_back( int pos ) {
cur_ans -= cnttot - qu_pres( pos );
up_val( pos, - );
}
void push_front( int pos ) {
cur_ans += qu_pres( pos- );
up_val( pos, + );
}
void pop_front( int pos ) {
cur_ans -= qu_pres( pos- );
up_val( pos, - );
} void partition() {
int len = (int)ceil(sqrt(n))+;
int stot = n/len;
rx[] = ;
for( int i=; i<=stot; i++ ) {
lx[i] = rx[i-]+;
rx[i] = rx[i-]+len;
}
if( rx[stot]!=n ) {
stot++;
lx[stot] = rx[stot-]+;
rx[stot] = n;
}
for( int i=; i<=stot; i++ )
for( int j=lx[i]; j<=rx[i]; j++ )
mccno[j] = i;
}
void work() {
sort( qu+, qu++m );
int lf, rg;
for( int q=; q<=m; q++ ) {
if( q== || mccno[qu[q].l] != mccno[qu[q-].l] ) {
lf = qu[q].l;
rg = qu[q].l-;
memset( cnt, , sizeof(cnt) );
cur_ans = cnttot = ;
}
while( lf<qu[q].l ) pop_front( w[lf++] );
while( lf>qu[q].l ) push_front( w[--lf] );
while( rg<qu[q].r ) push_back( w[++rg] );
while( rg>qu[q].r ) pop_back( w[rg--] );
ans[qu[q].id] = cur_ans;
}
}
int main() {
scanf( "%d", &n );
for( int i=; i<=n; i++ ) {
scanf( "%d", w+i );
disc[i] = w[i];
}
sort( disc+, disc++n );
for( int i=; i<=n; i++ )
w[i] = lower_bound( disc+, disc++n, w[i] ) - disc;
scanf( "%d", &m );
for( int i=; i<=m; i++ ) {
scanf( "%d%d", &qu[i].l, &qu[i].r );
qu[i].id = i;
}
partition();
work();
for( int i=; i<=m; i++ )
printf( "%lld\n", ans[i] );
}

bzoj 3289 莫队 逆序对的更多相关文章

  1. 【刷题】BZOJ 3295 [Cqoi2011]动态逆序对

    Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...

  2. BZOJ 3295: [Cqoi2011]动态逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3865  Solved: 1298[Submit][Sta ...

  3. BZOJ 3339 & 莫队+"所谓的暴力"

    题意: 给一段数字序列,求一段区间内未出现的最小自然数. SOL: 框架显然用莫队.因为它兹瓷离线. 然而在统计上我打了线段树...用&维护的结点...400w的线段树...然后二分查找... ...

  4. 【BZOJ 3295】动态逆序对 - 分块+树状数组

    题目描述 给定一个1~n的序列,然后m次删除元素,每次删除之前询问逆序对的个数. 分析:分块+树状数组 (PS:本题的CDQ分治解法见下一篇) 首先将序列分成T块,每一块开一个树状数组,并且先把最初的 ...

  5. bzoj 2038 莫队算法

    莫队算法,具体的可以看10年莫涛的论文. 大题思路就是假设对于区间l,r我们有了一个答案,那么对于区间l,r+1,我们 可以暴力的转移一个答案,那么对于区间l1,r1和区间l2,r2,需要暴力处理 的 ...

  6. bzoj 3295 [Cqoi2011]动态逆序对(cdq分治,BIT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3295 [题意] n个元素依次删除m个元素,求删除元素之前序列有多少个逆序对. [思路] ...

  7. 【Bzoj 3295】 动态逆序对(树套树|CDQ分治)

    [题意] 每次删除一个数,然后问删除前逆序对数. [分析] 没有AC不开心.. 我的树状数组套字母树,应该是爆空间的,空间复杂度O(nlogn^2)啊..哭.. 然后就没有然后了,别人家的树套树是树状 ...

  8. Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2886  Solved: 924[Submit][Stat ...

  9. Bzoj 2141: 排队 分块,逆序对,树状数组

    2141: 排队 Time Limit: 4 Sec  Memory Limit: 259 MBSubmit: 1310  Solved: 517[Submit][Status][Discuss] D ...

随机推荐

  1. xp,win7,win10系统安装GHO镜像下载

    淘宝买的纯净版系统镜像 不含任何垃圾软件 极致纯净 链接:http://pan.baidu.com/s/1eR12db0 密码:opjm 链接:http://pan.baidu.com/s/1mhEN ...

  2. lspci 虚拟机网卡对应关系

    我这个办法有点笨: 到 /sys/devices/ 下去搜索网卡 eth*,找到网卡对应的PCI 总线位置,例如:05:00.0. 然后通过 "lspci -s 05:00.0" ...

  3. USB基础介绍

    (转)USB (Universal Serial Bus) 全文地址:http://vlewang.blog.163.com/blog/static/105878151201032804347546/ ...

  4. 64_r1

    R-3.4.0-2.fc26.x86_64.rpm 15-May-2017 14:49 31030 R-ALL-1.6.0-4.fc26.noarch.rpm 17-Feb-2017 22:05 11 ...

  5. MySQL指定使用某个索引查询语句

    查询语句查询emp_no,所以先查询emp_no的索引使用primary select emp_no,salary from salaries use index(s_f_t) where emp_n ...

  6. 省市区ajax联动

    function setCity1(){ var areaId1 = $('#areaId1').val(); var cityId1 = $('#cityId1'); var cityOpt = $ ...

  7. Codeforces Round #456 (Div. 2)

    Codeforces Round #456 (Div. 2) A. Tricky Alchemy 题目描述:要制作三种球:黄.绿.蓝,一个黄球需要两个黄色水晶,一个绿球需要一个黄色水晶和一个蓝色水晶, ...

  8. [转载]hazard pointer

    hazard pointer 转载自: http://hi.baidu.com/rodimus/item/f6539cc179894f2f47d5c0ef 这是用于解决多线程并发下内存的回收,一块内存 ...

  9. Codeforces 445A Boredom(DP+单调队列优化)

    题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...

  10. POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)

    题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...