Submission #777115

# Submission time Handle Problem Language Result Execution time Memory
777115 2023-07-08T16:53:35 Z ind1v Addk (eJOI21_addk) C++11
100 / 100
86 ms 8516 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 100005;
const int K = 15;

struct fenwick_tree {
  int64_t fenw[N];
  
  fenwick_tree() {
    memset(fenw, 0, sizeof(fenw));
  }
  
  void upd(int idx, long long val) {
    for (; idx < N; idx |= (idx + 1)) {
      fenw[idx] += val;
    }
  }
  
  int64_t get(int idx) {
    int64_t res = 0;
    for (; idx >= 0; idx &= (idx + 1), --idx) {
      res += fenw[idx];
    }
    return res;
  }
  
  int64_t get(int l, int r) {
    return get(r) - get(l - 1);
  }
};

int n, k;
int a[N];
int64_t p[N];
int idx[K];
int q;
fenwick_tree ft1, ft2;

void upd(int idx, int64_t val) {
  ft1.upd(idx, val);
  ft2.upd(idx, 1LL * idx * val);
}

int64_t get(int idx) {
  return 1LL * (idx + 1) * ft1.get(idx) - ft2.get(idx);
}

int64_t get(int l, int r) {
  return get(r) - get(l - 1);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    p[i] = a[i] + p[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    upd(i, p[i] - p[i - 1]);
  }
  cin >> q;
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      for (int i = 1; i <= k; i++) {
        cin >> idx[i]; 
      }
      idx[k + 1] = idx[1];
      for (int i = 1; i <= k; i++) {
        upd(idx[i], a[idx[i + 1]] - a[idx[i]]);
      }
      int tmp = a[idx[1]];
      for (int i = 1; i <= k - 1; i++) {
        a[idx[i]] = a[idx[i + 1]];
      }
      a[idx[k]] = tmp;
    } else if (t == 2) {
      int l, r, m;
      cin >> l >> r >> m;
      cout << get(min(l + m - 1, r), r) - get(min(l + m - 1, r) - m, r - m) << '\n';
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1876 KB Output is correct
2 Correct 2 ms 1932 KB Output is correct
3 Correct 2 ms 1876 KB Output is correct
4 Correct 3 ms 2004 KB Output is correct
5 Correct 3 ms 2004 KB Output is correct
6 Correct 4 ms 2132 KB Output is correct
7 Correct 4 ms 2132 KB Output is correct
8 Correct 4 ms 2132 KB Output is correct
9 Correct 6 ms 2296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2676 KB Output is correct
2 Correct 17 ms 2924 KB Output is correct
3 Correct 21 ms 3140 KB Output is correct
4 Correct 40 ms 4248 KB Output is correct
5 Correct 49 ms 6756 KB Output is correct
6 Correct 46 ms 6684 KB Output is correct
7 Correct 46 ms 6596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 3148 KB Output is correct
2 Correct 51 ms 6220 KB Output is correct
3 Correct 86 ms 8516 KB Output is correct
4 Correct 52 ms 7224 KB Output is correct