/*
* author: kartik8800
*/
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr(a,b) for(int i = a; i < b; i++)
#define rep(i,a,b) for(int i = a; i < b; i++)
#define mod 1000000007
#define inf (1LL<<60)`
#define all(x) (x).begin(), (x).end()
#define prDouble(x) cout << fixed << setprecision(10) << x
#define triplet pair<ll,pair<ll,ll>>
#define goog(tno) cout << "Case #" << tno++ <<": "
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define read(x) int x; cin >> x
using namespace std;
void init_code(){
fast_io;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
struct Chunk{
std::vector<int> elementsInChunk;
int aggregatedValueForChunk;
};
class SquareRootDecomposition{
int block_size;
vector<Chunk> chunks;
// returns {chunk_number, index_inside_chunk} for given index.
pair<int,int> getChunkForIndex(int index){
int chunk_number = index / block_size;
int index_inside_chunk = index - (chunk_number * block_size);
return {chunk_number, index_inside_chunk};
}
int calculateAggregatedValue(Chunk& chunk, int index_from, int index_till){
int aggregatedValue = 0;
while(index_from <= index_till)
aggregatedValue += chunk.elementsInChunk[index_from++];
return aggregatedValue;
}
void fillChunks(vector<int>& dataVector){
int curr_chunk = 0;
for(int i = 0; i < dataVector.size(); i++){
if(i != 0 && ((i % block_size) == 0)){
curr_chunk++;
}
chunks[curr_chunk].elementsInChunk.push_back(dataVector[i]);
}
for(Chunk &chunk: chunks){
chunk.aggregatedValueForChunk = calculateAggregatedValue(
chunk, 0, (int)chunk.elementsInChunk.size() - 1);
}
}
int internalChunks(int l, int r){
if(l > r) return 0;
int aggregatedValue = 0;
while(l <= r)
aggregatedValue += chunks[l++].aggregatedValueForChunk;
return aggregatedValue;
}
public:
SquareRootDecomposition(std::vector<int>& dataVector){
block_size = sqrt((int)dataVector.size());
chunks = vector<Chunk>(block_size + 1);
fillChunks(dataVector);
}
int query(int L, int R){
pair<int,int> leftChunk = getChunkForIndex(L);
pair<int,int> rightChunk = getChunkForIndex(R);
if(leftChunk.first == rightChunk.first)
return calculateAggregatedValue(chunks[leftChunk.first], leftChunk.second, rightChunk.second);
else {
return calculateAggregatedValue(chunks[leftChunk.first], leftChunk.second, (int)chunks[leftChunk.first].elementsInChunk.size() - 1)
+ calculateAggregatedValue(chunks[rightChunk.first], 0, rightChunk.second)
+ internalChunks(leftChunk.first + 1, rightChunk.first - 1);
}
}
void update(int index, int value){
pair<int,int> indexInChunk = getChunkForIndex(index);
chunks[indexInChunk.first].elementsInChunk[indexInChunk.second] = value;
chunks[indexInChunk.first].aggregatedValueForChunk = calculateAggregatedValue(chunks[indexInChunk.first], 0, chunks[indexInChunk.first].elementsInChunk.size());
}
};
int main() {
init_code();
int t = 1; //cin >> t;
while(t--){
std::vector<int> v = {1,2,3,4,5,6,7,8,9,10};
SquareRootDecomposition myQueryHelper(v);
cout << myQueryHelper.query(0, 5) << '\n';; // should print 1+2+3+4+5+6
myQueryHelper.update(2,0);
cout << myQueryHelper.query(0, 5) << '\n'; // should print 1+2+0+4+5+6
}
return 0;
}
LyoKKiAgIGF1dGhvcjoga2FydGlrODgwMAoqLwogCiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgZnIoYSxiKSBmb3IoaW50IGkgPSBhOyBpIDwgYjsgaSsrKQojZGVmaW5lIHJlcChpLGEsYikgZm9yKGludCBpID0gYTsgaSA8IGI7IGkrKykKI2RlZmluZSBtb2QgMTAwMDAwMDAwNwojZGVmaW5lIGluZiAoMUxMPDw2MClgCiNkZWZpbmUgYWxsKHgpICh4KS5iZWdpbigpLCAoeCkuZW5kKCkKI2RlZmluZSBwckRvdWJsZSh4KSBjb3V0IDw8IGZpeGVkIDw8IHNldHByZWNpc2lvbigxMCkgPDwgeAojZGVmaW5lIHRyaXBsZXQgcGFpcjxsbCxwYWlyPGxsLGxsPj4KI2RlZmluZSBnb29nKHRubykgY291d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4X3RpbGwpewogICAgICAgIGludCBhZ2dyZWdhdGVkVmFsdWUgPSAwOwogICAgICAgIHdoaWxlKGluZGV4X2Zyb20gPD0gaW5kZXhfdGlsbCkKICAgICAgICAgICAgYWdncmVnYXRlZFZhbHVlICs9IGNodW5rLmVsZW1lbnRzSW5DaHVua1tpbmRleF9mcm9tKytdOwogICAgICAgIHJldHVybiBhZ2dyZWdhdGVkVmFsdWU7CiAgICB9CgogICAgdm9pZCBmaWxsQ2h1bmtzKHZlY3RvcjxpbnQ+JiBkYXRhVmVjdG9yKXsKICAgICAgICBpbnQgY3Vycl9jaHVuayA9IDA7CiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IGRhdGFWZWN0b3Iuc2l6ZSgpOyBpKyspewogICAgICAgICAgICBpZihpICE9IDAgJiYgKChpICUgYmxvY2tfc2l6ZSkgPT0gMCkpewogICAgICAgICAgICAgICAgY3Vycl9jaHVuaysrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGNodW5rc1tjdXJyX2NodW5rXS5lbGVtZW50c0luQ2h1bmsucHVzaF9iYWNrKGRhdGFWZWN0b3JbaV0pOwogICAgICAgIH0KCiAgICAgICAgZm9yKENodW5rICZjaHVuazogY2h1bmtzKXsKICAgICAgICAgICAgY2h1bmsuYWdncmVnYXRlZFZhbHVlRm9yQ2h1bmsgPSBjYWxjdWxhdGVBZ2dyZWdhdGVkVmFsdWUoCiAgICAgICAgICAgICAgICAgIGNodW5rLCAwLCAoaW50KWNodW5rLmVsZW1lbnRzSW5DaHVuay5zaXplKCkgLSAxKTsKICAgICAgICB9CiAgICB9CgogICAgaW50IGludGVybmFsQ2h1bmtzKGludCBsLCBpbnQgcil7CiAgICAgICAgaWYobCA+IHIpIHJldHVybiAwOwogICAgICAgIGludCBhZ2dyZWdhdGVkVmFsdWUgPSAwOwogICAgICAgIHdoaWxlKGwgPD0gcikKICAgICAgICAgICAgYWdncmVnYXRlZFZhbHVlICs9IGNodW5rc1tsKytdLmFnZ3JlZ2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