Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
- #define ll long long
- #define ld long double
- #define pb push_back
- #define fe first
- #define se second
- #define nl "\n"
- #define pp pair < ll , ll >
- #define sz(x) (ll)x.size()
- #define st(x) sort(x.begin(),x.end())
- #define rst(x) sort(x.rbegin(), x.rend())
- #define all(x) x.begin(),x.end()
- long double pi = 3.14159265358979323;
- const double EPS = 1e-12;
- const int N = 2e5 + 1;
- const int mod = 1e9 + 7;
- // square root decomposition
- ll a[N], bsize ;
- ll block[N];
- // bsize is the size of block
- void pre(ll n){
- //we can find the block number using i/bsize
- for(int i = 0 ; i < n; i++ ){
- block[i/bsize] += a[i];
- }
- }
- void update(ll idx , ll val){
- int id = idx/bsize;
- block[id] += val - a[idx];
- a[idx] = val;
- }
- ll query(ll l ,ll r){
- ll sum = 0 ;
- //for query there are three points
- //1.find the sum of block which starts from l
- //we are taking l < r because in last loop we are considring the case l <= r
- while(l % bsize != 0 && l < r){
- sum += a[l];
- l++;
- }
- //2.find the sum of all the block b/w l and r
- //take the case l starts from 1 and blk size is 3 then l + p -1 will points to 3
- //which is second block
- while(l + bsize - 1 <= r){
- sum += block[l/bsize];
- l += bsize;
- }
- //3.find the sum of block where r ends
- while(l <= r){
- sum += a[l];
- l++;
- }
- return sum;
- }
- //time complexity of query will be O(sqrt(n));
- int main()
- {
- fast;
- ll n;
- cin >> n ;
- bsize = (sqrt(n));
- // cout << bsize << " ";
- for(int i = 0 ; i < n; i++ )
- cin >> a[i];
- pre(n);
- ll q;
- cin >> q;
- while(q--){
- ll ch;
- cin >> ch ;
- if(ch == 1){
- ll idx , val;
- cin >> idx >> val;
- idx--;
- update(idx ,val);
- }
- else{
- ll a , b;
- cin >> a >> b ;
- a-- , b--;
- cout << query(a,b) << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment