Skip to content
Snippets Groups Projects
Commit 0488e55a authored by Max Nilsson's avatar Max Nilsson
Browse files

Week5

parent a01baca1
No related branches found
No related tags found
No related merge requests found
#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
......@@ -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 Tree
| 2023-11-23 | Week 5 | Fenwick and Segment Trees | Implement an easy to use 2-d segment tree
| 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 |
......
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment