답안 #475638

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
475638 2021-09-23T14:25:06 Z nickmet2004 Addk (eJOI21_addk) C++11
100 / 100
244 ms 8696 KB
#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
using namespace std;
 
const int N = 1e6 + 5;
int n , k , q,a[N] , Y[11] ,H[N];
pair<int, int> F[2*N];
void upd(int i , int d1 , int d2){
    while(i <= n) {
        F[i].f += d1 , F[i].s += d2;
        i += i & -i;
    }
}
pair<int , int>  get(int i){
    pair<int , int> sum = {0,0};
    while(i) {
        sum.f += F[i].f , sum.s += F[i].s;
        i -= i & -i;
    }
    return sum;
}
 
 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] , a[i]*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 = 0; i < k; ++i) H[Y[i]] = a[Y[i]];
            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]] - H[Y[i]] , (a[Y[i]] - H[Y[i]])*Y[i]);
 
        }else {
            int l , r , m;
            cin >> l >> r >> m;
            m = min(m , r - l - m + 2);
            int ans = 0;
            pair<int,int> gR = get(l + m - 1);
            pair<int,int> gL = get(l - 1);
            ans += (gR.s - gL.s - (l - 1) * (gR.f - gL.f));
            ans += (get(r - m).f - get(l + m - 1).f)*m;
            gL = get(r-m);
            gR = get(r);
            ans += (r+1)*(gR.f-gL.f)-(gR.s-gL.s);
            cout << ans << endl;
        }
    }
}

Compilation message

Main.cpp:25:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   25 |  main (){
      |  ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 4 ms 332 KB Output is correct
4 Correct 9 ms 460 KB Output is correct
5 Correct 8 ms 588 KB Output is correct
6 Correct 9 ms 588 KB Output is correct
7 Correct 11 ms 676 KB Output is correct
8 Correct 15 ms 744 KB Output is correct
9 Correct 22 ms 844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 1440 KB Output is correct
2 Correct 59 ms 1916 KB Output is correct
3 Correct 74 ms 2300 KB Output is correct
4 Correct 141 ms 4052 KB Output is correct
5 Correct 244 ms 7408 KB Output is correct
6 Correct 179 ms 7108 KB Output is correct
7 Correct 182 ms 7128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 107 ms 2628 KB Output is correct
2 Correct 164 ms 6268 KB Output is correct
3 Correct 175 ms 8696 KB Output is correct
4 Correct 204 ms 7664 KB Output is correct