【BZOJ2141】排队(树套树)

题面

BZOJ

洛谷

题解

傻逼题啊。。。

裸的树套树

树状数组套线段树,每次交换的时候,考虑一下前后的贡献,先删掉贡献,再重新算一遍就好了。。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 22222
#define lb(x) (x&(-x))
inline int read()
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int len,S[MAX],a[MAX],n,Q;
struct Node{int ls,rs,v;}t[MAX<<8];
int rt[MAX],tot;
void Modify(int &x,int l,int r,int p,int w)
{
if(!x)x=++tot;t[x].v+=w;
if(l==r)return;
int mid=(l+r)>>1;
if(p<=mid)Modify(t[x].ls,l,mid,p,w);
else Modify(t[x].rs,mid+1,r,p,w);
}
void PreModify(int x,int p,int w)
{
for(int i=x;i<=n;i+=lb(i))
Modify(rt[i],1,len,p,w);
}
int Query(int x,int l,int r,int L,int R)
{
if(L<=l&&r<=R)return t[x].v;
int mid=(l+r)>>1,ret=0;
if(L<=mid)ret+=Query(t[x].ls,l,mid,L,R);
if(R>mid)ret+=Query(t[x].rs,mid+1,r,L,R);
return ret;
}
int PreQuery(int x,int L,int R)
{
int ret=0;
if(L>R)return 0;
for(int i=x;i;i-=lb(i))ret+=Query(rt[i],1,len,L,R);
return ret;
}
int ans;
int main()
{
n=read();
for(int i=1;i<=n;++i)a[i]=S[i]=read();
sort(&S[1],&S[n+1]);len=unique(&S[1],&S[n+1])-S-1;
for(int i=1;i<=n;++i)a[i]=lower_bound(&S[1],&S[len+1],a[i])-S;
for(int i=1;i<=n;++i)
{
ans+=PreQuery(i,a[i]+1,len);
PreModify(i,a[i],1);
}
printf("%d\n",ans);
Q=read();
while(Q--)
{
int x=read(),y=read();
if(x<y)swap(x,y);
PreModify(x,a[x],-1);
ans-=PreQuery(x,a[x]+1,len);
ans-=PreQuery(n,1,a[x]-1)-PreQuery(x,1,a[x]-1);
PreModify(y,a[y],-1);
ans-=PreQuery(y,a[y]+1,len);
ans-=PreQuery(n,1,a[y]-1)-PreQuery(y,1,a[y]-1);
swap(a[x],a[y]);
ans+=PreQuery(y,a[y]+1,len);
ans+=PreQuery(n,1,a[y]-1)-PreQuery(y,1,a[y]-1);
PreModify(y,a[y],1);
ans+=PreQuery(x,a[x]+1,len);
ans+=PreQuery(n,1,a[x]-1)-PreQuery(x,1,a[x]-1);
PreModify(x,a[x],1);
printf("%d\n",ans);
}
return 0;
}

【BZOJ2141】排队(树套树)的更多相关文章

  1. 树套树Day1线段树套平衡树bzoj3196

    您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的前驱(前驱定义为小于x,且最大的数)5.查 ...

  2. BZOJ 3110: [Zjoi2013]K大数查询 [树套树]

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6050  Solved: 2007[Submit][Sta ...

  3. BZOJ4170 极光(CDQ分治 或 树套树)

    传送门 BZOJ上的题目没有题面-- [样例输入] 3 5 2 4 3 Query 2 2 Modify 1 3 Query 2 2 Modify 1 2 Query 1 1 [样例输出] 2 3 3 ...

  4. bzoj3262: 陌上花开(树套树)

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  5. bzoj3295: [Cqoi2011]动态逆序对(树套树)

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  6. BZOJ 3110 k大数查询 & 树套树

    题意: 有n个位置,每个位置可以看做一个集合,现在要求你实现一个数据结构支持以下功能: 1:在a-b的集合中插入一个数 2:询问a-b集合中所有元素的第k大. SOL: 调得火大! 李建说数据结构题能 ...

  7. BZOJ 3110 树套树 && 永久化标记

    感觉树套树是个非常高深的数据结构.从来没写过 #include <iostream> #include <cstdio> #include <algorithm> ...

  8. 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树套树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1901 这题调了我相当长的时间,1wa1a,我是第一次写树套树,这个是树状数组套splay,在每个区间 ...

  9. hdu 4417 Super Mario/树套树

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意很简单,给定一个序列求一个区间 [L, R,]中小于等于H的元素的个数. 好像函数式线段树可 ...

随机推荐

  1. L016-linux系统文件权限体系实战深入讲解小节

    L016-linux系统文件权限体系实战深入讲解小节 不知道今天能不能写完哈,能写完发出来就是这周发两次小结了,有进步哦,不过L015和L016两节课内容也确实不多,进入正题 上一课学到了chmod. ...

  2. Mac 安装PHP Redis 扩展

    其实 Mac 安装 Redis 还是很简单,以下为个人搭建配置.注意:文章中的“*”代表任意版本号 安装 Redis 服务 安装 brew install redis 使用 # 启动 redis-se ...

  3. 你的第一个接口测试:Python 接口测试

    前言: 首先我们先明确一个概念,什么叫接口.什么叫接口测试? 接口的全称叫[Application Programming Interface 又叫API],是提供应用程序与开发人员基于某软件或硬件得 ...

  4. 获取json键值对的对应字符串

    获取json中的姓名 json串ac 关键字key public class Json { public static String json(String  key;String  ac) { JS ...

  5. JY播放器【喜马拉雅FM电脑端,附带下载功能】

    今天给大家带来一款神器----JY播放器.可以不用打开网页就在电脑端听喜马拉雅FM的节目,而且可以直接下载,对于我这种强迫症患者来说真的是神器.我是真的不喜欢电脑任务栏上面密密麻麻的. 目前已经支持平 ...

  6. leetcode-优美的排列

    假设有从 1 到 N 的 N 个整数,如果从这 N 个数字中成功构造出一个数组,使得数组的第 i 位 (1 <= i <= N) 满足如下两个条件中的一个,我们就称这个数组为一个优美的排列 ...

  7. MobSF 框架安装使用部署

    1.MobSF 简介 MobSF是Mobile Security Framework的缩写,这是一款智能化的开源移动应用(Android.IOS.Windows)测试框架,可以对应用进行动态.静态分析 ...

  8. dede 后台登录以后一片空白

    网上说的是 找到:include/common.inc.php文件,打开,查找程序代码: //error_reporting(E_ALL);  error_reporting(E_ALL || ~E_ ...

  9. Linux 150命令之查看文件及内容处理命令 cat tac less head tail cut

    cat 查看文件内容 [root@mysql tmp]# cat 2.txt 1234 -n 查看行号 [root@mysql tmp]# cat -n 2.txt      1 1234       ...

  10. 01慕课网《vue.js2.5入门》——基础知识

    前端框架 Vue.js2.5 2018-05-12 Vue官网:https://cn.vuejs.org/ 基础语法+案例实践+TodoList+Vue-cli构建工具+TodoList Vue基础语 ...