Showing posts with label Data Structures. Show all posts
Showing posts with label Data Structures. Show all posts

Wednesday, November 07, 2012

Segment Tree [ On the run ]

I'm trying to implement Segment Tree and maybe will solve some problems using it, prior to ICPC. Here's some resource that I'm using
  • http://codeforces.com/blog/entry/3327
  • http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor
  • http://apps.topcoder.com/forums/;jsessionid=2DA32E9BC12C8C331BCC8441B68E278E?module=Thread&threadID=754366&start=0&mc=4#1572259
  • http://ronzii.wordpress.com/2011/07/08/segment-tree-tutorial/
  • http://en.wikipedia.org/wiki/Segment_tree

Wednesday, November 16, 2011

[UVa LIVE] 4493 - This is your queue

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

deque<int> M;
stack<int> Q;
int main()
{
    int P, C, i, SER, print, m, t, kase=1;
    char CHR[10];
    while (scanf("%d %d",&P,&C)==2)
    {
        if (!P && !C)
            break;
        printf("Case %d:\n",kase++);


        //getchar();
        //cout << "---> " << endl;
        M.clear();
        m = min(P,1000);
        for (i=1 ; i<=m ; i++)
        {
            M.push_back(i);
        }
        for (i=1 ; i<=C ; i++)
        {
            t = scanf("%s",CHR);

            if (!strcmp(CHR,"N"))
            {
                print = M.front();
                M.pop_front();
                printf("%d\n",print);
                M.push_back(print);
            } else if (!strcmp(CHR,"E"))
            {
                scanf("%d",&SER);
                while (!Q.empty()) Q.pop();
                //cout << "------------------------" << endl;
                //printf("-> %d\n",SER);
                while (!M.empty() && M.front() != SER)
                {
                    //cout << M.front() << endl;
                    Q.push(M.front());
                    M.pop_front();
                }
                if(!M.empty()) M.pop_front();
                //cout << "------------------------" << endl;
                while (!Q.empty())
                {
                    M.push_front(Q.top());
                    Q.pop();
                }
                M.push_front(SER);
            }
        }
    }
    return 0;
}

Thursday, September 09, 2010

Finding the highest array position needed for linear representation of a Binary Tree

If a binary tree has suppose 9 nodes then array position for storing its highest possible Root is found like this:

9 = 2^3 + 1

So it’s possible maximum Root is 2^3 – 1 = 7.

So it will take 7 x 2 = 14 places to linearly represent that tree.
And if we are to store even the possible NULLs then it takes 14 x 2 + 1 = 29 Places to linearly represent that tree.
It actually corresponds to the 2^d + 1 formula.
See, if 9 is the maximum node then the tree’s depth is actually 4. And the highest node or Root possible at that level is simply 2^d-1.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...