【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的元素的个数. 好像函数式线段树可 ...
随机推荐
- 无旋treap的区间操作实现
最近真的不爽...一道维修数列就做了我1上午+下午1h+1晚上+晚上1h+上午2h... 一道不错的自虐题... 由于这一片主要讲思想,代码我放这里了 不会无旋treap的童鞋可以进这里 呵呵... ...
- 在Centos7下安装与部署.net core
在Centos7下安装与部署.net core 2018年02月28日 19:36:16 阅读数:388 个人安装流程,参照文档 https://www.cnblogs.com/Burt/p/6566 ...
- Ubuntu常用shell命令
目录 ls cd mkdir mv cp scp rm df du chmod chown chgrp head tail screen apt-get Ubuntu常用shell命令 Ubuntu作 ...
- Winrar去广告图文教程
一.前言 1.1 Winrar 解压缩工具 市场上有很多优秀的压缩工具,常用的有Winrar 和360 压缩工具.Winrar是免费压缩工具,特色是每次使用都会弹出广告.影响用户体验和工作效率,当然最 ...
- 接口测试工具Postman接口测试图文教程
Postman接口测试工具使用 一.前言 在前后端分离开发时,后端工作人员完成系统接口开发后,需要与前端人员对接,测试调试接口,验证接口的正确性可用性.而这要求前端开发进度和后端进度保持基本一 ...
- UVa 10071
简单运动学公式 v=v0+at x=v0t+1/2*a*t^2=2vt #include<stdio.h> int main() { int v, t; while((scanf(&quo ...
- ES6的新特性(10)——Class 的基本语法
Class 的基本语法 简介 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子. function Point(x, y) { this.x = x; this.y ...
- Alpha冲刺——第七天
Alpha第七天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- 软件图书,偏.net方向
深入理解计算机系统(原书第2版) 作者:Randal E.Bryant:1981年在麻省理工学院获计算机科学博士学位,现任美国卡内基·梅隆大学计算机学院院长 内容: 深入浅出地介绍了处理器.编译器.操 ...
- lintcode-420-报数
420-报数 报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数.如下所示: 1, 11, 21, 1211, 111221, ... 1 读作 "one 1" -> ...