diff --git a/Other_Tasks/CF1988_E_Range_Minimum_Sum.cpp b/Other_Tasks/CF1988_E_Range_Minimum_Sum.cpp new file mode 100644 index 0000000..834a9fd --- /dev/null +++ b/Other_Tasks/CF1988_E_Range_Minimum_Sum.cpp @@ -0,0 +1,103 @@ +#pragma GCC optimize("Ofast") +#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") +#pragma GCC optimize("unroll-loops") +#include +using namespace std; + +typedef long long ll; +typedef vector vi; + +int n; +vector a; +class rupq { + public: + vector arr; + + rupq(int n) { arr.resize(n + 1, 0); } + + void update(int l, int r, ll val) { // inclusive l, r + arr[l] += val; + arr[r + 1] -= val; + } + + vector get() { + vector res; + ll cur = 0; + for (int i = 0; i < arr.size() - 1; i++) { + cur += arr[i]; + res.push_back(cur); + } + return res; + } +}; + +int main() { + int tc; + cin >> tc; + while (tc--) { + cin >> n; + a.resize(n); + for (int i = 0; i < n; i++) cin >> a[i]; + + set s; + s.insert(-1); + s.insert(n); + vector contribution(n, 0); // Contribution via being minimum in a subarray + rupq adjustment(n); // Contribution via being present but not minimum in a subarray + vector extra(n, 0); // Extra contribution gotten by others if you were not present + + vector> x; + for (int i = 0; i < n; i++) { + x.push_back({a[i], i}); + } + sort(x.begin(), x.end()); + + ll fa = 0; + for (int i = 0; i < n; i++) { + // find the left and right bounds + ll val = x[i].first; + int idx = x[i].second; + auto rightit = s.upper_bound(idx); + auto leftit = rightit; + leftit--; + + ll right = *rightit; + ll left = *leftit; + + contribution[idx] = (val * (right - idx) * (idx - left)); + fa += contribution[idx]; + + // everything from left+1 to idx-1 will have the adjustment of (val * (right - idx)) + adjustment.update(left + 1, idx - 1, val * (right - idx)); + + // everything from idx+1 to right-1 will have the adjustment of (val * (idx - left)) + adjustment.update(idx + 1, right - 1, val * (idx - left)); + + if (left != -1) { + // if left was removed, we would get some extra contribution + auto lleftit = leftit; + lleftit--; + ll lleft = *lleftit; + extra[left] += ((left - lleft - 1) * (right - idx) * val); + } + + if (right != n) { + // if right was removed, we would get some extra contribution + auto rrightit = rightit; + rrightit++; + ll rright = *rrightit; + extra[right] += ((idx - left) * (rright - right - 1) * val); + } + + s.insert(idx); + } + + auto xx = adjustment.get(); + for (int i = 0; i < n; i++) { + cout << fa - contribution[i] - xx[i] + extra[i] << " "; + } + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/Other_Tasks/CF1990_A_Submission_Bait.py b/Other_Tasks/CF1990_A_Submission_Bait.py new file mode 100644 index 0000000..6d2cd15 --- /dev/null +++ b/Other_Tasks/CF1990_A_Submission_Bait.py @@ -0,0 +1,19 @@ +t = int(input()) +for _ in range(t): + n = int(input()) + a = list(map(int, input().split())) + + count = {} + for i in a: + if i in count: + count[i] += 1 + else: + count[i] = 1 + + # if count has an odd number then yes + for i in count: + if count[i] % 2 == 1: + print("YES") + break + else: + print("NO") diff --git a/Other_Tasks/CF1990_B_Array_Craft.py b/Other_Tasks/CF1990_B_Array_Craft.py new file mode 100644 index 0000000..3c4a690 --- /dev/null +++ b/Other_Tasks/CF1990_B_Array_Craft.py @@ -0,0 +1,22 @@ +t = int(input()) +for _ in range(t): + n, x, y = map(int, input().split()) + x-=1 + y-=1 + + arr = [0 for i in range(n)] + for i in range(y, x+1): + arr[i] = 1 + + c = -1 + for i in range(x+1, n): + arr[i] = c + c *= -1 + + c = -1 + for i in range(y-1, -1, -1): + arr[i] = c + c *= -1 + + print(*arr) + \ No newline at end of file diff --git a/Other_Tasks/CF1990_C_Mad_MAD_Sum.py b/Other_Tasks/CF1990_C_Mad_MAD_Sum.py new file mode 100644 index 0000000..34bffd0 --- /dev/null +++ b/Other_Tasks/CF1990_C_Mad_MAD_Sum.py @@ -0,0 +1,33 @@ +t = int(input()) +for _ in range(t): + n = int(input()) + arr = list(map(int, input().split())) + + def process(arr): + hasOne = set() + curMax = 0 + + brr = [0 for i in range(n)] + for i in range(n): + if arr[i] in hasOne: + curMax = max(curMax, arr[i]) + else: + hasOne.add(arr[i]) + + brr[i] = curMax + + return brr + + ans = sum(arr) + arr = process(arr) + + ans += sum(arr) + arr = process(arr) + + for i in range(n): + ans += arr[i]*(n-i) + + print(ans) + + + \ No newline at end of file diff --git a/Other_Tasks/CF1990_D_Grid_Puzzle.py b/Other_Tasks/CF1990_D_Grid_Puzzle.py new file mode 100644 index 0000000..ae35bad --- /dev/null +++ b/Other_Tasks/CF1990_D_Grid_Puzzle.py @@ -0,0 +1,43 @@ +from functools import lru_cache +from sys import setrecursionlimit + +t = int(input()) +for _ in range(t): + n = int(input()) + arr = list(map(int, input().split())) + + ans = 0 + state = None + for u in range(n): + if arr[u] == 0: + state = None + continue + + if state is None: + if arr[u] <= 2: + ans += 1 + state = 0 + else: + ans += 1 + state = None + elif state == 0: + if arr[u] <= 2: + state = None + elif arr[u] <= 4: + ans += 1 + state = 2 + else: + ans += 1 + state = None + elif state == 2: + if arr[u] <= 4: + ans += 1 + state = 0 + else: + ans += 1 + state = None + else: + assert False + + print(ans) + \ No newline at end of file