0% found this document useful (0 votes)
71 views

Binary Indexed Tree

The document provides solutions to problems on the spoj competitive programming website. It gives the code for implementing a binary indexed tree (BIT) which can be used for range sum and point update queries in logarithmic time. The BIT code is in C++ and is explained briefly.

Uploaded by

Viraj Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Binary Indexed Tree

The document provides solutions to problems on the spoj competitive programming website. It gives the code for implementing a binary indexed tree (BIT) which can be used for range sum and point update queries in logarithmic time. The BIT code is in C++ and is explained briefly.

Uploaded by

Viraj Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SPOJSOLUTIONS

Hereyouwillfindsolutionsofmanyproblemsonspoj.Ifyouwantsolutionofsomeproblemwhichisnotlistedinblogorhavedoubtregardinganyspojproblem(whichihavesolved)oranypro
[email protected]@gmail.com
Andmyhumblerequesttoyouallthatdon'tcopythecodeonlytrytounderstandthelogicandalgorithmbehindthecode.Ihavestartedthisbecauseifyoutriedashardasyoucanandstill
thenyoucanrefertothis.
YoucanreadmyanswerhowtostartcompetitiveprogrammingCLICKHERE

Sunday,March23,2014

binaryindexedtreeBIT
BelowgivencodeisforimplementingbinaryindexedtreeorBITforrangesumandpointupdates.
forFurtherdetailsyoucanreadHERE
//NISHANTRAJ
//source:>http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees
//created:>23/03/2014;04:36
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<sstream>
#include<fstream>
#include<climits>
#include<ctime>
#include<algorithm>
#include<bits/stdc++.h>
usingnamespacestd;
inta[100000],cum[100000],res[100000],maxval;
intget_value(intidx)//readingcumulativefrequencyofindexidxO(log(maxval)).
{
intsum=0;
while(idx>0)
{
sum+=res[idx];
idx=(idx&idx);
}
returnsum;
}
voidupdate(intidx,intval)//updatingfrequencyrange
{
while(idx<=maxval)
{
res[idx]+=val;
idx+=(idx&idx);
}
}
intmain()
{
intt;
cin>>t;
while(t)
{
intj,idx,val,k;
cin>>maxval;
cum[0]=0;
for(inti=1;i<=maxval;i++)
{
cin>>a[i];//actualfrequencygivenbyuser.
cum[i]+=a[i]+cum[i1];//cumulativefrequencyarray.
}
for(inti=1;i<=maxval;i++)

{
j=i+1(1<<(__lg(i&i)));//__lg(num)>calculates//the
positionofMSB
res[i]=cum[i]cum[j1];//calcualtingfrequencyofparticularrange
}
intqury;
cin>>qury;
stringin,upd="U",find="S";
while(qury)
{
cin>>in;
if(in==upd)
{
cin>>idx>>val;
update(idx,val);//takesO(log(maxval))time.
}
if(in==find)
{
cin>>j>>k;//takes2*O(log(maxval))time.
cout<<(get_value(k)get_value(j1))<<endl;
}
}
}
return0;
}

PostedbyNishantRajat4:45AM
EmailThisBlogThis!SharetoTwitterSharetoFacebookSharetoPinterest

NewerPostOlderPostHome
Subscribeto:PostComments(Atom)

SearchThisBlog
Loading...

ALLPOST
2015(34)
October(9)
September(5)
August(5)
June(1)
February(6)
January(8)
2014(143)
December(6)
November(9)
October(15)
September(1)
August(9)
July(5)
June(10)
May(11)
April(7)
March(31)
SIRNUMSSIRCHIRAGANDMAGIC
NUMBERS
BLOPEROperators
RMIDRunningMedianRevisited
AMSCO1TheAMSCOcipher
AMSCO2DeciphertheAMSCOcipher
ICPC12CTheEncryptedPassword
FERT21_0Matches
RMIDRunningMedian
GSSQUNCESequence
IITWPC4DArrangementValidity
binaryindexedtreeBIT
JOKER1KnifesAreFun
QN01XORGame
IITKWPCOCreateCollections
LOOPEXPLoopExpectation
SPCJGopuandCreateCollectionsPartTwo
BOGGLEBoggleScoring
IITWPC4CMagguandVectors
AKVOD05RossgeneratesData

GOPI_SWGopiandSandwich
MSCHEDMilkScheduling
GSHOPRamaandFriends
BTCD14AGameofPips
LKSLargeKnapsack
HLP_RAMSTopperRamaRao
EMTY2CanYouMakeItEmpty
TRIGALGEEasyCalculation
SNGMSGEncodeMessage
SYNC13CWHATACOACCIDENT
PRISMSATRIANGULARPRISM
TipTopGameTIPTOP
February(30)
January(9)
2013(34)
December(34)

FacebookFollowers

Subscribe

Posts

Comments

pagehits

Therewasanerrorinthisgadget

FollowbyEmail
Emailaddress...

Submit

Google+Followers

Contributors
NishantRaj
NishantRaj

Labels
math(27)implementation(23)Graph(12)
BinarySearch(11)prime(11)Dynamic
Programming(10)BFS(8)BIT(7)Strings
(7)easy(7)primefactorization(7)DFS(6)
greedy(6)Hash(5)SegmentTree(5)Suffix
Array(4)AVLTree(2)ConvexHull(2)Game
Theory(2)HeightBalancedTree(2)Z
function(2)factorization(2)fibonacci(2)
matrixexponentiation(2)sorting(2)2DBIT
(1)BipartiteGraph(1)Editdistance(1)
GrundyNumbers(1)Heap(1)LCP(1)
LinkedListdatastructure(1)PolicyBased
DataStructure(1)PriorityQueue(1)Rolling

Hash(1)TriesDatastructure(1)contest(1)
floydwarshall(1)inversemodulo(1)
moderate(1)offlinequery(1)queue(1)
sieve(1)stack(1)stringmatching(1)
tetranacci(1)tree(1)twopointer(1)

PopularPosts

AGGRCOWAggressivecows
BelowgivencodeisforAGGRCOWspojAggressivecowsspoj.Hint:ThinkBinarysearch,#include<bits/stdc++.h>usingnamesp...
PPATHPrimePath
PrimePathBelowgivencodeisforPPATHspojorPrimepathspoj.Hint:UseBFSandsieve.#include<bits/stdc++.h>us...
PT07ZLongestpathinatree
LongestpathinatreeGivenbelowcodeisforPTZ07ZspojorLongestpathinatreespoj.YoucansolvethisusingDFSofapplyi...
PALINTheNextPalindrome
Google+BadgeItisasolutiontothe5thandoneofmostattractiveproblemtonovice,aswellasexperiencedhackeriftheyhaven'tdonewithit...
SegmenttreeGSS3&GSS1
Firstofallreadaboutsegmenttreeanditbasicfunctioning.YoucanreaditfromHERE.GivenbelowcodeisforGSS3&GSS1spoj...

Myotherblogs

RealThrills

NISHNATRAJ.PoweredbyBlogger.

You might also like