Skip to content

Commit c14d00d

Browse files
Add Tutorial for LOJ-1183: Computing Fast Average (en) (#441)
* Added en.md for LOJ-1183 * Added source code for LOJ-1183 * Added Tutorial
1 parent 65de424 commit c14d00d

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

1183/en.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# LOJ 1183 - Computing Fast Average
2+
3+
## Summary
4+
The problem requires implementing two types of queries on an array of integers (initially all values are 0):
5+
6+
1. Changing the value of elements in a range of indices with a specific value given. (**update query**)
7+
8+
2. Finding the average value of integers in a range of indices. (**get query**)
9+
10+
## Prerequisite
11+
Segment tree with lazy propagation:
12+
13+
https://cp-algorithms.com/data_structures/segment_tree.html#find-the-smallest-number-greater-or-equal-to-a-specified-number-acceleration-with-fractional-cascading
14+
15+
(See the section titled **Range updates (Lazy Propagation)**)
16+
17+
## Solution
18+
The solution is nothing but formulating it to the **range update-sum query**. The range update query is equivalent to the classic lazy update (setting a value to a range of indices) so is the range sum query. To form an irreducible fraction we divide both the nominator (range sum) and the denominator (total indices) by the gcd of them.
19+
20+
## Complexity
21+
- Time Complexity: O(T * (N + Q * $log{_2}{N}$)).
22+
- Memory Complexity: O(N).
23+
24+
## Code
25+
26+
### C++
27+
28+
```cpp
29+
#include <bits/stdc++.h>
30+
31+
using namespace std;
32+
33+
34+
struct SegmentTree {
35+
int n;
36+
vector <int> tree, lazy;
37+
38+
SegmentTree(int n) : n(n) {
39+
tree.assign(n<<2, 0);
40+
lazy.assign(n<<2, -1);
41+
}
42+
43+
void propagate(int now, int l, int r) {
44+
int left = now<<1;
45+
int right = left|1;
46+
47+
int mid = (l+r)>>1;
48+
49+
tree[left] = (mid-l+1) * lazy[now];
50+
tree[right] = (r-mid) * lazy[now];
51+
lazy[left] = lazy[right] = lazy[now];
52+
lazy[now] = -1;
53+
}
54+
55+
void update(int now, int l, int r, const int i, const int j, const int val) {
56+
if (i > r || j < l) return ;
57+
if (i <= l && j >= r) {
58+
tree[now] = (r-l+1) * val;
59+
lazy[now] = val;
60+
return ;
61+
}
62+
63+
int mid = (l+r)>>1;
64+
int left = now<<1;
65+
int right = left|1;
66+
67+
if (lazy[now] != -1) propagate(now, l, r);
68+
69+
update(left, l, mid, i, j, val);
70+
update(right, mid+1, r, i, j, val);
71+
72+
tree[now] = tree[left] + tree[right];
73+
}
74+
75+
int query(int now, int l, int r, const int i, const int j) {
76+
if (i > r || j < l) return 0;
77+
if (i <= l && j >= r) return tree[now];
78+
79+
int mid = (l+r)>>1;
80+
int left = now<<1;
81+
int right = left|1;
82+
83+
if (lazy[now] != -1) propagate(now, l, r);
84+
85+
return query(left, l, mid, i, j) + query(right, mid+1, r, i, j);
86+
}
87+
};
88+
89+
int main() {
90+
91+
// for fast I/O
92+
ios_base::sync_with_stdio(false);
93+
cin.tie(nullptr);
94+
95+
int t;
96+
cin >> t;
97+
98+
for(int ts = 1; ts <= t; ++ts) {
99+
int n, q;
100+
cin >> n >> q;
101+
102+
SegmentTree seg_tree(n);
103+
104+
cout << "Case " << ts << ":\n";
105+
while (q--) {
106+
int type, i, j;
107+
cin >> type >> i >> j;
108+
109+
// 1-indexing
110+
++i;
111+
++j;
112+
113+
if (type == 1) {
114+
int v;
115+
cin >> v;
116+
seg_tree.update(1, 1, n, i, j, v);
117+
}
118+
else {
119+
int nominator = seg_tree.query(1, 1, n, i, j);
120+
int denominator = j-i+1;
121+
int gcd = __gcd(nominator, denominator);
122+
nominator /= gcd;
123+
denominator /= gcd;
124+
125+
if (denominator == 1) cout << nominator << '\n';
126+
else cout << nominator << '/' << denominator << '\n';
127+
}
128+
}
129+
}
130+
131+
return 0;
132+
}
133+
```

0 commit comments

Comments
 (0)