Submission #839617

#TimeUsernameProblemLanguageResultExecution timeMemory
839617alishejhfAddk (eJOI21_addk)C++17
100 / 100
82 ms7624 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...