답안 #630108

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
630108 2022-08-15T17:01:19 Z ZsofiaKeresztely Addk (eJOI21_addk) C++14
100 / 100
1246 ms 9652 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> lstairs, rstairs, pref;
ll n;

void upd(vector<ll> &a){
  pref.assign(n+1, 0);
  lstairs.assign(n+1, 0);
  rstairs.assign(n+1, 0);
  for (ll i=1; i<n+1; i++){
    pref[i] = pref[i-1] + a[i];
    lstairs[i] = lstairs[i-1] + pref[i];
  }
  rstairs[n] = a[n];
  for (ll i=n-1; i>0; i--){
    rstairs[i] = rstairs[i+1] + (pref[n] - pref[i-1]);
  }
  return;
}

int main(){
  ll k;
  cin >> n >> k;
  vector<ll> a(n+1);
  vector<ll> orig;
  for (ll i=1; i<n+1; i++){
    cin >> a[i];
  }
  orig = a;
  upd(a);
  cerr << "\n";
  ll q, type, val, l, r, m, sum, lastl, lastr, x;
  vector<ll> ind(k);
  set<ll> toupd;
  cin >> q;
  for (ll i=0; i<q; i++){
    cin >> type;
    if (type == 1){
      cin >> ind[0];
      toupd.insert(ind[0]);
      val = a[ind[0]];
      for (ll j=1; j<k; j++){
        cin >> ind[j];
        toupd.insert(ind[j]);
        a[ind[j-1]] = a[ind[j]];
      }
      a[ind[k-1]] = val;
      val = toupd.size();
      if (val * val >= n * k){
        upd(a);
        orig = a;
        toupd.clear();
      }
      cerr << "\n";
    }
    else{
      cin >> l >> r >> m;
      lastl = min(l+m-1, r-m+1);
      lastr = max(l+m-1, r-m+1);
      if (lastl == lastr){
        lastr++;
      }
      sum = rstairs[l] - rstairs[lastl+1] - (lastl - l + 1) * (pref[n] - pref[lastl])
      + lstairs[r] - lstairs[lastr-1] - (r - lastr + 1) * pref[lastr - 1]
      + (lastl - l + 1) * (pref[lastr-1] - pref[lastl]);
      for (auto i : toupd){
        if (i > lastl && i < lastr){
          x = lastl - l + 1;
        }
        else{
          x = min(i - l + 1, r - i + 1);
        }
        if (x > 0){
          sum += (a[i] - orig[i]) * x;
        }
      }
      cout << sum << "\n";
    }
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 5 ms 340 KB Output is correct
3 Correct 6 ms 408 KB Output is correct
4 Correct 12 ms 340 KB Output is correct
5 Correct 14 ms 500 KB Output is correct
6 Correct 18 ms 548 KB Output is correct
7 Correct 17 ms 608 KB Output is correct
8 Correct 25 ms 628 KB Output is correct
9 Correct 29 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 1380 KB Output is correct
2 Correct 101 ms 1876 KB Output is correct
3 Correct 161 ms 2388 KB Output is correct
4 Correct 265 ms 3996 KB Output is correct
5 Correct 445 ms 5732 KB Output is correct
6 Correct 459 ms 5520 KB Output is correct
7 Correct 424 ms 5516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 453 ms 2776 KB Output is correct
2 Correct 720 ms 7104 KB Output is correct
3 Correct 1246 ms 9652 KB Output is correct
4 Correct 764 ms 8564 KB Output is correct