Menu

Distinct Values Queries | CSES - Algoritmit ongelmanratkaisussa
📰

Distinct Values Queries | CSES - Algoritmit ongelmanratkaisussa

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

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

#include <iostream> #include <vector> #include <map> #include <utility>

using namespace std ;

struct SegmentTree { int n ; vector < int > tree ;

SegmentTree ( int n ) { this -> n = n ; tree . assign ( 2 * n , 0 ) ; }

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

int query ( int l , int r ) { l += n ; r += n ;

int 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 ; } } ;

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

int n , q ; cin >> n >> q ;

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

int a , b ; vector < vector < pair < int , int >>> queries ( n ) ; for ( int i = 0 ; i < q ; i ++ ) { cin >> a >> b ; queries [ b - 1 ] .…

Continue reading — create a free account

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