【BZOJ2141】排队(树套树)
【BZOJ2141】排队(树套树)
题面
题解
傻逼题啊。。。
裸的树套树
树状数组套线段树,每次交换的时候,考虑一下前后的贡献,先删掉贡献,再重新算一遍就好了。。
#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】排队(树套树)的更多相关文章
- 树套树Day1线段树套平衡树bzoj3196
您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的前驱(前驱定义为小于x,且最大的数)5.查 ...
- BZOJ 3110: [Zjoi2013]K大数查询 [树套树]
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6050 Solved: 2007[Submit][Sta ...
- 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 ...
- bzoj3262: 陌上花开(树套树)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- bzoj3295: [Cqoi2011]动态逆序对(树套树)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- BZOJ 3110 k大数查询 & 树套树
题意: 有n个位置,每个位置可以看做一个集合,现在要求你实现一个数据结构支持以下功能: 1:在a-b的集合中插入一个数 2:询问a-b集合中所有元素的第k大. SOL: 调得火大! 李建说数据结构题能 ...
- BZOJ 3110 树套树 && 永久化标记
感觉树套树是个非常高深的数据结构.从来没写过 #include <iostream> #include <cstdio> #include <algorithm> ...
- 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树套树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 这题调了我相当长的时间,1wa1a,我是第一次写树套树,这个是树状数组套splay,在每个区间 ...
- hdu 4417 Super Mario/树套树
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意很简单,给定一个序列求一个区间 [L, R,]中小于等于H的元素的个数. 好像函数式线段树可 ...
随机推荐
- python开发ftp服务器第一天(pyftpdlib)
学习了大约快一个月的python,现在开始有意识做一些项目.(我的新书<Python爬虫开发与项目实战>出版了,大家可以看一下样章) 据我了解,python现在更多的是用于自动化运维方面, ...
- javaweb总结(四十)——编写自己的JDBC框架
一.元数据介绍 元数据指的是"数据库"."表"."列"的定义信息. 1.1.DataBaseMetaData元数据 Connection.g ...
- Linux 内核3.10.5 专场
今天本人十分靠谱地下载了linux 内核的3.10.5版本,这个版本是最新的稳定版. 听路飞大虾(哪个路飞?就是那个戴草帽的橡胶小伙,航海很多时候都很空闲的,于是最近他也开始研读linux 内核了.) ...
- ffmpeg 压缩H265 Windows 硬件编码
硬件NVIDIA:ffmpeg.exe -i input.avi -c:v hevc_nvenc -preset:v fast output.mp4 软件 :ffmpeg.exe - ...
- 初识JMM
目录 what is JMM? JMM变量存储结构 JMM三大特性 原子性 可见性 有序性 java 堆栈 静态存储 栈式存储 堆式存储 JVM是啥 参考<Inside JVM> what ...
- Jmeter接口测试(一) Jmeter简介
一.Jmeter介绍 (一)Jmeter简介 Apache JMeter 是 Apache 组织的开放源代码项目,是一个纯 Java 桌面应用,用于压力测试和性能测试.它最初被设计用于 Web 应用测 ...
- 搭建RTSP服务器时nginx的nginx.conf文件配置
worker_processes 1; events { worker_connections 1024;} http { include mime.types; default_type appli ...
- 搭建docker 私有镜像仓库
前期准备 服务器:centos 7.3 docker-ce: 18.06.1-ce docker-compose: 1.22.0 docker 安装 首先,更新系统 yum update yum up ...
- 高可用Kubernetes集群-4. kubectl客户端工具
六.部署kubectl客户端工具 1. 下载 [root@kubenode1 ~]# cd /usr/local/src/ [root@kubenode1 src]# wget https://sto ...
- thinkphp5框架生成二维码
二话不说,先上代码: 第一中: 不用再本地保存文件,直接在前台页面显示: 这是控制器里面的内容,哦,对啦,首先要下载SDK:.phpqrcode类文件下载,下载地址:https://sourcefor ...