Submission #74710830


Source Code Expand

import sys
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lii = lambda: list(mii())
inf = 1 << 64
class SortedList:
    def __init__(self, iterable=None, _load=200):
        """Initialize sorted list instance."""
        if iterable is None:
            iterable = []
        values = sorted(iterable)
        self._len = _len = len(values)
        self._load = _load
        self._lists = _lists = [values[i:i + _load]
                                for i in range(0, _len, _load)]
        self._list_lens = [len(_list) for _list in _lists]
        self._min_s = [_list[0] for _list in _lists]
        self._fen_tree = []
        self._rebuild = True

    def _fen_build(self):
        """Build a fenwick tree instance."""
        self._fen_tree[:] = self._list_lens
        _fen_tree = self._fen_tree
        for i in range(len(_fen_tree)):
            if i | i + 1 < len(_fen_tree):
                _fen_tree[i | i + 1] += _fen_tree[i]
        self._rebuild = False

    def _fen_update(self, index, value):
        """Update `fen_tree[index] += value`."""
        if not self._rebuild:
            _fen_tree = self._fen_tree
            while index < len(_fen_tree):
                _fen_tree[index] += value
                index |= index + 1

    def _fen_query(self, end):
        """Return `sum(_fen_tree[:end])`."""
        if self._rebuild:
            self._fen_build()

        _fen_tree = self._fen_tree
        x = 0
        while end:
            x += _fen_tree[end - 1]
            end &= end - 1
        return x

    def _fen_findkth(self, k):
        """Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`)."""
        _list_lens = self._list_lens
        if k < _list_lens[0]:
            return 0, k
        if k >= self._len - _list_lens[-1]:
            return len(_list_lens) - 1, k + _list_lens[-1] - self._len
        if self._rebuild:
            self._fen_build()

        _fen_tree = self._fen_tree
        idx = -1
        for d in reversed(range(len(_fen_tree).bit_length())):
            right_idx = idx + (1 << d)
            if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
                idx = right_idx
                k -= _fen_tree[idx]
        return idx + 1, k

    def _delete(self, pos, idx):
        """Delete value at the given `(pos, idx)`."""
        _lists = self._lists
        _mins = self._min_s
        _list_lens = self._list_lens

        self._len -= 1
        self._fen_update(pos, -1)
        del _lists[pos][idx]
        _list_lens[pos] -= 1

        if _list_lens[pos]:
            _mins[pos] = _lists[pos][0]
        else:
            del _lists[pos]
            del _list_lens[pos]
            del _mins[pos]
            self._rebuild = True

    def _loc_left(self, value):
        """Return an index pair that corresponds to the first position of `value` in the sorted list."""
        if not self._len:
            return 0, 0

        _lists = self._lists
        _mins = self._min_s

        lo, pos = -1, len(_lists) - 1
        while lo + 1 < pos:
            mi = (lo + pos) >> 1
            if value <= _mins[mi]:
                pos = mi
            else:
                lo = mi

        if pos and value <= _lists[pos - 1][-1]:
            pos -= 1

        _list = _lists[pos]
        lo, idx = -1, len(_list)
        while lo + 1 < idx:
            mi = (lo + idx) >> 1
            if value <= _list[mi]:
                idx = mi
            else:
                lo = mi

        return pos, idx

    def _loc_right(self, value):
        """Return an index pair that corresponds to the last position of `value` in the sorted list."""
        if not self._len:
            return 0, 0

        _lists = self._lists
        _mins = self._min_s

        pos, hi = 0, len(_lists)
        while pos + 1 < hi:
            mi = (pos + hi) >> 1
            if value < _mins[mi]:
                hi = mi
            else:
                pos = mi

        _list = _lists[pos]
        lo, idx = -1, len(_list)
        while lo + 1 < idx:
            mi = (lo + idx) >> 1
            if value < _list[mi]:
                idx = mi
            else:
                lo = mi

        return pos, idx

    def add(self, value):
        """Add `value` to sorted list."""
        _load = self._load
        _lists = self._lists
        _mins = self._min_s
        _list_lens = self._list_lens

        self._len += 1
        if _lists:
            pos, idx = self._loc_right(value)
            self._fen_update(pos, 1)
            _list = _lists[pos]
            _list.insert(idx, value)
            _list_lens[pos] += 1
            _mins[pos] = _list[0]
            if _load + _load < len(_list):
                _lists.insert(pos + 1, _list[_load:])
                _list_lens.insert(pos + 1, len(_list) - _load)
                _mins.insert(pos + 1, _list[_load])
                _list_lens[pos] = _load
                del _list[_load:]
                self._rebuild = True
        else:
            _lists.append([value])
            _mins.append(value)
            _list_lens.append(1)
            self._rebuild = True

    def discard(self, value):
        """Remove `value` from sorted list if it is a member."""
        _lists = self._lists
        if _lists:
            pos, idx = self._loc_right(value)
            if idx and _lists[pos][idx - 1] == value:
                self._delete(pos, idx - 1)

    def remove(self, value):
        """Remove `value` from sorted list; `value` must be a member."""
        _len = self._len
        self.discard(value)
        if _len == self._len:
            raise ValueError('{0!r} not in list'.format(value))

    def pop(self, index=-1):
        """Remove and return value at `index` in sorted list."""
        pos, idx = self._fen_findkth(self._len + index if index < 0 else index)
        value = self._lists[pos][idx]
        self._delete(pos, idx)
        return value

    def bisect_left(self, value):
        """Return the first index to insert `value` in the sorted list."""
        pos, idx = self._loc_left(value)
        return self._fen_query(pos) + idx

    def bisect_right(self, value):
        """Return the last index to insert `value` in the sorted list."""
        pos, idx = self._loc_right(value)
        return self._fen_query(pos) + idx

    def count(self, value):
        """Return number of occurrences of `value` in the sorted list."""
        return self.bisect_right(value) - self.bisect_left(value)

    def __len__(self):
        """Return the size of the sorted list."""
        return self._len

    def __getitem__(self, index):
        """Lookup value at `index` in sorted list."""
        pos, idx = self._fen_findkth(self._len + index if index < 0 else index)
        return self._lists[pos][idx]

    def __delitem__(self, index):
        """Remove value at `index` from sorted list."""
        pos, idx = self._fen_findkth(self._len + index if index < 0 else index)
        self._delete(pos, idx)

    def __contains__(self, value):
        """Return true if `value` is an element of the sorted list."""
        _lists = self._lists
        if _lists:
            pos, idx = self._loc_left(value)
            return idx < len(_lists[pos]) and _lists[pos][idx] == value
        return False

    def __iter__(self):
        """Return an iterator over the sorted list."""
        return (value for _list in self._lists for value in _list)

    def __reversed__(self):
        """Return a reverse iterator over the sorted list."""
        return (value for _list in reversed(self._lists)
                for value in reversed(_list))

    def __repr__(self):
        """Return strings representation of sorted list."""
        return 'SortedList({0})'.format(list(self))
