【题目链接】: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实现;



【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define ps push_back
  10. #define fi first
  11. #define se second
  12. #define rei(x) scanf("%d",&x)
  13. #define rel(x) scanf("%lld",&x)
  14. #define ref(x) scanf("%lf",&x)
  15. typedef pair<int, int> pii;
  16. typedef pair<LL, LL> pll;
  17. const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
  18. const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
  19. const double pi = acos(-1.0);
  20. const int K = 500+20;//500个块
  21. const int N = 2e5 + 100;
  22. int n,q,belong[N],l[K],r[K],a[N];
  23. LL ans = 0;
  24. vector <int> v[K];
  25. void init()
  26. {
  27. int len = sqrt(n);
  28. if ((len*len) != n)
  29. {
  30. len++;
  31. }
  32. rep1(i, 1, n)
  33. {
  34. belong[i] = ((i - 1) / len) + 1;
  35. v[belong[i]].ps(i);
  36. a[i] = i;
  37. }
  38. rep1(i, 1, len)
  39. {
  40. l[i] = len*(i - 1) + 1;
  41. r[i] = len*i;
  42. }
  43. if ((len*len) != n)
  44. r[len] = n;
  45. }
  46. LL smaller(int L, int R, LL x)
  47. {
  48. if (L > R) return 0;
  49. LL cnt = 0;
  50. if (belong[L] == belong[R])
  51. {
  52. rep1(i, L, R)
  53. {
  54. if (a[i] < x)
  55. cnt++;
  56. }
  57. return cnt;
  58. }
  59. rep1(i, L, r[belong[L]])
  60. {
  61. if (a[i] < x)
  62. cnt++;
  63. }
  64. rep1(i, belong[L] + 1, belong[R] - 1)
  65. {
  66. LL pos = lower_bound(v[i].begin(), v[i].end(), x) - v[i].begin()+1;
  67. cnt += pos;
  68. }
  69. rep1(i, l[belong[R]], R)
  70. if (a[i] < x)
  71. cnt++;
  72. return cnt;
  73. }
  74. void change(int l, int r)
  75. {
  76. int x = belong[l],y = belong[r];
  77. v[x].erase(lower_bound(v[x].begin(), v[x].end(), a[l]));
  78. v[x].insert(upper_bound(v[x].begin(), v[x].end(), a[r]),a[r]);
  79. v[y].erase(lower_bound(v[y].begin(), v[y].end(), a[r]));
  80. v[y].insert(upper_bound(v[y].begin(), v[y].end(), a[l]), a[l]);
  81. swap(a[l], a[r]);
  82. }
  83. int main()
  84. {
  85. //freopen("F:\\rush.txt", "r", stdin);
  86. rei(n), rei(q);
  87. init();
  88. rep1(i, 1, q)
  89. {
  90. int l, r;
  91. rei(l), rei(r);
  92. if (l > r) swap(l, r);
  93. if (l == r)
  94. {
  95. printf("%lld\n", ans);
  96. continue;
  97. }
  98. LL temp = smaller(l + 1, r - 1, a[l]);
  99. LL temp1 = r - 1 - (l + 1) + 1 - temp;
  100. //temp个数比a[l]小 temp1个数比a[l]大
  101. //之后a[l]会跑到r位置
  102. ans -= temp; ans += temp1;
  103. temp = smaller(l + 1, r - 1, a[r]);
  104. temp1 = r - 1 - (l + 1) + 1 - temp;
  105. //temp个数比a[r]小 temp1个数比a[r]大
  106. //之后a[r]会跑到l位置
  107. ans += temp; ans -= temp1;
  108. if (a[l] < a[r])
  109. ans++;
  110. else
  111. ans--;
  112. change(l, r);
  113. printf("%lld\n", ans);
  114. }
  115. //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
  116. return 0;
  117. }

【codeforces 785E】Anton and Permutation的更多相关文章

  1. 【25.00%】【codeforces 584E】Anton and Ira

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【29.89%】【codeforces 734D】Anton and Chess

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【13.77%】【codeforces 734C】Anton and Making Potions

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【81.37%】【codeforces 734B】Anton and Digits

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【77.39%】【codeforces 734A】Anton and Danik

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【codeforces 508B】Anton and currency you all know

    [题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...

  8. 【codeforces 785D】Anton and School - 2

    [题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...

  9. 【codeforces 734F】Anton and School

    [题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...

随机推荐

  1. C# 获得资源文件下图片的路径

    最终实现正确的代码是: button8.Image = System.Drawing.Image.FromFile(@"..\\..\\Resources\\GAOJIBAN.png&quo ...

  2. GoLang笔记-数组和切片,本质是就是长度不可变的可变的区别

    数组 Arrays 数组是内置(build-in)类型,是一组同类型数据的集合,它是值类型,通过从0开始的下标索引访问元素值.在初始化后长度是固定的,无法修改其长度.当作为方法的入参传入时将复制一份数 ...

  3. codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  4. [luogu_U15116]珈百璃堕落的开始

    https://www.zybuluo.com/ysner/note/1239458 题面 给定\(n\)个二元组\((x,y)\),问有多少种方案,使得选出其中几个后,\(\sum x=\sum y ...

  5. Centos搭建图形界面VNC

    #!/bin/sh unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS exec /etc/X11/xinit/xinitrc

  6. [Swift通天遁地]二、表格表单-(6)创建美观的表格弹性下拉刷新效果

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. Vue.js中学习使用Vuex详解

    在SPA单页面组件的开发中 Vue的vuex和React的Redux 都统称为同一状态管理,个人的理解是全局状态管理更合适:简单的理解就是你在state中定义了一个数据之后,你可以在所在项目中的任何一 ...

  8. Vue使用html2canvas将页面转化为图片

    需求是微信端将页面截屏之后保存到本地,使用了html2canvas插件 先引入插件 npm install --save html2canvas 之后在你所需要使用的页面引入 import html2 ...

  9. Testng1

    Testng 简介: Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性: annotations  注释,如 @test ...

  10. ACM_题目这么难,来局愉快的昆特牌吧

    题目这么难,来局愉快的昆特牌吧 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小Z打比赛,然而比赛太难了,他坐在电脑面前被题淹没 ...