Submission #786817

# Submission time Handle Problem Language Result Execution time Memory
786817 2023-07-18T13:10:19 Z SalihSahin Addk (eJOI21_addk) C++14
100 / 100
370 ms 17380 KB
#include <bits/stdc++.h>
#define pb push_back
#define fastio cin.tie(0); ios_base::sync_with_stdio(false);
#define int long long
using namespace std;

const int N = 1e5 + 5;
const int mod = 1e9+7;
const int inf = 3e18 + 5;

struct SegmentTree{
  vector<int> tree;

  void init(int n){
    tree.resize(n*4);
  }

  void build(vector<int> &a, int v, int l, int r){
    if(l == r) tree[v] = a[l];
    else{
        int m = (l + r)/2;
        build(a, v*2, l, m);
        build(a, v*2+1, m+1, r);
        tree[v] = tree[v*2] + tree[v*2+1];
    }
  }

  int query(int v, int l, int r, int tl, int tr){
    if(l > r || r < tl || l > tr) return 0;
    if(tl <= l && tr >= r) return tree[v];

    int m = (l + r)/2;
    return query(v*2, l, m, tl, tr) + query(v*2+1, m+1, r, tl, tr);
  }

  void update(int v, int pos, int value, int l, int r){
    if(l == r) tree[v] = value;
    else{
        int m = (l+r)/2;

        if(pos <= m) update(v*2, pos, value, l, m);
        else update(v*2+1, pos, value, m+1, r);

        tree[v] = tree[v*2] + tree[v*2+1];
    }
  }
};

int32_t main(){
  fastio;
  int n, k;
  cin>>n>>k;
  vector<int> a(n), la(n), ra(n);
  for(int i = 0; i < n; i++){
    cin>>a[i];
    la[i] = a[i] * (i + 1);
    ra[i] = a[i] * (n - i);
  }

  SegmentTree sum, pre, suf;
  sum.init(n);
  sum.build(a, 1, 0, n-1);
  pre.init(n);
  pre.build(la, 1, 0, n-1);
  suf.init(n);
  suf.build(ra, 1, 0, n-1);

  int q;
  cin>>q;
  while(q--){
    int type;
    cin>>type;

    if(type == 1){ // left shift
        vector<int> ind(k);
        for(int i = 0; i < k; i++){
            cin>>ind[i];
            ind[i]--;
        }

        int temp = a[ind[0]];
        for(int i = 0; i < k-1; i++){
            a[ind[i]] = a[ind[i+1]];
        }
        a[ind[k-1]] = temp;
        for(int i = 0; i < k; i++){
            la[ind[i]] = a[ind[i]] * (ind[i] + 1);
            ra[ind[i]] = a[ind[i]] * (n - ind[i]);
        }

        for(int i = 0; i < k; i++){
            sum.update(1, ind[i], a[ind[i]], 0, n-1);
            pre.update(1, ind[i], la[ind[i]], 0, n-1);
            suf.update(1, ind[i], ra[ind[i]], 0, n-1);
        }

    }
    else{ // sum query
        int l, r, m;
        cin>>l>>r>>m;
        l--, r--;
        int mid_len = (r - l + 1) - 2*(m-1);
        int left_len = ((r - l + 1) - mid_len)/2;
        int right_len = ((r - l + 1) - mid_len)/2;

        if(mid_len >= 0){
            int mid_sum = sum.query(1, 0, n-1, l + left_len, r - right_len) * m;
            int left_sum = pre.query(1, 0, n-1, l, l + left_len - 1) - sum.query(1, 0, n-1, l, l + left_len - 1) * (l + 1 - 1);
            int right_sum = suf.query(1, 0, n-1, r - right_len + 1, r) - sum.query(1, 0, n-1, r - right_len + 1, r) * (n - r - 1);

            cout<<left_sum + mid_sum + right_sum<<endl;
            //cout<<left_len<<" "<<mid_len<<" "<<right_len<<endl;
        }
        else{
            int mval = (r - l + 1) - m + 1;
            int left_len = mval - 1;
            int mid_len = (r - l + 1) - left_len * 2;
            int right_len = mval - 1;

            int mid_sum = sum.query(1, 0, n-1, l + left_len, r - right_len) * mval;
            int left_sum = pre.query(1, 0, n-1, l, l + left_len - 1) - sum.query(1, 0, n-1, l, l + left_len - 1) * (l + 1 - 1);
            int right_sum = suf.query(1, 0, n-1, r - right_len + 1, r) - sum.query(1, 0, n-1, r - right_len + 1, r) * (n - r - 1);
            cout<<left_sum + mid_sum + right_sum<<endl;
        }
    }
  }

  return 0;
}

Compilation message

Main.cpp: In function 'int32_t main()':
Main.cpp:117:17: warning: unused variable 'mid_len' [-Wunused-variable]
  117 |             int mid_len = (r - l + 1) - left_len * 2;
      |                 ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 456 KB Output is correct
3 Correct 5 ms 588 KB Output is correct
4 Correct 7 ms 724 KB Output is correct
5 Correct 9 ms 852 KB Output is correct
6 Correct 11 ms 1108 KB Output is correct
7 Correct 13 ms 1236 KB Output is correct
8 Correct 22 ms 1356 KB Output is correct
9 Correct 22 ms 1848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 2972 KB Output is correct
2 Correct 70 ms 5000 KB Output is correct
3 Correct 100 ms 6464 KB Output is correct
4 Correct 187 ms 11308 KB Output is correct
5 Correct 258 ms 16016 KB Output is correct
6 Correct 237 ms 15760 KB Output is correct
7 Correct 275 ms 15784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 6652 KB Output is correct
2 Correct 235 ms 13168 KB Output is correct
3 Correct 370 ms 17380 KB Output is correct
4 Correct 281 ms 16220 KB Output is correct