def solve():
    n,d = mii()
    a = lii()
    def check(nums,k):
        sl = SortedList()
        inv = 0
        ans = 0
        l = 0
        for r, x in enumerate(nums):
            sl.add(x)
            inv += len(sl) - sl.bisect_right(x)
            while inv>=k and l<n and sl:
                out = nums[l]
                inv -= sl.bisect_left(out) 
                sl.remove(out)
                l+=1
            ans+=l
        return ans
    ans1 = check(a,d)
    ans2 = check(a,d+1)
    print(ans1-ans2)
t = 1
# t = ii()
for _ in range(t):
    solve()

Submission Info

Submission Time
Task F - Interval Inversion Count
User xiaoe
Language Python (PyPy 3.11-v7.3.20)
Score 500
Code Size 8680 Byte
Status AC
Exec Time 1289 ms
Memory 184468 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 55
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 55 ms 81428 KiB
00_sample_01.txt AC 54 ms 81216 KiB
00_sample_02.txt AC 55 ms 81596 KiB
01_random_03.txt AC 709 ms 158464 KiB
01_random_04.txt AC 653 ms 146624 KiB
01_random_05.txt AC 566 ms 135756 KiB
01_random_06.txt AC 470 ms 135688 KiB
01_random_07.txt AC 591 ms 148184 KiB
01_random_08.txt AC 437 ms 136192 KiB
01_random_09.txt AC 584 ms 139336 KiB
01_random_10.txt AC 1216 ms 183808 KiB
01_random_11.txt AC 1237 ms 184204 KiB
01_random_12.txt AC 1235 ms 184320 KiB
01_random_13.txt AC 1099 ms 184032 KiB
01_random_14.txt AC 1003 ms 184192 KiB
01_random_15.txt AC 1255 ms 184224 KiB
01_random_16.txt AC 1289 ms 184468 KiB
01_random_17.txt AC 1205 ms 183928 KiB
01_random_18.txt AC 947 ms 184128 KiB
01_random_19.txt AC 1213 ms 184160 KiB
01_random_20.txt AC 903 ms 184124 KiB
01_random_21.txt AC 982 ms 184356 KiB
01_random_22.txt AC 972 ms 184120 KiB
01_random_23.txt AC 1264 ms 184292 KiB
01_random_24.txt AC 413 ms 184204 KiB
01_random_25.txt AC 533 ms 183588 KiB
01_random_26.txt AC 563 ms 183820 KiB
01_random_27.txt AC 449 ms 183724 KiB
01_random_28.txt AC 520 ms 183540 KiB
01_random_29.txt AC 566 ms 183684 KiB
01_random_30.txt AC 411 ms 184208 KiB
01_random_31.txt AC 462 ms 184260 KiB
01_random_32.txt AC 470 ms 184152 KiB
01_random_33.txt AC 470 ms 184196 KiB
01_random_34.txt AC 477 ms 184400 KiB
01_random_35.txt AC 471 ms 184040 KiB
01_random_36.txt AC 55 ms 81408 KiB
01_random_37.txt AC 475 ms 183776 KiB
01_random_38.txt AC 552 ms 183572 KiB
01_random_39.txt AC 439 ms 183600 KiB
01_random_40.txt AC 575 ms 183696 KiB
01_random_41.txt AC 499 ms 183820 KiB
01_random_42.txt AC 454 ms 183576 KiB
01_random_43.txt AC 396 ms 183824 KiB
01_random_44.txt AC 474 ms 183728 KiB
01_random_45.txt AC 571 ms 183724 KiB
01_random_46.txt AC 555 ms 183584 KiB
01_random_47.txt AC 578 ms 183568 KiB
01_random_48.txt AC 578 ms 183728 KiB
01_random_49.txt AC 573 ms 183668 KiB
01_random_50.txt AC 500 ms 159516 KiB
01_random_51.txt AC 347 ms 159604 KiB
01_random_52.txt AC 482 ms 159176 KiB
01_random_53.txt AC 401 ms 159444 KiB
01_random_54.txt AC 527 ms 170920 KiB