#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100'010;
vector<ll> fen1(MAXN, 0);
vector<ll> fen2(MAXN, 0);
void update(vector<ll> &bit, ll in, ll val)
{
while(in < MAXN)
{
bit[in]+=val;
in+=in&(-in);
}
}
long long query(vector<ll> &bit, ll in)
{
ll sum = 0;
while(in > 0)
{
sum+=bit[in];
in-=in&(-in);
}
return sum;
}
int main () {
int n, k;
cin >> n >> k;
long long arr[n+1];
//, prefix1[n+1] = {0}, prefix2[n+1] = {0};
for(int i = 1;i<=n;i++) {
cin >> arr[i];
update(fen1, i, arr[i]);
update(fen2, i, arr[i]*i);
//prefix1[i] = prefix1[i-1] + arr[i];
//prefix2[i] = prefix2[i-1] + arr[i]*i;
}
int q;
cin >> q;
while(q--) {
int type;
cin >> type;
if(type == 1) {
ll change[k];
for(int i= 0;i<k;i++)
{
cin >> change[i];
update(fen1, change[i], -arr[change[i]]);
update(fen2, change[i], -change[i]*arr[change[i]]);
}
reverse(change, change+k);
long long prev = arr[change[k-1]];
for(int i = 0;i<k;i++)
{
swap(prev, arr[change[i]]);
update(fen1, change[i], arr[change[i]]);
update(fen2, change[i], arr[change[i]]*change[i]);
}
}
else {
int ans = 0;
int l, r, m;
cin >> l >> r >> m;
m = min(r-l-m + 2, m);
long long a = (query(fen2, l+m-1) - query(fen2, l-1))-(query(fen1,l+m-1) - query(fen1, l-1))*(l-1);
long long b = (query(fen1, r-m) - query(fen1, l+m-1))*m;
long long c = (query(fen1, r) - query(fen1, r-m))*(r+1) - (query(fen2, r) - query(fen2, r-m));
cout<<a+b+c<<endl;
}
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:60:8: warning: unused variable 'ans' [-Wunused-variable]
60 | int ans = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
6 ms |
1852 KB |
Output is correct |
3 |
Correct |
7 ms |
1876 KB |
Output is correct |
4 |
Correct |
10 ms |
1896 KB |
Output is correct |
5 |
Correct |
11 ms |
2004 KB |
Output is correct |
6 |
Correct |
14 ms |
2004 KB |
Output is correct |
7 |
Correct |
16 ms |
2084 KB |
Output is correct |
8 |
Correct |
18 ms |
2160 KB |
Output is correct |
9 |
Correct |
26 ms |
2284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
2216 KB |
Output is correct |
2 |
Correct |
78 ms |
3148 KB |
Output is correct |
3 |
Correct |
110 ms |
3720 KB |
Output is correct |
4 |
Correct |
192 ms |
5196 KB |
Output is correct |
5 |
Correct |
289 ms |
6612 KB |
Output is correct |
6 |
Correct |
254 ms |
6368 KB |
Output is correct |
7 |
Correct |
239 ms |
6340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
2644 KB |
Output is correct |
2 |
Correct |
237 ms |
5916 KB |
Output is correct |
3 |
Correct |
324 ms |
8012 KB |
Output is correct |
4 |
Correct |
307 ms |
6864 KB |
Output is correct |