Menu

Range Update Queries | CSES - Algoritmit ongelmanratkaisussa
📰

Range Update Queries | CSES - Algoritmit ongelmanratkaisussa

DEV Community: cpp·Guhan M·6 days ago
#mPeXtTwE
#devtocpp#devto#dev#include#long
Long article (365 words) — no AI summary yet.
Reading 0:00
15s threshold

Problem: https://cses.fi/alon/task/1651

#include <iostream> #include <vector>

using namespace std ;

int n ; int q ; int type ; long long arr [ 200002 ] ; vector < long long > tree ;

void change ( int k , long long x ) { k += n + 1 ; tree [ k ] += x ; for ( k /= 2 ; k >= 1 ; k /= 2 ) { tree [ k ] = tree [ 2 * k ] + tree [ 2 * k + 1 ] ; } }

long long query ( int l , int r ) { l += n + 1 ; r += n + 1 ; long long sum = 0 ; while ( l <= r ) { if ( l % 2 == 1 ) sum += tree [ l ++ ] ; if ( r % 2 == 0 ) sum += tree [ r -- ] ; l /= 2 ; r /= 2 ; } return sum ; }

void build ( ) { for ( int i = 1 ; i <= n ; i ++ ) { change ( i , arr [ i ] - arr [ i - 1 ] ) ; } }

int main ( ) { ios_base :: sync_with_stdio ( false ) ; cin . tie ( nullptr ) ;

cin >> n >> q ;

for ( int i = 1 ; i <= n ; i ++ ) { cin >> arr [ i ] ; }

tree .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.