1401F - Reverse and Swap - CodeForces Solution


binary search bitmasks data structures *2400

Please click on ads to support us..

Python Code:

import sys
RI = lambda: list(map(int, sys.stdin.readline().strip().split()))
n, q = RI()
oa = RI()
le = len(oa) - 1
f = [0] * (n + 1)
ps = [0] * (le * 2 + 2)
def up(t, i, l, r, x, d):
    if x < l or x > r:
        return -1
    ps[t] += d
    if l == r:
        return l
    if f[i]:
        x = l + r - x
    mi = l + r >> 1
    if x > mi:
        return up(t * 2 + 1, i + 1, mi + 1, r, x, d)
    return up(t * 2, i + 1, l, mi, x, d)
def qu(t, i, l, r, a, b):
    if b < l or a > r:
        return 0
    if a <= l and b >= r:
        return ps[t]
    if f[i]:
        a, b = l + r - b, l + r - a
    mi = l + r >> 1
    return qu(t * 2, i + 1, l, mi, a, b) + qu(t * 2 + 1, i + 1, mi + 1, r, a, b)
for i, v in enumerate(oa):
    up(1, 0,0, le, i, v)
for _ in range(q):
    ql = RI()
    qt, qi = ql[0], ql[1]
    if qt & 2:
        f[n - qi] ^= 1
        f[n - qi - 1] ^= qt & 1
    elif qt & 1:
        gi = up(1, 0,0, le, qi - 1, 0)
        up(1, 0, 0, le, qi - 1, ql[2] - oa[gi])
        oa[gi] = ql[2]
    else:
        print(qu(1, 0, 0, le, ql[1] - 1, ql[2] - 1))


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST