P1975 [国家集训队]排队
题目链接
题意分析
我们考虑 交换两个数\([le,ri]\)的贡献
减少的逆序对数\([le,ri]\)中小于\(num[le]\)以及大于\(num[ri]\)的数
增加的\([le,ri]\)中大于\(num[le]\)以及小于\(num[ri]\)的数
同时注意 如果\(num[le]!=num[ri]\)
二者相互的贡献 就多算了一次
所以我们需要特判一下
至于修改和查询 我们可以使用树套树维护
CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<deque>
#include<vector>
#include<ctime>
#define ll long long
#define inf 0x7fffffff
#define N 150008
#define IL inline
#define M 1008611
#define D double
#define ull unsigned long long
#define R register
using namespace std;
template<typename T>IL void read(T &_)
{
T __=0,___=1;char ____=getchar();
while(!isdigit(____)) {if(____=='-') ___=0;____=getchar();}
while(isdigit(____)) {__=(__<<1)+(__<<3)+____-'0';____=getchar();}
_=___ ? __:-__;
}
/*-------------OI使我快乐-------------*/
int n,q,tot,cnt,cnta,cntb,top;ll ans;
int num[N],res[N],sta[N],tmp[N];
int root[N],lson[N*50],rson[N*50],siz[N*50];
int cdy[N],wzy[N];
IL int new_code(){return top ? sta[top--]:++tot;}
IL void solve(int le,int ri)
{
if(le==ri) return;
int mid=(le+ri)>>1;
solve(le,mid);solve(mid+1,ri);
int ix=le,jx=mid+1,kx=le;
for(;ix<=mid&&jx<=ri;)
{
if(res[ix]<=res[jx]) tmp[kx++]=res[ix++];
else ans+=mid-ix+1,tmp[kx++]=res[jx++];
}
for(;ix<=mid;) tmp[kx++]=res[ix++];
for(;jx<=ri;) tmp[kx++]=res[jx++];
for(R int i=le;i<=ri;++i) res[i]=tmp[i];
}
IL void insert(int &now,int le,int ri,int pos,int d)
{
if(!now) now=new_code();siz[now]+=d;
if(le==ri) return;
int mid=(le+ri)>>1;
if(pos<=mid) insert(lson[now],le,mid,pos,d);
else insert(rson[now],mid+1,ri,pos,d);
if(!siz[now]) sta[++top]=now,now=0;
}
IL void add(int x,int pos,int d)
{
for(R int i=x;i<=n;i+=i&-i) insert(root[i],1,cnt,pos,d);
}
IL void prework(int le,int ri)
{
cnta=cntb=0;
for(R int x=le-1;x;x-=x&-x) cdy[++cnta]=root[x];
for(R int x=ri;x;x-=x&-x) wzy[++cntb]=root[x];
}
IL int getsum()
{
int res=0;
for(R int i=1;i<=cnta;++i) res-=siz[cdy[i]];
for(R int i=1;i<=cntb;++i) res+=siz[wzy[i]];
return res;
}
IL int getsumcdy()
{
int res=0;
for(R int i=1;i<=cnta;++i) res-=siz[lson[cdy[i]]];
for(R int i=1;i<=cntb;++i) res+=siz[lson[wzy[i]]];
return res;
}
IL int getsumwzy()
{
int res=0;
for(R int i=1;i<=cnta;++i) res-=siz[rson[cdy[i]]];
for(R int i=1;i<=cntb;++i) res+=siz[rson[wzy[i]]];
return res;
}
IL void pushcdy()
{
for(R int i=1;i<=cnta;++i) cdy[i]=lson[cdy[i]];
for(R int i=1;i<=cntb;++i) wzy[i]=lson[wzy[i]];
}
IL void pushwzy()
{
for(R int i=1;i<=cnta;++i) cdy[i]=rson[cdy[i]];
for(R int i=1;i<=cntb;++i) wzy[i]=rson[wzy[i]];
}
IL int get_cdy(int le,int ri,int pos)
{
if(pos<1) return 0;
if(le==ri) return getsum();
int mid=(le+ri)>>1,tmp=getsumcdy();
if(pos<=mid) {pushcdy();return get_cdy(le,mid,pos);}
else {pushwzy();return get_cdy(mid+1,ri,pos)+tmp;}
}
IL int get_wzy(int le,int ri,int pos)
{
if(pos>cnt) return 0;
if(le==ri) return getsum();
int mid=(le+ri)>>1,tmp=getsumwzy();
if(pos<=mid) {pushcdy();return get_wzy(le,mid,pos)+tmp;}
else {pushwzy();return get_wzy(mid+1,ri,pos);}
}
int main()
{
// freopen("cd.in","r",stdin);
// freopen("cd.out","w",stdout);
read(n);
for(R int i=1;i<=n;++i) read(num[i]),res[i]=num[i];
solve(1,n);cnt=unique(res+1,res+n+1)-res-1;
for(R int i=1;i<=n;++i) num[i]=lower_bound(res+1,res+cnt+1,num[i])-res;
for(R int i=1;i<=n;++i) add(i,num[i],1);
printf("%lld\n",ans);
// prework(1,n);
// for(R int i=1;i<=n;++i) printf("%d%c",num[i],(i==n ? '\n':' '));
// prework(1,n);
// printf("%d\n",get_cdy(1,cnt,cnt));
// prework(1,n);
// printf("%d\n",get_wzy(1,cnt,1));
read(q);
while(q--)
{
int x,y,tmpx,tmpy;read(x);read(y);
if(x==y) continue;tmpx=num[x];tmpy=num[y];
// printf("now is %d %d\n",tmpx,tmpy);
int cdy,wzy,zjz,ghj;
prework(x,y);
cdy=get_cdy(1,cnt,tmpx-1);
prework(x,y);
wzy=get_wzy(1,cnt,tmpx+1);
prework(x,y);
zjz=get_cdy(1,cnt,tmpy-1);
prework(x,y);
ghj=get_wzy(1,cnt,tmpy+1);
// printf("check %d %d %d %d\n",cdy,wzy,zjz,ghj);
ans=ans-cdy+wzy+zjz-ghj;
if(tmpx<tmpy) ans--;
else if(tmpx>tmpy) ans++;
add(x,tmpx,-1);add(y,tmpy,-1);
swap(num[x],num[y]);
add(x,tmpy,1);add(y,tmpx,1);
printf("%lld\n",ans);
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
HEOI 2019 RP++
P1975 [国家集训队]排队的更多相关文章
- P1975 [国家集训队]排队 线段树套平衡树维护动态逆序对查询
$ \color{#0066ff}{ 题目描述 }$ 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和. 红星幼儿园的小朋友们排起了长长地队伍 ...
- 洛谷 P1975 [国家集训队]排队 Lebal:块内排序+树状数组
题目描述 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和. 红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的身高有所区别 ...
- 「Luogu P1975 [国家集训队]排队」
题目大意 给出一个序列 \(h\),支持交换其中的两数,求出每一时刻的逆序对个数. 分析 求逆序对是 \(O(N\log_2N)\) 的,有 \(M\) 个操作,如果暴力求的话时间复杂度就是 \(O( ...
- 【LG1975】[国家集训队]排队
[LG1975][国家集训队]排队 题面 洛谷 题解 又是一个偏序问题 显然\(CDQ\) 交换操作不好弄怎么办? 可以看成两次删除两次插入 排序问题要注意一下 代码 #include <ios ...
- Luogu-1975 [国家集训队]排队
Luogu-1975 [国家集训队]排队 题面 Luogu-1975 题解 题意:给出一个长度为n的数列以及m个交换两个数的操作,问每次操作后逆序对数量 时间,下标和数的大小三维偏序,,,把交换操作看 ...
- [国家集训队]排队 [cdq分治]
题面 洛谷 和动态逆序对那道题没有什么区别 把一个交换换成两个删除和两个插入 #include <cstdio> #include <cstdlib> #include < ...
- luogu1975 [国家集训队]排队
思路 序列中 |i | 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| |----|--|--|--|--|--|--|--|--|--|--| |a[i]| a| b| c| L| d ...
- [luoguP1975] [国家集训队]排队(分块)
传送门 直接暴力分块,然后在每一个块内排序. 查询时可以在每一个块内二分. #include <cmath> #include <cstdio> #include <io ...
- BZOJ 2039: [2009国家集训队]employ人员雇佣
2039: [2009国家集训队]employ人员雇佣 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1369 Solved: 667[Submit ...
随机推荐
- [Training Video - 1] [Selenium Basics] [What is Selenium]
What is Selenium? Browser Automation Testings Tool: Mozilla IE 6,7,8 Google Chrome Opera 8,9,10 Safa ...
- c11时间库一个小例子
#pragma once #include <chrono> #include <string> #include <iostream> #include < ...
- [转]go中的main函数和init函数
Go里面有两个保留的函数:init函数(能够应用于所有的package)和main函数(只能应用于package main).这两个函数在定义时不能有任何的参数和返回值.虽然一个package里面可以 ...
- redis集群种类(转)
原文:http://blog.csdn.net/c295477887/article/details/52487621 关于redis主从.哨兵.集群的介绍网上很多,这里就不赘述了. 一.主从 通过持 ...
- UVa 1606 Amphiphilic Carbon Molecules (扫描法+极角排序)
题意:平面上有 n 个点,每个点不是黑的就是白的,现在要放一个隔板,把它们分成两部分,使得一侧的白点数加上另一侧的黑点数最多. 析:这个题很容易想到的就是暴力,不妨假设隔板至少经过两个点,即使不经过也 ...
- tp5和gateworer集成
第一步:安装thinkpph程序包 第二步:切换到根目录,使用composer require workerman/gateway-worker 安装Linux版本的gateway.(前提是你服务器安 ...
- Python之模块和包学习
模块简介 python是由一系列的模块组成的,每个模块就是一个py为后缀的文件,同时模块也是一个命名空间,从而避免了变量名称冲突的问题.模块我们就可以理解为lib库,如果需要使用某个模块中的函数或对象 ...
- Linux 基础教程 37-进程命令
pidof 我们知道每个小孩一出生就会一个全国唯一的编号来对其进行标识,用于以后上学,办社保等,就是我们的身份证号.那么在Linux系统中,用来管理运行程序的标识叫做PID,就是大家熟知的进程 ...
- ZOJ1648 Circuit Board 2017-04-18 20:31 34人阅读 评论(0) 收藏
Circuit Board Time Limit: 2 Seconds Memory Limit: 65536 KB On the circuit board, there are lots ...
- java并发编程实战:第十二章---并发程序的测试
并发程序中潜在错误的发生并不具有确定性,而是随机的. 安全性测试:通常会采用测试不变性条件的形式,即判断某个类的行为是否与其规范保持一致 活跃性测试:进展测试和无进展测试两方面,这些都是很难量化的(性 ...