forked from MathProgrammer/CodeForces
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ef4792
commit 9f422db
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
2020/Div 2/643 Div 2/Explanation/Counting Triangles Explanation.txt.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
- We know $x < y \< z$ so we will count the number of triplets where $z < x + y$ | ||
- $x + y \in [A + B, A + C]$ | ||
- For every possible sum, we will calculate the number of ways of getting it. | ||
- If we fix a value of $x$, we can get every value from $x + B \to x + C$ | ||
- We can do this with a linear sweep. | ||
- For every possible $z$, we will count the number of ways to get any sum $> z$, | ||
which can be gotten using a suffix sum over the number of ways array. | ||
|
||
----- | ||
|
||
|
||
int main() | ||
{ | ||
int A, B, C, D; | ||
cin >> A >> B >> C >> D; | ||
|
||
vector <long long> no_of_starts(B + C + 5, 0), no_of_finishes(B + C + 5, 0); | ||
for(int x = A; x <= B; x++) | ||
{ | ||
no_of_starts[x + B]++; | ||
no_of_finishes[x + C + 1]++; | ||
} | ||
|
||
vector <long long> no_of_ways(B + C + 5, 0); | ||
for(int i = B; i <= B + C; i++) | ||
{ | ||
no_of_ways[i] = no_of_ways[i - 1] + no_of_starts[i] - no_of_finishes[i]; | ||
} | ||
|
||
vector <long long> suffix_sum(B + C + 5, 0); | ||
for(int i = B + C; i >= B; i--) | ||
{ | ||
suffix_sum[i] = suffix_sum[i + 1] + no_of_ways[i]; | ||
} | ||
|
||
long long no_of_triangles = 0; | ||
for(int z = C; z <= min(D, B + C); z++) | ||
{ | ||
no_of_triangles += suffix_sum[z + 1]; | ||
} | ||
|
||
cout << no_of_triangles << "\n"; | ||
return 0; | ||
} |