Submission #475107

# Submission time Handle Problem Language Result Execution time Memory
475107 2021-09-21T08:08:47 Z nickmet2004 Addk (eJOI21_addk) C++11
100 / 100
460 ms 10628 KB
#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e6 + 5;
int n , k , q,a[N] , Y[11];
struct Node{
    int F , S;
    Node operator+(Node b){
        Node ret;
        ret.F = F + b.F;
        ret.S = S + b.S;
        return ret;
    }
}T[4*N];
void upd(int id , int d , int l = 1 , int r = n , int pos = 1){
    if(id < l || id > r) return;
    if(l==r){T[pos].F = d , T[pos].S = d*id; return;}
    int mid = (l + r) >> 1;
    upd(id , d , l , mid , pos *2 + 1); upd(id , d , mid + 1 , r , pos * 2 + 2);
    T[pos] = T[pos * 2 + 1] + T[pos * 2 + 2];
}
Node get(int L , int R , int l = 1 , int r = n , int pos = 1){
    if(r < L || R < l) return {0 ,0};
    if(L<=l&&r<=R) return T[pos];
    int mid = (l + r) >> 1;
    return get(L , R , l , mid , pos * 2 + 1) + get(L , R ,mid + 1 , r , pos * 2 + 2);
}

 main (){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> k;
    for(int i = 1; i <= n; ++i)cin >> a[i];
    for(int i = 1; i<= n; ++i) upd(i , a[i]);
    cin >> q;

    while(q--){
        int tp;
        cin >> tp;
        if(tp==1){
            for(int i = 0; i < k; ++i)cin >> Y[i];
            sort(Y , Y + k);
            for(int i = 1; i < k; ++i) swap(a[Y[i]] , a[Y[i - 1]]);
            for(int i = 0; i < k; ++i)upd(Y[i] , a[Y[i]]);
        }else {
            int l , r , m;
            cin >> l >> r >> m;
            m = min(m , r - l - m + 2);
            int ans = 0;
            Node gR = get(0 , l + m - 1);
            Node gL = get(0 , l - 1);
            ans += (gR.S - gL.S - (l - 1) * (gR.F - gL.F));
            ans += (get(0 , r - m).F - get(0 , l + m - 1).F)*m;
            gL = get(0,r-m);
            gR = get(0,r);
            ans += (r+1)*(gR.F-gL.F)-(gR.S-gL.S);
            cout << ans << endl;
        }
    }
}

Compilation message

Main.cpp:30:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   30 |  main (){
      |  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 3 ms 400 KB Output is correct
3 Correct 6 ms 460 KB Output is correct
4 Correct 10 ms 476 KB Output is correct
5 Correct 13 ms 632 KB Output is correct
6 Correct 16 ms 716 KB Output is correct
7 Correct 21 ms 816 KB Output is correct
8 Correct 22 ms 800 KB Output is correct
9 Correct 32 ms 1184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 2004 KB Output is correct
2 Correct 107 ms 2420 KB Output is correct
3 Correct 148 ms 4036 KB Output is correct
4 Correct 274 ms 7084 KB Output is correct
5 Correct 390 ms 8500 KB Output is correct
6 Correct 371 ms 8388 KB Output is correct
7 Correct 369 ms 8304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 199 ms 5064 KB Output is correct
2 Correct 329 ms 8592 KB Output is correct
3 Correct 460 ms 10628 KB Output is correct
4 Correct 426 ms 9460 KB Output is correct