Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Special acknowledgement to School of Computing, National University of Singapore for allowing Steven to prepare and distribute these teaching materials.
CS3233 CompetitiveProgramming p g g
Dr.StevenHalim Dr. Steven Halim Week02 DataStructures&Libraries FocusonBitManipulation&BinaryIndexedTree
CS3233 CompetitiveProgramming, StevenHalim,SoC,NUS
Outline
MiniContest1+Break(discussionofA/B) / SomeAdmins DataStructuresWithBuiltinLibraries
Justaquickwalkthrough
Read/experiment with the details on your own Read/experimentwiththedetailsonyourown
LINEARDATASTRUCTURES WITHBUILTINLIBRARIES
CS3233 CompetitiveProgramming, StevenHalim,SoC,NUS
Iam I am
1. ApureCcoder 2. A pure C++ coder ApureC++coder 3. Amixbetween C/C++coder C/C d 4. ApureJavacoder p 5. Amultilingual coder:C/C++/Java d C/C++/J
0
0 of 120
CS3233 CompetitiveProgramming, 1 StevenHalim,SoC,NUS
0
2
0
3
0
4
0
5
SORTING+SEARCHING INVOLVINGARRAY
CS3233 CompetitiveProgramming, StevenHalim,SoC,NUS
PopularSortingAlgorithms
O( 2) l i h O(n )algorithms:Bubble/Selection/InsertionSort B bbl /S l i /I i S O(nlogn)algorithms:Merge/Quick^/HeapSort Specialpurpose:Counting/Radix/BucketSort
Reference:
http://en.wikipedia.org/wiki/Sorting_algorithm
CS3233 CompetitiveProgramming, StevenHalim,SoC,NUS
Binarysearchistrickytocode!
I t d Instead,useC++STLalgorithm::lower_bound C STL l ith l b d
4. Bitmask 4 Bit k
a.k.a.lightweightsetofBooleanorbitstring Explanationvia:
http://www.comp.nus.edu.sg/~stevenha/visualization/bitmask.html
6. Stack,C++STLstack,JavaStack 6 St k C STL t k J St k
UsedbydefaultinRecursion,PostfixCalculation, BracketMatching,etc
NONLINEARDATASTRUCTURES WITHBUILTINLIBRARIES
CS3233 CompetitiveProgramming, StevenHalim,SoC,NUS
Fretnot,justuse:C++STLmap(JavaTreeMap)
UVa 10226 (Hardwood Species)* UVa10226 (HardwoodSpecies)
InJava:TreeSet
Example: p
UVa 11849 CD
CS3233 CompetitiveProgramming, StevenHalim,SoC,NUS
Heap
Heap
C++STLalgorithm hassomeheapalgorithms g p g
partial_sort usesheapsort
B t But,werarelyseepureheapproblemsinICPC l h bl i ICPC
Nevertheless,O(logn)usingmap isusuallyok
SUPPLEMENTARY
Want More? Add libraries that you frequently use into this template, e.g.: ctype.h t h bitset etc
5. STL/Librariesalltheway! / y
isalpha (ctype.h) inline bool isletter(char c) { return (c>='A'&&c<='Z')||(c>='a'&&c<='z'); } abs (math.h) inline int abs(int a) { return a >= 0 ? a : -a; } pow (math.h) a, int power(int a int b) { int res=1; for (; b>=1; b--) res*=a; return res; }
7. Usememset/assign/constructoreffectively!
memset(dist, 127, sizeof(dist)); // useful to initialize shortest path distances, set INF to 127! memset(dp_memo, -1, sizeof(dp_memo)); // useful to initialize DP memoization table memset(arr, 0, sizeof(arr)); // useful to clear array of integers ( , , ( )); y g vector<int> dist(v, 2000000000); dist.assign(v, -1);
0
2
Graph (notdiscussedtoday,revisitedinWeek05/08) UnionFindDisjointSets (notdiscussedtoday,readCh2onyourown) U i Fi d Di j i t S t ( t di dt d d Ch2 ) SegmentTree (notdiscussedtoday,readCh2onyourown) FenwickTree (discussedtoday) Fenwick Tree (discussed today)
DATASTRUCTURES WITHOUTBUILTINLIBRARIES
CS3233 CompetitiveProgramming, StevenHalim,SoC,NUS
Applythisformulaiterativelyuntilb is0
Analysis: A l i This is O(log n) Why? Example: ft rsq(ft, 6) Example:ft_rsq(ft,
b=6=0110,b=b LSOne(b)=0110 0010,b'=4=0100 b'=4=0100,b=b LSOne(b)=0100 0100,b''=0,stop
int ft_rsq(const vi &t, int a, int b) { // returns RSQ(a, b) return ft t ft_rsq(t, b) - ( == 1 ? 0 : ft (t (a ft_rsq(t, a - 1)) } (t 1)); // adjusts value of the k-th element by v (v can be +ve/inc or -ve/dec) j ( , , ) void ft_adjust(vi &ft, int k, int v) { for (; k < (int)ft.size(); k += LSOne(k)) ft[k] += v; }
Ifyourcoderunsin0.342secs,whatisyourrank?
HowtouseFenwickTreetodealwiththisproblem? p
CS3233 CompetitiveProgramming, StevenHalim,SoC,NUS
0
0 of 120
CS3233 CompetitiveProgramming, 1 StevenHalim,SoC,NUS
0
2
0
3
Summary
TherearealotofgreatDataStructuresoutthere
Weneedthemostefficientoneforourproblem
DifferentDSsuitsdifferentproblem!
Manyofthemhavebuiltinlibraries
Forsomeothers,wehavetobuildourown(focusonFT)
Studytheselibraries!Donotrebuildthemduringcontests!
FromWeek03onwardsandfutureICPCs/IOIs, useC++STLand/orJavaAPIandourbuiltinlibraries!
Now,yourteamshouldbeinrank3045(from60) (stillsolving~12problemsoutof10,butfaster)
CS3233 CompetitiveProgramming, StevenHalim,SoC,NUS