Compete: Find Members by Username or Skill
Compete: Find Members by Username or Skill
MENU
REGISTER LOG IN
COMPETE
DESIGN CHALLENGES
DEVELOPMENT CHALLENGES
COMPETITIVE PROGRAMMING
LEARN
GET STARTED
DESIGN
DEVELOPMENT
DATA SCIENCE
COMPETITIVE PROGRAMMING
COMMUNITY
OVERVIEW
TCO
PROGRAMS
FORUMS
STATISTICS
EVENTS
BLOG
Forums Round Tables External Competitions Re: Persistent Set Revision History (1 edit)
Re: Persistent Set (response to post by Egor) May 4, 2011 at 12:34 AM EDT
http://www.spoj.pl/problems/DQUERY can be solved using some kind of persistent set, not a persistent balanced
search tree but a persistent segment tree, maybe you can find it useful. For this problem there is an o line solution
using a Binary Indexed Tree, but with a persistent data structure queries can be answered online.
Re: Persistent Set (response to post by Egor) May 4, 2011 at 12:33 AM EDT
http://www.spoj.pl/problems/DQUERY can be solved using some kind of persistent set, not a persistent balanced search
tree but a persistent segment tree, maybe you can find it useful. For this problem there is an o line solution using a Binary
Indexed Tree, but with a persistent data structure queries can be answered online.
mukel /*
13 posts Alfonso2 Peterssen (mukel)
8 - 1 - 2008
SPOJ "DQUERY"
Online algorithm
Preprocessing: O(n lg n)
Query: O(lg n)
Memory: O(n lg n)
*/
#include <cstdio>
#include <map>
using std::map;
const int
MAXN = 40000,
MAXLGN = 16;
int N, Q;
map< int, int > pos;
int tree[MAXN];
int cant;
struct node {
int val, L, R, size;
} buff[2 * MAXN * MAXLGN];
int mid = ( lo + hi ) / 2;
buff[idx] = (node){ mid, build( lo, mid - 1 ), build( mid + 1, hi ), 0 };
return idx;
}
if ( x == 0 ) return 0;
int L = buff[x].L;
int R = buff[x].R;
if ( val < buff[x].val ) L = update( L, val, amount );
if ( val > buff[x].val ) R = update( R, val, amount );
return idx;
}
int main() {
tree[0] = build( 1, N );
for ( int i = 1; i <= N; i++ ) {
int x, posx;
scanf( "%d", &x ); posx = pos[x];
if ( posx != 0 )
tree[i] = update( update( tree[i - 1], posx, -1 ), i, +1 );
else
tree[i] = update( tree[i - 1], i, +1 );
pos[x] = i;
}
return 0;
}
OTHERS
SITEMAP
ABOUT US
CONTACT US
HELP CENTER
PRIVACY POLICY
TERMS
topcoder is also on