#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define F first
#define S second
#define pb push_back
using namespace std;
ll n,f1[400005],f2[400005],a[100005];
void upd1(ll id,ll L,ll R,ll f,ll d)
{
if (f<L || R<f)
return;
if (L==R)
{
f1[id]=d;
f2[id]=d*(f+1);
return;
}
upd1( 2*id , L ,(L+R)/2,f,d);
upd1(2*id+1,(L+R)/2+1, R ,f,d);
f1[id]=f1[2*id]+f1[2*id+1];
f2[id]=f2[2*id]+f2[2*id+1];
return;
}
void upd(vector< pair<ll,ll> > u)
{
ll temp=u[0].F;
for (ll i=0; i<(ll)(u.size())-1; i++)
a[u[i].S]=u[i+1].F;
a[u[u.size()-1].S]=temp;
for (ll i=0; i<(ll)(u.size()); i++)
upd1(1,0,n-1,u[i].S,u[(i+1)%u.size()].F);
return;
}
ll get1(ll id,ll L,ll R,ll l,ll r)
{
if (R<l || r<L)
return 0;
if (l<=L && R<=r)
return f1[id];
return get1( 2*id , L ,(L+R)/2,l,r)+
get1(2*id+1,(L+R)/2+1, R ,l,r);
}
ll get2(ll id,ll L,ll R,ll l,ll r)
{
if (R<l || r<L)
return 0;
if (l<=L && R<=r)
return f2[id];
return get2( 2*id , L ,(L+R)/2,l,r)+
get2(2*id+1,(L+R)/2+1, R ,l,r);
}
ll get(ll L,ll R,ll m)
{
if (m==1)
return get1(1,0,n-1,L,R);
ll ans=0;
ans+=get2(1,0,n-1,L,min(L+m-1,R-m+1)-1);
ans-=get1(1,0,n-1,L,min(L+m-1,R-m+1)-1)*L;
ans+=get1(1,0,n-1,min(L+m-1,R-m+1),max(L+m-1,R-m+1))
*(min(L+m-1,R-m+1)-L+1);
ans-=get2(1,0,n-1,max(L+m-1,R-m+1)+1,R);
ans+=get1(1,0,n-1,max(L+m-1,R-m+1)+1,R)*(R+2);
return ans;
}
ll k,q,qt,l,r,m;
vector< pair<ll,ll> > u;
int main()
{
cin>>n>>k;
u.pb(mp(0,0));
for (ll i=0; i<n; i++)
{
cin>>a[i];
u[0]=mp(a[i],i);
upd(u);
}
for (ll i=1; i<k; i++)
u.pb(mp(0,0));
cin>>q;
while (q--)
{
cin>>qt;
if (qt==1)
{
for (ll i=0; i<k; i++)
{
cin>>u[i].S;
u[i].S--;
u[i].F=a[u[i].S];
}
upd(u);
}
else
{
cin>>l>>r>>m;
cout<<get(l-1,r-1,m)<<endl;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
4 ms |
332 KB |
Output is correct |
3 |
Correct |
12 ms |
404 KB |
Output is correct |
4 |
Correct |
17 ms |
500 KB |
Output is correct |
5 |
Correct |
24 ms |
508 KB |
Output is correct |
6 |
Correct |
23 ms |
588 KB |
Output is correct |
7 |
Correct |
27 ms |
688 KB |
Output is correct |
8 |
Correct |
31 ms |
716 KB |
Output is correct |
9 |
Correct |
48 ms |
1028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
1692 KB |
Output is correct |
2 |
Correct |
151 ms |
1964 KB |
Output is correct |
3 |
Correct |
204 ms |
3188 KB |
Output is correct |
4 |
Correct |
366 ms |
5996 KB |
Output is correct |
5 |
Correct |
534 ms |
6596 KB |
Output is correct |
6 |
Correct |
502 ms |
6436 KB |
Output is correct |
7 |
Correct |
506 ms |
6464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
305 ms |
3164 KB |
Output is correct |
2 |
Correct |
468 ms |
6072 KB |
Output is correct |
3 |
Correct |
676 ms |
5860 KB |
Output is correct |
4 |
Correct |
573 ms |
6652 KB |
Output is correct |