Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std ;
- typedef long long int ll ;
- const int N = 1 << 20 ;
- ll a[N] , b_index , b_size , lol[N] , n ;
- void update(ll pos , ll val ) {
- lol[pos / b_size] = lol[pos / b_size] - a[pos] + val ;
- a[pos] = val ;
- }
- ll query(ll left , ll right ) {
- ll sum = 0 ;
- while(left <= right and left % b_size != 0 ) {
- sum += a[left] ;
- left++ ;
- }
- while(left + b_size <= right){
- sum += lol[left / b_size] ;
- left += b_size ;
- }
- while(left <= right) {
- sum += a[left] ;
- left++ ;
- }
- return sum ;
- }
- void add() {
- b_index = -1 ;
- b_size = sqrt(n) ;
- for(int i = 0 ; i < n ;i++ ) {
- if( i % b_size == 0 )
- b_index++ ;
- lol[b_index] += a[i] ;
- }
- }
- int main () {
- cin >> n ;
- for(int i = 0 ; i < n ; i++) {
- cin >> a[i] ;
- }
- add() ;
- int q ;
- cin >> q ;
- while(q--) {
- int x , y , z ;
- cin >> z ;
- if(z == 1) {
- int pos , val ;
- cin >> pos >> val ;
- update(pos , val ) ;
- }
- else {
- ll left , right ;
- cin >> left >> right ;
- cout << query(left - 1 , right - 1 ) << endl ;
- }
- }
- }
Add Comment
Please, Sign In to add comment