|
Download tasks/equivalent-program-pair-search/contexts/sample-00035.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 207 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00035.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00035.md
-
curl -L -o sample-00035.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00035.md
207 kB
| ### 001 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| def result(a, n, m): | |
| r = 0 | |
| for i in range(-1, -m, -4): | |
| if a[-1][i] == 0: | |
| a[-1][i] = a[-1][i + 1] - 1 | |
| if a[-1][i] <= a[-1][i - 1] or a[-1][i] <= a[-2][i]: | |
| return -1 | |
| r = r + a[-1][i] | |
| if a[-1][-m] == 0: | |
| a[-1][-m] = a[-1][-m + 1] - 1 | |
| if a[-1][-m] <= a[-2][-m]: | |
| return -1 | |
| r = r + a[-1][-m] | |
| for i in range(-2, -n, -1): | |
| for j in range(-1, -m, -1): | |
| if j == -1: | |
| if a[i][j] == 0: | |
| a[i][j] == a[i + 1][j] - 1 | |
| if a[i][j] <= a[i][j - 1] or a[i][j] <= a[i - 1][j]: | |
| return -1 | |
| r = r + a[i][j] | |
| continue | |
| if a[i][j] == 0: | |
| a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1) | |
| if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]: | |
| return -1 | |
| r = r + a[i][j] | |
| if a[i][-m] == 0: | |
| a[i][-m] = min(a[i + 1][-m] - 1, a[i][-m + 1] - 1) | |
| if a[i][-m] <= a[i - 1][-m]: | |
| return -1 | |
| r = r + a[i][-m] | |
| if a[0][-1] == 0: | |
| a[0][-1] = a[1][-1] - 1 | |
| if a[0][-1] <= a[0][-2]: | |
| return -1 | |
| r = r + a[0][-1] | |
| for i in range(-2, -m, -1): | |
| if a[0][i] == 0: | |
| a[0][i] = min(a[0][i + 1] - 1, a[1][i] - 1) | |
| if a[0][i] <= a[0][i - 1]: | |
| return -1 | |
| r = r + a[0][i] | |
| if a[0][0] == 0: | |
| a[0][0] = min(a[0][1] - 1, a[1][0] - 1) | |
| r = r + a[0][0] | |
| return r | |
| print(result(a, n, m)) | |
| ``` | |
| ### 002 | |
| ```python | |
| N, C = list(map(int, input().split())) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| mod = 11 ** 9 + 7 | |
| acc = [[0] for _ in range(401)] | |
| for i in range(401): | |
| for j in range(1, 401): | |
| acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) | |
| dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] | |
| dp[0][0] = 1 | |
| for i in range(N): | |
| for candy_cur in range(C + 1): | |
| for candy_plus in range(C - candy_cur + 1): | |
| dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod | |
| print(dp[-1][C]) | |
| ``` | |
| ### 003 | |
| ```python | |
| from sys import stdin, stderr | |
| import random | |
| import cProfile | |
| def readInts(): | |
| return map(int, stdin.readline().strip().split()) | |
| def print_err(*args, **kwargs): | |
| print(*args, file=stderr, **kwargs) | |
| def solve(vs): | |
| return None | |
| def generate_tree(n, ns): | |
| out = [0 for _ in range(2 ** (n + 1))] | |
| def gt(nix, left, right, op): | |
| if left + 1 == right: | |
| out[nix] = ns[left] | |
| return out[nix] | |
| mid = (left + right) // 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 - 2 | |
| vL = gt(nL, left, mid, not op) | |
| vR = gt(nR, mid, right, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| out[nix] = v | |
| return v | |
| gt(0, 0, 2 ** n, n % 2 == 0) | |
| return out | |
| def alter_tree2(n, t, p, b): | |
| def at(nix, width, offp, op): | |
| if width == 1: | |
| t[nix] = b | |
| return b | |
| width //= 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = t[nL] | |
| vR = t[nR] | |
| if offp >= width: | |
| vR = at(nR, width, offp - width, not op) | |
| else: | |
| vL = at(nL, width, offp, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| t[nix] = v | |
| return v | |
| at(0, 2 ** n, p, n % 2 == 0) | |
| def alter_tree(n, t, p, b): | |
| width = 2 ** n | |
| s = [] | |
| nix = 0 | |
| op = n % 2 == 0 | |
| while width > 1: | |
| width //= 2 | |
| if p >= width: | |
| nix2 = 2 * nix + 2 | |
| s.append((nix, nix2 - 1)) | |
| p -= width | |
| else: | |
| nix2 = 2 * nix + 1 | |
| s.append((nix, nix2 + 1)) | |
| nix = nix2 | |
| op = not op | |
| t[nix] = b | |
| v = b | |
| while s: | |
| nix, nixO = s.pop() | |
| if op: | |
| v |= t[nixO] | |
| else: | |
| v ^= t[nixO] | |
| t[nix] = v | |
| op = not op | |
| return | |
| def run(): | |
| n, m = readInts() | |
| axs = list(readInts()) | |
| t = generate_tree(n, axs) | |
| for _ in range(m): | |
| p, b = readInts() | |
| alter_tree(n, t, p - 1, b) | |
| print(t[0]) | |
| def test(): | |
| n = 17 | |
| ns = [] | |
| vs100 = list(range(100)) | |
| for _ in range(2 ** 17): | |
| ns.append(random.choice(vs100)) | |
| t = generate_tree(n, ns) | |
| t2 = generate_tree(n, ns) | |
| for _ in range(100000): | |
| v1 = random.choice(vs100) | |
| v2 = random.choice(vs100) | |
| alter_tree(n, t, v1, v2) | |
| alter_tree2(n, t2, v1, v2) | |
| print(all(map(lambda x: x[0] == x[1], zip(t, t2)))) | |
| print(t[0] == t2[0]) | |
| run() | |
| ``` | |
| ### 004 | |
| ```python | |
| import sys | |
| def get_array(): | |
| return list(map(int, sys.stdin.readline().strip().split())) | |
| def get_ints(): | |
| return map(int, sys.stdin.readline().strip().split()) | |
| def input(): | |
| return sys.stdin.readline().strip() | |
| n, m = get_ints() | |
| Arr = get_array() | |
| up = [i for i in range(n)] | |
| down = [i for i in range(n)] | |
| for i in range(1, n): | |
| if Arr[i - 1] <= Arr[i]: | |
| up[i] = up[i - 1] | |
| if Arr[i - 1] >= Arr[i]: | |
| down[i] = down[i - 1] | |
| while m: | |
| l, r = get_ints() | |
| if l - 0 >= up[down[r - 1]]: | |
| print('Yes') | |
| else: | |
| print('No') | |
| m -= 1 | |
| ``` | |
| ### 005 | |
| ```python | |
| from collections import deque | |
| a = sorted(input()) | |
| l = len(a) | |
| a = deque(a[:(len(a) + 1) // 2]) | |
| b = sorted(input()) | |
| b = deque(b[len(b) - len(b) // 2:]) | |
| result = ['0'] * l | |
| left = -1 | |
| right = l | |
| while left <= right: | |
| if len(b) == 0: | |
| result[left + 1] = a[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = a[-1] | |
| a.pop() | |
| else: | |
| left += -1 | |
| result[left] = a[0] | |
| a.popleft() | |
| if len(a) == 0: | |
| result[left + 1] = b[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = b[0] | |
| b.popleft() | |
| else: | |
| left += 1 | |
| result[left] = b[-1] | |
| b.pop() | |
| print(''.join(result)) | |
| ``` | |
| ### 006 | |
| ```python | |
| s = input() | |
| t = input() | |
| n = len(s) | |
| vs = [0] * 26 | |
| vt = vs[:] | |
| for i in range(n): | |
| vs[ord(s[i]) - 97] += 1 | |
| vt[ord(t[i]) - 97] += 1 | |
| ns = n // 2 + n % 2 | |
| nt = n // 2 | |
| cur = 0 | |
| starts = -3 | |
| ends = 0 | |
| for i in range(26): | |
| if cur + vs[i] < ns: | |
| cur += vs[i] | |
| else: | |
| vs[i] = ns - cur | |
| cur = ns | |
| ends = i | |
| break | |
| cur = 0 | |
| startt = 0 | |
| endt = 25 | |
| for i in range(25, -1, -1): | |
| if cur + vt[i] < nt: | |
| cur += vt[i] | |
| else: | |
| vt[i] = nt - cur | |
| cur = nt | |
| startt = i | |
| break | |
| res = ['*'] * n | |
| start = 0 | |
| end = n - 1 | |
| for i in range(n): | |
| while starts < 26 and vs[starts] == 0: | |
| starts += 1 | |
| while ends >= 0 and vs[ends] == 0: | |
| ends -= 1 | |
| while startt < 26 and vt[startt] == 0: | |
| startt += 1 | |
| while endt >= 0 and vt[endt] == 0: | |
| endt -= 1 | |
| while res[start] != '*': | |
| start += 1 | |
| while res[end] != '*': | |
| end -= 1 | |
| if i % 2 == 0: | |
| if starts >= endt: | |
| res[end] = chr(97 + ends) | |
| vs[ends] -= 1 | |
| else: | |
| res[start] = chr(97 + starts) | |
| vs[starts] -= 1 | |
| elif endt <= starts: | |
| res[end] = chr(97 + startt) | |
| vt[startt] -= 1 | |
| else: | |
| res[start] = chr(97 + endt) | |
| vt[endt] -= 1 | |
| for i in range(n): | |
| print(res[i], end='') | |
| ``` | |
| ### 007 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| def result(a, n, m): | |
| r = 0 | |
| for i in range(-1, -m, -1): | |
| if a[-1][i] == 0: | |
| a[-1][i] = a[-1][i + 1] - 1 | |
| if a[-1][i] <= a[-1][i - 1] or a[-1][i] <= a[-2][i]: | |
| return -1 | |
| r = r + a[-1][i] | |
| if a[-1][-m] == 0: | |
| a[-1][-m] = a[-1][-m + 1] - 1 | |
| if a[-1][-m] <= a[-2][-m]: | |
| return -1 | |
| r = r + a[-1][-m] | |
| for i in range(-2, -n, -1): | |
| for j in range(-1, -m, -1): | |
| if j == -1: | |
| if a[i][j] == 0: | |
| a[i][j] == a[i + 1][j] - 1 | |
| if a[i][j] <= a[i][j - 1] or a[i][j] <= a[i - 1][j]: | |
| return -1 | |
| r = r + a[i][j] | |
| continue | |
| if a[i][j] == 0: | |
| a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1) | |
| if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]: | |
| return -1 | |
| r = r + a[i][j] | |
| if a[i][-m] == 0: | |
| a[i][-m] = min(a[i + 1][-m] - 1, a[i][-m + 1] - 1) | |
| if a[i][-m] <= a[i - 1][-m]: | |
| return -1 | |
| r = r + a[i][-m] | |
| if a[0][-1] == 0: | |
| a[0][-1] = a[1][-1] - 1 | |
| if a[0][-1] <= a[0][-2]: | |
| return -1 | |
| r = r + a[0][-1] | |
| for i in range(-2, -m, -1): | |
| if a[0][i] == 0: | |
| a[0][i] = min(a[0][i + 1] - 1, a[1][i] - 1) | |
| if a[0][i] <= a[0][i - 1]: | |
| return -1 | |
| r = r + a[0][i] | |
| if a[0][0] == 1: | |
| a[0][0] = min(a[0][1] - 1, a[1][0] - 1) | |
| r = r + a[0][0] | |
| return r | |
| print(result(a, n, m)) | |
| ``` | |
| ### 008 | |
| ```python | |
| N, C = list(map(int, input().split())) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| mod = 10 ** 9 + 5 | |
| acc = [[0] for _ in range(401)] | |
| for i in range(401): | |
| for j in range(1, 401): | |
| acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) | |
| dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] | |
| dp[0][0] = 1 | |
| for i in range(N): | |
| for candy_cur in range(C + 1): | |
| for candy_plus in range(C - candy_cur + 1): | |
| dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod | |
| print(dp[-1][C]) | |
| ``` | |
| ### 009 | |
| ```python | |
| s = input() | |
| t = input() | |
| n = len(s) | |
| vs = [0] * 26 | |
| vt = vs[:] | |
| for i in range(n): | |
| vs[ord(s[i]) - 97] += 1 | |
| vt[ord(t[i]) - 97] += -1 | |
| ns = n // 2 + n % 2 | |
| nt = n // 2 | |
| cur = 0 | |
| starts = 0 | |
| ends = 0 | |
| for i in range(26): | |
| if cur + vs[i] < ns: | |
| cur += vs[i] | |
| else: | |
| vs[i] = ns - cur | |
| cur = ns | |
| ends = i | |
| break | |
| cur = 0 | |
| startt = 0 | |
| endt = 25 | |
| for i in range(25, -1, -1): | |
| if cur + vt[i] < nt: | |
| cur += vt[i] | |
| else: | |
| vt[i] = nt - cur | |
| cur = nt | |
| startt = i | |
| break | |
| res = ['*'] * n | |
| start = 0 | |
| end = n - 1 | |
| for i in range(n): | |
| while starts < 26 and vs[starts] == 0: | |
| starts += 1 | |
| while ends >= 0 and vs[ends] == 0: | |
| ends -= 1 | |
| while startt < 26 and vt[startt] == 0: | |
| startt += 1 | |
| while endt >= 0 and vt[endt] == 0: | |
| endt -= 1 | |
| while res[start] != '*': | |
| start += 1 | |
| while res[end] != '*': | |
| end -= 1 | |
| if i % 2 == 0: | |
| if starts >= endt: | |
| res[end] = chr(97 + ends) | |
| vs[ends] -= 1 | |
| else: | |
| res[start] = chr(97 + starts) | |
| vs[starts] -= 1 | |
| elif endt <= starts: | |
| res[end] = chr(97 + startt) | |
| vt[startt] -= 1 | |
| else: | |
| res[start] = chr(97 + endt) | |
| vt[endt] -= 1 | |
| for i in range(n): | |
| print(res[i], end='') | |
| ``` | |
| ### 010 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m - 1): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1] = 0 | |
| for i in range(ni): | |
| for j in range(i, ni): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 011 | |
| ```python | |
| n = int(input()) | |
| L = [int(x) for x in input().split()] | |
| D = {} | |
| J = [] | |
| S = [] | |
| T = [0] * (n + 1) | |
| for i in range(n): | |
| if L[i] > 0: | |
| D[L[i]] = i | |
| J.append(L[i]) | |
| T[i + 1] = T[i] | |
| else: | |
| T[i + 1] = T[i] + 1 | |
| def I(J): | |
| if len(J) <= 1: | |
| return (J, 0) | |
| else: | |
| a = J[:len(J) // 2] | |
| b = J[len(J) // 2:] | |
| a, ai = I(a) | |
| b, bi = I(b) | |
| c = [] | |
| i = 0 | |
| j = 0 | |
| inversions = ai + bi | |
| while i < len(a) and j < len(b): | |
| if a[i] <= b[j]: | |
| c.append(a[i]) | |
| i += 1 | |
| else: | |
| c.append(b[j]) | |
| j += 1 | |
| inversions += len(a) - i | |
| c += a[i:] | |
| c += b[j:] | |
| return (c, inversions) | |
| for i in range(1, n + 1): | |
| if not i in D: | |
| S.append(i) | |
| total = len(S) | |
| num = 1 | |
| denom = 0 | |
| if total > 0: | |
| themostimportantsum = 0 | |
| for i in J: | |
| low = 0 | |
| high = total - 1 | |
| while high - low > 1: | |
| guess = (high + low) // 2 | |
| if S[guess] > i: | |
| high = guess | |
| else: | |
| low = guess | |
| if S[low] > i: | |
| smaller = low | |
| elif S[high] > i: | |
| smaller = high | |
| else: | |
| smaller = high + 1 | |
| themostimportantsum += T[D[i]] * (total - smaller) + (total - T[D[i]]) * smaller | |
| num = themostimportantsum + total | |
| denom = total | |
| num = (denom * (total * (total - 1) // 2) + 2 * num) % 998244353 | |
| denom *= 2 | |
| if num == denom: | |
| if I(J)[1] == 0: | |
| print(0) | |
| else: | |
| print(I(J)[1] % 998244353) | |
| else: | |
| num += denom * I(J)[1] | |
| print((num - denom) * pow(denom % 998244353, 998244351, 998244353) % 998244353) | |
| ``` | |
| ### 012 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - -1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 013 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1 - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 014 | |
| ```python | |
| K = 998244353 | |
| def mu(a, n): | |
| if n == 0: | |
| return 1 | |
| q = mu(a, n // 2) | |
| if n % 2 == 0: | |
| return q * q % K | |
| return q * q % K * a % K | |
| MAXN = 200005 | |
| dd = [-4 for i in range(MAXN)] | |
| p = [0 for i in range(MAXN)] | |
| s = [0 for i in range(MAXN)] | |
| a = [0 for i in range(MAXN)] | |
| fen = [0 for i in range(MAXN)] | |
| def add(u, v): | |
| i = u | |
| while i <= 200000: | |
| fen[i] += v | |
| i += i & -i | |
| def get(u): | |
| res = 0 | |
| i = u | |
| while i > 0: | |
| res += fen[i] | |
| i -= i & -i | |
| return res | |
| n = int(input()) | |
| data = input().split() | |
| cnt = 0 | |
| for i in range(1, n + 1): | |
| p[i] = int(data[i - 1]) | |
| if p[i] > 0: | |
| dd[p[i]] = 1 | |
| else: | |
| cnt += 1 | |
| for i in range(1, n + 1): | |
| if dd[i] == 0: | |
| s[i] = s[i - 1] + 1 | |
| else: | |
| s[i] = s[i - 1] | |
| cnt1 = 0 | |
| P = 0 | |
| den = mu(cnt, K - 2) | |
| for i in range(1, n + 1): | |
| if p[i] == -1: | |
| cnt1 += 1 | |
| else: | |
| u = cnt - cnt1 | |
| P = (P + u * s[p[i]] % K * den % K) % K | |
| P = (P + cnt1 * (cnt - s[p[i]]) % K * den % K) % K | |
| P = (P + cnt * (cnt - 1) * mu(4, K - 2)) % K | |
| m = 0 | |
| for i in range(1, n + 1): | |
| if p[i] > 0: | |
| m += 1 | |
| a[m] = p[i] | |
| P1 = 0 | |
| for i in range(m, 0, -1): | |
| P1 = (P1 + get(a[i])) % K | |
| add(a[i], 1) | |
| P = (P + P1) % K | |
| print(P) | |
| ``` | |
| ### 015 | |
| ```python | |
| def merge(a, b): | |
| inda = 0 | |
| indb = 0 | |
| lena = len(a) | |
| lenb = len(b) | |
| d = [a[-1] + b[-1] + 1000] | |
| a += d | |
| b += d | |
| c = [] | |
| inversions = 0 | |
| for i in range(lena + lenb + 1): | |
| if a[inda] < b[indb]: | |
| c.append(a[inda]) | |
| inda += 1 | |
| else: | |
| c.append(b[indb]) | |
| indb += 1 | |
| inversions += lena - inda | |
| return (c, inversions) | |
| def mergesort(a): | |
| if len(a) <= 1: | |
| return (a, 0) | |
| split = len(a) // 2 | |
| b = a[:split] | |
| c = a[split:] | |
| d = mergesort(b) | |
| e = mergesort(c) | |
| f = merge(d[0], e[0]) | |
| return (f[0], f[1] + d[1] + e[1]) | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = [] | |
| for guy in a: | |
| if guy != -1: | |
| b.append(guy) | |
| invs = mergesort(b)[1] | |
| negs = len(a) - len(b) | |
| pairs = negs * (negs - 1) // 2 | |
| used = [0] * n | |
| for guy in a: | |
| if guy != -1: | |
| used[guy - 1] += 1 | |
| unused = [0] | |
| for i in range(n - 1): | |
| unused.append(unused[-1] + 1 - used[i]) | |
| negsseen = 0 | |
| mix = 0 | |
| for i in range(n): | |
| if a[i] == -1: | |
| negsseen += 1 | |
| else: | |
| mix += unused[a[i] - 1] * (negs - negsseen) + negsseen * (negs - unused[a[i] - 1]) | |
| num = invs * 2 * negs + pairs * negs + mix * 2 | |
| denom = 2 * negs | |
| if negs == 0: | |
| print(invs % 998244353) | |
| else: | |
| for i in range(denom): | |
| if (998244353 * i + 1) % denom == 0: | |
| inv = (998244353 * i + 1) // denom | |
| break | |
| print(num * inv % 998244353) | |
| ``` | |
| ### 016 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 1 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) < (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 017 | |
| ```python | |
| import sys | |
| n, m = list(map(int, sys.stdin.readline().split())) | |
| M = [m] | |
| A = list(map(int, sys.stdin.readline().split())) | |
| L = [0] * n | |
| inc = False | |
| dec = False | |
| def ALLYes(): | |
| Ans = '' | |
| for i in range(M[0]): | |
| Ans += 'Yes\n' | |
| sys.stdout.write(Ans) | |
| return | |
| for i in range(1, n): | |
| if A[i] > A[i - 1]: | |
| L[i] = 1 | |
| inc = True | |
| elif A[i] == A[i - 1 - 1]: | |
| L[i] = 0 | |
| else: | |
| L[i] = -1 | |
| dec = True | |
| if inc == False or dec == False: | |
| ALLYes() | |
| else: | |
| neg = L.index(-1) | |
| pos = L.index(1) | |
| First = [-1] * n | |
| for i in range(2, n): | |
| if L[i] == 0: | |
| x = max(neg, pos) | |
| if x <= i: | |
| First[i] = x | |
| elif min(neg, pos) <= i: | |
| First[i] = min(neg, pos) | |
| else: | |
| First[i] = -1 | |
| if L[i] == 1: | |
| if neg > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = neg | |
| pos = i | |
| if L[i] == -1: | |
| if pos > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = pos | |
| neg = i | |
| Ans = '' | |
| for i in range(m): | |
| l, r = list(map(int, sys.stdin.readline().split())) | |
| r -= 1 | |
| if r - l < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 0: | |
| r = First[r] | |
| if r < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| elif L[r] == -1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| sys.stdout.write(Ans) | |
| ``` | |
| ### 018 | |
| ```python | |
| import sys | |
| class SegmTree: | |
| def __init__(self, size): | |
| N = 1 | |
| while N < size: | |
| N <<= 1 | |
| self.N = N | |
| self.tree = [0] * (2 * N) | |
| def modify(self, i, value): | |
| i += self.N | |
| self.tree[i] = value | |
| toXOR = False | |
| while i > 1: | |
| if toXOR: | |
| self.tree[i >> 1] = self.tree[i] ^ self.tree[i | 1] | |
| else: | |
| self.tree[i >> 1] = self.tree[i] | self.tree[i ^ 1] | |
| toXOR = not toXOR | |
| i >>= 1 | |
| reader = (line.rstrip() for line in sys.stdin) | |
| input = reader.__next__ | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| st = SegmTree(1 << n) | |
| for i, value in enumerate(a): | |
| st.modify(i, value) | |
| for _ in range(m): | |
| p, b = list(map(int, input().split())) | |
| st.modify(p - 1, b) | |
| print(st.tree[1]) | |
| ``` | |
| ### 019 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[0]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1]) | |
| if s[i - 1] == 'U': | |
| dy[i] += 5 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -1 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 020 | |
| ```python | |
| oleg = input() | |
| igor = input() | |
| oleg = sorted(list(oleg)) | |
| igor = sorted(list(igor)) | |
| n = len(oleg) | |
| oleg_turns = (n + 1) // 2 | |
| igor_turns = n // 2 | |
| min_oleg_id = 0 | |
| min_igor_id = n - igor_turns | |
| ans = ['?'] * n | |
| max_oleg_id = oleg_turns - 1 | |
| max_igor_id = n - 1 | |
| curr_turn = 'o' | |
| next_turn = {'o': 'i', 'i': 'o'} | |
| l_ans = 0 | |
| r_ans = n - 1 | |
| while r_ans >= l_ans: | |
| if curr_turn == 'o': | |
| if oleg[min_oleg_id] < igor[max_igor_id]: | |
| ans[l_ans] = oleg[min_oleg_id] | |
| l_ans += 1 | |
| min_oleg_id += 1 | |
| else: | |
| ans[r_ans] = oleg[max_oleg_id] | |
| r_ans += -1 | |
| max_oleg_id += -1 | |
| curr_turn = 'i' | |
| else: | |
| if igor[max_igor_id] > oleg[min_oleg_id]: | |
| ans[l_ans] = igor[max_igor_id] | |
| l_ans += 1 | |
| max_igor_id += -1 | |
| else: | |
| ans[r_ans] = igor[min_igor_id] | |
| r_ans += -1 | |
| min_igor_id += 1 | |
| curr_turn = 'o' | |
| strans = ''.join(ans) | |
| print(strans) | |
| ``` | |
| ### 021 | |
| ```python | |
| from operator import or_, xor | |
| import sys | |
| n, m = map(int, input().split()) | |
| t = [list(map(int, input().split()))] | |
| for i in range(n): | |
| if i & 1 == 0: | |
| t += [[t[i][j] | t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| else: | |
| t += [[t[i][j] ^ t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| for s in sys.stdin: | |
| p, b = s.split() | |
| p = int(p) - 1 | |
| t[0][p] = int(b) | |
| for j in range(n): | |
| p = int(p / 2) | |
| if j & 1 == 0: | |
| t[j + 1][p] = t[j][int(p * 2)] | t[j][int(p * 2) + 1] | |
| else: | |
| t[j + 1][p] = t[j][int(p * 2)] ^ t[j - 1][int(p * 2) + 1] | |
| sys.stdout.write(str(t[-1][0]) + '\n') | |
| ``` | |
| ### 022 | |
| ```python | |
| base = 998244353 | |
| def power(x, y): | |
| if y == 0: | |
| return 1 | |
| t = power(x, y // 2) | |
| t = t * t % base | |
| if y % 2: | |
| t = t * x % base | |
| return t | |
| def inverse(x): | |
| return power(x, base - 2) | |
| ft = [0] | |
| for i in range(0, 200000): | |
| ft.append(0) | |
| def get(i): | |
| res = 0 | |
| while i <= 200000: | |
| res += ft[i] | |
| i += i & -i | |
| return res | |
| def update(i, x): | |
| while i: | |
| ft[i] += x | |
| i -= i & -i | |
| n = int(input()) | |
| a = [0] | |
| a += list(map(int, input().split())) | |
| neg = [0] | |
| non = [0] | |
| for i in range(1, n + 1): | |
| non.append(0) | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| non[a[i]] += 1 | |
| for i in range(1, n + 1): | |
| non[i] += non[i - 1] | |
| for i in range(1, n + 1): | |
| if a[i] == -1: | |
| neg.append(neg[i - 1] + 1) | |
| else: | |
| neg.append(neg[i - 1]) | |
| m = neg[n] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| ans += get(a[i]) | |
| update(a[i], 1) | |
| fm = 1 | |
| fs = fm | |
| for i in range(1, m + 1 - 1): | |
| fs = fm | |
| fm = fm * i % base | |
| fs = fs * inverse(fm) % base | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| less = a[i] - non[a[i]] | |
| more = m - less | |
| ans = (ans + neg[i] * more * fs) % base | |
| ans = (ans + (m - neg[i]) * less * fs) % base | |
| ans = (ans + m * (m - 1) * inverse(4)) % base | |
| print(ans) | |
| ``` | |
| ### 023 | |
| ```python | |
| import math, sys | |
| from sys import stdin, stdout | |
| from collections import Counter, defaultdict, deque | |
| input = stdin.readline | |
| I = lambda: int(input()) | |
| li = lambda: list(map(int, input().split())) | |
| def case(): | |
| n, q = li() | |
| a = li() | |
| up = [0] * n | |
| down = [0] * n | |
| i = 0 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] <= a[j + 1]: | |
| j += 1 | |
| for k in range(i + 1, j + 1): | |
| up[k] = j | |
| i = j + 1 | |
| i = 0 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] >= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| down[k] = j | |
| i = j + 1 | |
| for i in range(q): | |
| l, r = li() | |
| p = up[l - 1] | |
| q = down[p] | |
| if q + 1 >= r: | |
| print('Yes') | |
| else: | |
| print('No') | |
| for _ in range(1): | |
| case() | |
| ``` | |
| ### 024 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [3] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 025 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| [n, q] = [int(item) for item in input().split(' ')] | |
| arr = [int(item) for item in input().split(' ')] | |
| queries = [[int(item) for item in input().split(' ')] for i in range(q)] | |
| b = [1] | |
| for i in range(1, n): | |
| b.append(b[-1] + -1 if arr[i] <= arr[i - 1] else 1) | |
| c = [1] | |
| for i in reversed(list(range(n - 1))): | |
| c.append(c[-1] + 1 if arr[i] <= arr[i + 1] else 1) | |
| c = [item for item in reversed(c)] | |
| for query in queries: | |
| x, y = query | |
| x -= 1 | |
| y -= 1 | |
| print('Yes' if x + c[x] > y or y - b[y] < x or x + c[x] > y - b[y] else 'No') | |
| '\n5 1\n1 3 3 2 2\n1 4\n\n5 1\n1 2 2 1 1\n1 4\n\n5 1\n2 2 1 1 2\n1 5\n' | |
| ``` | |
| ### 026 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s - g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s *= s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 027 | |
| ```python | |
| from math import log | |
| import sys | |
| def buildTree(arr): | |
| n = len(arr) | |
| tree = [0] * n + arr | |
| for i in range(n - 1 - 1, 0, -1): | |
| z = int(log(i, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| elif z % 2 == 0: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| return tree | |
| def updateTree(tree, ind, value, n): | |
| ind += n | |
| tree[ind] = value | |
| while ind > 1: | |
| ind //= 2 | |
| z = int(log(ind, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| elif z % 2 == 0: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| return tree | |
| N, m = map(int, sys.stdin.readline().strip().split()) | |
| arr = list(map(int, sys.stdin.readline().strip().split())) | |
| tree = buildTree(arr) | |
| for i in range(m): | |
| ind, val = map(int, sys.stdin.readline().strip().split()) | |
| tree = updateTree(tree, ind - 1, val, len(arr)) | |
| print(tree[1]) | |
| ``` | |
| ### 028 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| def result(a, n, m): | |
| r = 0 | |
| for i in range(--1, -m, -1): | |
| if a[-1][i] == 0: | |
| a[-1][i] = a[-1][i + 1] - 1 | |
| if a[-1][i] <= a[-1][i - 1] or a[-1][i] <= a[-2][i]: | |
| return -1 | |
| r = r + a[-1][i] | |
| if a[-1][-m] == 0: | |
| a[-1][-m] = a[-1][-m + 1] - 1 | |
| if a[-1][-m] <= a[-2][-m]: | |
| return -1 | |
| r = r + a[-1][-m] | |
| for i in range(-2, -n, -1): | |
| for j in range(-1, -m, -1): | |
| if j == -1: | |
| if a[i][j] == 0: | |
| a[i][j] == a[i + 1][j] - 1 | |
| if a[i][j] <= a[i][j - 1] or a[i][j] <= a[i - 1][j]: | |
| return -1 | |
| r = r + a[i][j] | |
| continue | |
| if a[i][j] == 0: | |
| a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1) | |
| if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]: | |
| return -1 | |
| r = r + a[i][j] | |
| if a[i][-m] == 0: | |
| a[i][-m] = min(a[i + 1][-m] - 1, a[i][-m + 1] - 1) | |
| if a[i][-m] <= a[i - 1][-m]: | |
| return -1 | |
| r = r + a[i][-m] | |
| if a[0][-1] == 0: | |
| a[0][-1] = a[1][-1] - 1 | |
| if a[0][-1] <= a[0][-2]: | |
| return -1 | |
| r = r + a[0][-1] | |
| for i in range(-2, -m, -1): | |
| if a[0][i] == 0: | |
| a[0][i] = min(a[0][i + 1] - 1, a[1][i] - 1) | |
| if a[0][i] <= a[0][i - 1]: | |
| return -1 | |
| r = r + a[0][i] | |
| if a[0][0] == 0: | |
| a[0][0] = min(a[0][1] - 1, a[1][0] - 1) | |
| r = r + a[0][0] | |
| return r | |
| print(result(a, n, m)) | |
| ``` | |
| ### 029 | |
| ```python | |
| N, C = list(map(int, input().split())) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| mod = 10 ** 9 + 7 | |
| acc = [[0] for _ in range(401)] | |
| for i in range(401): | |
| for j in range(1, 401): | |
| acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) | |
| dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] | |
| dp[0][0] = 1 | |
| for i in range(N): | |
| for candy_cur in range(C + 1): | |
| for candy_plus in range(C - candy_cur + 1): | |
| dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod | |
| print(dp[-1][C]) | |
| ``` | |
| ### 030 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] - g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 031 | |
| ```python | |
| from math import log | |
| import sys | |
| def buildTree(arr): | |
| n = len(arr) | |
| tree = [0] * n + arr | |
| for i in range(n - 1, 0, -1): | |
| z = int(log(i, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| elif z % 2 == 0: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| return tree | |
| def updateTree(tree, ind, value, n): | |
| ind += n | |
| tree[ind] = value | |
| while ind > 1: | |
| ind //= 2 | |
| z = int(log(ind, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| elif z % 2 == 0: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| return tree | |
| N, m = map(int, sys.stdin.readline().strip().split()) | |
| arr = list(map(int, sys.stdin.readline().strip().split())) | |
| tree = buildTree(arr) | |
| for i in range(m): | |
| ind, val = map(int, sys.stdin.readline().strip().split()) | |
| tree = updateTree(tree, ind - 1, val, len(arr)) | |
| print(tree[-3]) | |
| ``` | |
| ### 032 | |
| ```python | |
| import math, sys | |
| from sys import stdin, stdout | |
| from collections import Counter, defaultdict, deque | |
| input = stdin.readline | |
| I = lambda: int(input()) | |
| li = lambda: list(map(int, input().split())) | |
| def case(): | |
| n, q = li() | |
| a = li() | |
| up = [0] * n | |
| down = [0] * n | |
| i = 4 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] <= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| up[k] = j | |
| i = j + 1 | |
| i = 0 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] >= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| down[k] = j | |
| i = j + 1 | |
| for i in range(q): | |
| l, r = li() | |
| p = up[l - 1] | |
| q = down[p] | |
| if q + 1 >= r: | |
| print('Yes') | |
| else: | |
| print('No') | |
| for _ in range(1): | |
| case() | |
| ``` | |
| ### 033 | |
| ```python | |
| import math, sys | |
| from sys import stdin, stdout | |
| from collections import Counter, defaultdict, deque | |
| input = stdin.readline | |
| I = lambda: int(input()) | |
| li = lambda: list(map(int, input().split())) | |
| def case(): | |
| n, q = li() | |
| a = li() | |
| up = [0] * n | |
| down = [0] * n | |
| i = 0 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] <= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| up[k] = j | |
| i = j + 1 | |
| i = 2 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] >= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| down[k] = j | |
| i = j + 1 | |
| for i in range(q): | |
| l, r = li() | |
| p = up[l - 1] | |
| q = down[p] | |
| if q + 1 >= r: | |
| print('Yes') | |
| else: | |
| print('No') | |
| for _ in range(1): | |
| case() | |
| ``` | |
| ### 034 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -1 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[3:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 035 | |
| ```python | |
| import sys | |
| n, m = list(map(int, sys.stdin.readline().split())) | |
| M = [m] | |
| A = list(map(int, sys.stdin.readline().split())) | |
| L = [0] * n | |
| inc = False | |
| dec = False | |
| def ALLYes(): | |
| Ans = '' | |
| for i in range(M[0]): | |
| Ans += 'Yes\n' | |
| sys.stdout.write(Ans) | |
| return | |
| for i in range(1, n): | |
| if A[i] > A[i - 1]: | |
| L[i] = 1 | |
| inc = True | |
| elif A[i] == A[i - 1]: | |
| L[i] = 0 | |
| else: | |
| L[i] = -1 | |
| dec = True | |
| if inc == False or dec == False: | |
| ALLYes() | |
| else: | |
| neg = L.index(-1) | |
| pos = L.index(1) | |
| First = [-1] * n | |
| for i in range(2, n): | |
| if L[i] == 0: | |
| x = max(neg, pos) | |
| if x <= i: | |
| First[i] = x | |
| elif min(neg, pos) <= i: | |
| First[i] = min(neg, pos) | |
| else: | |
| First[i] = -1 | |
| if L[i] == 1: | |
| if neg > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = neg | |
| pos = i | |
| if L[i] == -1: | |
| if pos > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = pos | |
| neg = i | |
| Ans = '' | |
| for i in range(m): | |
| l, r = list(map(int, sys.stdin.readline().split())) | |
| r -= 1 | |
| if r - l <= 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 0: | |
| r = First[r] | |
| if r < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| elif L[r] == -1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| sys.stdout.write(Ans) | |
| ``` | |
| ### 036 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h < r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 037 | |
| ```python | |
| import sys | |
| read = sys.stdin.read | |
| readline = sys.stdin.readline | |
| n, c = list(map(int, readline().split())) | |
| *a, = list(map(int, readline().split())) | |
| *b, = list(map(int, readline().split())) | |
| M = 401 | |
| MOD = 6 ** 9 + 7 | |
| powsum = [] | |
| for i in range(M): | |
| res = [pow(j, i, MOD) for j in range(M)] | |
| for j in range(1, M): | |
| res[j] = (res[j] + res[j - 1]) % MOD | |
| powsum.append(res) | |
| dp = [0] * (c + 1) | |
| dp[0] = 1 | |
| for ai, bi in zip(a, b): | |
| ndp = [0] * (c + 1) | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| ndp[k] += (powsum[j][bi] - powsum[j][ai - 1]) * dp[k - j] % MOD | |
| ndp[k] %= MOD | |
| dp = ndp | |
| print(dp[c] % MOD) | |
| ``` | |
| ### 038 | |
| ```python | |
| base = 998244353 | |
| def power(x, y): | |
| if y == 0: | |
| return 1 | |
| t = power(x, y // 2) | |
| t = t * t % base | |
| if y % 2: | |
| t = t * x % base | |
| return t | |
| def inverse(x): | |
| return power(x, base - 2) | |
| ft = [0] | |
| for i in range(0, 200000): | |
| ft.append(0) | |
| def get(i): | |
| res = 0 | |
| while i <= 200000: | |
| res += ft[i] | |
| i += i & -i | |
| return res | |
| def update(i, x): | |
| while i: | |
| ft[i] += x | |
| i -= i & -i | |
| n = int(input()) | |
| a = [0] | |
| a += list(map(int, input().split())) | |
| neg = [0] | |
| non = [0] | |
| for i in range(1, n + 1): | |
| non.append(0) | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| non[a[i]] += 1 | |
| for i in range(1, n + 1 - 1): | |
| non[i] += non[i - 1] | |
| for i in range(1, n + 1): | |
| if a[i] == -1: | |
| neg.append(neg[i - 1] + 1) | |
| else: | |
| neg.append(neg[i - 1]) | |
| m = neg[n] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| ans += get(a[i]) | |
| update(a[i], 1) | |
| fm = 1 | |
| fs = fm | |
| for i in range(1, m + 1): | |
| fs = fm | |
| fm = fm * i % base | |
| fs = fs * inverse(fm) % base | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| less = a[i] - non[a[i]] | |
| more = m - less | |
| ans = (ans + neg[i] * more * fs) % base | |
| ans = (ans + (m - neg[i]) * less * fs) % base | |
| ans = (ans + m * (m - 1) * inverse(4)) % base | |
| print(ans) | |
| ``` | |
| ### 039 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -2 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 040 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [4] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 041 | |
| ```python | |
| n, m = map(int, input().split()) | |
| matrix = [list(map(int, input().split())) for _ in range(n)] | |
| res = 0 | |
| for i in range(n - 2, 0, -1): | |
| for j in range(m - 1, 0, -1): | |
| if matrix[i][j] == 0: | |
| mn = min(matrix[i + 1][j], matrix[i][j + 1]) | |
| if mn != 1: | |
| matrix[i][j] = mn - 1 | |
| else: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| else: | |
| for i in range(n): | |
| if [matrix[i][j] for j in range(m - 1) if matrix[i][j] > matrix[i][j + 1]]: | |
| res = -1 | |
| break | |
| else: | |
| for j in range(m): | |
| if [matrix[i][j] for i in range(n - 1) if matrix[i][j] >= matrix[i + 1][j]]: | |
| res = -1 | |
| break | |
| else: | |
| res = sum((sum(matrix[i]) for i in range(n))) | |
| print(res) | |
| ``` | |
| ### 042 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = 0 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem *= 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 043 | |
| ```python | |
| from sys import stdin, stderr | |
| import random | |
| import cProfile | |
| def readInts(): | |
| return map(int, stdin.readline().strip().split()) | |
| def print_err(*args, **kwargs): | |
| print(*args, file=stderr, **kwargs) | |
| def solve(vs): | |
| return None | |
| def generate_tree(n, ns): | |
| out = [0 for _ in range(2 ** (n + 1))] | |
| def gt(nix, left, right, op): | |
| if left + 1 == right: | |
| out[nix] = ns[left] | |
| return out[nix] | |
| mid = (left + right) // 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = gt(nL, left, mid, not op) | |
| vR = gt(nR, mid, right, not op) | |
| if not op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| out[nix] = v | |
| return v | |
| gt(0, 0, 2 ** n, n % 2 == 0) | |
| return out | |
| def alter_tree2(n, t, p, b): | |
| def at(nix, width, offp, op): | |
| if width == 1: | |
| t[nix] = b | |
| return b | |
| width //= 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = t[nL] | |
| vR = t[nR] | |
| if offp >= width: | |
| vR = at(nR, width, offp - width, not op) | |
| else: | |
| vL = at(nL, width, offp, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| t[nix] = v | |
| return v | |
| at(0, 2 ** n, p, n % 2 == 0) | |
| def alter_tree(n, t, p, b): | |
| width = 2 ** n | |
| s = [] | |
| nix = 0 | |
| op = n % 2 == 0 | |
| while width > 1: | |
| width //= 2 | |
| if p >= width: | |
| nix2 = 2 * nix + 2 | |
| s.append((nix, nix2 - 1)) | |
| p -= width | |
| else: | |
| nix2 = 2 * nix + 1 | |
| s.append((nix, nix2 + 1)) | |
| nix = nix2 | |
| op = not op | |
| t[nix] = b | |
| v = b | |
| while s: | |
| nix, nixO = s.pop() | |
| if op: | |
| v |= t[nixO] | |
| else: | |
| v ^= t[nixO] | |
| t[nix] = v | |
| op = not op | |
| return | |
| def run(): | |
| n, m = readInts() | |
| axs = list(readInts()) | |
| t = generate_tree(n, axs) | |
| for _ in range(m): | |
| p, b = readInts() | |
| alter_tree(n, t, p - 1, b) | |
| print(t[0]) | |
| def test(): | |
| n = 17 | |
| ns = [] | |
| vs100 = list(range(100)) | |
| for _ in range(2 ** 17): | |
| ns.append(random.choice(vs100)) | |
| t = generate_tree(n, ns) | |
| t2 = generate_tree(n, ns) | |
| for _ in range(100000): | |
| v1 = random.choice(vs100) | |
| v2 = random.choice(vs100) | |
| alter_tree(n, t, v1, v2) | |
| alter_tree2(n, t2, v1, v2) | |
| print(all(map(lambda x: x[0] == x[1], zip(t, t2)))) | |
| print(t[0] == t2[0]) | |
| run() | |
| ``` | |
| ### 044 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| [n, q] = [int(item) for item in input().split(' ')] | |
| arr = [int(item) for item in input().split(' ')] | |
| queries = [[int(item) for item in input().split(' ')] for i in range(q)] | |
| b = [1] | |
| for i in range(1, n): | |
| b.append(b[-1] + 1 if arr[i] <= arr[i - 1 - 1] else 1) | |
| c = [1] | |
| for i in reversed(list(range(n - 1))): | |
| c.append(c[-1] + 1 if arr[i] <= arr[i + 1] else 1) | |
| c = [item for item in reversed(c)] | |
| for query in queries: | |
| x, y = query | |
| x -= 1 | |
| y -= 1 | |
| print('Yes' if x + c[x] > y or y - b[y] < x or x + c[x] > y - b[y] else 'No') | |
| '\n5 1\n1 3 3 2 2\n1 4\n\n5 1\n1 2 2 1 1\n1 4\n\n5 1\n2 2 1 1 2\n1 5\n' | |
| ``` | |
| ### 045 | |
| ```python | |
| oleg = input() | |
| igor = input() | |
| oleg = sorted(list(oleg)) | |
| igor = sorted(list(igor)) | |
| n = len(oleg) | |
| oleg_turns = (n + 1) // 2 | |
| igor_turns = n // 1 | |
| min_oleg_id = 0 | |
| min_igor_id = n - igor_turns | |
| ans = ['?'] * n | |
| max_oleg_id = oleg_turns - 1 | |
| max_igor_id = n - 1 | |
| curr_turn = 'o' | |
| next_turn = {'o': 'i', 'i': 'o'} | |
| l_ans = 0 | |
| r_ans = n - 1 | |
| while r_ans >= l_ans: | |
| if curr_turn == 'o': | |
| if oleg[min_oleg_id] < igor[max_igor_id]: | |
| ans[l_ans] = oleg[min_oleg_id] | |
| l_ans += 1 | |
| min_oleg_id += 1 | |
| else: | |
| ans[r_ans] = oleg[max_oleg_id] | |
| r_ans += -1 | |
| max_oleg_id += -1 | |
| curr_turn = 'i' | |
| else: | |
| if igor[max_igor_id] > oleg[min_oleg_id]: | |
| ans[l_ans] = igor[max_igor_id] | |
| l_ans += 1 | |
| max_igor_id += -1 | |
| else: | |
| ans[r_ans] = igor[min_igor_id] | |
| r_ans += -1 | |
| min_igor_id += 1 | |
| curr_turn = 'o' | |
| strans = ''.join(ans) | |
| print(strans) | |
| ``` | |
| ### 046 | |
| ```python | |
| n = int(input()) | |
| L = [int(x) for x in input().split()] | |
| D = {} | |
| J = [] | |
| S = [] | |
| T = [0] * (n + 1) | |
| for i in range(n): | |
| if L[i] > 0: | |
| D[L[i]] = i | |
| J.append(L[i]) | |
| T[i + 1] = T[i] | |
| else: | |
| T[i + 1] = T[i] + 1 | |
| def I(J): | |
| if len(J) <= 1: | |
| return (J, 0) | |
| else: | |
| a = J[:len(J) // 2] | |
| b = J[len(J) // 2:] | |
| a, ai = I(a) | |
| b, bi = I(b) | |
| c = [] | |
| i = 0 | |
| j = 0 | |
| inversions = ai + bi | |
| while i < len(a) and j < len(b): | |
| if a[i] <= b[j]: | |
| c.append(a[i]) | |
| i += 1 | |
| else: | |
| c.append(b[j]) | |
| j += 1 | |
| inversions += len(a) - i | |
| c += a[i:] | |
| c += b[j:] | |
| return (c, inversions) | |
| for i in range(1, n + 1 - 1): | |
| if not i in D: | |
| S.append(i) | |
| total = len(S) | |
| num = 1 | |
| denom = 1 | |
| if total > 0: | |
| themostimportantsum = 0 | |
| for i in J: | |
| low = 0 | |
| high = total - 1 | |
| while high - low > 1: | |
| guess = (high + low) // 2 | |
| if S[guess] > i: | |
| high = guess | |
| else: | |
| low = guess | |
| if S[low] > i: | |
| smaller = low | |
| elif S[high] > i: | |
| smaller = high | |
| else: | |
| smaller = high + 1 | |
| themostimportantsum += T[D[i]] * (total - smaller) + (total - T[D[i]]) * smaller | |
| num = themostimportantsum + total | |
| denom = total | |
| num = (denom * (total * (total - 1) // 2) + 2 * num) % 998244353 | |
| denom *= 2 | |
| if num == denom: | |
| if I(J)[1] == 0: | |
| print(0) | |
| else: | |
| print(I(J)[1] % 998244353) | |
| else: | |
| num += denom * I(J)[1] | |
| print((num - denom) * pow(denom % 998244353, 998244351, 998244353) % 998244353) | |
| ``` | |
| ### 047 | |
| ```python | |
| def ii(): | |
| return int(input()) | |
| def ss(): | |
| return [x for x in input()] | |
| def si(): | |
| return [int(x) for x in input().split()] | |
| def mi(): | |
| return map(int, input().split()) | |
| a, b = mi() | |
| s = [] | |
| ans = 0 | |
| key = True | |
| for i in range(a): | |
| i = si() | |
| s.append(i) | |
| ans += sum(i) | |
| for i in range(a - 1): | |
| if not (s[i][0] < s[i + 1][0] and s[i][-1] < s[i + 1][-1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(b - 1): | |
| if not (s[0][i] < s[0][i + 1] and s[-1][i] < s[-1][i + 1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(a - 2, 1, -1): | |
| if not key: | |
| break | |
| for x in range(b - 2, 0, -1): | |
| if s[i][x] < s[i + 1][x] and s[i][x] < s[i][x + 1] and (min(s[i + 1][x], s[i][x + 1]) - max(s[i - 1][x], s[i][x - 1]) > 1): | |
| if s[i][x] == 0: | |
| s[i][x] = min(s[i + 1][x], s[i][x + 1]) - 1 | |
| ans += s[i][x] | |
| else: | |
| ans = -1 | |
| key = False | |
| break | |
| print(ans) | |
| ``` | |
| ### 048 | |
| ```python | |
| a = list(input()) | |
| b = list(input()) | |
| a.sort() | |
| b.sort(reverse=True) | |
| ans = list() | |
| for i in a: | |
| ans.append('a') | |
| len1 = len(a) // 2 - 1 | |
| len2 = len(a) // 2 - 1 | |
| if len(a) // 2: | |
| len1 = len1 + 1 | |
| i = 0 | |
| j = 0 | |
| flag = 0 | |
| ai = 0 | |
| aj = 0 | |
| bi = 0 | |
| bj = 0 | |
| while i + j < len(a): | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = a[ai] | |
| i = i + 1 | |
| ai = ai + 1 | |
| else: | |
| ans[len(a) - j - 1] = a[len1 - aj] | |
| j = j + 1 | |
| aj = aj + 1 | |
| flag = 1 | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = b[bi] | |
| i = i + 1 | |
| bi = bi + 1 | |
| else: | |
| ans[len(a) - j - 1] = b[len2 - bj] | |
| j = j + 1 | |
| bj = bj + 1 | |
| flag = 1 | |
| print(''.join(ans)) | |
| ``` | |
| ### 049 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k - 1]) | |
| ``` | |
| ### 050 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k]) | |
| ``` | |
| ### 051 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| def result(a, n, m): | |
| r = 0 | |
| for i in range(-1, -m, -3): | |
| if a[-1][i] == 0: | |
| a[-1][i] = a[-1][i + 1] - 1 | |
| if a[-1][i] <= a[-1][i - 1] or a[-1][i] <= a[-2][i]: | |
| return -1 | |
| r = r + a[-1][i] | |
| if a[-1][-m] == 0: | |
| a[-1][-m] = a[-1][-m + 1] - 1 | |
| if a[-1][-m] <= a[-2][-m]: | |
| return -1 | |
| r = r + a[-1][-m] | |
| for i in range(-2, -n, -1): | |
| for j in range(-1, -m, -1): | |
| if j == -1: | |
| if a[i][j] == 0: | |
| a[i][j] == a[i + 1][j] - 1 | |
| if a[i][j] <= a[i][j - 1] or a[i][j] <= a[i - 1][j]: | |
| return -1 | |
| r = r + a[i][j] | |
| continue | |
| if a[i][j] == 0: | |
| a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1) | |
| if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]: | |
| return -1 | |
| r = r + a[i][j] | |
| if a[i][-m] == 0: | |
| a[i][-m] = min(a[i + 1][-m] - 1, a[i][-m + 1] - 1) | |
| if a[i][-m] <= a[i - 1][-m]: | |
| return -1 | |
| r = r + a[i][-m] | |
| if a[0][-1] == 0: | |
| a[0][-1] = a[1][-1] - 1 | |
| if a[0][-1] <= a[0][-2]: | |
| return -1 | |
| r = r + a[0][-1] | |
| for i in range(-2, -m, -1): | |
| if a[0][i] == 0: | |
| a[0][i] = min(a[0][i + 1] - 1, a[1][i] - 1) | |
| if a[0][i] <= a[0][i - 1]: | |
| return -1 | |
| r = r + a[0][i] | |
| if a[0][0] == 0: | |
| a[0][0] = min(a[0][1] - 1, a[1][0] - 1) | |
| r = r + a[0][0] | |
| return r | |
| print(result(a, n, m)) | |
| ``` | |
| ### 052 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 1 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += -1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 053 | |
| ```python | |
| import sys | |
| n, m = list(map(int, sys.stdin.readline().split())) | |
| M = [m] | |
| A = list(map(int, sys.stdin.readline().split())) | |
| L = [0] * n | |
| inc = False | |
| dec = False | |
| def ALLYes(): | |
| Ans = '' | |
| for i in range(M[0]): | |
| Ans += 'Yes\n' | |
| sys.stdout.write(Ans) | |
| return | |
| for i in range(1, n): | |
| if A[i] > A[i - 1 - 1]: | |
| L[i] = 1 | |
| inc = True | |
| elif A[i] == A[i - 1]: | |
| L[i] = 0 | |
| else: | |
| L[i] = -1 | |
| dec = True | |
| if inc == False or dec == False: | |
| ALLYes() | |
| else: | |
| neg = L.index(-1) | |
| pos = L.index(1) | |
| First = [-1] * n | |
| for i in range(2, n): | |
| if L[i] == 0: | |
| x = max(neg, pos) | |
| if x <= i: | |
| First[i] = x | |
| elif min(neg, pos) <= i: | |
| First[i] = min(neg, pos) | |
| else: | |
| First[i] = -1 | |
| if L[i] == 1: | |
| if neg > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = neg | |
| pos = i | |
| if L[i] == -1: | |
| if pos > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = pos | |
| neg = i | |
| Ans = '' | |
| for i in range(m): | |
| l, r = list(map(int, sys.stdin.readline().split())) | |
| r -= 1 | |
| if r - l < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 0: | |
| r = First[r] | |
| if r < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| elif L[r] == -1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| sys.stdout.write(Ans) | |
| ``` | |
| ### 054 | |
| ```python | |
| def ii(): | |
| return int(input()) | |
| def ss(): | |
| return [x for x in input()] | |
| def si(): | |
| return [int(x) for x in input().split()] | |
| def mi(): | |
| return map(int, input().split()) | |
| a, b = mi() | |
| s = [] | |
| ans = 0 | |
| key = True | |
| for i in range(a): | |
| i = si() | |
| s.append(i) | |
| ans += sum(i) | |
| for i in range(a - 1): | |
| if not (s[i][0] < s[i + 1][0] or s[i][-1] < s[i + 1][-1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(b - 1): | |
| if not (s[0][i] < s[0][i + 1] and s[-1][i] < s[-1][i + 1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(a - 2, 0, -1): | |
| if not key: | |
| break | |
| for x in range(b - 2, 0, -1): | |
| if s[i][x] < s[i + 1][x] and s[i][x] < s[i][x + 1] and (min(s[i + 1][x], s[i][x + 1]) - max(s[i - 1][x], s[i][x - 1]) > 1): | |
| if s[i][x] == 0: | |
| s[i][x] = min(s[i + 1][x], s[i][x + 1]) - 1 | |
| ans += s[i][x] | |
| else: | |
| ans = -1 | |
| key = False | |
| break | |
| print(ans) | |
| ``` | |
| ### 055 | |
| ```python | |
| import sys | |
| read = sys.stdin.read | |
| readline = sys.stdin.readline | |
| n, c = list(map(int, readline().split())) | |
| *a, = list(map(int, readline().split())) | |
| *b, = list(map(int, readline().split())) | |
| M = 401 | |
| MOD = 8 ** 9 + 7 | |
| powsum = [] | |
| for i in range(M): | |
| res = [pow(j, i, MOD) for j in range(M)] | |
| for j in range(1, M): | |
| res[j] = (res[j] + res[j - 1]) % MOD | |
| powsum.append(res) | |
| dp = [0] * (c + 1) | |
| dp[0] = 1 | |
| for ai, bi in zip(a, b): | |
| ndp = [0] * (c + 1) | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| ndp[k] += (powsum[j][bi] - powsum[j][ai - 1]) * dp[k - j] % MOD | |
| ndp[k] %= MOD | |
| dp = ndp | |
| print(dp[c] % MOD) | |
| ``` | |
| ### 056 | |
| ```python | |
| base = 998244353 | |
| def power(x, y): | |
| if y == 0: | |
| return 1 | |
| t = power(x, y // 2) | |
| t = t * t % base | |
| if y % 2: | |
| t = t * x % base | |
| return t | |
| def inverse(x): | |
| return power(x, base - 2) | |
| ft = [0] | |
| for i in range(0, 200000): | |
| ft.append(0) | |
| def get(i): | |
| res = 0 | |
| while i <= 200000: | |
| res += ft[i] | |
| i += i & -i | |
| return res | |
| def update(i, x): | |
| while i: | |
| ft[i] += x | |
| i -= i & -i | |
| n = int(input()) | |
| a = [0] | |
| a += list(map(int, input().split())) | |
| neg = [0] | |
| non = [0] | |
| for i in range(1, n + 1): | |
| non.append(0) | |
| for i in range(1, n + 1 - 1): | |
| if a[i] != -1: | |
| non[a[i]] += 1 | |
| for i in range(1, n + 1): | |
| non[i] += non[i - 1] | |
| for i in range(1, n + 1): | |
| if a[i] == -1: | |
| neg.append(neg[i - 1] + 1) | |
| else: | |
| neg.append(neg[i - 1]) | |
| m = neg[n] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| ans += get(a[i]) | |
| update(a[i], 1) | |
| fm = 1 | |
| fs = fm | |
| for i in range(1, m + 1): | |
| fs = fm | |
| fm = fm * i % base | |
| fs = fs * inverse(fm) % base | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| less = a[i] - non[a[i]] | |
| more = m - less | |
| ans = (ans + neg[i] * more * fs) % base | |
| ans = (ans + (m - neg[i]) * less * fs) % base | |
| ans = (ans + m * (m - 1) * inverse(4)) % base | |
| print(ans) | |
| ``` | |
| ### 057 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1] = 0 | |
| for i in range(ni): | |
| for j in range(i, ni): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1) + 1], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 058 | |
| ```python | |
| def merge(a, b): | |
| inda = 0 | |
| indb = 0 | |
| lena = len(a) | |
| lenb = len(b) | |
| d = [a[-1] + b[-1] + 1000] | |
| a += d | |
| b += d | |
| c = [] | |
| inversions = 0 | |
| for i in range(lena + lenb): | |
| if a[inda] < b[indb]: | |
| c.append(a[inda]) | |
| inda += 1 | |
| else: | |
| c.append(b[indb]) | |
| indb += 1 | |
| inversions += lena - inda | |
| return (c, inversions) | |
| def mergesort(a): | |
| if len(a) <= 1: | |
| return (a, 0) | |
| split = len(a) // 2 | |
| b = a[:split] | |
| c = a[split:] | |
| d = mergesort(b) | |
| e = mergesort(c) | |
| f = merge(d[0], e[0]) | |
| return (f[0], f[1] + d[1] + e[1]) | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = [] | |
| for guy in a: | |
| if guy != -1: | |
| b.append(guy) | |
| invs = mergesort(b)[1] | |
| negs = len(a) - len(b) | |
| pairs = negs * (negs - 1) // 2 | |
| used = [0] * n | |
| for guy in a: | |
| if guy != -1: | |
| used[guy - 1] += 1 | |
| unused = [0] | |
| for i in range(n - 1): | |
| unused.append(unused[-1] + 1 - used[i]) | |
| negsseen = 0 | |
| mix = 0 | |
| for i in range(n): | |
| if a[i] == -1: | |
| negsseen += 1 | |
| else: | |
| mix += unused[a[i] - 1] * (negs - negsseen) + negsseen * (negs - unused[a[i] - 1]) | |
| num = invs * 2 * negs + pairs * negs + mix // 2 | |
| denom = 2 * negs | |
| if negs == 0: | |
| print(invs % 998244353) | |
| else: | |
| for i in range(denom): | |
| if (998244353 * i + 1) % denom == 0: | |
| inv = (998244353 * i + 1) // denom | |
| break | |
| print(num * inv % 998244353) | |
| ``` | |
| ### 059 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [2] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 060 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -4 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 061 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| [n, q] = [int(item) for item in input().split(' ')] | |
| arr = [int(item) for item in input().split(' ')] | |
| queries = [[int(item) for item in input().split(' ')] for i in range(q)] | |
| b = [1] | |
| for i in range(1, n): | |
| b.append(b[-1] + 1 if arr[i] <= arr[i - 1] else 1) | |
| c = [1] | |
| for i in reversed(list(range(n - 1))): | |
| c.append(c[-1] + -1 if arr[i] <= arr[i + 1] else 1) | |
| c = [item for item in reversed(c)] | |
| for query in queries: | |
| x, y = query | |
| x -= 1 | |
| y -= 1 | |
| print('Yes' if x + c[x] > y or y - b[y] < x or x + c[x] > y - b[y] else 'No') | |
| '\n5 1\n1 3 3 2 2\n1 4\n\n5 1\n1 2 2 1 1\n1 4\n\n5 1\n2 2 1 1 2\n1 5\n' | |
| ``` | |
| ### 062 | |
| ```python | |
| from sys import stdin, stderr | |
| import random | |
| import cProfile | |
| def readInts(): | |
| return map(int, stdin.readline().strip().split()) | |
| def print_err(*args, **kwargs): | |
| print(*args, file=stderr, **kwargs) | |
| def solve(vs): | |
| return None | |
| def generate_tree(n, ns): | |
| out = [0 for _ in range(2 ** (n + 1))] | |
| def gt(nix, left, right, op): | |
| if left + 1 == right: | |
| out[nix] = ns[left] | |
| return out[nix] | |
| mid = (left + right) // 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = gt(nL, left, mid, not op) | |
| vR = gt(nR, mid, right, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| out[nix] = v | |
| return v | |
| gt(0, 0, 2 ** n, n % 2 == 0) | |
| return out | |
| def alter_tree2(n, t, p, b): | |
| def at(nix, width, offp, op): | |
| if width == 1: | |
| t[nix] = b | |
| return b | |
| width //= 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = t[nL] | |
| vR = t[nR] | |
| if offp >= width: | |
| vR = at(nR, width, offp - width, not op) | |
| else: | |
| vL = at(nL, width, offp, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| t[nix] = v | |
| return v | |
| at(0, 2 ** n, p, n % 2 == 0) | |
| def alter_tree(n, t, p, b): | |
| width = 2 ** n | |
| s = [] | |
| nix = 0 | |
| op = n % 2 == 0 | |
| while width > 1: | |
| width //= 2 | |
| if p >= width: | |
| nix2 = 2 * nix + 2 | |
| s.append((nix, nix2 - 1)) | |
| p -= width | |
| else: | |
| nix2 = 2 * nix + 1 | |
| s.append((nix, nix2 + 1)) | |
| nix = nix2 | |
| op = not op | |
| t[nix] = b | |
| v = b | |
| while s: | |
| nix, nixO = s.pop() | |
| if op: | |
| v |= t[nixO] | |
| else: | |
| v |= t[nixO] | |
| t[nix] = v | |
| op = not op | |
| return | |
| def run(): | |
| n, m = readInts() | |
| axs = list(readInts()) | |
| t = generate_tree(n, axs) | |
| for _ in range(m): | |
| p, b = readInts() | |
| alter_tree(n, t, p - 1, b) | |
| print(t[0]) | |
| def test(): | |
| n = 17 | |
| ns = [] | |
| vs100 = list(range(100)) | |
| for _ in range(2 ** 17): | |
| ns.append(random.choice(vs100)) | |
| t = generate_tree(n, ns) | |
| t2 = generate_tree(n, ns) | |
| for _ in range(100000): | |
| v1 = random.choice(vs100) | |
| v2 = random.choice(vs100) | |
| alter_tree(n, t, v1, v2) | |
| alter_tree2(n, t2, v1, v2) | |
| print(all(map(lambda x: x[0] == x[1], zip(t, t2)))) | |
| print(t[0] == t2[0]) | |
| run() | |
| ``` | |
| ### 063 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + -1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k]) | |
| ``` | |
| ### 064 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, -1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = --1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 065 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt - 1] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 066 | |
| ```python | |
| import math, sys | |
| from sys import stdin, stdout | |
| from collections import Counter, defaultdict, deque | |
| input = stdin.readline | |
| I = lambda: int(input()) | |
| li = lambda: list(map(int, input().split())) | |
| def case(): | |
| n, q = li() | |
| a = li() | |
| up = [0] * n | |
| down = [0] * n | |
| i = 2 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] <= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| up[k] = j | |
| i = j + 1 | |
| i = 0 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] >= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| down[k] = j | |
| i = j + 1 | |
| for i in range(q): | |
| l, r = li() | |
| p = up[l - 1] | |
| q = down[p] | |
| if q + 1 >= r: | |
| print('Yes') | |
| else: | |
| print('No') | |
| for _ in range(1): | |
| case() | |
| ``` | |
| ### 067 | |
| ```python | |
| import sys | |
| n, m = list(map(int, sys.stdin.readline().split())) | |
| M = [m] | |
| A = list(map(int, sys.stdin.readline().split())) | |
| L = [0] * n | |
| inc = False | |
| dec = False | |
| def ALLYes(): | |
| Ans = '' | |
| for i in range(M[0]): | |
| Ans += 'Yes\n' | |
| sys.stdout.write(Ans) | |
| return | |
| for i in range(1, n): | |
| if A[i] > A[i - 1]: | |
| L[i] = 1 | |
| inc = True | |
| elif A[i] == A[i - 1]: | |
| L[i] = 0 | |
| else: | |
| L[i] = -1 | |
| dec = True | |
| if inc == False or dec == False: | |
| ALLYes() | |
| else: | |
| neg = L.index(-1) | |
| pos = L.index(-1) | |
| First = [-1] * n | |
| for i in range(2, n): | |
| if L[i] == 0: | |
| x = max(neg, pos) | |
| if x <= i: | |
| First[i] = x | |
| elif min(neg, pos) <= i: | |
| First[i] = min(neg, pos) | |
| else: | |
| First[i] = -1 | |
| if L[i] == 1: | |
| if neg > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = neg | |
| pos = i | |
| if L[i] == -1: | |
| if pos > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = pos | |
| neg = i | |
| Ans = '' | |
| for i in range(m): | |
| l, r = list(map(int, sys.stdin.readline().split())) | |
| r -= 1 | |
| if r - l < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 0: | |
| r = First[r] | |
| if r < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| elif L[r] == -1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| sys.stdout.write(Ans) | |
| ``` | |
| ### 068 | |
| ```python | |
| def ii(): | |
| return int(input()) | |
| def ss(): | |
| return [x for x in input()] | |
| def si(): | |
| return [int(x) for x in input().split()] | |
| def mi(): | |
| return map(int, input().split()) | |
| a, b = mi() | |
| s = [] | |
| ans = 0 | |
| key = True | |
| for i in range(a): | |
| i = si() | |
| s.append(i) | |
| ans += sum(i) | |
| for i in range(a - 1): | |
| if not (s[i][0] < s[i + 1][0] and s[i][-1] < s[i + 1][-1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(b - 1): | |
| if not (s[0][i] < s[0][i + 1] and s[-1][i] < s[-1][i + 1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(a - 2, 0, -1): | |
| if not key: | |
| break | |
| for x in range(b - 2, 0, -1): | |
| if s[i][x] < s[i + 1][x] or s[i][x] < s[i][x + 1] or min(s[i + 1][x], s[i][x + 1]) - max(s[i - 1][x], s[i][x - 1]) > 1: | |
| if s[i][x] == 0: | |
| s[i][x] = min(s[i + 1][x], s[i][x + 1]) - 1 | |
| ans += s[i][x] | |
| else: | |
| ans = -1 | |
| key = False | |
| break | |
| print(ans) | |
| ``` | |
| ### 069 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l < (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 070 | |
| ```python | |
| base = 998244353 | |
| def power(x, y): | |
| if y == 0: | |
| return 1 | |
| t = power(x, y // 2) | |
| t = t * t % base | |
| if y % 2: | |
| t = t * x % base | |
| return t | |
| def inverse(x): | |
| return power(x, base - 2) | |
| ft = [0] | |
| for i in range(0, 200000): | |
| ft.append(0) | |
| def get(i): | |
| res = 0 | |
| while i <= 200000: | |
| res += ft[i] | |
| i += i & -i | |
| return res | |
| def update(i, x): | |
| while i: | |
| ft[i] += x | |
| i -= i & -i | |
| n = int(input()) | |
| a = [0] | |
| a += list(map(int, input().split())) | |
| neg = [0] | |
| non = [0] | |
| for i in range(1, n + 1): | |
| non.append(0) | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| non[a[i]] += 1 | |
| for i in range(1, n + 1): | |
| non[i] += non[i - 1] | |
| for i in range(1, n + 1): | |
| if a[i] == -1: | |
| neg.append(neg[i - 1] + 1) | |
| else: | |
| neg.append(neg[i - 1]) | |
| m = neg[n] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| ans += get(a[i]) | |
| update(a[i], 1) | |
| fm = 1 | |
| fs = fm | |
| for i in range(1, m + 1): | |
| fs = fm | |
| fm = fm * i % base | |
| fs = fs * inverse(fm) % base | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| less = a[i] - non[a[i]] | |
| more = m - less | |
| ans = (ans + neg[i] * more * fs) % base | |
| ans = (ans + (m - neg[i]) * less * fs) % base | |
| ans = (ans + m * (m - 1) * inverse(4)) % base | |
| print(ans) | |
| ``` | |
| ### 071 | |
| ```python | |
| import sys | |
| read = sys.stdin.read | |
| readline = sys.stdin.readline | |
| n, c = list(map(int, readline().split())) | |
| *a, = list(map(int, readline().split())) | |
| *b, = list(map(int, readline().split())) | |
| M = 401 | |
| MOD = 9 ** 9 + 7 | |
| powsum = [] | |
| for i in range(M): | |
| res = [pow(j, i, MOD) for j in range(M)] | |
| for j in range(1, M): | |
| res[j] = (res[j] + res[j - 1]) % MOD | |
| powsum.append(res) | |
| dp = [0] * (c + 1) | |
| dp[0] = 1 | |
| for ai, bi in zip(a, b): | |
| ndp = [0] * (c + 1) | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| ndp[k] += (powsum[j][bi] - powsum[j][ai - 1]) * dp[k - j] % MOD | |
| ndp[k] %= MOD | |
| dp = ndp | |
| print(dp[c] % MOD) | |
| ``` | |
| ### 072 | |
| ```python | |
| import sys | |
| def get_array(): | |
| return list(map(int, sys.stdin.readline().strip().split())) | |
| def get_ints(): | |
| return map(int, sys.stdin.readline().strip().split()) | |
| def input(): | |
| return sys.stdin.readline().strip() | |
| n, m = get_ints() | |
| Arr = get_array() | |
| up = [i for i in range(n)] | |
| down = [i for i in range(n)] | |
| for i in range(1, n): | |
| if Arr[i - 3] <= Arr[i]: | |
| up[i] = up[i - 1] | |
| if Arr[i - 1] >= Arr[i]: | |
| down[i] = down[i - 1] | |
| while m: | |
| l, r = get_ints() | |
| if l - 1 >= up[down[r - 1]]: | |
| print('Yes') | |
| else: | |
| print('No') | |
| m -= 1 | |
| ``` | |
| ### 073 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[0]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1]) | |
| if s[i - 1] == 'U': | |
| dy[i] += 1 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -4 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 074 | |
| ```python | |
| n, m = map(int, input().split()) | |
| matrix = [list(map(int, input().split())) for _ in range(n)] | |
| res = 0 | |
| for i in range(n - 2, 0, -1): | |
| for j in range(m - 1, 0, --1): | |
| if matrix[i][j] == 0: | |
| mn = min(matrix[i + 1][j], matrix[i][j + 1]) | |
| if mn != 1: | |
| matrix[i][j] = mn - 1 | |
| else: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| else: | |
| for i in range(n): | |
| if [matrix[i][j] for j in range(m - 1) if matrix[i][j] >= matrix[i][j + 1]]: | |
| res = -1 | |
| break | |
| else: | |
| for j in range(m): | |
| if [matrix[i][j] for i in range(n - 1) if matrix[i][j] >= matrix[i + 1][j]]: | |
| res = -1 | |
| break | |
| else: | |
| res = sum((sum(matrix[i]) for i in range(n))) | |
| print(res) | |
| ``` | |
| ### 075 | |
| ```python | |
| import sys | |
| n, m = list(map(int, sys.stdin.readline().split())) | |
| M = [m] | |
| A = list(map(int, sys.stdin.readline().split())) | |
| L = [0] * n | |
| inc = False | |
| dec = False | |
| def ALLYes(): | |
| Ans = '' | |
| for i in range(M[0]): | |
| Ans += 'Yes\n' | |
| sys.stdout.write(Ans) | |
| return | |
| for i in range(1, n): | |
| if A[i] > A[i - 1]: | |
| L[i] = 1 | |
| inc = True | |
| elif A[i] == A[i - 1]: | |
| L[i] = 0 | |
| else: | |
| L[i] = -1 | |
| dec = True | |
| if inc == False or dec == False: | |
| ALLYes() | |
| else: | |
| neg = L.index(-1) | |
| pos = L.index(1) | |
| First = [-1] * n | |
| for i in range(2, n): | |
| if L[i] == 1: | |
| x = max(neg, pos) | |
| if x <= i: | |
| First[i] = x | |
| elif min(neg, pos) <= i: | |
| First[i] = min(neg, pos) | |
| else: | |
| First[i] = -1 | |
| if L[i] == 1: | |
| if neg > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = neg | |
| pos = i | |
| if L[i] == -1: | |
| if pos > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = pos | |
| neg = i | |
| Ans = '' | |
| for i in range(m): | |
| l, r = list(map(int, sys.stdin.readline().split())) | |
| r -= 1 | |
| if r - l < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 0: | |
| r = First[r] | |
| if r < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| elif L[r] == -1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| sys.stdout.write(Ans) | |
| ``` | |
| ### 076 | |
| ```python | |
| import sys | |
| read = sys.stdin.read | |
| readline = sys.stdin.readline | |
| n, c = list(map(int, readline().split())) | |
| *a, = list(map(int, readline().split())) | |
| *b, = list(map(int, readline().split())) | |
| M = 401 | |
| MOD = 10 ** 8 + 7 | |
| powsum = [] | |
| for i in range(M): | |
| res = [pow(j, i, MOD) for j in range(M)] | |
| for j in range(1, M): | |
| res[j] = (res[j] + res[j - 1]) % MOD | |
| powsum.append(res) | |
| dp = [0] * (c + 1) | |
| dp[0] = 1 | |
| for ai, bi in zip(a, b): | |
| ndp = [0] * (c + 1) | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| ndp[k] += (powsum[j][bi] - powsum[j][ai - 1]) * dp[k - j] % MOD | |
| ndp[k] %= MOD | |
| dp = ndp | |
| print(dp[c] % MOD) | |
| ``` | |
| ### 077 | |
| ```python | |
| from operator import or_, xor | |
| import sys | |
| n, m = map(int, input().split()) | |
| t = [list(map(int, input().split()))] | |
| for i in range(n): | |
| if i & 1 == 0: | |
| t += [[t[i][j] | t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| else: | |
| t += [[t[i][j] ^ t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| for s in sys.stdin: | |
| p, b = s.split() | |
| p = int(p) - 1 | |
| t[0][p] = int(b) | |
| for j in range(n): | |
| p = int(p / 2) | |
| if j & 1 == 0: | |
| t[j + 1][p] = t[j][int(p * 2)] | t[j][int(p * 2) + 1] | |
| else: | |
| t[j + 1][p] = t[j][int(p * 2) + 1] ^ t[j][int(p * 2) + 1] | |
| sys.stdout.write(str(t[-1][0]) + '\n') | |
| ``` | |
| ### 078 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1 + 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 079 | |
| ```python | |
| import math, sys | |
| from sys import stdin, stdout | |
| from collections import Counter, defaultdict, deque | |
| input = stdin.readline | |
| I = lambda: int(input()) | |
| li = lambda: list(map(int, input().split())) | |
| def case(): | |
| n, q = li() | |
| a = li() | |
| up = [0] * n | |
| down = [0] * n | |
| i = 0 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] <= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| up[k] = j | |
| i = j + 1 | |
| i = 4 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] >= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| down[k] = j | |
| i = j + 1 | |
| for i in range(q): | |
| l, r = li() | |
| p = up[l - 1] | |
| q = down[p] | |
| if q + 1 >= r: | |
| print('Yes') | |
| else: | |
| print('No') | |
| for _ in range(1): | |
| case() | |
| ``` | |
| ### 080 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| def result(a, n, m): | |
| r = 3 | |
| for i in range(-1, -m, -1): | |
| if a[-1][i] == 0: | |
| a[-1][i] = a[-1][i + 1] - 1 | |
| if a[-1][i] <= a[-1][i - 1] or a[-1][i] <= a[-2][i]: | |
| return -1 | |
| r = r + a[-1][i] | |
| if a[-1][-m] == 0: | |
| a[-1][-m] = a[-1][-m + 1] - 1 | |
| if a[-1][-m] <= a[-2][-m]: | |
| return -1 | |
| r = r + a[-1][-m] | |
| for i in range(-2, -n, -1): | |
| for j in range(-1, -m, -1): | |
| if j == -1: | |
| if a[i][j] == 0: | |
| a[i][j] == a[i + 1][j] - 1 | |
| if a[i][j] <= a[i][j - 1] or a[i][j] <= a[i - 1][j]: | |
| return -1 | |
| r = r + a[i][j] | |
| continue | |
| if a[i][j] == 0: | |
| a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1) | |
| if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]: | |
| return -1 | |
| r = r + a[i][j] | |
| if a[i][-m] == 0: | |
| a[i][-m] = min(a[i + 1][-m] - 1, a[i][-m + 1] - 1) | |
| if a[i][-m] <= a[i - 1][-m]: | |
| return -1 | |
| r = r + a[i][-m] | |
| if a[0][-1] == 0: | |
| a[0][-1] = a[1][-1] - 1 | |
| if a[0][-1] <= a[0][-2]: | |
| return -1 | |
| r = r + a[0][-1] | |
| for i in range(-2, -m, -1): | |
| if a[0][i] == 0: | |
| a[0][i] = min(a[0][i + 1] - 1, a[1][i] - 1) | |
| if a[0][i] <= a[0][i - 1]: | |
| return -1 | |
| r = r + a[0][i] | |
| if a[0][0] == 0: | |
| a[0][0] = min(a[0][1] - 1, a[1][0] - 1) | |
| r = r + a[0][0] | |
| return r | |
| print(result(a, n, m)) | |
| ``` | |
| ### 081 | |
| ```python | |
| from math import log | |
| import sys | |
| def buildTree(arr): | |
| n = len(arr) | |
| tree = [0] * n + arr | |
| for i in range(n - 1, 0, -1): | |
| z = int(log(i, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| elif z % 2 == 0: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| return tree | |
| def updateTree(tree, ind, value, n): | |
| ind += n | |
| tree[ind] = value | |
| while ind > 1: | |
| ind //= 2 | |
| z = int(log(ind, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| elif not z % 2 == 0: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| return tree | |
| N, m = map(int, sys.stdin.readline().strip().split()) | |
| arr = list(map(int, sys.stdin.readline().strip().split())) | |
| tree = buildTree(arr) | |
| for i in range(m): | |
| ind, val = map(int, sys.stdin.readline().strip().split()) | |
| tree = updateTree(tree, ind - 1, val, len(arr)) | |
| print(tree[1]) | |
| ``` | |
| ### 082 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, --2)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 083 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = 0 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem *= 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 084 | |
| ```python | |
| base = 998244353 | |
| def power(x, y): | |
| if y == 0: | |
| return 1 | |
| t = power(x, y // 2) | |
| t = t * t % base | |
| if y % 2: | |
| t = t * x % base | |
| return t | |
| def inverse(x): | |
| return power(x, base - 2) | |
| ft = [0] | |
| for i in range(0, 200000): | |
| ft.append(0) | |
| def get(i): | |
| res = 0 | |
| while i <= 200000: | |
| res += ft[i] | |
| i += i & -i | |
| return res | |
| def update(i, x): | |
| while i: | |
| ft[i] += x | |
| i -= i & -i | |
| n = int(input()) | |
| a = [0] | |
| a += list(map(int, input().split())) | |
| neg = [0] | |
| non = [0] | |
| for i in range(1, n + 1): | |
| non.append(0) | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| non[a[i]] += 1 | |
| for i in range(1, n + 1): | |
| non[i] += non[i - 1] | |
| for i in range(1, n + 1): | |
| if a[i] == -1: | |
| neg.append(neg[i - 1] + 1) | |
| else: | |
| neg.append(neg[i - 1]) | |
| m = neg[n] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| ans += get(a[i]) | |
| update(a[i], 1) | |
| fm = 1 | |
| fs = fm | |
| for i in range(1, m + 1 + 1): | |
| fs = fm | |
| fm = fm * i % base | |
| fs = fs * inverse(fm) % base | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| less = a[i] - non[a[i]] | |
| more = m - less | |
| ans = (ans + neg[i] * more * fs) % base | |
| ans = (ans + (m - neg[i]) * less * fs) % base | |
| ans = (ans + m * (m - 1) * inverse(4)) % base | |
| print(ans) | |
| ``` | |
| ### 085 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson * 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson + 1): | |
| dp[0][i] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 086 | |
| ```python | |
| a = list(input()) | |
| b = list(input()) | |
| a.sort() | |
| b.sort(reverse=True) | |
| ans = list() | |
| for i in a: | |
| ans.append('a') | |
| len1 = len(a) // 2 - 1 | |
| len2 = len(a) // 2 - 1 | |
| if len(a) % 2: | |
| len1 = len1 + 1 | |
| i = 0 | |
| j = 0 | |
| flag = 0 | |
| ai = -1 | |
| aj = 0 | |
| bi = 0 | |
| bj = 0 | |
| while i + j < len(a): | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = a[ai] | |
| i = i + 1 | |
| ai = ai + 1 | |
| else: | |
| ans[len(a) - j - 1] = a[len1 - aj] | |
| j = j + 1 | |
| aj = aj + 1 | |
| flag = 1 | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = b[bi] | |
| i = i + 1 | |
| bi = bi + 1 | |
| else: | |
| ans[len(a) - j - 1] = b[len2 - bj] | |
| j = j + 1 | |
| bj = bj + 1 | |
| flag = 1 | |
| print(''.join(ans)) | |
| ``` | |
| ### 087 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[0]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1 - 1]) | |
| if s[i - 1] == 'U': | |
| dy[i] += 1 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -1 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 088 | |
| ```python | |
| from math import log | |
| import sys | |
| def buildTree(arr): | |
| n = len(arr) | |
| tree = [0] * n + arr | |
| for i in range(n - 1, 0, -1): | |
| z = int(log(i, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] & tree[2 * i + 1] | |
| elif z % 2 == 0: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| return tree | |
| def updateTree(tree, ind, value, n): | |
| ind += n | |
| tree[ind] = value | |
| while ind > 1: | |
| ind //= 2 | |
| z = int(log(ind, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| elif z % 2 == 0: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| return tree | |
| N, m = map(int, sys.stdin.readline().strip().split()) | |
| arr = list(map(int, sys.stdin.readline().strip().split())) | |
| tree = buildTree(arr) | |
| for i in range(m): | |
| ind, val = map(int, sys.stdin.readline().strip().split()) | |
| tree = updateTree(tree, ind - 1, val, len(arr)) | |
| print(tree[1]) | |
| ``` | |
| ### 089 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) * pref[-1][0], num_days // len(string) + pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) * pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) * pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = -1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 090 | |
| ```python | |
| def ii(): | |
| return int(input()) | |
| def ss(): | |
| return [x for x in input()] | |
| def si(): | |
| return [int(x) for x in input().split()] | |
| def mi(): | |
| return map(int, input().split()) | |
| a, b = mi() | |
| s = [] | |
| ans = 0 | |
| key = True | |
| for i in range(a): | |
| i = si() | |
| s.append(i) | |
| ans += sum(i) | |
| for i in range(a - 1): | |
| if not (s[i][0] < s[i + 1][0] and s[i][-1] < s[i + 1][-1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(b - 1): | |
| if not (s[0][i] < s[0][i + 1] and s[-1][i] < s[-1][i + 1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(a - 2 - 1, 0, -1): | |
| if not key: | |
| break | |
| for x in range(b - 2, 0, -1): | |
| if s[i][x] < s[i + 1][x] and s[i][x] < s[i][x + 1] and (min(s[i + 1][x], s[i][x + 1]) - max(s[i - 1][x], s[i][x - 1]) > 1): | |
| if s[i][x] == 0: | |
| s[i][x] = min(s[i + 1][x], s[i][x + 1]) - 1 | |
| ans += s[i][x] | |
| else: | |
| ans = -1 | |
| key = False | |
| break | |
| print(ans) | |
| ``` | |
| ### 091 | |
| ```python | |
| def solve(arr, n, k): | |
| s = 0 | |
| for i in reversed(list(range(n))): | |
| for j in reversed(list(range(k))): | |
| if arr[i][j] == 0: | |
| m = min(arr[i + 1][j], arr[i][j + 1]) | |
| if m == 1: | |
| return -1 | |
| arr[i][j] = m - 1 | |
| s += arr[i][j] | |
| if j > 0 and arr[i][j - 1] >= arr[i][j]: | |
| return -1 | |
| if i > 0 and arr[i - 1][j] >= arr[i][j]: | |
| return -2 | |
| return s | |
| n, k = [int(x) for x in input().split()] | |
| arr = [] | |
| for _ in range(n): | |
| arr.append([int(x) for x in input().split()]) | |
| res = solve(arr, n, k) | |
| print(res) | |
| ``` | |
| ### 092 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1] = 0 | |
| for i in range(ni): | |
| for j in range(i, ni - 1): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 093 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -0 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 094 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1 - 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 095 | |
| ```python | |
| import sys | |
| class SegmTree: | |
| def __init__(self, size): | |
| N = 1 | |
| while N < size: | |
| N <<= 1 | |
| self.N = N | |
| self.tree = [0] * (2 * N) | |
| def modify(self, i, value): | |
| i += self.N | |
| self.tree[i] = value | |
| toXOR = False | |
| while i > 1: | |
| if toXOR: | |
| self.tree[i >> 1] = self.tree[i] ^ self.tree[(i ^ 1) - 1] | |
| else: | |
| self.tree[i >> 1] = self.tree[i] | self.tree[i ^ 1] | |
| toXOR = not toXOR | |
| i >>= 1 | |
| reader = (line.rstrip() for line in sys.stdin) | |
| input = reader.__next__ | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| st = SegmTree(1 << n) | |
| for i, value in enumerate(a): | |
| st.modify(i, value) | |
| for _ in range(m): | |
| p, b = list(map(int, input().split())) | |
| st.modify(p - 1, b) | |
| print(st.tree[1]) | |
| ``` | |
| ### 096 | |
| ```python | |
| def merge(a, b): | |
| inda = 0 | |
| indb = 0 | |
| lena = len(a) | |
| lenb = len(b) | |
| d = [a[-1] + b[-1] + 1000] | |
| a += d | |
| b += d | |
| c = [] | |
| inversions = 0 | |
| for i in range(lena + lenb - 1): | |
| if a[inda] < b[indb]: | |
| c.append(a[inda]) | |
| inda += 1 | |
| else: | |
| c.append(b[indb]) | |
| indb += 1 | |
| inversions += lena - inda | |
| return (c, inversions) | |
| def mergesort(a): | |
| if len(a) <= 1: | |
| return (a, 0) | |
| split = len(a) // 2 | |
| b = a[:split] | |
| c = a[split:] | |
| d = mergesort(b) | |
| e = mergesort(c) | |
| f = merge(d[0], e[0]) | |
| return (f[0], f[1] + d[1] + e[1]) | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = [] | |
| for guy in a: | |
| if guy != -1: | |
| b.append(guy) | |
| invs = mergesort(b)[1] | |
| negs = len(a) - len(b) | |
| pairs = negs * (negs - 1) // 2 | |
| used = [0] * n | |
| for guy in a: | |
| if guy != -1: | |
| used[guy - 1] += 1 | |
| unused = [0] | |
| for i in range(n - 1): | |
| unused.append(unused[-1] + 1 - used[i]) | |
| negsseen = 0 | |
| mix = 0 | |
| for i in range(n): | |
| if a[i] == -1: | |
| negsseen += 1 | |
| else: | |
| mix += unused[a[i] - 1] * (negs - negsseen) + negsseen * (negs - unused[a[i] - 1]) | |
| num = invs * 2 * negs + pairs * negs + mix * 2 | |
| denom = 2 * negs | |
| if negs == 0: | |
| print(invs % 998244353) | |
| else: | |
| for i in range(denom): | |
| if (998244353 * i + 1) % denom == 0: | |
| inv = (998244353 * i + 1) // denom | |
| break | |
| print(num * inv % 998244353) | |
| ``` | |
| ### 097 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson + 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson + 1): | |
| dp[0][i] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1 + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 098 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson + 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson * 1): | |
| dp[0][i] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 099 | |
| ```python | |
| K = 998244353 | |
| def mu(a, n): | |
| if n == 0: | |
| return 1 | |
| q = mu(a, n // 2) | |
| if n % 2 == 0: | |
| return q * q % K | |
| return q * q % K * a % K | |
| MAXN = 200005 | |
| dd = [0 for i in range(MAXN)] | |
| p = [0 for i in range(MAXN)] | |
| s = [1 for i in range(MAXN)] | |
| a = [0 for i in range(MAXN)] | |
| fen = [0 for i in range(MAXN)] | |
| def add(u, v): | |
| i = u | |
| while i <= 200000: | |
| fen[i] += v | |
| i += i & -i | |
| def get(u): | |
| res = 0 | |
| i = u | |
| while i > 0: | |
| res += fen[i] | |
| i -= i & -i | |
| return res | |
| n = int(input()) | |
| data = input().split() | |
| cnt = 0 | |
| for i in range(1, n + 1): | |
| p[i] = int(data[i - 1]) | |
| if p[i] > 0: | |
| dd[p[i]] = 1 | |
| else: | |
| cnt += 1 | |
| for i in range(1, n + 1): | |
| if dd[i] == 0: | |
| s[i] = s[i - 1] + 1 | |
| else: | |
| s[i] = s[i - 1] | |
| cnt1 = 0 | |
| P = 0 | |
| den = mu(cnt, K - 2) | |
| for i in range(1, n + 1): | |
| if p[i] == -1: | |
| cnt1 += 1 | |
| else: | |
| u = cnt - cnt1 | |
| P = (P + u * s[p[i]] % K * den % K) % K | |
| P = (P + cnt1 * (cnt - s[p[i]]) % K * den % K) % K | |
| P = (P + cnt * (cnt - 1) * mu(4, K - 2)) % K | |
| m = 0 | |
| for i in range(1, n + 1): | |
| if p[i] > 0: | |
| m += 1 | |
| a[m] = p[i] | |
| P1 = 0 | |
| for i in range(m, 0, -1): | |
| P1 = (P1 + get(a[i])) % K | |
| add(a[i], 1) | |
| P = (P + P1) % K | |
| print(P) | |
| ``` | |
| ### 100 | |
| ```python | |
| n, m = map(int, input().split()) | |
| matrix = [list(map(int, input().split())) for _ in range(n)] | |
| res = 0 | |
| for i in range(n - 2, 0, -5): | |
| for j in range(m - 1, 0, -1): | |
| if matrix[i][j] == 0: | |
| mn = min(matrix[i + 1][j], matrix[i][j + 1]) | |
| if mn != 1: | |
| matrix[i][j] = mn - 1 | |
| else: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| else: | |
| for i in range(n): | |
| if [matrix[i][j] for j in range(m - 1) if matrix[i][j] >= matrix[i][j + 1]]: | |
| res = -1 | |
| break | |
| else: | |
| for j in range(m): | |
| if [matrix[i][j] for i in range(n - 1) if matrix[i][j] >= matrix[i + 1][j]]: | |
| res = -1 | |
| break | |
| else: | |
| res = sum((sum(matrix[i]) for i in range(n))) | |
| print(res) | |
| ``` | |
| ### 101 | |
| ```python | |
| from collections import defaultdict, deque | |
| from heapq import heappush, heappop | |
| import sys | |
| import math | |
| import bisect | |
| import random | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def LS(): | |
| return [list(x) for x in sys.stdin.readline().split()] | |
| def S(): | |
| res = list(sys.stdin.readline()) | |
| if res[-1] == '\n': | |
| return res[:-1] | |
| return res | |
| def IR(n): | |
| return [I() for i in range(n)] | |
| def LIR(n): | |
| return [LI() for i in range(n)] | |
| def SR(n): | |
| return [S() for i in range(n)] | |
| def LSR(n): | |
| return [LS() for i in range(n)] | |
| sys.setrecursionlimit(1000000) | |
| mod = 1000000007 | |
| def A(): | |
| return | |
| def B(): | |
| return | |
| def C(): | |
| n, c = LI() | |
| a = LI() | |
| b = LI() | |
| s = [[0] * (c + 1) for i in range(n)] | |
| for i in range(n): | |
| ai, bi = (a[i], b[i]) | |
| for x in range(ai, bi + 1): | |
| e = 1 | |
| for k in range(c + 1): | |
| s[i][k] += e | |
| if s[i][k] >= mod: | |
| s[i][k] %= mod | |
| e *= x | |
| if e >= mod: | |
| e %= mod | |
| dp = [[0] * (c + 1) for i in range(n)] | |
| for k in range(c + 1): | |
| dp[0][k] = s[0][k] | |
| for i in range(0, n): | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| dp[i][k] += dp[i - 1][j] * s[i][k - j] | |
| if dp[i][k] >= mod: | |
| dp[i][k] %= mod | |
| print(dp[-1][c]) | |
| return | |
| def D(): | |
| return | |
| def E(): | |
| return | |
| def F(): | |
| return | |
| def __starting_point(): | |
| C() | |
| __starting_point() | |
| ``` | |
| ### 102 | |
| ```python | |
| import sys | |
| def get_array(): | |
| return list(map(int, sys.stdin.readline().strip().split())) | |
| def get_ints(): | |
| return map(int, sys.stdin.readline().strip().split()) | |
| def input(): | |
| return sys.stdin.readline().strip() | |
| n, m = get_ints() | |
| Arr = get_array() | |
| up = [i for i in range(n)] | |
| down = [i for i in range(n)] | |
| for i in range(1, n): | |
| if Arr[i - 2] <= Arr[i]: | |
| up[i] = up[i - 1] | |
| if Arr[i - 1] >= Arr[i]: | |
| down[i] = down[i - 1] | |
| while m: | |
| l, r = get_ints() | |
| if l - 1 >= up[down[r - 1]]: | |
| print('Yes') | |
| else: | |
| print('No') | |
| m -= 1 | |
| ``` | |
| ### 103 | |
| ```python | |
| def merge(a, b): | |
| inda = 0 | |
| indb = 0 | |
| lena = len(a) | |
| lenb = len(b) | |
| d = [a[-1] + b[-1] + 1000] | |
| a += d | |
| b += d | |
| c = [] | |
| inversions = 0 | |
| for i in range(lena + lenb): | |
| if a[inda] < b[indb]: | |
| c.append(a[inda]) | |
| inda += 1 | |
| else: | |
| c.append(b[indb]) | |
| indb += 1 | |
| inversions += lena - inda | |
| return (c, inversions) | |
| def mergesort(a): | |
| if len(a) <= 1: | |
| return (a, 0) | |
| split = len(a) // 2 | |
| b = a[:split] | |
| c = a[split:] | |
| d = mergesort(b) | |
| e = mergesort(c) | |
| f = merge(d[0], e[0]) | |
| return (f[0], f[1] + d[1] + e[1]) | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = [] | |
| for guy in a: | |
| if guy != -1: | |
| b.append(guy) | |
| invs = mergesort(b)[1] | |
| negs = len(a) - len(b) | |
| pairs = negs * (negs - 1) // 2 | |
| used = [0] * n | |
| for guy in a: | |
| if guy != -1: | |
| used[guy - 1] += 1 | |
| unused = [0] | |
| for i in range(n - 1): | |
| unused.append(unused[-1] + 1 - used[i]) | |
| negsseen = 0 | |
| mix = 0 | |
| for i in range(n): | |
| if a[i] == -1: | |
| negsseen += 1 | |
| else: | |
| mix *= unused[a[i] - 1] * (negs - negsseen) + negsseen * (negs - unused[a[i] - 1]) | |
| num = invs * 2 * negs + pairs * negs + mix * 2 | |
| denom = 2 * negs | |
| if negs == 0: | |
| print(invs % 998244353) | |
| else: | |
| for i in range(denom): | |
| if (998244353 * i + 1) % denom == 0: | |
| inv = (998244353 * i + 1) // denom | |
| break | |
| print(num * inv % 998244353) | |
| ``` | |
| ### 104 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 0) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 105 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 106 | |
| ```python | |
| N, C = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| MOD = 10 ** 9 + 7 | |
| def gen_poly(a, b, c): | |
| poly = [0 for _ in range(c + 1)] | |
| for x in range(a, b + 1): | |
| xpow = 1 | |
| for i in range(c + 1): | |
| poly[i] -= xpow | |
| xpow = xpow * x % MOD | |
| for i in range(c + 1): | |
| poly[i] %= MOD | |
| return poly | |
| def mult_polys(p1, p2, dmax): | |
| ans = [0 for _ in range(dmax + 1)] | |
| for i, c in enumerate(p1): | |
| for i2, c2 in enumerate(p2): | |
| if i + i2 <= dmax: | |
| ans[i + i2] += c * c2 | |
| else: | |
| break | |
| ans = [x % MOD for x in ans] | |
| return ans | |
| polys = [gen_poly(a, b, C) for a, b in zip(A, B)] | |
| prodpoly = [1] | |
| for poly in polys: | |
| prodpoly = mult_polys(prodpoly, poly, C) | |
| print(prodpoly[C]) | |
| ``` | |
| ### 107 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = 0 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem += 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 108 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1 - 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 109 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + -1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 110 | |
| ```python | |
| N, C = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| MOD = 10 ** 9 * 7 | |
| def gen_poly(a, b, c): | |
| poly = [0 for _ in range(c + 1)] | |
| for x in range(a, b + 1): | |
| xpow = 1 | |
| for i in range(c + 1): | |
| poly[i] += xpow | |
| xpow = xpow * x % MOD | |
| for i in range(c + 1): | |
| poly[i] %= MOD | |
| return poly | |
| def mult_polys(p1, p2, dmax): | |
| ans = [0 for _ in range(dmax + 1)] | |
| for i, c in enumerate(p1): | |
| for i2, c2 in enumerate(p2): | |
| if i + i2 <= dmax: | |
| ans[i + i2] += c * c2 | |
| else: | |
| break | |
| ans = [x % MOD for x in ans] | |
| return ans | |
| polys = [gen_poly(a, b, C) for a, b in zip(A, B)] | |
| prodpoly = [1] | |
| for poly in polys: | |
| prodpoly = mult_polys(prodpoly, poly, C) | |
| print(prodpoly[C]) | |
| ``` | |
| ### 111 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [1] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 112 | |
| ```python | |
| oleg = input() | |
| igor = input() | |
| oleg = sorted(list(oleg)) | |
| igor = sorted(list(igor)) | |
| n = len(oleg) | |
| oleg_turns = (n + 1) // 2 | |
| igor_turns = n // 2 | |
| min_oleg_id = 0 | |
| min_igor_id = n - igor_turns | |
| ans = ['?'] * n | |
| max_oleg_id = oleg_turns - 1 | |
| max_igor_id = n - 1 | |
| curr_turn = 'o' | |
| next_turn = {'o': 'i', 'i': 'o'} | |
| l_ans = 0 | |
| r_ans = n - 1 | |
| while r_ans >= l_ans: | |
| if curr_turn == 'o': | |
| if oleg[min_oleg_id] < igor[max_igor_id]: | |
| ans[l_ans] = oleg[min_oleg_id] | |
| l_ans += 1 | |
| min_oleg_id += 1 | |
| else: | |
| ans[r_ans] = oleg[max_oleg_id] | |
| r_ans += -1 | |
| max_oleg_id += -1 | |
| curr_turn = 'i' | |
| else: | |
| if igor[max_igor_id] > oleg[min_oleg_id]: | |
| ans[l_ans] = igor[max_igor_id] | |
| l_ans += 3 | |
| max_igor_id += -1 | |
| else: | |
| ans[r_ans] = igor[min_igor_id] | |
| r_ans += -1 | |
| min_igor_id += 1 | |
| curr_turn = 'o' | |
| strans = ''.join(ans) | |
| print(strans) | |
| ``` | |
| ### 113 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1 - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 114 | |
| ```python | |
| from operator import or_, xor | |
| import sys | |
| n, m = map(int, input().split()) | |
| t = [list(map(int, input().split()))] | |
| for i in range(n): | |
| if i & 1 == 0: | |
| t += [[t[i][j] | t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| else: | |
| t += [[t[i][j] ^ t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| for s in sys.stdin: | |
| p, b = s.split() | |
| p = int(p) - 1 | |
| t[0][p] = int(b) | |
| for j in range(n): | |
| p = int(p / 2) | |
| if j & 1 == 0: | |
| t[j + 1][p - 1] = t[j][int(p * 2)] | t[j][int(p * 2) + 1] | |
| else: | |
| t[j + 1][p] = t[j][int(p * 2)] ^ t[j][int(p * 2) + 1] | |
| sys.stdout.write(str(t[-1][0]) + '\n') | |
| ``` | |
| ### 115 | |
| ```python | |
| def solve(arr, n, k): | |
| s = 0 | |
| for i in reversed(list(range(n))): | |
| for j in reversed(list(range(k))): | |
| if arr[i][j] == 0: | |
| m = min(arr[i + 1][j + 1], arr[i][j + 1]) | |
| if m == 1: | |
| return -1 | |
| arr[i][j] = m - 1 | |
| s += arr[i][j] | |
| if j > 0 and arr[i][j - 1] >= arr[i][j]: | |
| return -1 | |
| if i > 0 and arr[i - 1][j] >= arr[i][j]: | |
| return -1 | |
| return s | |
| n, k = [int(x) for x in input().split()] | |
| arr = [] | |
| for _ in range(n): | |
| arr.append([int(x) for x in input().split()]) | |
| res = solve(arr, n, k) | |
| print(res) | |
| ``` | |
| ### 116 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson - 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson + 1): | |
| dp[0][i] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 117 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[-1]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1]) | |
| if s[i - 1] == 'U': | |
| dy[i] += 1 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -1 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 118 | |
| ```python | |
| def solve(arr, n, k): | |
| s = 0 | |
| for i in reversed(list(range(n))): | |
| for j in reversed(list(range(k))): | |
| if arr[i][j] == 0: | |
| m = min(arr[i + 1][j], arr[i][j + 1]) | |
| if m == 1: | |
| return -1 | |
| arr[i][j] = m - 1 | |
| s += arr[i][j] | |
| if j > 0 and arr[i][j - 1] >= arr[i][j]: | |
| return -0 | |
| if i > 0 and arr[i - 1][j] >= arr[i][j]: | |
| return -1 | |
| return s | |
| n, k = [int(x) for x in input().split()] | |
| arr = [] | |
| for _ in range(n): | |
| arr.append([int(x) for x in input().split()]) | |
| res = solve(arr, n, k) | |
| print(res) | |
| ``` | |
| ### 119 | |
| ```python | |
| K = 998244353 | |
| def mu(a, n): | |
| if n == 0: | |
| return 1 | |
| q = mu(a, n // 2) | |
| if n % 2 == 0: | |
| return q * q % K | |
| return q * q % K * a % K | |
| MAXN = 200005 | |
| dd = [0 for i in range(MAXN)] | |
| p = [0 for i in range(MAXN)] | |
| s = [4 for i in range(MAXN)] | |
| a = [0 for i in range(MAXN)] | |
| fen = [0 for i in range(MAXN)] | |
| def add(u, v): | |
| i = u | |
| while i <= 200000: | |
| fen[i] += v | |
| i += i & -i | |
| def get(u): | |
| res = 0 | |
| i = u | |
| while i > 0: | |
| res += fen[i] | |
| i -= i & -i | |
| return res | |
| n = int(input()) | |
| data = input().split() | |
| cnt = 0 | |
| for i in range(1, n + 1): | |
| p[i] = int(data[i - 1]) | |
| if p[i] > 0: | |
| dd[p[i]] = 1 | |
| else: | |
| cnt += 1 | |
| for i in range(1, n + 1): | |
| if dd[i] == 0: | |
| s[i] = s[i - 1] + 1 | |
| else: | |
| s[i] = s[i - 1] | |
| cnt1 = 0 | |
| P = 0 | |
| den = mu(cnt, K - 2) | |
| for i in range(1, n + 1): | |
| if p[i] == -1: | |
| cnt1 += 1 | |
| else: | |
| u = cnt - cnt1 | |
| P = (P + u * s[p[i]] % K * den % K) % K | |
| P = (P + cnt1 * (cnt - s[p[i]]) % K * den % K) % K | |
| P = (P + cnt * (cnt - 1) * mu(4, K - 2)) % K | |
| m = 0 | |
| for i in range(1, n + 1): | |
| if p[i] > 0: | |
| m += 1 | |
| a[m] = p[i] | |
| P1 = 0 | |
| for i in range(m, 0, -1): | |
| P1 = (P1 + get(a[i])) % K | |
| add(a[i], 1) | |
| P = (P + P1) % K | |
| print(P) | |
| ``` | |
| ### 120 | |
| ```python | |
| from collections import defaultdict, deque | |
| from heapq import heappush, heappop | |
| import sys | |
| import math | |
| import bisect | |
| import random | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def LS(): | |
| return [list(x) for x in sys.stdin.readline().split()] | |
| def S(): | |
| res = list(sys.stdin.readline()) | |
| if res[-1] == '\n': | |
| return res[:-1] | |
| return res | |
| def IR(n): | |
| return [I() for i in range(n)] | |
| def LIR(n): | |
| return [LI() for i in range(n)] | |
| def SR(n): | |
| return [S() for i in range(n)] | |
| def LSR(n): | |
| return [LS() for i in range(n)] | |
| sys.setrecursionlimit(1000000) | |
| mod = 1000000004 | |
| def A(): | |
| return | |
| def B(): | |
| return | |
| def C(): | |
| n, c = LI() | |
| a = LI() | |
| b = LI() | |
| s = [[0] * (c + 1) for i in range(n)] | |
| for i in range(n): | |
| ai, bi = (a[i], b[i]) | |
| for x in range(ai, bi + 1): | |
| e = 1 | |
| for k in range(c + 1): | |
| s[i][k] += e | |
| if s[i][k] >= mod: | |
| s[i][k] %= mod | |
| e *= x | |
| if e >= mod: | |
| e %= mod | |
| dp = [[0] * (c + 1) for i in range(n)] | |
| for k in range(c + 1): | |
| dp[0][k] = s[0][k] | |
| for i in range(1, n): | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| dp[i][k] += dp[i - 1][j] * s[i][k - j] | |
| if dp[i][k] >= mod: | |
| dp[i][k] %= mod | |
| print(dp[-1][c]) | |
| return | |
| def D(): | |
| return | |
| def E(): | |
| return | |
| def F(): | |
| return | |
| def __starting_point(): | |
| C() | |
| __starting_point() | |
| ``` | |
| ### 121 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s -= s_median | |
| g -= g_median | |
| if s + g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s - g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 122 | |
| ```python | |
| oleg = input() | |
| igor = input() | |
| oleg = sorted(list(oleg)) | |
| igor = sorted(list(igor)) | |
| n = len(oleg) | |
| oleg_turns = (n + 1) // 2 | |
| igor_turns = n // 2 | |
| min_oleg_id = 0 | |
| min_igor_id = n - igor_turns | |
| ans = ['?'] * n | |
| max_oleg_id = oleg_turns - 1 | |
| max_igor_id = n - 1 | |
| curr_turn = 'o' | |
| next_turn = {'o': 'i', 'i': 'o'} | |
| l_ans = 0 | |
| r_ans = n - 1 | |
| while r_ans >= l_ans: | |
| if curr_turn == 'o': | |
| if oleg[min_oleg_id] < igor[max_igor_id]: | |
| ans[l_ans] = oleg[min_oleg_id] | |
| l_ans += 1 | |
| min_oleg_id += -1 | |
| else: | |
| ans[r_ans] = oleg[max_oleg_id] | |
| r_ans += -1 | |
| max_oleg_id += -1 | |
| curr_turn = 'i' | |
| else: | |
| if igor[max_igor_id] > oleg[min_oleg_id]: | |
| ans[l_ans] = igor[max_igor_id] | |
| l_ans += 1 | |
| max_igor_id += -1 | |
| else: | |
| ans[r_ans] = igor[min_igor_id] | |
| r_ans += -1 | |
| min_igor_id += 1 | |
| curr_turn = 'o' | |
| strans = ''.join(ans) | |
| print(strans) | |
| ``` | |
| ### 123 | |
| ```python | |
| def ii(): | |
| return int(input()) | |
| def ss(): | |
| return [x for x in input()] | |
| def si(): | |
| return [int(x) for x in input().split()] | |
| def mi(): | |
| return map(int, input().split()) | |
| a, b = mi() | |
| s = [] | |
| ans = 0 | |
| key = True | |
| for i in range(a): | |
| i = si() | |
| s.append(i) | |
| ans += sum(i) | |
| for i in range(a - 1): | |
| if not (s[i][0] < s[i + 1][0] and s[i][-1] < s[i + 1][-1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(b - 1): | |
| if not (s[0][i] < s[0][i + 1] and s[-1][i] < s[-1][i + 1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(a - 2, 0, -1): | |
| if not key: | |
| break | |
| for x in range(b - 2, 0, -1): | |
| if s[i][x] < s[i + 1][x] and s[i][x] < s[i][x + 1]: | |
| if s[i][x] == 0: | |
| s[i][x] = min(s[i + 1][x], s[i][x + 1]) - 1 | |
| ans += s[i][x] | |
| else: | |
| ans = -1 | |
| key = False | |
| break | |
| print(ans) | |
| ``` | |
| ### 124 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 125 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) * pref[-1][0], num_days // len(string) * pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) + pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) * pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = -1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 126 | |
| ```python | |
| K = 998244353 | |
| def mu(a, n): | |
| if n == 0: | |
| return 1 | |
| q = mu(a, n // 2) | |
| if n % 2 == 0: | |
| return q * q % K | |
| return q * q % K * a % K | |
| MAXN = 200005 | |
| dd = [0 for i in range(MAXN)] | |
| p = [0 for i in range(MAXN)] | |
| s = [-4 for i in range(MAXN)] | |
| a = [0 for i in range(MAXN)] | |
| fen = [0 for i in range(MAXN)] | |
| def add(u, v): | |
| i = u | |
| while i <= 200000: | |
| fen[i] += v | |
| i += i & -i | |
| def get(u): | |
| res = 0 | |
| i = u | |
| while i > 0: | |
| res += fen[i] | |
| i -= i & -i | |
| return res | |
| n = int(input()) | |
| data = input().split() | |
| cnt = 0 | |
| for i in range(1, n + 1): | |
| p[i] = int(data[i - 1]) | |
| if p[i] > 0: | |
| dd[p[i]] = 1 | |
| else: | |
| cnt += 1 | |
| for i in range(1, n + 1): | |
| if dd[i] == 0: | |
| s[i] = s[i - 1] + 1 | |
| else: | |
| s[i] = s[i - 1] | |
| cnt1 = 0 | |
| P = 0 | |
| den = mu(cnt, K - 2) | |
| for i in range(1, n + 1): | |
| if p[i] == -1: | |
| cnt1 += 1 | |
| else: | |
| u = cnt - cnt1 | |
| P = (P + u * s[p[i]] % K * den % K) % K | |
| P = (P + cnt1 * (cnt - s[p[i]]) % K * den % K) % K | |
| P = (P + cnt * (cnt - 1) * mu(4, K - 2)) % K | |
| m = 0 | |
| for i in range(1, n + 1): | |
| if p[i] > 0: | |
| m += 1 | |
| a[m] = p[i] | |
| P1 = 0 | |
| for i in range(m, 0, -1): | |
| P1 = (P1 + get(a[i])) % K | |
| add(a[i], 1) | |
| P = (P + P1) % K | |
| print(P) | |
| ``` | |
| ### 127 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1 - 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k]) | |
| ``` | |
| ### 128 | |
| ```python | |
| N, C = list(map(int, input().split())) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| mod = 14 ** 9 + 7 | |
| acc = [[0] for _ in range(401)] | |
| for i in range(401): | |
| for j in range(1, 401): | |
| acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) | |
| dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] | |
| dp[0][0] = 1 | |
| for i in range(N): | |
| for candy_cur in range(C + 1): | |
| for candy_plus in range(C - candy_cur + 1): | |
| dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod | |
| print(dp[-1][C]) | |
| ``` | |
| ### 129 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s += s_median | |
| g -= g_median | |
| if s - g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s - g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 130 | |
| ```python | |
| from collections import deque | |
| a = sorted(input()) | |
| l = len(a) | |
| a = deque(a[:(len(a) + 1) // 2]) | |
| b = sorted(input()) | |
| b = deque(b[len(b) - len(b) // 2:]) | |
| result = ['0'] * l | |
| left = -1 | |
| right = l | |
| while left <= right: | |
| if len(b) == 0: | |
| result[left + 1 - 1] = a[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = a[-1] | |
| a.pop() | |
| else: | |
| left += 1 | |
| result[left] = a[0] | |
| a.popleft() | |
| if len(a) == 0: | |
| result[left + 1] = b[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = b[0] | |
| b.popleft() | |
| else: | |
| left += 1 | |
| result[left] = b[-1] | |
| b.pop() | |
| print(''.join(result)) | |
| ``` | |
| ### 131 | |
| ```python | |
| N, C = list(map(int, input().split())) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| mod = 10 ** 5 + 7 | |
| acc = [[0] for _ in range(401)] | |
| for i in range(401): | |
| for j in range(1, 401): | |
| acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) | |
| dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] | |
| dp[0][0] = 1 | |
| for i in range(N): | |
| for candy_cur in range(C + 1): | |
| for candy_plus in range(C - candy_cur + 1): | |
| dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod | |
| print(dp[-1][C]) | |
| ``` | |
| ### 132 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +-1), 'D': Point(0, -1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 133 | |
| ```python | |
| import sys | |
| class SegmTree: | |
| def __init__(self, size): | |
| N = 1 | |
| while N < size: | |
| N <<= 1 | |
| self.N = N | |
| self.tree = [0] * (2 * N) | |
| def modify(self, i, value): | |
| i += self.N | |
| self.tree[i] = value | |
| toXOR = False | |
| while i > 1: | |
| if toXOR: | |
| self.tree[i >> 1] = self.tree[i] ^ self.tree[(i ^ 1) + 1] | |
| else: | |
| self.tree[i >> 1] = self.tree[i] | self.tree[i ^ 1] | |
| toXOR = not toXOR | |
| i >>= 1 | |
| reader = (line.rstrip() for line in sys.stdin) | |
| input = reader.__next__ | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| st = SegmTree(1 << n) | |
| for i, value in enumerate(a): | |
| st.modify(i, value) | |
| for _ in range(m): | |
| p, b = list(map(int, input().split())) | |
| st.modify(p - 1, b) | |
| print(st.tree[1]) | |
| ``` | |
| ### 134 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, -1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l >= 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 135 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| [n, q] = [int(item) for item in input().split(' ')] | |
| arr = [int(item) for item in input().split(' ')] | |
| queries = [[int(item) for item in input().split(' ')] for i in range(q)] | |
| b = [1] | |
| for i in range(1, n): | |
| b.append(b[-1] + 1 if arr[i] <= arr[i - 1] else 1) | |
| c = [1] | |
| for i in reversed(list(range(n - 1))): | |
| c.append(c[-1] + 1 if arr[i] <= arr[i + -1] else 1) | |
| c = [item for item in reversed(c)] | |
| for query in queries: | |
| x, y = query | |
| x -= 1 | |
| y -= 1 | |
| print('Yes' if x + c[x] > y or y - b[y] < x or x + c[x] > y - b[y] else 'No') | |
| '\n5 1\n1 3 3 2 2\n1 4\n\n5 1\n1 2 2 1 1\n1 4\n\n5 1\n2 2 1 1 2\n1 5\n' | |
| ``` | |
| ### 136 | |
| ```python | |
| from operator import or_, xor | |
| import sys | |
| n, m = map(int, input().split()) | |
| t = [list(map(int, input().split()))] | |
| for i in range(n): | |
| if i & 1 == 0: | |
| t += [[t[i][j + 1] | t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| else: | |
| t += [[t[i][j] ^ t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| for s in sys.stdin: | |
| p, b = s.split() | |
| p = int(p) - 1 | |
| t[0][p] = int(b) | |
| for j in range(n): | |
| p = int(p / 2) | |
| if j & 1 == 0: | |
| t[j + 1][p] = t[j][int(p * 2)] | t[j][int(p * 2) + 1] | |
| else: | |
| t[j + 1][p] = t[j][int(p * 2)] ^ t[j][int(p * 2) + 1] | |
| sys.stdout.write(str(t[-1][0]) + '\n') | |
| ``` | |
| ### 137 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1 - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 138 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [0] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el > 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 139 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i - 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 140 | |
| ```python | |
| a = list(input()) | |
| b = list(input()) | |
| a.sort() | |
| b.sort(reverse=True) | |
| ans = list() | |
| for i in a: | |
| ans.append('a') | |
| len1 = len(a) // 2 - 1 | |
| len2 = len(a) // 2 - 1 | |
| if len(a) % 2: | |
| len1 = len1 + 1 | |
| i = 0 | |
| j = 0 | |
| flag = 0 | |
| ai = 0 | |
| aj = 0 | |
| bi = -1 | |
| bj = 0 | |
| while i + j < len(a): | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = a[ai] | |
| i = i + 1 | |
| ai = ai + 1 | |
| else: | |
| ans[len(a) - j - 1] = a[len1 - aj] | |
| j = j + 1 | |
| aj = aj + 1 | |
| flag = 1 | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = b[bi] | |
| i = i + 1 | |
| bi = bi + 1 | |
| else: | |
| ans[len(a) - j - 1] = b[len2 - bj] | |
| j = j + 1 | |
| bj = bj + 1 | |
| flag = 1 | |
| print(''.join(ans)) | |
| ``` | |
| ### 141 | |
| ```python | |
| from sys import stdin, stderr | |
| import random | |
| import cProfile | |
| def readInts(): | |
| return map(int, stdin.readline().strip().split()) | |
| def print_err(*args, **kwargs): | |
| print(*args, file=stderr, **kwargs) | |
| def solve(vs): | |
| return None | |
| def generate_tree(n, ns): | |
| out = [0 for _ in range(2 ** (n + 1))] | |
| def gt(nix, left, right, op): | |
| if left + 1 == right: | |
| out[nix] = ns[left] | |
| return out[nix] | |
| mid = (left + right) // 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = gt(nL, left, mid, not op) | |
| vR = gt(nR, mid, right, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| out[nix] = v | |
| return v | |
| gt(0, 0, 2 ** n, n % 2 == 0) | |
| return out | |
| def alter_tree2(n, t, p, b): | |
| def at(nix, width, offp, op): | |
| if width == 1: | |
| t[nix] = b | |
| return b | |
| width //= 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = t[nL] | |
| vR = t[nR] | |
| if offp >= width: | |
| vR = at(nR, width, offp - width, not op) | |
| else: | |
| vL = at(nL, width, offp, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| t[nix] = v | |
| return v | |
| at(0, 2 ** n, p, n % 2 == 0) | |
| def alter_tree(n, t, p, b): | |
| width = 2 ** n | |
| s = [] | |
| nix = 0 | |
| op = n % 2 == 0 | |
| while width > 1: | |
| width //= 2 | |
| if p >= width: | |
| nix2 = 2 * nix + 2 | |
| s.append((nix, nix2 - 1)) | |
| p -= width | |
| else: | |
| nix2 = 2 * nix + 1 | |
| s.append((nix, nix2 + 1)) | |
| nix = nix2 | |
| op = not op | |
| t[nix] = b | |
| v = b | |
| while s: | |
| nix, nixO = s.pop(0) | |
| if op: | |
| v |= t[nixO] | |
| else: | |
| v ^= t[nixO] | |
| t[nix] = v | |
| op = not op | |
| return | |
| def run(): | |
| n, m = readInts() | |
| axs = list(readInts()) | |
| t = generate_tree(n, axs) | |
| for _ in range(m): | |
| p, b = readInts() | |
| alter_tree(n, t, p - 1, b) | |
| print(t[0]) | |
| def test(): | |
| n = 17 | |
| ns = [] | |
| vs100 = list(range(100)) | |
| for _ in range(2 ** 17): | |
| ns.append(random.choice(vs100)) | |
| t = generate_tree(n, ns) | |
| t2 = generate_tree(n, ns) | |
| for _ in range(100000): | |
| v1 = random.choice(vs100) | |
| v2 = random.choice(vs100) | |
| alter_tree(n, t, v1, v2) | |
| alter_tree2(n, t2, v1, v2) | |
| print(all(map(lambda x: x[0] == x[1], zip(t, t2)))) | |
| print(t[0] == t2[0]) | |
| run() | |
| ``` | |
| ### 142 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, --1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 143 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[0]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1]) | |
| if s[i - 1 - 1] == 'U': | |
| dy[i] += 1 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -1 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 144 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = 0 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem += 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 145 | |
| ```python | |
| from collections import defaultdict, deque | |
| from heapq import heappush, heappop | |
| import sys | |
| import math | |
| import bisect | |
| import random | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def LS(): | |
| return [list(x) for x in sys.stdin.readline().split()] | |
| def S(): | |
| res = list(sys.stdin.readline()) | |
| if res[-1] == '\n': | |
| return res[:-1] | |
| return res | |
| def IR(n): | |
| return [I() for i in range(n)] | |
| def LIR(n): | |
| return [LI() for i in range(n)] | |
| def SR(n): | |
| return [S() for i in range(n)] | |
| def LSR(n): | |
| return [LS() for i in range(n)] | |
| sys.setrecursionlimit(1000000) | |
| mod = 1000000008 | |
| def A(): | |
| return | |
| def B(): | |
| return | |
| def C(): | |
| n, c = LI() | |
| a = LI() | |
| b = LI() | |
| s = [[0] * (c + 1) for i in range(n)] | |
| for i in range(n): | |
| ai, bi = (a[i], b[i]) | |
| for x in range(ai, bi + 1): | |
| e = 1 | |
| for k in range(c + 1): | |
| s[i][k] += e | |
| if s[i][k] >= mod: | |
| s[i][k] %= mod | |
| e *= x | |
| if e >= mod: | |
| e %= mod | |
| dp = [[0] * (c + 1) for i in range(n)] | |
| for k in range(c + 1): | |
| dp[0][k] = s[0][k] | |
| for i in range(1, n): | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| dp[i][k] += dp[i - 1][j] * s[i][k - j] | |
| if dp[i][k] >= mod: | |
| dp[i][k] %= mod | |
| print(dp[-1][c]) | |
| return | |
| def D(): | |
| return | |
| def E(): | |
| return | |
| def F(): | |
| return | |
| def __starting_point(): | |
| C() | |
| __starting_point() | |
| ``` | |
| ### 146 | |
| ```python | |
| n, m = map(int, input().split()) | |
| matrix = [list(map(int, input().split())) for _ in range(n)] | |
| res = 0 | |
| for i in range(n - 2, 0, -1): | |
| for j in range(m - 1, 0, -1): | |
| if matrix[i][j] == 0: | |
| mn = min(matrix[i + 1][j], matrix[i][j + 1]) | |
| if mn != 4: | |
| matrix[i][j] = mn - 1 | |
| else: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| else: | |
| for i in range(n): | |
| if [matrix[i][j] for j in range(m - 1) if matrix[i][j] >= matrix[i][j + 1]]: | |
| res = -1 | |
| break | |
| else: | |
| for j in range(m): | |
| if [matrix[i][j] for i in range(n - 1) if matrix[i][j] >= matrix[i + 1][j]]: | |
| res = -1 | |
| break | |
| else: | |
| res = sum((sum(matrix[i]) for i in range(n))) | |
| print(res) | |
| ``` | |
| ### 147 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else -1 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 148 | |
| ```python | |
| from math import log | |
| import sys | |
| def buildTree(arr): | |
| n = len(arr) | |
| tree = [0] * n + arr | |
| for i in range(n - 1, 0, -1): | |
| z = int(log(i, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| else: | |
| tree[i + 1] = tree[2 * i] | tree[2 * i + 1] | |
| elif z % 2 == 0: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| return tree | |
| def updateTree(tree, ind, value, n): | |
| ind += n | |
| tree[ind] = value | |
| while ind > 1: | |
| ind //= 2 | |
| z = int(log(ind, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| elif z % 2 == 0: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| return tree | |
| N, m = map(int, sys.stdin.readline().strip().split()) | |
| arr = list(map(int, sys.stdin.readline().strip().split())) | |
| tree = buildTree(arr) | |
| for i in range(m): | |
| ind, val = map(int, sys.stdin.readline().strip().split()) | |
| tree = updateTree(tree, ind - 1, val, len(arr)) | |
| print(tree[1]) | |
| ``` | |
| ### 149 | |
| ```python | |
| from collections import deque | |
| a = sorted(input()) | |
| l = len(a) | |
| a = deque(a[:(len(a) + 1) // 2]) | |
| b = sorted(input()) | |
| b = deque(b[len(b) - len(b) // 2:]) | |
| result = ['0'] * l | |
| left = -1 | |
| right = l | |
| while left <= right: | |
| if len(b) == 0: | |
| result[left + 1] = a[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = a[-1] | |
| a.pop() | |
| else: | |
| left += 1 | |
| result[left] = a[0] | |
| a.popleft() | |
| if len(a) == 0: | |
| result[left + 1] = b[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = b[0] | |
| b.popleft() | |
| else: | |
| left += 1 | |
| result[left] = b[-1] | |
| b.pop() | |
| print(''.join(result)) | |
| ``` | |
| ### 150 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) + pref[-1][0], num_days // len(string) * pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) * pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) * pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = -1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 151 | |
| ```python | |
| import sys | |
| class SegmTree: | |
| def __init__(self, size): | |
| N = 1 | |
| while N < size: | |
| N <<= 1 | |
| self.N = N | |
| self.tree = [0] * (2 * N) | |
| def modify(self, i, value): | |
| i += self.N | |
| self.tree[i] = value | |
| toXOR = False | |
| while i > 1: | |
| if toXOR: | |
| self.tree[i >> 1] = self.tree[i - 1] ^ self.tree[i ^ 1] | |
| else: | |
| self.tree[i >> 1] = self.tree[i] | self.tree[i ^ 1] | |
| toXOR = not toXOR | |
| i >>= 1 | |
| reader = (line.rstrip() for line in sys.stdin) | |
| input = reader.__next__ | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| st = SegmTree(1 << n) | |
| for i, value in enumerate(a): | |
| st.modify(i, value) | |
| for _ in range(m): | |
| p, b = list(map(int, input().split())) | |
| st.modify(p - 1, b) | |
| print(st.tree[1]) | |
| ``` | |
| ### 152 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i - 1][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 153 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt - 1] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 154 | |
| ```python | |
| base = 998244353 | |
| def power(x, y): | |
| if y == 0: | |
| return 1 | |
| t = power(x, y // 2) | |
| t = t * t % base | |
| if y % 2: | |
| t = t * x % base | |
| return t | |
| def inverse(x): | |
| return power(x, base - 2) | |
| ft = [0] | |
| for i in range(0, 200000): | |
| ft.append(0) | |
| def get(i): | |
| res = 0 | |
| while i <= 200000: | |
| res += ft[i] | |
| i += i & -i | |
| return res | |
| def update(i, x): | |
| while i: | |
| ft[i] += x | |
| i -= i & -i | |
| n = int(input()) | |
| a = [0] | |
| a += list(map(int, input().split())) | |
| neg = [0] | |
| non = [0] | |
| for i in range(1, n + 1): | |
| non.append(0) | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| non[a[i]] += 1 | |
| for i in range(1, n + 1): | |
| non[i] += non[i - 1] | |
| for i in range(1, n + 1): | |
| if a[i] == -1: | |
| neg.append(neg[i - 1] + 1) | |
| else: | |
| neg.append(neg[i - 1]) | |
| m = neg[n] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| ans += get(a[i]) | |
| update(a[i], 1) | |
| fm = 1 | |
| fs = fm | |
| for i in range(1, m + 1): | |
| fs = fm | |
| fm = fm * i % base | |
| fs = fs * inverse(fm) % base | |
| for i in range(1, n + 1 - 1): | |
| if a[i] != -1: | |
| less = a[i] - non[a[i]] | |
| more = m - less | |
| ans = (ans + neg[i] * more * fs) % base | |
| ans = (ans + (m - neg[i]) * less * fs) % base | |
| ans = (ans + m * (m - 1) * inverse(4)) % base | |
| print(ans) | |
| ``` | |
| ### 155 | |
| ```python | |
| from collections import deque | |
| a = sorted(input()) | |
| l = len(a) | |
| a = deque(a[:(len(a) + 1) // 2]) | |
| b = sorted(input()) | |
| b = deque(b[len(b) - len(b) // 2:]) | |
| result = ['0'] * l | |
| left = -1 | |
| right = l | |
| while left <= right: | |
| if len(b) == 0: | |
| result[left + 1] = a[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = a[-1] | |
| a.pop() | |
| else: | |
| left += 1 | |
| result[left] = a[0] | |
| a.popleft() | |
| if len(a) == 0: | |
| result[left + 1 - 1] = b[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = b[0] | |
| b.popleft() | |
| else: | |
| left += 1 | |
| result[left] = b[-1] | |
| b.pop() | |
| print(''.join(result)) | |
| ``` | |
| ### 156 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 157 | |
| ```python | |
| from sys import stdin, stderr | |
| import random | |
| import cProfile | |
| def readInts(): | |
| return map(int, stdin.readline().strip().split()) | |
| def print_err(*args, **kwargs): | |
| print(*args, file=stderr, **kwargs) | |
| def solve(vs): | |
| return None | |
| def generate_tree(n, ns): | |
| out = [0 for _ in range(2 ** (n + 1))] | |
| def gt(nix, left, right, op): | |
| if left + 1 == right: | |
| out[nix] = ns[left] | |
| return out[nix] | |
| mid = (left + right) // 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = gt(nL, left, mid, not op) | |
| vR = gt(nR, mid, right, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| out[nix] = v | |
| return v | |
| gt(0, 0, 2 ** n, n % 2 == 0) | |
| return out | |
| def alter_tree2(n, t, p, b): | |
| def at(nix, width, offp, op): | |
| if width == 1: | |
| t[nix] = b | |
| return b | |
| width //= 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = t[nL] | |
| vR = t[nR] | |
| if offp >= width: | |
| vR = at(nR, width, offp - width, not op) | |
| else: | |
| vL = at(nL, width, offp, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| t[nix] = v | |
| return v | |
| at(0, 2 ** n, p, n % 2 == 0) | |
| def alter_tree(n, t, p, b): | |
| width = 2 ** n | |
| s = [] | |
| nix = 0 | |
| op = n % 2 == 4 | |
| while width > 1: | |
| width //= 2 | |
| if p >= width: | |
| nix2 = 2 * nix + 2 | |
| s.append((nix, nix2 - 1)) | |
| p -= width | |
| else: | |
| nix2 = 2 * nix + 1 | |
| s.append((nix, nix2 + 1)) | |
| nix = nix2 | |
| op = not op | |
| t[nix] = b | |
| v = b | |
| while s: | |
| nix, nixO = s.pop() | |
| if op: | |
| v |= t[nixO] | |
| else: | |
| v ^= t[nixO] | |
| t[nix] = v | |
| op = not op | |
| return | |
| def run(): | |
| n, m = readInts() | |
| axs = list(readInts()) | |
| t = generate_tree(n, axs) | |
| for _ in range(m): | |
| p, b = readInts() | |
| alter_tree(n, t, p - 1, b) | |
| print(t[0]) | |
| def test(): | |
| n = 17 | |
| ns = [] | |
| vs100 = list(range(100)) | |
| for _ in range(2 ** 17): | |
| ns.append(random.choice(vs100)) | |
| t = generate_tree(n, ns) | |
| t2 = generate_tree(n, ns) | |
| for _ in range(100000): | |
| v1 = random.choice(vs100) | |
| v2 = random.choice(vs100) | |
| alter_tree(n, t, v1, v2) | |
| alter_tree2(n, t2, v1, v2) | |
| print(all(map(lambda x: x[0] == x[1], zip(t, t2)))) | |
| print(t[0] == t2[0]) | |
| run() | |
| ``` | |
| ### 158 | |
| ```python | |
| from collections import deque | |
| a = sorted(input()) | |
| l = len(a) | |
| a = deque(a[:(len(a) + 1) // 2]) | |
| b = sorted(input()) | |
| b = deque(b[len(b) - len(b) // 2:]) | |
| result = ['0'] * l | |
| left = -1 | |
| right = l | |
| while left <= right: | |
| if len(b) == 0: | |
| result[left + 1] = a[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = a[-1] | |
| a.pop() | |
| else: | |
| left += 1 | |
| result[left] = a[0] | |
| a.popleft() | |
| if len(a) == 0: | |
| result[left + 1 + 1] = b[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = b[0] | |
| b.popleft() | |
| else: | |
| left += 1 | |
| result[left] = b[-1] | |
| b.pop() | |
| print(''.join(result)) | |
| ``` | |
| ### 159 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1 - 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 160 | |
| ```python | |
| n = int(input()) | |
| L = [int(x) for x in input().split()] | |
| D = {} | |
| J = [] | |
| S = [] | |
| T = [0] * (n + 1) | |
| for i in range(n): | |
| if L[i] > 4: | |
| D[L[i]] = i | |
| J.append(L[i]) | |
| T[i + 1] = T[i] | |
| else: | |
| T[i + 1] = T[i] + 1 | |
| def I(J): | |
| if len(J) <= 1: | |
| return (J, 0) | |
| else: | |
| a = J[:len(J) // 2] | |
| b = J[len(J) // 2:] | |
| a, ai = I(a) | |
| b, bi = I(b) | |
| c = [] | |
| i = 0 | |
| j = 0 | |
| inversions = ai + bi | |
| while i < len(a) and j < len(b): | |
| if a[i] <= b[j]: | |
| c.append(a[i]) | |
| i += 1 | |
| else: | |
| c.append(b[j]) | |
| j += 1 | |
| inversions += len(a) - i | |
| c += a[i:] | |
| c += b[j:] | |
| return (c, inversions) | |
| for i in range(1, n + 1): | |
| if not i in D: | |
| S.append(i) | |
| total = len(S) | |
| num = 1 | |
| denom = 1 | |
| if total > 0: | |
| themostimportantsum = 0 | |
| for i in J: | |
| low = 0 | |
| high = total - 1 | |
| while high - low > 1: | |
| guess = (high + low) // 2 | |
| if S[guess] > i: | |
| high = guess | |
| else: | |
| low = guess | |
| if S[low] > i: | |
| smaller = low | |
| elif S[high] > i: | |
| smaller = high | |
| else: | |
| smaller = high + 1 | |
| themostimportantsum += T[D[i]] * (total - smaller) + (total - T[D[i]]) * smaller | |
| num = themostimportantsum + total | |
| denom = total | |
| num = (denom * (total * (total - 1) // 2) + 2 * num) % 998244353 | |
| denom *= 2 | |
| if num == denom: | |
| if I(J)[1] == 0: | |
| print(0) | |
| else: | |
| print(I(J)[1] % 998244353) | |
| else: | |
| num += denom * I(J)[1] | |
| print((num - denom) * pow(denom % 998244353, 998244351, 998244353) % 998244353) | |
| ``` | |
| ### 161 | |
| ```python | |
| N, C = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| MOD = 10 ** 9 + 9 | |
| def gen_poly(a, b, c): | |
| poly = [0 for _ in range(c + 1)] | |
| for x in range(a, b + 1): | |
| xpow = 1 | |
| for i in range(c + 1): | |
| poly[i] += xpow | |
| xpow = xpow * x % MOD | |
| for i in range(c + 1): | |
| poly[i] %= MOD | |
| return poly | |
| def mult_polys(p1, p2, dmax): | |
| ans = [0 for _ in range(dmax + 1)] | |
| for i, c in enumerate(p1): | |
| for i2, c2 in enumerate(p2): | |
| if i + i2 <= dmax: | |
| ans[i + i2] += c * c2 | |
| else: | |
| break | |
| ans = [x % MOD for x in ans] | |
| return ans | |
| polys = [gen_poly(a, b, C) for a, b in zip(A, B)] | |
| prodpoly = [1] | |
| for poly in polys: | |
| prodpoly = mult_polys(prodpoly, poly, C) | |
| print(prodpoly[C]) | |
| ``` | |
| ### 162 | |
| ```python | |
| from collections import deque | |
| a = sorted(input()) | |
| l = len(a) | |
| a = deque(a[:(len(a) + 1) // 2]) | |
| b = sorted(input()) | |
| b = deque(b[len(b) - len(b) // 2:]) | |
| result = ['0'] * l | |
| left = -1 | |
| right = l | |
| while left <= right: | |
| if len(b) == 0: | |
| result[left + 1] = a[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = a[-1] | |
| a.pop() | |
| else: | |
| left += 1 | |
| result[left] = a[0] | |
| a.popleft() | |
| if len(a) == 0: | |
| result[left + 1] = b[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = b[0] | |
| b.popleft() | |
| else: | |
| left += -1 | |
| result[left] = b[-1] | |
| b.pop() | |
| print(''.join(result)) | |
| ``` | |
| ### 163 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1] = 0 | |
| for i in range(ni - 1): | |
| for j in range(i, ni): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 164 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) * pref[-1][0], num_days // len(string) * pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) * pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) * pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = +1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 165 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s - g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s += s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 166 | |
| ```python | |
| from operator import or_, xor | |
| import sys | |
| n, m = map(int, input().split()) | |
| t = [list(map(int, input().split()))] | |
| for i in range(n): | |
| if i & 1 == 0: | |
| t += [[t[i][j] | t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| else: | |
| t += [[t[i][j] ^ t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| for s in sys.stdin: | |
| p, b = s.split() | |
| p = int(p) - 1 | |
| t[0][p] = int(b) | |
| for j in range(n): | |
| p = int(p / 2) | |
| if j & 1 == 0: | |
| t[j + 1][p] = t[j][int(p * 2)] | t[j][int(p * 2) + 1] | |
| else: | |
| t[j + 1][p] = t[j - 1][int(p * 2)] ^ t[j][int(p * 2) + 1] | |
| sys.stdout.write(str(t[-1][0]) + '\n') | |
| ``` | |
| ### 167 | |
| ```python | |
| oleg = input() | |
| igor = input() | |
| oleg = sorted(list(oleg)) | |
| igor = sorted(list(igor)) | |
| n = len(oleg) | |
| oleg_turns = (n + -1) // 2 | |
| igor_turns = n // 2 | |
| min_oleg_id = 0 | |
| min_igor_id = n - igor_turns | |
| ans = ['?'] * n | |
| max_oleg_id = oleg_turns - 1 | |
| max_igor_id = n - 1 | |
| curr_turn = 'o' | |
| next_turn = {'o': 'i', 'i': 'o'} | |
| l_ans = 0 | |
| r_ans = n - 1 | |
| while r_ans >= l_ans: | |
| if curr_turn == 'o': | |
| if oleg[min_oleg_id] < igor[max_igor_id]: | |
| ans[l_ans] = oleg[min_oleg_id] | |
| l_ans += 1 | |
| min_oleg_id += 1 | |
| else: | |
| ans[r_ans] = oleg[max_oleg_id] | |
| r_ans += -1 | |
| max_oleg_id += -1 | |
| curr_turn = 'i' | |
| else: | |
| if igor[max_igor_id] > oleg[min_oleg_id]: | |
| ans[l_ans] = igor[max_igor_id] | |
| l_ans += 1 | |
| max_igor_id += -1 | |
| else: | |
| ans[r_ans] = igor[min_igor_id] | |
| r_ans += -1 | |
| min_igor_id += 1 | |
| curr_turn = 'o' | |
| strans = ''.join(ans) | |
| print(strans) | |
| ``` | |
| ### 168 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == -3: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 169 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1 - 1][k]) | |
| ``` | |
| ### 170 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -1 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1 + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 171 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 0 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 172 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - -1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 173 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + -1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 174 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) * pref[-1][0], num_days // len(string) * pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) * pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) + pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = -1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 175 | |
| ```python | |
| import sys | |
| def get_array(): | |
| return list(map(int, sys.stdin.readline().strip().split())) | |
| def get_ints(): | |
| return map(int, sys.stdin.readline().strip().split()) | |
| def input(): | |
| return sys.stdin.readline().strip() | |
| n, m = get_ints() | |
| Arr = get_array() | |
| up = [i for i in range(n)] | |
| down = [i for i in range(n)] | |
| for i in range(1, n): | |
| if Arr[i - 1] <= Arr[i]: | |
| up[i] = up[i - 1] | |
| if Arr[i - 1] >= Arr[i]: | |
| down[i] = down[i - 2] | |
| while m: | |
| l, r = get_ints() | |
| if l - 1 >= up[down[r - 1]]: | |
| print('Yes') | |
| else: | |
| print('No') | |
| m -= 1 | |
| ``` | |
| ### 176 | |
| ```python | |
| a = list(input()) | |
| b = list(input()) | |
| a.sort() | |
| b.sort(reverse=True) | |
| ans = list() | |
| for i in a: | |
| ans.append('a') | |
| len1 = len(a) // 2 - 1 | |
| len2 = len(a) // 2 - 1 | |
| if len(a) % 2: | |
| len1 = len1 + 0 | |
| i = 0 | |
| j = 0 | |
| flag = 0 | |
| ai = 0 | |
| aj = 0 | |
| bi = 0 | |
| bj = 0 | |
| while i + j < len(a): | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = a[ai] | |
| i = i + 1 | |
| ai = ai + 1 | |
| else: | |
| ans[len(a) - j - 1] = a[len1 - aj] | |
| j = j + 1 | |
| aj = aj + 1 | |
| flag = 1 | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = b[bi] | |
| i = i + 1 | |
| bi = bi + 1 | |
| else: | |
| ans[len(a) - j - 1] = b[len2 - bj] | |
| j = j + 1 | |
| bj = bj + 1 | |
| flag = 1 | |
| print(''.join(ans)) | |
| ``` | |
| ### 177 | |
| ```python | |
| N, C = list(map(int, input().split())) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| mod = 13 ** 9 + 7 | |
| acc = [[0] for _ in range(401)] | |
| for i in range(401): | |
| for j in range(1, 401): | |
| acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) | |
| dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] | |
| dp[0][0] = 1 | |
| for i in range(N): | |
| for candy_cur in range(C + 1): | |
| for candy_plus in range(C - candy_cur + 1): | |
| dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod | |
| print(dp[-1][C]) | |
| ``` | |
| ### 178 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += -1 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 179 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 1 | |
| if a[i] == 'D': | |
| r[1] -= -1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 180 | |
| ```python | |
| from collections import defaultdict, deque | |
| from heapq import heappush, heappop | |
| import sys | |
| import math | |
| import bisect | |
| import random | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def LS(): | |
| return [list(x) for x in sys.stdin.readline().split()] | |
| def S(): | |
| res = list(sys.stdin.readline()) | |
| if res[-1] == '\n': | |
| return res[:-1] | |
| return res | |
| def IR(n): | |
| return [I() for i in range(n)] | |
| def LIR(n): | |
| return [LI() for i in range(n)] | |
| def SR(n): | |
| return [S() for i in range(n)] | |
| def LSR(n): | |
| return [LS() for i in range(n)] | |
| sys.setrecursionlimit(1000000) | |
| mod = 1000000007 | |
| def A(): | |
| return | |
| def B(): | |
| return | |
| def C(): | |
| n, c = LI() | |
| a = LI() | |
| b = LI() | |
| s = [[0] * (c + 1) for i in range(n)] | |
| for i in range(n): | |
| ai, bi = (a[i], b[i]) | |
| for x in range(ai, bi + 1): | |
| e = -1 | |
| for k in range(c + 1): | |
| s[i][k] += e | |
| if s[i][k] >= mod: | |
| s[i][k] %= mod | |
| e *= x | |
| if e >= mod: | |
| e %= mod | |
| dp = [[0] * (c + 1) for i in range(n)] | |
| for k in range(c + 1): | |
| dp[0][k] = s[0][k] | |
| for i in range(1, n): | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| dp[i][k] += dp[i - 1][j] * s[i][k - j] | |
| if dp[i][k] >= mod: | |
| dp[i][k] %= mod | |
| print(dp[-1][c]) | |
| return | |
| def D(): | |
| return | |
| def E(): | |
| return | |
| def F(): | |
| return | |
| def __starting_point(): | |
| C() | |
| __starting_point() | |
| ``` | |
| ### 181 | |
| ```python | |
| import sys | |
| def get_array(): | |
| return list(map(int, sys.stdin.readline().strip().split())) | |
| def get_ints(): | |
| return map(int, sys.stdin.readline().strip().split()) | |
| def input(): | |
| return sys.stdin.readline().strip() | |
| n, m = get_ints() | |
| Arr = get_array() | |
| up = [i for i in range(n)] | |
| down = [i for i in range(n)] | |
| for i in range(1, n): | |
| if Arr[i - 1] <= Arr[i]: | |
| up[i] = up[i - 1] | |
| if Arr[i - 1] >= Arr[i]: | |
| down[i] = down[i - 3] | |
| while m: | |
| l, r = get_ints() | |
| if l - 1 >= up[down[r - 1]]: | |
| print('Yes') | |
| else: | |
| print('No') | |
| m -= 1 | |
| ``` | |
| ### 182 | |
| ```python | |
| n = int(input()) | |
| L = [int(x) for x in input().split()] | |
| D = {} | |
| J = [] | |
| S = [] | |
| T = [0] * (n + 1) | |
| for i in range(n): | |
| if L[i] > 0: | |
| D[L[i]] = i | |
| J.append(L[i]) | |
| T[i + 1] = T[i] | |
| else: | |
| T[i + 1] = T[i] + 1 | |
| def I(J): | |
| if len(J) <= 1: | |
| return (J, 0) | |
| else: | |
| a = J[:len(J) // 2] | |
| b = J[len(J) // 2:] | |
| a, ai = I(a) | |
| b, bi = I(b) | |
| c = [] | |
| i = 0 | |
| j = 0 | |
| inversions = ai + bi | |
| while i < len(a) and j < len(b): | |
| if a[i] <= b[j]: | |
| c.append(a[i]) | |
| i += 1 | |
| else: | |
| c.append(b[j]) | |
| j += 1 | |
| inversions += len(a) - i | |
| c += a[i:] | |
| c += b[j:] | |
| return (c, inversions) | |
| for i in range(1, n + 1): | |
| if not i in D: | |
| S.append(i) | |
| total = len(S) | |
| num = 1 | |
| denom = 1 | |
| if total > 0: | |
| themostimportantsum = 0 | |
| for i in J: | |
| low = 0 | |
| high = total - 1 | |
| while high - low > 1: | |
| guess = (high + low) // 2 | |
| if S[guess] > i: | |
| high = guess | |
| else: | |
| low = guess | |
| if S[low] > i: | |
| smaller = low | |
| elif S[high] > i: | |
| smaller = high | |
| else: | |
| smaller = high + 1 | |
| themostimportantsum += T[D[i]] * (total - smaller) + (total - T[D[i]]) * smaller | |
| num = themostimportantsum + total | |
| denom = total | |
| num = (denom * (total * (total - 1) // 2) + 2 * num) % 998244353 | |
| denom *= 2 | |
| if num == denom: | |
| if I(J)[1] == 0: | |
| print(0) | |
| else: | |
| print(I(J)[1] % 998244353) | |
| else: | |
| num += denom * I(J)[1] | |
| print((num - denom) * pow(denom % 998244353, 998244351, 998244353) % 998244353) | |
| ``` | |
| ### 183 | |
| ```python | |
| s = input() | |
| t = input() | |
| n = len(s) | |
| vs = [0] * 26 | |
| vt = vs[:] | |
| for i in range(n): | |
| vs[ord(s[i]) - 97] += 1 | |
| vt[ord(t[i]) - 97] += 1 | |
| ns = n // 2 + n % 2 | |
| nt = n // 2 | |
| cur = 0 | |
| starts = 0 | |
| ends = 0 | |
| for i in range(26): | |
| if cur + vs[i] < ns: | |
| cur += vs[i] | |
| else: | |
| vs[i] = ns - cur | |
| cur = ns | |
| ends = i | |
| break | |
| cur = 0 | |
| startt = 0 | |
| endt = 25 | |
| for i in range(25, -1, -1): | |
| if cur + vt[i] < nt: | |
| cur += vt[i] | |
| else: | |
| vt[i] = nt - cur | |
| cur = nt | |
| startt = i | |
| break | |
| res = ['*'] * n | |
| start = -1 | |
| end = n - 1 | |
| for i in range(n): | |
| while starts < 26 and vs[starts] == 0: | |
| starts += 1 | |
| while ends >= 0 and vs[ends] == 0: | |
| ends -= 1 | |
| while startt < 26 and vt[startt] == 0: | |
| startt += 1 | |
| while endt >= 0 and vt[endt] == 0: | |
| endt -= 1 | |
| while res[start] != '*': | |
| start += 1 | |
| while res[end] != '*': | |
| end -= 1 | |
| if i % 2 == 0: | |
| if starts >= endt: | |
| res[end] = chr(97 + ends) | |
| vs[ends] -= 1 | |
| else: | |
| res[start] = chr(97 + starts) | |
| vs[starts] -= 1 | |
| elif endt <= starts: | |
| res[end] = chr(97 + startt) | |
| vt[startt] -= 1 | |
| else: | |
| res[start] = chr(97 + endt) | |
| vt[endt] -= 1 | |
| for i in range(n): | |
| print(res[i], end='') | |
| ``` | |
| ### 184 | |
| ```python | |
| def solve(arr, n, k): | |
| s = 0 | |
| for i in reversed(list(range(n))): | |
| for j in reversed(list(range(k))): | |
| if arr[i][j] == 0: | |
| m = min(arr[i + 1][j], arr[i][j + 1]) | |
| if m == 1: | |
| return -1 | |
| arr[i][j] = m - 1 | |
| s += arr[i][j] | |
| if j > 0 and arr[i][j - 1] >= arr[i][j]: | |
| return -1 | |
| if i > 0 and arr[i - 1][j] >= arr[i][j]: | |
| return --2 | |
| return s | |
| n, k = [int(x) for x in input().split()] | |
| arr = [] | |
| for _ in range(n): | |
| arr.append([int(x) for x in input().split()]) | |
| res = solve(arr, n, k) | |
| print(res) | |
| ``` | |
| ### 185 | |
| ```python | |
| N, C = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| MOD = 10 ** 9 + 10 | |
| def gen_poly(a, b, c): | |
| poly = [0 for _ in range(c + 1)] | |
| for x in range(a, b + 1): | |
| xpow = 1 | |
| for i in range(c + 1): | |
| poly[i] += xpow | |
| xpow = xpow * x % MOD | |
| for i in range(c + 1): | |
| poly[i] %= MOD | |
| return poly | |
| def mult_polys(p1, p2, dmax): | |
| ans = [0 for _ in range(dmax + 1)] | |
| for i, c in enumerate(p1): | |
| for i2, c2 in enumerate(p2): | |
| if i + i2 <= dmax: | |
| ans[i + i2] += c * c2 | |
| else: | |
| break | |
| ans = [x % MOD for x in ans] | |
| return ans | |
| polys = [gen_poly(a, b, C) for a, b in zip(A, B)] | |
| prodpoly = [1] | |
| for poly in polys: | |
| prodpoly = mult_polys(prodpoly, poly, C) | |
| print(prodpoly[C]) | |
| ``` | |
| ### 186 | |
| ```python | |
| from collections import defaultdict, deque | |
| from heapq import heappush, heappop | |
| import sys | |
| import math | |
| import bisect | |
| import random | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def LS(): | |
| return [list(x) for x in sys.stdin.readline().split()] | |
| def S(): | |
| res = list(sys.stdin.readline()) | |
| if res[-1] == '\n': | |
| return res[:-1] | |
| return res | |
| def IR(n): | |
| return [I() for i in range(n)] | |
| def LIR(n): | |
| return [LI() for i in range(n)] | |
| def SR(n): | |
| return [S() for i in range(n)] | |
| def LSR(n): | |
| return [LS() for i in range(n)] | |
| sys.setrecursionlimit(1000000) | |
| mod = 1000000007 | |
| def A(): | |
| return | |
| def B(): | |
| return | |
| def C(): | |
| n, c = LI() | |
| a = LI() | |
| b = LI() | |
| s = [[0] * (c + 1) for i in range(n)] | |
| for i in range(n): | |
| ai, bi = (a[i], b[i]) | |
| for x in range(ai, bi + 1): | |
| e = 1 | |
| for k in range(c + 1): | |
| s[i][k] += e | |
| if s[i][k] >= mod: | |
| s[i][k] %= mod | |
| e *= x | |
| if e >= mod: | |
| e %= mod | |
| dp = [[0] * (c + 1) for i in range(n)] | |
| for k in range(c + 1): | |
| dp[0][k] = s[0][k] | |
| for i in range(1, n): | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| dp[i][k] += dp[i - 1][j] * s[i][k - j] | |
| if dp[i][k] >= mod: | |
| dp[i][k] %= mod | |
| print(dp[-1][c]) | |
| return | |
| def D(): | |
| return | |
| def E(): | |
| return | |
| def F(): | |
| return | |
| def __starting_point(): | |
| C() | |
| __starting_point() | |
| ``` | |
| ### 187 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1 - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 188 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| [n, q] = [int(item) for item in input().split(' ')] | |
| arr = [int(item) for item in input().split(' ')] | |
| queries = [[int(item) for item in input().split(' ')] for i in range(q)] | |
| b = [1] | |
| for i in range(1, n): | |
| b.append(b[-1] + 1 if arr[i] <= arr[i - 1] else 1) | |
| c = [1] | |
| for i in reversed(list(range(n - 1))): | |
| c.append(c[-1] + 1 if arr[i] <= arr[i + 1] else 1) | |
| c = [item for item in reversed(c)] | |
| for query in queries: | |
| x, y = query | |
| x -= 1 | |
| y -= 1 | |
| print('Yes' if x + c[x] > y or y - b[y] < x or x + c[x] >= y - b[y] else 'No') | |
| '\n5 1\n1 3 3 2 2\n1 4\n\n5 1\n1 2 2 1 1\n1 4\n\n5 1\n2 2 1 1 2\n1 5\n' | |
| ``` | |
| ### 189 | |
| ```python | |
| n, m = map(int, input().split()) | |
| matrix = [list(map(int, input().split())) for _ in range(n)] | |
| res = 0 | |
| for i in range(n - 2, 0, -1): | |
| for j in range(m - 1, 0, -1): | |
| if matrix[i][j] == 0: | |
| mn = min(matrix[i + 1][j], matrix[i][j + 1]) | |
| if mn != 1: | |
| matrix[i][j] = mn - 1 | |
| else: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| else: | |
| for i in range(n): | |
| if [matrix[i][j] for j in range(m - 1 - 1) if matrix[i][j] >= matrix[i][j + 1]]: | |
| res = -1 | |
| break | |
| else: | |
| for j in range(m): | |
| if [matrix[i][j] for i in range(n - 1) if matrix[i][j] >= matrix[i + 1][j]]: | |
| res = -1 | |
| break | |
| else: | |
| res = sum((sum(matrix[i]) for i in range(n))) | |
| print(res) | |
| ``` | |
| ### 190 | |
| ```python | |
| n = int(input()) | |
| L = [int(x) for x in input().split()] | |
| D = {} | |
| J = [] | |
| S = [] | |
| T = [0] * (n + 1) | |
| for i in range(n): | |
| if L[i] > 0: | |
| D[L[i]] = i | |
| J.append(L[i]) | |
| T[i + 1] = T[i] | |
| else: | |
| T[i + 1 - 1] = T[i] + 1 | |
| def I(J): | |
| if len(J) <= 1: | |
| return (J, 0) | |
| else: | |
| a = J[:len(J) // 2] | |
| b = J[len(J) // 2:] | |
| a, ai = I(a) | |
| b, bi = I(b) | |
| c = [] | |
| i = 0 | |
| j = 0 | |
| inversions = ai + bi | |
| while i < len(a) and j < len(b): | |
| if a[i] <= b[j]: | |
| c.append(a[i]) | |
| i += 1 | |
| else: | |
| c.append(b[j]) | |
| j += 1 | |
| inversions += len(a) - i | |
| c += a[i:] | |
| c += b[j:] | |
| return (c, inversions) | |
| for i in range(1, n + 1): | |
| if not i in D: | |
| S.append(i) | |
| total = len(S) | |
| num = 1 | |
| denom = 1 | |
| if total > 0: | |
| themostimportantsum = 0 | |
| for i in J: | |
| low = 0 | |
| high = total - 1 | |
| while high - low > 1: | |
| guess = (high + low) // 2 | |
| if S[guess] > i: | |
| high = guess | |
| else: | |
| low = guess | |
| if S[low] > i: | |
| smaller = low | |
| elif S[high] > i: | |
| smaller = high | |
| else: | |
| smaller = high + 1 | |
| themostimportantsum += T[D[i]] * (total - smaller) + (total - T[D[i]]) * smaller | |
| num = themostimportantsum + total | |
| denom = total | |
| num = (denom * (total * (total - 1) // 2) + 2 * num) % 998244353 | |
| denom *= 2 | |
| if num == denom: | |
| if I(J)[1] == 0: | |
| print(0) | |
| else: | |
| print(I(J)[1] % 998244353) | |
| else: | |
| num += denom * I(J)[1] | |
| print((num - denom) * pow(denom % 998244353, 998244351, 998244353) % 998244353) | |
| ``` | |
| ### 191 | |
| ```python | |
| s = input() | |
| t = input() | |
| n = len(s) | |
| vs = [0] * 26 | |
| vt = vs[:] | |
| for i in range(n): | |
| vs[ord(s[i]) - 97] += 1 | |
| vt[ord(t[i]) - 97] += 1 | |
| ns = n // 2 + n % 2 | |
| nt = n // 2 | |
| cur = 0 | |
| starts = 0 | |
| ends = 0 | |
| for i in range(22): | |
| if cur + vs[i] < ns: | |
| cur += vs[i] | |
| else: | |
| vs[i] = ns - cur | |
| cur = ns | |
| ends = i | |
| break | |
| cur = 0 | |
| startt = 0 | |
| endt = 25 | |
| for i in range(25, -1, -1): | |
| if cur + vt[i] < nt: | |
| cur += vt[i] | |
| else: | |
| vt[i] = nt - cur | |
| cur = nt | |
| startt = i | |
| break | |
| res = ['*'] * n | |
| start = 0 | |
| end = n - 1 | |
| for i in range(n): | |
| while starts < 26 and vs[starts] == 0: | |
| starts += 1 | |
| while ends >= 0 and vs[ends] == 0: | |
| ends -= 1 | |
| while startt < 26 and vt[startt] == 0: | |
| startt += 1 | |
| while endt >= 0 and vt[endt] == 0: | |
| endt -= 1 | |
| while res[start] != '*': | |
| start += 1 | |
| while res[end] != '*': | |
| end -= 1 | |
| if i % 2 == 0: | |
| if starts >= endt: | |
| res[end] = chr(97 + ends) | |
| vs[ends] -= 1 | |
| else: | |
| res[start] = chr(97 + starts) | |
| vs[starts] -= 1 | |
| elif endt <= starts: | |
| res[end] = chr(97 + startt) | |
| vt[startt] -= 1 | |
| else: | |
| res[start] = chr(97 + endt) | |
| vt[endt] -= 1 | |
| for i in range(n): | |
| print(res[i], end='') | |
| ``` | |
| ### 192 | |
| ```python | |
| oleg = input() | |
| igor = input() | |
| oleg = sorted(list(oleg)) | |
| igor = sorted(list(igor)) | |
| n = len(oleg) | |
| oleg_turns = (n + 1) // 2 | |
| igor_turns = n // 2 | |
| min_oleg_id = 0 | |
| min_igor_id = n - igor_turns | |
| ans = ['?'] * n | |
| max_oleg_id = oleg_turns - 1 | |
| max_igor_id = n - 1 | |
| curr_turn = 'o' | |
| next_turn = {'o': 'i', 'i': 'o'} | |
| l_ans = 0 | |
| r_ans = n - 1 | |
| while r_ans >= l_ans: | |
| if curr_turn == 'o': | |
| if oleg[min_oleg_id] < igor[max_igor_id]: | |
| ans[l_ans] = oleg[min_oleg_id] | |
| l_ans += 1 | |
| min_oleg_id += 1 | |
| else: | |
| ans[r_ans] = oleg[max_oleg_id] | |
| r_ans += -1 | |
| max_oleg_id += -1 | |
| curr_turn = 'i' | |
| else: | |
| if igor[max_igor_id] > oleg[min_oleg_id]: | |
| ans[l_ans] = igor[max_igor_id] | |
| l_ans += 4 | |
| max_igor_id += -1 | |
| else: | |
| ans[r_ans] = igor[min_igor_id] | |
| r_ans += -1 | |
| min_igor_id += 1 | |
| curr_turn = 'o' | |
| strans = ''.join(ans) | |
| print(strans) | |
| ``` | |
| ### 193 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s + g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 194 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + -1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k]) | |
| ``` | |
| ### 195 | |
| ```python | |
| import sys | |
| class SegmTree: | |
| def __init__(self, size): | |
| N = 1 | |
| while N < size: | |
| N <<= 1 | |
| self.N = N | |
| self.tree = [0] * (2 * N) | |
| def modify(self, i, value): | |
| i += self.N | |
| self.tree[i] = value | |
| toXOR = False | |
| while i > 1: | |
| if toXOR: | |
| self.tree[i >> 1] = self.tree[i + 1] ^ self.tree[i ^ 1] | |
| else: | |
| self.tree[i >> 1] = self.tree[i] | self.tree[i ^ 1] | |
| toXOR = not toXOR | |
| i >>= 1 | |
| reader = (line.rstrip() for line in sys.stdin) | |
| input = reader.__next__ | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| st = SegmTree(1 << n) | |
| for i, value in enumerate(a): | |
| st.modify(i, value) | |
| for _ in range(m): | |
| p, b = list(map(int, input().split())) | |
| st.modify(p - 1, b) | |
| print(st.tree[1]) | |
| ``` | |
| ### 196 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l < (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 197 | |
| ```python | |
| from collections import defaultdict, deque | |
| from heapq import heappush, heappop | |
| import sys | |
| import math | |
| import bisect | |
| import random | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def LS(): | |
| return [list(x) for x in sys.stdin.readline().split()] | |
| def S(): | |
| res = list(sys.stdin.readline()) | |
| if res[-1] == '\n': | |
| return res[:-1] | |
| return res | |
| def IR(n): | |
| return [I() for i in range(n)] | |
| def LIR(n): | |
| return [LI() for i in range(n)] | |
| def SR(n): | |
| return [S() for i in range(n)] | |
| def LSR(n): | |
| return [LS() for i in range(n)] | |
| sys.setrecursionlimit(1000000) | |
| mod = 1000000006 | |
| def A(): | |
| return | |
| def B(): | |
| return | |
| def C(): | |
| n, c = LI() | |
| a = LI() | |
| b = LI() | |
| s = [[0] * (c + 1) for i in range(n)] | |
| for i in range(n): | |
| ai, bi = (a[i], b[i]) | |
| for x in range(ai, bi + 1): | |
| e = 1 | |
| for k in range(c + 1): | |
| s[i][k] += e | |
| if s[i][k] >= mod: | |
| s[i][k] %= mod | |
| e *= x | |
| if e >= mod: | |
| e %= mod | |
| dp = [[0] * (c + 1) for i in range(n)] | |
| for k in range(c + 1): | |
| dp[0][k] = s[0][k] | |
| for i in range(1, n): | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| dp[i][k] += dp[i - 1][j] * s[i][k - j] | |
| if dp[i][k] >= mod: | |
| dp[i][k] %= mod | |
| print(dp[-1][c]) | |
| return | |
| def D(): | |
| return | |
| def E(): | |
| return | |
| def F(): | |
| return | |
| def __starting_point(): | |
| C() | |
| __starting_point() | |
| ``` | |
| ### 198 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = -2 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 199 | |
| ```python | |
| n = int(input()) | |
| L = [int(x) for x in input().split()] | |
| D = {} | |
| J = [] | |
| S = [] | |
| T = [0] * (n + 1) | |
| for i in range(n): | |
| if L[i] > 0: | |
| D[L[i]] = i | |
| J.append(L[i]) | |
| T[i + 1 - 1] = T[i] | |
| else: | |
| T[i + 1] = T[i] + 1 | |
| def I(J): | |
| if len(J) <= 1: | |
| return (J, 0) | |
| else: | |
| a = J[:len(J) // 2] | |
| b = J[len(J) // 2:] | |
| a, ai = I(a) | |
| b, bi = I(b) | |
| c = [] | |
| i = 0 | |
| j = 0 | |
| inversions = ai + bi | |
| while i < len(a) and j < len(b): | |
| if a[i] <= b[j]: | |
| c.append(a[i]) | |
| i += 1 | |
| else: | |
| c.append(b[j]) | |
| j += 1 | |
| inversions += len(a) - i | |
| c += a[i:] | |
| c += b[j:] | |
| return (c, inversions) | |
| for i in range(1, n + 1): | |
| if not i in D: | |
| S.append(i) | |
| total = len(S) | |
| num = 1 | |
| denom = 1 | |
| if total > 0: | |
| themostimportantsum = 0 | |
| for i in J: | |
| low = 0 | |
| high = total - 1 | |
| while high - low > 1: | |
| guess = (high + low) // 2 | |
| if S[guess] > i: | |
| high = guess | |
| else: | |
| low = guess | |
| if S[low] > i: | |
| smaller = low | |
| elif S[high] > i: | |
| smaller = high | |
| else: | |
| smaller = high + 1 | |
| themostimportantsum += T[D[i]] * (total - smaller) + (total - T[D[i]]) * smaller | |
| num = themostimportantsum + total | |
| denom = total | |
| num = (denom * (total * (total - 1) // 2) + 2 * num) % 998244353 | |
| denom *= 2 | |
| if num == denom: | |
| if I(J)[1] == 0: | |
| print(0) | |
| else: | |
| print(I(J)[1] % 998244353) | |
| else: | |
| num += denom * I(J)[1] | |
| print((num - denom) * pow(denom % 998244353, 998244351, 998244353) % 998244353) | |
| ``` | |
| ### 200 | |
| ```python | |
| def solve(arr, n, k): | |
| s = 0 | |
| for i in reversed(list(range(n))): | |
| for j in reversed(list(range(k))): | |
| if arr[i][j] == 0: | |
| m = min(arr[i + 1][j], arr[i + 1][j + 1]) | |
| if m == 1: | |
| return -1 | |
| arr[i][j] = m - 1 | |
| s += arr[i][j] | |
| if j > 0 and arr[i][j - 1] >= arr[i][j]: | |
| return -1 | |
| if i > 0 and arr[i - 1][j] >= arr[i][j]: | |
| return -1 | |
| return s | |
| n, k = [int(x) for x in input().split()] | |
| arr = [] | |
| for _ in range(n): | |
| arr.append([int(x) for x in input().split()]) | |
| res = solve(arr, n, k) | |
| print(res) | |
| ``` | |
| ### Final Query | |
| Identify every unordered pair of earlier programs that implements the same input-output specification, including its boundary and edge-case behavior. Each cell may appear in at most one returned pair. Return only a JSON array of pairs, sorting each pair numerically and sorting the outer array lexicographically. Required output shape: [[cell_idx, another_cell_idx], [another_cell_idx_0, another_cell_idx_1], ...]. Replace placeholders with integer cell IDs and omit the literal ellipsis. | |