【codeforces 785E】Anton and Permutation
【题目链接】:http://codeforces.com/problemset/problem/785/E
【题意】
给你一个初始序列1..n顺序
然后每次让你交换任意两个位置上面的数字;
让你实时输出q个操作,每个操作过后整个序列逆序对的个数;
【题解】
分块法;
分成根号n个块.
每个块按照数字升序排;
然后再用一个a数组具体记录每个位置上的数字;
找逆序对的方式如下:
对于交换l,r
查找l+1..r-1当中比a[l]小的数字,比a[l]大的数字;
查找l+1..r-1当中比a[r]小…比..大的数字;
根据这4个参数来更新逆序对的个数;
这就要求查询l+1..r-1当中比x小的数的个数;
对于belong[l]块,->belong[i]指的是第i个位置上面的数字对应的是第几个块
在l..R[belong[l]]当中找比x小的数->暴力枚举就好
在L[belong[r]]..r当中找比x小的数->也是暴力枚举;
然后在belong[l]+1..belong[r]-1这些块中,用二分(lower_bound)查找比x小的数的个数(因为是有序的,所以可以这样做);
然后如果a[l]< a[r]则再多递增一个逆序对否则减少1个
然后交换这两个数;
->有序的vector中交换,然后原数组a[i]中也要交换;
vector中的交换也可以用lower_bound实现;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int K = 500+20;//500个块
const int N = 2e5 + 100;
int n,q,belong[N],l[K],r[K],a[N];
LL ans = 0;
vector <int> v[K];
void init()
{
int len = sqrt(n);
if ((len*len) != n)
{
len++;
}
rep1(i, 1, n)
{
belong[i] = ((i - 1) / len) + 1;
v[belong[i]].ps(i);
a[i] = i;
}
rep1(i, 1, len)
{
l[i] = len*(i - 1) + 1;
r[i] = len*i;
}
if ((len*len) != n)
r[len] = n;
}
LL smaller(int L, int R, LL x)
{
if (L > R) return 0;
LL cnt = 0;
if (belong[L] == belong[R])
{
rep1(i, L, R)
{
if (a[i] < x)
cnt++;
}
return cnt;
}
rep1(i, L, r[belong[L]])
{
if (a[i] < x)
cnt++;
}
rep1(i, belong[L] + 1, belong[R] - 1)
{
LL pos = lower_bound(v[i].begin(), v[i].end(), x) - v[i].begin()+1;
cnt += pos;
}
rep1(i, l[belong[R]], R)
if (a[i] < x)
cnt++;
return cnt;
}
void change(int l, int r)
{
int x = belong[l],y = belong[r];
v[x].erase(lower_bound(v[x].begin(), v[x].end(), a[l]));
v[x].insert(upper_bound(v[x].begin(), v[x].end(), a[r]),a[r]);
v[y].erase(lower_bound(v[y].begin(), v[y].end(), a[r]));
v[y].insert(upper_bound(v[y].begin(), v[y].end(), a[l]), a[l]);
swap(a[l], a[r]);
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(n), rei(q);
init();
rep1(i, 1, q)
{
int l, r;
rei(l), rei(r);
if (l > r) swap(l, r);
if (l == r)
{
printf("%lld\n", ans);
continue;
}
LL temp = smaller(l + 1, r - 1, a[l]);
LL temp1 = r - 1 - (l + 1) + 1 - temp;
//temp个数比a[l]小 temp1个数比a[l]大
//之后a[l]会跑到r位置
ans -= temp; ans += temp1;
temp = smaller(l + 1, r - 1, a[r]);
temp1 = r - 1 - (l + 1) + 1 - temp;
//temp个数比a[r]小 temp1个数比a[r]大
//之后a[r]会跑到l位置
ans += temp; ans -= temp1;
if (a[l] < a[r])
ans++;
else
ans--;
change(l, r);
printf("%lld\n", ans);
}
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 785E】Anton and Permutation的更多相关文章
- 【25.00%】【codeforces 584E】Anton and Ira
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【13.77%】【codeforces 734C】Anton and Making Potions
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【81.37%】【codeforces 734B】Anton and Digits
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【77.39%】【codeforces 734A】Anton and Danik
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 508B】Anton and currency you all know
[题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...
- 【codeforces 785D】Anton and School - 2
[题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...
- 【codeforces 734F】Anton and School
[题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...
随机推荐
- MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式。
原文链接:https://www.muhanxue.com/essays/2015/01/8623699.html MVC web api 返回JSON的几种方式 1.在WebApiConfig的Re ...
- 9.12NOIP模拟题
NOIP 2017 全假模拟冲刺 hkd 题目名称 Spfa 走楼梯缩小版 滑稽 题目类型 传统 传统 传统 ...
- AirtestIDE详解(跨平台的UI自动化编辑器)
Airtest 是网易出品的一款基于图像识别和poco控件识别的一款UI自动化测试工具. AirtestIDE 是一个跨平台.多端(Windows.web.android.ios.游戏)的UI自动化测 ...
- akka设计模式系列-慎用ask
慎用ask应该是Akka设计的一个准则,很多时候我们应该禁用ask.之所以单独把ask拎出来作为一篇博文,主要是akka的初学者往往对ask的使用比较疑惑. "Using ask will ...
- Java初级进阶中高级工程师必备技能
很多人学了javase以为自己学的已经很OK了,但是其实javase里边有很多的知识点是你不知道的,不管你找的是哪里的javase的视频,大多数是不会讲这些东西,而这些东西你平时业务又不会主动去接触, ...
- 数据库部署到linux服务器,供本地访问。
1. 将本地的sql文件上传至服务器 scp /Users/fangke/Documents/article.sql root@IP:/usr/local 2. 登陆服务器的mysql 3. 创建数 ...
- mysql触发器的操作
一.创建触发器 1.创建有一条执行语句的触发器 CREATE TRIGGER trigger_name BEFORE|AFTER trigger_EVENT(INSERT|DELETE|UPDATE) ...
- MVC系列学习(十)-生成URL与表单
本次学习,在路由配置信息中,有两个路由规则,在网站第一次启动的时候,注册了两个路由表 1.动态生成url A.在路由规则中,因为Default在前面,所以最新找到该路由表,此时不管 自己定义的控制器名 ...
- Java 中 父类变量访问子类方法 需要使用 类型转换 (instenceof)关键字 /类型判断/
通过数组元素访问方法的时候只能访问在 Animal中定义的方法,对 于 Tiger类和 Fish中定义的方法时却不能调用,例如语句 animal[2].swim();就是不正确的.当 需要访问这些 ...
- Android FileProvider相关 Failed to find configured root that contains
问题: 使用FileProvider构造SD卡中文件uri时异常 java.lang.IllegalArgumentException: Failed to find configured root ...