fork download
  1.  
  2. struct fenwik {
  3. vector<int> t;
  4. int allsum;
  5.  
  6. fenwik() {}
  7. fenwik(int n) {
  8. t = vector<int>(n, 0);
  9. allsum = 0;
  10. }
  11. int as() {
  12. return allsum;
  13. }
  14. int get(int r) {
  15. r++;
  16. int ans = 0;
  17. while (r > 0) {
  18. ans += t[r - 1];
  19. r -= r & -r;
  20. }
  21. return ans;
  22. }
  23. void add(int pos) {
  24. allsum++;
  25. pos++;
  26. while (pos <= t.size()) {
  27. t[pos - 1]++;
  28. pos += pos & -pos;
  29. }
  30. }
  31. };
  32.  
  33. vector<int> tree[4 * maxn];
  34. fenwik bit[4 * maxn];
  35.  
  36. int pos[maxn], a[maxn];
  37.  
  38. void build(int v, int tl, int tr) {
  39. bit[v] = fenwik(tr - tl + 1);
  40. if (tl == tr) {
  41. tree[v].pb(pos[tl]);
  42. return;
  43. }
  44. int tm = (tl + tr) >> 1;
  45. build(v<<1, tl, tm);
  46. build(v<<1|1, tm + 1, tr);
  47. merge(all(tree[v<<1]), all(tree[v<<1|1]), back_inserter(tree[v]));
  48. }
  49.  
  50. void upd(int v, int tl, int tr, int pos) {
  51. int cur_it = lower_bound(all(tree[v]), ::pos[pos]) - tree[v].begin();
  52. bit[v].add(cur_it);
  53.  
  54. if (tl == tr)
  55. return;
  56. int tm = (tl + tr) >> 1;
  57. if (pos <= tm)
  58. upd(v<<1, tl, tm, pos);
  59. else
  60. upd(v<<1|1, tm + 1, tr, pos);
  61. }
  62.  
  63. int get(int v, int tl, int tr, int l, int r, int x) {
  64. if (l > r)
  65. return 0;
  66. if (tl == l && tr == r) {
  67. int cur_it = upper_bound(all(tree[v]), x) - tree[v].begin() - 1;
  68. return bit[v].as() - bit[v].get(cur_it);
  69. }
  70. int tm = (tl + tr) >> 1;
  71. return get(v<<1 , tl, tm, l, min(r, tm), x) +
  72. get(v<<1|1, tm + 1, tr, max(l, tm + 1), r, x);
  73. }
  74.  
  75. int main() {
  76. files;
  77. // freopen("input.txt", "r", stdin);
  78. int n, k;
  79. read(n, k);
  80.  
  81. for (int i = 0; i < n; i++) {
  82. read(a[i]);
  83. a[i]--;
  84. }
  85. for (int i = 0; i < n; i++) {
  86. int x;
  87. read(x);
  88. x--;
  89.  
  90. pos[x] = i;
  91. }
  92. build(1, 0, n - 1);
  93.  
  94. ll ans = 0;
  95. for (int i = 0; i < n; i++) {
  96. int x = a[i];
  97. int res = 0;
  98.  
  99. int r = x - k - 1;
  100. res += get(1, 0, n - 1, 0, r, pos[x]);
  101.  
  102. int l = x + k + 1;
  103. res += get(1, 0, n - 1, l, n - 1, pos[x]);
  104.  
  105. ans += res;
  106. upd(1, 0, n - 1, x);
  107. }
  108. cout << ans;
  109. return 0;
  110. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:5: error: ‘vector’ does not name a type
     vector<int> t;
     ^~~~~~
prog.cpp: In constructor ‘fenwik::fenwik(int)’:
prog.cpp:8:9: error: ‘t’ was not declared in this scope
         t = vector<int>(n, 0);
         ^
prog.cpp:8:13: error: ‘vector’ was not declared in this scope
         t = vector<int>(n, 0);
             ^~~~~~
prog.cpp:8:20: error: expected primary-expression before ‘int’
         t = vector<int>(n, 0);
                    ^~~
prog.cpp: In member function ‘int fenwik::get(int)’:
prog.cpp:18:20: error: ‘t’ was not declared in this scope
             ans += t[r - 1];
                    ^
prog.cpp: In member function ‘void fenwik::add(int)’:
prog.cpp:26:23: error: ‘t’ was not declared in this scope
         while (pos <= t.size()) {
                       ^
prog.cpp: At global scope:
prog.cpp:33:1: error: ‘vector’ does not name a type
 vector<int> tree[4 * maxn];
 ^~~~~~
prog.cpp:34:16: error: ‘maxn’ was not declared in this scope
 fenwik bit[4 * maxn];
                ^~~~
prog.cpp:36:9: error: ‘maxn’ was not declared in this scope
 int pos[maxn], a[maxn];
         ^~~~
prog.cpp:36:18: error: ‘maxn’ was not declared in this scope
 int pos[maxn], a[maxn];
                  ^~~~
prog.cpp: In function ‘void build(int, int, int)’:
prog.cpp:39:5: error: ‘bit’ was not declared in this scope
     bit[v] = fenwik(tr - tl + 1);
     ^~~
prog.cpp:41:9: error: ‘tree’ was not declared in this scope
         tree[v].pb(pos[tl]);
         ^~~~
prog.cpp:41:20: error: ‘pos’ was not declared in this scope
         tree[v].pb(pos[tl]);
                    ^~~
prog.cpp:47:15: error: ‘tree’ was not declared in this scope
     merge(all(tree[v<<1]), all(tree[v<<1|1]), back_inserter(tree[v]));
               ^~~~
prog.cpp:47:25: error: ‘all’ was not declared in this scope
     merge(all(tree[v<<1]), all(tree[v<<1|1]), back_inserter(tree[v]));
                         ^
prog.cpp:47:68: error: ‘back_inserter’ was not declared in this scope
     merge(all(tree[v<<1]), all(tree[v<<1|1]), back_inserter(tree[v]));
                                                                    ^
prog.cpp:47:69: error: ‘merge’ was not declared in this scope
     merge(all(tree[v<<1]), all(tree[v<<1|1]), back_inserter(tree[v]));
                                                                     ^
prog.cpp: In function ‘void upd(int, int, int, int)’:
prog.cpp:51:34: error: ‘tree’ was not declared in this scope
     int cur_it = lower_bound(all(tree[v]), ::pos[pos]) - tree[v].begin();
                                  ^~~~
prog.cpp:51:41: error: ‘all’ was not declared in this scope
     int cur_it = lower_bound(all(tree[v]), ::pos[pos]) - tree[v].begin();
                                         ^
prog.cpp:51:44: error: ‘::pos’ has not been declared
     int cur_it = lower_bound(all(tree[v]), ::pos[pos]) - tree[v].begin();
                                            ^~
prog.cpp:51:54: error: ‘lower_bound’ was not declared in this scope
     int cur_it = lower_bound(all(tree[v]), ::pos[pos]) - tree[v].begin();
                                                      ^
prog.cpp:52:5: error: ‘bit’ was not declared in this scope
     bit[v].add(cur_it);
     ^~~
prog.cpp: In function ‘int get(int, int, int, int, int, int)’:
prog.cpp:67:38: error: ‘tree’ was not declared in this scope
         int cur_it = upper_bound(all(tree[v]), x) - tree[v].begin() - 1;
                                      ^~~~
prog.cpp:67:45: error: ‘all’ was not declared in this scope
         int cur_it = upper_bound(all(tree[v]), x) - tree[v].begin() - 1;
                                             ^
prog.cpp:67:49: error: ‘upper_bound’ was not declared in this scope
         int cur_it = upper_bound(all(tree[v]), x) - tree[v].begin() - 1;
                                                 ^
prog.cpp:68:16: error: ‘bit’ was not declared in this scope
         return bit[v].as() - bit[v].get(cur_it);
                ^~~
prog.cpp:71:44: error: ‘min’ was not declared in this scope
     return get(v<<1  , tl, tm, l, min(r, tm), x) +
                                            ^
prog.cpp:72:49: error: ‘max’ was not declared in this scope
            get(v<<1|1, tm + 1, tr, max(l, tm + 1), r, x);
                                                 ^
prog.cpp: In function ‘int main()’:
prog.cpp:76:5: error: ‘files’ was not declared in this scope
     files;
     ^~~~~
prog.cpp:79:14: error: ‘read’ was not declared in this scope
     read(n, k);
              ^
prog.cpp:82:14: error: ‘a’ was not declared in this scope
         read(a[i]);
              ^
prog.cpp:90:9: error: ‘pos’ was not declared in this scope
         pos[x] = i;
         ^~~
prog.cpp:94:5: error: ‘ll’ was not declared in this scope
     ll ans = 0;
     ^~
prog.cpp:96:17: error: ‘a’ was not declared in this scope
         int x = a[i];
                 ^
prog.cpp:100:39: error: ‘pos’ was not declared in this scope
         res += get(1, 0, n - 1, 0, r, pos[x]);
                                       ^~~
prog.cpp:105:9: error: ‘ans’ was not declared in this scope
         ans += res;
         ^~~
prog.cpp:108:5: error: ‘cout’ was not declared in this scope
     cout << ans;
     ^~~~
prog.cpp:108:13: error: ‘ans’ was not declared in this scope
     cout << ans;
             ^~~
stdout
Standard output is empty