#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<u.size()-1; i++)
a[u[i].S]=u[i+1].F;
a[u.size()-1]=temp;
for (ll i=0; i<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;
}
Compilation message
Main.cpp: In function 'void upd(std::vector<std::pair<long long int, long long int> >)':
Main.cpp:28:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (ll i=0; i<u.size()-1; i++)
| ~^~~~~~~~~~~
Main.cpp:31:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (ll i=0; i<u.size(); i++)
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
5 ms |
332 KB |
Output is correct |
3 |
Correct |
12 ms |
408 KB |
Output is correct |
4 |
Correct |
18 ms |
488 KB |
Output is correct |
5 |
Correct |
25 ms |
484 KB |
Output is correct |
6 |
Correct |
27 ms |
648 KB |
Output is correct |
7 |
Correct |
36 ms |
588 KB |
Output is correct |
8 |
Correct |
33 ms |
680 KB |
Output is correct |
9 |
Correct |
48 ms |
964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
1668 KB |
Output is correct |
2 |
Correct |
155 ms |
1864 KB |
Output is correct |
3 |
Correct |
204 ms |
3168 KB |
Output is correct |
4 |
Correct |
414 ms |
5904 KB |
Output is correct |
5 |
Correct |
571 ms |
6680 KB |
Output is correct |
6 |
Correct |
497 ms |
6504 KB |
Output is correct |
7 |
Correct |
569 ms |
6448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
312 ms |
3140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |