Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
PragmaticProgramming
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Regler
PragmaticProgramming
Commits
0488e55a
Commit
0488e55a
authored
1 year ago
by
Max Nilsson
Browse files
Options
Downloads
Patches
Plain Diff
Week5
parent
a01baca1
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Code/fenwick_segment_test.cpp
+127
-0
127 additions, 0 deletions
Code/fenwick_segment_test.cpp
README.md
+1
-1
1 addition, 1 deletion
README.md
Slides/Pragmatic_Programming_Week5.pdf
+0
-0
0 additions, 0 deletions
Slides/Pragmatic_Programming_Week5.pdf
with
128 additions
and
1 deletion
Code/fenwick_segment_test.cpp
0 → 100644
+
127
−
0
View file @
0488e55a
#include
<bits/stdc++.h>
using
namespace
std
;
#define Mod(x,y) (((x)%(y)+(y))%(y))
#define rep(i, a, b) for(int (i) = (a); (i) < (b); ++(i))
#define all(x) begin(x), end(x)
#define pb push_back
#define gcd __gcd
#define sz(x) (ll)(x.size())
typedef
long
long
ll
;
typedef
unsigned
long
long
ull
;
typedef
pair
<
int
,
int
>
pii
;
typedef
vector
<
int
>
vi
;
typedef
vector
<
pii
>
vii
;
const
bool
debug
=
false
;
const
int
numberOfServers
=
1e6
+
1
;
int
queries
=
1e6
;
ll
servers
[
numberOfServers
];
ll
fenwick
[
numberOfServers
];
ll
segment
[
4
*
numberOfServers
];
// NAIVE
void
updateServers
(
int
a
,
ll
x
)
{
servers
[
a
]
+=
x
;
}
ll
rangeSumServers
(
int
a
,
int
b
)
{
ll
sum
=
0
;
rep
(
i
,
a
,
min
(
b
+
1
,
numberOfServers
))
sum
+=
servers
[
i
];
return
sum
;
}
// FENWICK
void
updateFenwick
(
int
a
,
ll
x
)
{
while
(
a
<
numberOfServers
)
fenwick
[
a
]
+=
x
,
a
+=
(
a
&
(
-
a
));
}
ll
rangeSumFenwick
(
int
a
)
{
ll
sum
=
0
;
while
(
a
)
sum
+=
fenwick
[
a
],
a
-=
(
a
&
(
-
a
));
return
sum
;
}
ll
rangeSumFenwick
(
int
a
,
int
b
)
{
return
rangeSumFenwick
(
b
)
-
rangeSumFenwick
(
a
-
1
);
}
// SEGMENT
void
updateSegment
(
int
v
,
int
l
,
int
r
,
int
a
,
ll
x
)
{
if
(
l
==
r
)
{
segment
[
v
]
+=
x
;
return
;
}
int
mid
=
(
l
+
r
)
/
2
;
if
(
a
<=
mid
)
updateSegment
(
v
*
2
,
l
,
mid
,
a
,
x
);
else
updateSegment
(
v
*
2
+
1
,
mid
+
1
,
r
,
a
,
x
);
segment
[
v
]
=
segment
[
v
*
2
]
+
segment
[
v
*
2
+
1
];
}
void
updateSegment
(
int
a
,
ll
x
)
{
updateSegment
(
1
,
0
,
numberOfServers
,
a
,
x
);
}
ll
rangeSumSegment
(
int
v
,
int
l
,
int
r
,
int
a
,
int
b
)
{
if
(
a
>
b
)
return
0
;
if
(
a
==
l
&&
b
==
r
)
return
segment
[
v
];
int
mid
=
(
l
+
r
)
/
2
;
return
rangeSumSegment
(
v
*
2
,
l
,
mid
,
a
,
min
(
b
,
mid
))
+
rangeSumSegment
(
v
*
2
+
1
,
mid
+
1
,
r
,
max
(
a
,
mid
+
1
),
b
);
}
ll
rangeSumSegment
(
int
a
,
int
b
)
{
return
rangeSumSegment
(
1
,
0
,
numberOfServers
,
a
,
b
);
}
void
solve
()
{
bool
compareWithFenwick
=
true
;
bool
compareWithSegment
=
true
;
bool
compareWithNaive
=
false
;
if
(
compareWithFenwick
)
cout
<<
"Using Fenwick
\n
"
;
if
(
compareWithSegment
)
cout
<<
"Using Segment
\n
"
;
if
(
compareWithNaive
)
cout
<<
"Using Naive
\n
"
;
cout
<<
"With number Servers = "
<<
numberOfServers
-
1
<<
" and number of queries = "
<<
queries
<<
'\n'
;
// initialize servers
srand
(
time
(
NULL
));
rep
(
a
,
1
,
numberOfServers
)
{
ll
x
=
rand
()
%
1000
;
if
(
compareWithNaive
)
updateServers
(
a
,
x
);
if
(
compareWithFenwick
)
updateFenwick
(
a
,
x
);
if
(
compareWithSegment
)
updateSegment
(
a
,
x
);
}
// handling the queries
while
(
queries
--
)
{
int
type
=
rand
()
%
2
;
if
(
type
)
{
// updating servers
int
a
=
rand
()
%
(
numberOfServers
-
1
)
+
1
;
ll
x
=
rand
()
%
1000
;
if
(
compareWithNaive
)
updateServers
(
a
,
x
);
if
(
compareWithFenwick
)
updateFenwick
(
a
,
x
);
if
(
compareWithSegment
)
updateSegment
(
a
,
x
);
}
else
{
// range queries
int
a
=
rand
()
%
(
numberOfServers
-
1
)
+
1
;
int
b
=
rand
()
%
(
numberOfServers
-
1
)
+
1
;
if
(
b
<
a
)
swap
(
a
,
b
);
ll
sumFenwick
=
(
compareWithFenwick
?
rangeSumFenwick
(
a
,
b
)
:
-
1
);
ll
sumSegment
=
(
compareWithSegment
?
rangeSumSegment
(
a
,
b
)
:
-
1
);
ll
sumNaive
=
(
compareWithNaive
?
rangeSumServers
(
a
,
b
)
:
-
1
);
if
(
compareWithFenwick
&&
compareWithNaive
)
assert
(
sumFenwick
==
sumNaive
);
if
(
compareWithFenwick
&&
compareWithSegment
)
assert
(
sumSegment
==
sumFenwick
);
}
}
cout
<<
"EVERYTHING OK!
\n
"
;
}
int
main
()
{
ios
::
sync_with_stdio
(
0
);
cin
.
tie
(
0
);
cout
<<
setprecision
(
30
)
<<
fixed
;
int
test
=
1
;
// cin >> test;
while
(
test
--
)
solve
();
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
README.md
+
1
−
1
View file @
0488e55a
...
...
@@ -12,7 +12,7 @@ You can find the Kattis course page here https://lth.kattis.com/courses
| 2023-10-26 | Week 2 | Data Structures and Complexity | Prime Factorization and Union-Find
| 2023-11-09 | Week 3 | Graphs |
| 2023-11-16 | Week 4 | Dynamic Programming |
| 2023-11-23 | Week 5 | Fenwick and Segment Trees | Implement an easy to use
Fenwick T
ree
| 2023-11-23 | Week 5 | Fenwick and Segment Trees | Implement an easy to use
2-d segment t
ree
| 2023-11-30 | Week 6 | Geometry, Number Theory, and Probability |
| 2023-12-07 | Week 7 | Tries, Suffix Arrays, and Strings |
| 2023-12-14 | Week 8 | Dynamic Programming Revisited |
...
...
This diff is collapsed.
Click to expand it.
Slides/Pragmatic_Programming_Week5.pdf
0 → 100644
+
0
−
0
View file @
0488e55a
File added
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment