Submission #839617

# Submission time Handle Problem Language Result Execution time Memory
839617 2023-08-30T10:47:52 Z alishejhf Addk (eJOI21_addk) C++17
100 / 100
82 ms 7624 KB
#include<bits/stdc++.h>

#define ent '\n'
#define fi first
#define se second
#define sz(x) (int)x.size()
#define pb push_back
#define all(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int inf = (int)(1e9 + 7);
const ll INF = (ll)(2e18 + 7);
const ld eps = (ld)(1e-12);
const int dx[] = { -1,0,1,0,1,1,-1,-1 }, dy[] = { 0,1,0,-1,1,-1,1,-1 };
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
int n,k,a[N],id[N];
struct {
    ll f[N]={};
    void upd(int i,ll x){
        for(;i<N;i+=i&-i){
            f[i]+=x;
        }
    }
    ll get(int i){
        ll s=0;
        for(;i>0;i-=i&-i){
            s+=f[i];
        }
        return s;
    }
} f1,f2;
void solve(){
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        f1.upd(i,a[i]);
        f2.upd(i,a[i]*1ll*i);
    }
    int q;
    cin>>q;
    while(q--){
        int tp;
        cin>>tp;
        if(tp==1){
            for(int i=0;i<k;i++){
                cin>>id[i];
            }
            for(int i=0;i<k;i++){
                int p1=id[i],p2=id[(i+1)%k];
                f1.upd(p1,a[p2]-a[p1]);
                f2.upd(p1,(a[p2]-a[p1])*1ll*p1);
            }
            int x=a[id[0]];
            for(int i=0;i<k-1;i++){
                int p1=id[i],p2=id[i+1];
                a[p1]=a[p2];
            }
            a[id[k-1]]=x;
        }
        else{
            int l,r,m;
            cin>>l>>r>>m;
            m=min(m,r-l+1-m+1);
            ll s1=f2.get(l+m-2)-f2.get(l-1)-(l-1)*1ll*(f1.get(l+m-2)-f1.get(l-1));
            ll s2=m*1ll*(f1.get(r-m+1)-f1.get(l+m-2));
            ll s3=(r+1)*1ll*(f1.get(r)-f1.get(r-m+1))-(f2.get(r)-f2.get(r-m+1));
            cout<<s1+s2+s3<<ent;
        }
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    srand(time(0));
    //freopen(".in", "r", stdin);
    //freopen(".out","w",stdout);
    int ttt=1;
    //cin>>ttt;
    for(int i=1;i<=ttt;i++){
        //cout<<"NEWCASE"<<ent;
        solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 4 ms 596 KB Output is correct
9 Correct 5 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 980 KB Output is correct
2 Correct 15 ms 1364 KB Output is correct
3 Correct 20 ms 1748 KB Output is correct
4 Correct 36 ms 2760 KB Output is correct
5 Correct 51 ms 3788 KB Output is correct
6 Correct 50 ms 3700 KB Output is correct
7 Correct 51 ms 3712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 2260 KB Output is correct
2 Correct 60 ms 5404 KB Output is correct
3 Correct 82 ms 7624 KB Output is correct
4 Correct 65 ms 6620 KB Output is correct