Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonTang89 authored Jul 21, 2024
1 parent 746725a commit 7963f95
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 0 deletions.
103 changes: 103 additions & 0 deletions Other_Tasks/CF1988_E_Range_Minimum_Sum.cpp
Original file line number Diff line number Diff line change
@@ -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 <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;

int n;
vector<ll> a;
class rupq {
public:
vector<ll> 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<ll> get() {
vector<ll> 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<int> s;
s.insert(-1);
s.insert(n);
vector<ll> contribution(n, 0); // Contribution via being minimum in a subarray
rupq adjustment(n); // Contribution via being present but not minimum in a subarray
vector<ll> extra(n, 0); // Extra contribution gotten by others if you were not present

vector<pair<ll, ll>> 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;
}
19 changes: 19 additions & 0 deletions Other_Tasks/CF1990_A_Submission_Bait.py
Original file line number Diff line number Diff line change
@@ -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")
22 changes: 22 additions & 0 deletions Other_Tasks/CF1990_B_Array_Craft.py
Original file line number Diff line number Diff line change
@@ -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)

33 changes: 33 additions & 0 deletions Other_Tasks/CF1990_C_Mad_MAD_Sum.py
Original file line number Diff line number Diff line change
@@ -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)



43 changes: 43 additions & 0 deletions Other_Tasks/CF1990_D_Grid_Puzzle.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 7963f95

Please sign in to comment.