Submission #1039622

# Submission time Handle Problem Language Result Execution time Memory
1039622 2024-07-31T06:04:59 Z kebine Addk (eJOI21_addk) C++17
100 / 100
234 ms 16552 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld double
#define int ll
#define usaco(fname) freopen(#fname ".in","r",stdin);freopen(#fname ".out","w",stdout);
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
// const ll INF = 1e18;
const int INF = 1e9;
const int mod = 1e9+7;
const lld PI = acos(-1.0);
int di[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dj[] = {0, 0, 1, -1, 1, -1, 1, -1};
#define debug(x) cout << #x << ": " << x << endl;
#define add(a, b) a += b, a %= mod
#define mul(a, b) ((a % mod) * (b % mod)) % mod
#define all(x) x.begin(),x.end()

struct Seg {
  vector<vector<int>> seg;
  vector<int> a;
  int n;

  Seg(vector<int> &b) {
    a = b;
    n = a.size();
    seg = vector<vector<int>>(3, vector<int>());
    for (int i = 0; i < 3; i++) seg[i] = vector<int>(n*4);
  }

  int combine(int x, int y) {
    return x + y;
  }

  void build(int idx, int l, int r) {
    if (l == r) {
      seg[0][idx] = l*a[l];
      seg[1][idx] = a[l];
      seg[2][idx] = (n-l)*a[l];
    }
    else {
      int mid = (l+r)>>1;
      build(idx*2, l, mid);
      build(idx*2+1, mid+1, r);
      for(int i = 0; i < 3; i++) seg[i][idx] = combine(seg[i][idx*2], seg[i][idx*2+1]);
    }
  }

  int query(int idx, int l, int r, int ql, int qr, int hh) {
    if (l > r || l > qr || r < ql) return 0;
    if (l >= ql && r <= qr) return seg[hh][idx];

    int mid = (l+r)>>1;
    return combine(query(idx*2, l, mid, ql, qr, hh), query(idx*2+1, mid+1,r , ql, qr, hh));
  }

  void update(int idx, int l, int r, int i, int v) {
    if (l == r) {
      seg[0][idx] = l*v;
      seg[1][idx] = v;
      seg[2][idx] = (n-l)*v;
    } else {
      int mid = (l+r)>>1;
      if (i <= mid) update(idx*2, l, mid, i, v);
      else update(idx*2+1, mid+1, r, i, v);

      for(int i = 0; i < 3; i++) seg[i][idx] = combine(seg[i][idx*2], seg[i][idx*2+1]);
    }
  }
};

void solve() {
  int n,k;cin>>n>>k;
  vector<int> a(n+1);
  for (int i = 1; i <= n; i++) cin >> a[i];

  Seg seg(a);
  seg.build(1, 1, n);
  // debug("a");

  function<int(int, int)> sum = [&](int l, int r) {
    if (r < l) return 0ll;
    return seg.query(1, 1, n, l, r, 1);
  };

  function<int(int,int)> sum_mul = [&](int l, int r) {
    return seg.query(1, 1, n, l, r, 0);
  };

  function<int(int, int)> suff_mul = [&](int l, int r) {
    return seg.query(1, 1, n, l, r, 2);
  };

  function<int(int, int)> rnge = [&](int l, int r) {
    return sum_mul(l, r) - (l-1) * sum(l, r);
  };

  function<int(int, int)> rngesuff = [&](int l, int r) {
    return suff_mul(l, r) - (n-r) * sum(l, r);
  };

  int q;cin>>q;
  vector<int> b(k), c(k);
  while (q--) {
    int t;cin>>t;
    if (t == 1) {
      for (int i = 0; i < k; i++) {
        cin >> b[i];
        c[i] = a[b[i]];
      }

      // debug(c);

      int frnt = c.front();
      c.erase(c.begin()); 
      c.push_back(frnt);
      for (int i = 0; i < k; i++) seg.update(1, 1, n, b[i], c[i]), a[b[i]] = c[i];
      // for (int i = 1; i <= n; i++) cout << seg.query(1, 1, n, i, i, 1) << " ";
      // cout << endl;
    } else {
      int l,r,m;cin>>l>>r>>m;

      int len = (r-l+1);
      int x = min(len-m+1, m)-1;
      // debug(x);
      // debug(rnge(l, l+x-1));
      // debug((x+1)*sum(l+x, r - x));
      // debug(rngesuff(r-x+1, r));

      // debug(r-x+1);
      // debug(rngesuff(r-x+1, r));

      cout << rnge(l, l+x-1) + (x+1)*sum(l+x, r - x) + rngesuff(r-x+1, r) << endl;
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0);cin.tie(0);
  int t=1;
  while(t--) solve();

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 4 ms 604 KB Output is correct
4 Correct 7 ms 876 KB Output is correct
5 Correct 8 ms 1008 KB Output is correct
6 Correct 11 ms 1160 KB Output is correct
7 Correct 13 ms 1116 KB Output is correct
8 Correct 15 ms 1372 KB Output is correct
9 Correct 26 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 3308 KB Output is correct
2 Correct 68 ms 4688 KB Output is correct
3 Correct 89 ms 6316 KB Output is correct
4 Correct 164 ms 10836 KB Output is correct
5 Correct 219 ms 15184 KB Output is correct
6 Correct 189 ms 14932 KB Output is correct
7 Correct 208 ms 15092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 8200 KB Output is correct
2 Correct 175 ms 12728 KB Output is correct
3 Correct 234 ms 16552 KB Output is correct
4 Correct 233 ms 15700 KB Output is correct