fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define x first
  6. #define y second
  7.  
  8. const int MOD = 5e6, maxn = 1e5;
  9.  
  10. vector<int> sum;
  11.  
  12. void merge_sort(vector<pair<int, int> > &to_sort, vector<int> &inv, vector<int> &res, int l, int r)
  13. {
  14. if(r - l == 1)
  15. return;
  16. int m = (l + r) / 2;
  17. merge_sort(to_sort, inv, res, l, m);
  18. merge_sort(to_sort, inv, res, m, r);
  19. vector<pair<int, int> > tmp;
  20. tmp.reserve(r - l + 1);
  21. int it1 = l, it2 = m;
  22. int cnt = 0;
  23. while(it1 < m || it2 < r)
  24. {
  25. if(it2 == r || (it1 != m && to_sort[it1].x < to_sort[it2].x))
  26. {
  27. tmp.push_back(to_sort[it1]);
  28. cnt += inv[to_sort[it1].y];
  29. if(cnt > MOD) cnt -= MOD;
  30. it1++;
  31. }
  32. else
  33. {
  34. tmp.push_back(to_sort[it2]);
  35. res[to_sort[it2].y] += cnt;
  36. if(res[to_sort[it2].y] > MOD) res[to_sort[it2].y] -= MOD;
  37. it2++;
  38. }
  39. }
  40. for(int i = l; i < r; i++)
  41. to_sort[i] = tmp[i - l];
  42.  
  43. }
  44.  
  45. int main()
  46. {
  47. ios::sync_with_stdio(0);
  48. cin.tie(0);
  49. int n, k;
  50. cin >> n >> k;
  51. vector<pair<int, int> > a(n);
  52. vector<int> b(maxn + 1, 1);
  53. for(int i = 0; i < n; i++)
  54. cin >> a[i].x, a[i].y = i, a[i].x++;
  55. for(int i = 1; i < k; i++)
  56. {
  57. vector<pair<int, int> > ta(a);
  58. vector<int> tb(maxn + 1, 0);
  59. merge_sort(ta, b, tb, 0, n);
  60. b = tb;
  61. }
  62. int cnt = 0;
  63. for(int i = 0; i < n; i++)
  64. {
  65. cnt += b[i];
  66. if(cnt > MOD) cnt -= MOD;
  67. }
  68. cout << cnt;
  69. return 0;
  70. }
  71.  
Runtime error #stdin #stdout #stderr 0s 4460KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc