[CC-CHEFINV]Chef and Swaps
[CC-CHEFINV]Chef and Swaps
题目大意:
长度为\(n(n\le2\times10^5)\)的数列,\(q(q\le2\times10^5)\)次询问,每次问交换\(A_x\)和\(A_y\)后逆序对个数。询问互相独立。
思路:
一开始先把逆序对求好,然后用主席树计算交换对答案的影响即可。
时间复杂度\(\mathcal O((n+q)\log n)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<vector>
#include<algorithm>
inline int getint() {register char ch;while(!isdigit(ch=getchar()));register int x=ch^'0';while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');return x;
}
typedef long long int64;
const int N=2e5+1,logN=20;
int n,m,a[N];
std::vector<int> v;
class FenwickTree {private:int val[N];int lowbit(const int &x) const {return x&-x;}public:void modify(int p) {for(;p<=m;p+=lowbit(p)) val[p]++;}int query(int p) const {int ret=0;for(;p;p-=lowbit(p)) ret+=val[p];return ret;}
};
FenwickTree bit;
class FotileTree {#define mid ((b+e)>>1)private:struct Node {int val,left,right;};Node node[N*logN];int sz,new_node(const int &p) {node[++sz]=node[p];return sz;}public: int root[N];void insert(int &p,const int &b,const int &e,const int &x) {p=new_node(p);node[p].val++;if(b==e) return;if(x<=mid) insert(node[p].left,b,mid,x);if(x>mid) insert(node[p].right,mid+1,e,x);}int query(const int &p,const int &q,const int &b,const int &e,const int &l,const int &r) const {if(b==l&&e==r) return node[q].val-node[p].val;int ret=0;if(l<=mid) ret+=query(node[p].left,node[q].left,b,mid,l,std::min(mid,r));if(r>mid) ret+=query(node[p].right,node[q].right,mid+1,e,std::max(mid+1,l),r);return ret;}#undef mid
};
FotileTree t;
int main() {n=getint();const int q=getint();for(register int i=1;i<=n;i++) {a[i]=getint();v.push_back(a[i]);}std::sort(v.begin(),v.end());v.resize(m=std::unique(v.begin(),v.end())-v.begin());for(register int i=1;i<=n;i++) {a[i]=std::lower_bound(v.begin(),v.end(),a[i])-v.begin()+1;}int64 tot=0;for(register int i=n;i>=1;i--) {tot+=bit.query(a[i]-1);bit.modify(a[i]);}for(register int i=1;i<=n;i++) {t.insert(t.root[i]=t.root[i-1],1,m,a[i]);}for(register int i=0;i<q;i++) {int x=getint(),y=getint();if(x>y) std::swap(x,y);const int tmp1=a[x]!=1?t.query(t.root[x],t.root[y],1,m,1,a[x]-1):0;const int tmp2=a[x]!=m?t.query(t.root[x],t.root[y],1,m,a[x]+1,m):0;const int tmp3=a[y]!=1?t.query(t.root[x],t.root[y],1,m,1,a[y]-1):0;const int tmp4=a[y]!=m?t.query(t.root[x],t.root[y],1,m,a[y]+1,m):0;printf("%lld\n",tot-tmp1+tmp2+tmp3-tmp4);}return 0;
}
转载于:https://www.cnblogs.com/skylee03/p/9495605.html
[CC-CHEFINV]Chef and Swaps相关推荐
- 1500: [NOI2005]维修数列 (Splay)
1W1A(inf开太大) #include<algorithm> #include<iostream> #include<cstring> #include< ...
- Two Strings Swaps(CF-1006D)
Problem Description You are given two strings a and b consisting of lowercase English letters, both ...
- Cocos Creator里cc.tween的stopAllActions() 和 repeatForever的用法
this.useNowBtn.stopAllActions(); this.useNowBtn.setPosition(46 + t * 120, 360); cc.tween(this.useNow ...
- ORB_SLAM2程序入口(System.cc)
程序入口 ORB_SLAM2的程序入口为src/System.cc.在CMakeList.txt中可知,ORB_SLAM2的可执行程序为: Examples/Stereo/stereo_kitti ...
- ORB_SLAM2源码:ORBmatcher.cc
ORBmatcher.cc中的函数,主要实现(1)路标点和特征点的匹配(2D-3D点对).(2)特征点和特征点的匹配(2D-2D点对).SearchByProjection的函数重载看得我一脸懵逼 ...
- 如何安装蓝湖插件支持Photoshop CC 2017
蓝湖 - 高效的产品设计协作平台蓝湖是一款产品文档和设计图的共享平台,帮助互联网团队更好地管理文档和设计图.蓝湖可以在线展示Axure,自动生成设计图标注,与团队共享设计图,展示页面之间的跳转关系.蓝 ...
- 从头开始学习Adobe Photoshop CC图像编辑
完整课程,涵盖所有关于Adobe Photoshop CC的基本课程 你会学到什么 课程获取:从头开始学习Adobe Photoshop CC图像编辑-云桥网 Photoshop用户界面之旅 转型导论 ...
- g++编译c++11特性 的.cc文件
写一个.cc文件,其中抱哈std::lock_guard以及std::thread等c++11特性,开始使用gcc编译,过程中出现如下问题 gcc test_lock.cc -o test_lock ...
- 统计s=hello alex alex hello haiyan cc haiyan com中每个单词的个数
这个题可以有好几种解题方法. 一.索引值获取 s="hello alex alex hello haiyan cc haiyan com" l=s.split() dic={} f ...
- 【cocos2d-js官方文档】九、cc.loader
概述 原来的cc.Loader被改造为一个单例cc.loader,采用了插件机制设计,让loader做更纯粹的事. 各种资源类型的loader可以在外部注册进来,而不是直接将所有的代码杂揉在cc.Lo ...
最新文章
- java二分法找数数_JavaSE语言基础之数组二分法查找
- Ubuntu16.04 下python2 | python3
- mysql数据库truncate 夯住_MySQL如何优雅的删除大表实例详解
- (本地源)安装CDH Manager
- 经典C语言程序100例之十九
- MM看过来!教你如何打扮变成时尚达人 - 生活至上,美容至尚!
- XML序列化和反序列化 以及相关类的写法
- Mysql-sql_mode
- Android开发之本地音乐播放器(简单粗暴版)
- AMiner推荐论文:Strongly coupled N-doped graphene quantum dots/Ni(Fe)OxHy electrocatalysts with accelerat
- 7种大屏设计与布局思路,你不知道就亏了
- skyline 查询 和 验证索引数据结构MR-tree的构建
- 看完这篇文章前千万别做微信营销
- Pb数字变成英文字母金额
- 从yield到yield from
- c#Ulong用一个高位Uint和低位Uint表示
- 产品经理VISIO操作
- NTUD3174NZT5G规格参数介绍
- Linux--如何解压分卷压缩的文件(zip,tar等等)
- Linux系统之查看进程监听端口方法
热门文章
- 20155313 杨瀚 《网络对抗技术》实验八 Web基础
- RobotFramework自动化测试框架-移动手机自动化测试Clear Text关键字的使用
- RabbitMQ八:交换机类型Exchange Types--Topic介绍
- 修改MySQL中字段的类型和长度
- CSS3 Media Queries模板
- ModelAndView简介
- 再谈Activator.CreateInstance(Type type)方法创建对象和Expression Tree创建对象性能的比较(更新版)...
- 配置远程jupyter notebook
- java 利用同步工具类控制线程
- Windows批处理(cmd/bat)常用命令小结