Problem:


Score : 600 points

Problem Statement

We have a set S of N points in a two-dimensional plane. The coordinates of the i-th point are (x_i, y_i). The N points have distinct x-coordinates and distinct y-coordinates.

For a non-empty subset T of S, let f(T) be the number of points contained in the smallest rectangle, whose sides are parallel to the coordinate axes, that contains all the points in T. More formally, we define f(T) as follows:

f(T) :=  (the number of integers i (1 \leq i \leq N) such that a \leq x_i \leq b and c \leq y_i \leq d, where a, b, c, and d are the minimum x-coordinate, the maximum x-coordinate, the minimum y-coordinate, and the maximum y-coordinate of the points in T)

Find the sum of f(T) over all non-empty subset T of S. Since it can be enormous, print the sum modulo 998244353.

Constraints

1 \leq N \leq 2 \times 10^5

-10^9 \leq x_i, y_i \leq 10^9

x_i \neq x_j (i \neq j)

y_i \neq y_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the sum of f(T) over all non-empty subset T of S, modulo 998244353.

Sample Input 1

3
-1 3
2 1
3 -2

Sample Output 1

13

Let the first, second, and third points be P_1, P_2, and P_3, respectively. S = \{P_1, P_2, P_3\} has seven non-empty subsets, and f has the following values for each of them:

f(\{P_1\}) = 1

f(\{P_2\}) = 1

f(\{P_3\}) = 1

f(\{P_1, P_2\}) = 2

f(\{P_2, P_3\}) = 2

f(\{P_3, P_1\}) = 3

f(\{P_1, P_2, P_3\}) = 3

The sum of these is 13.

Sample Input 2

4
1 4
2 1
3 3
4 2

Sample Output 2

34

Sample Input 3

10
19 -11
-3 -12
5 3
3 -15
8 -14
-9 -20
10 -9
0 2
-7 17
6 -6

Sample Output 3

7222

Be sure to print the sum modulo 998244353.



Sample input:
3
-1 3
2 1
3 -2


Sample output:
13

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
# TLE


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


N = int(input())
hx = []
hy = []

for i in range(N):
    x, y = map(int, input().split())
    heappush(hx, (x, y))

y_list = [0] * N
for i in range(N):
    x, y = heappop(hx)
    y_list[i] = y
    heappush(hy, (y, i))

y_nor_list = [0] * N
for j in range(N):
    y, i = heappop(hy)
    y_nor_list[i] = j

sg_left = Bit(N)
sg_right = Bit(N)

left_lower = [0] * N
left_upper = [0] * N
right_lower = [0] * N
right_upper = [0] * N

for i in range(N):
    c = y_nor_list[i] + 1
    left_lower[i] = sg_left.sum(c)
    left_upper[i] = i - left_lower[i]
    sg_left.add(c, 1)

for i in range(N-1, -1, -1):
    c = y_nor_list[i] + 1
    right_lower[i] = sg_right.sum(c)
    right_upper[i] = N - 1 - i - right_lower[i]
    sg_right.add(c, 1)

# print(left_lower)
# print(left_upper)
# print(right_lower)
# print(right_upper)

LARGE = 998244353

res = 0
for i in range(N):
    r = pow(2, N, LARGE) - 1
    r -= pow(2, left_lower[i] + left_upper[i], LARGE) - 1
    r -= pow(2, right_lower[i] + right_upper[i], LARGE) - 1
    r -= pow(2, left_lower[i] + right_lower[i], LARGE) - 1
    r -= pow(2, left_upper[i] + right_upper[i], LARGE) - 1
    r += pow(2, left_lower[i], LARGE) - 1
    r += pow(2, left_upper[i], LARGE) - 1
    r += pow(2, right_lower[i], LARGE) - 1
    r += pow(2, right_upper[i], LARGE) - 1
    res += r

print(res % LARGE)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
# TLE


class SegmentTree(object):

    def __init__(self, init_array, seg_func=min, seg_func_null=10 ** 9 + 7):

        self.seg_func = seg_func
        self.seg_func_null = seg_func_null
        self.n = 1
        while self.n < len(init_array):
            self.n *= 2
        self.dat = [0] * (2 * self.n - 1)
        for i in range(len(init_array)):
            self.dat[self.n - 1 + i] = init_array[i]
        for i in range(self.n - 2, -1, -1):
            self.dat[i] = self.seg_func(self.dat[2 * i + 1], self.dat[2 * i + 2])

    def update(self, k, a):
        k += self.n - 1
        self.dat[k] = a
        while k > 0:
            k = (k - 1) // 2
            self.dat[k] = self.seg_func(self.dat[k * 2 + 1], self.dat[k * 2 + 2])

    def query(self, p, q):
        # [p, q)
        if q <= p:
            return self.seg_func_null

        p += self.n - 1
        q += self.n - 2
        res = self.seg_func_null

        while q - p > 1:
            if p & 1 == 0:
                res = self.seg_func(res, self.dat[p])
            if q & 1 == 1:
                res = self.seg_func(res, self.dat[q])
                q -= 1
            p = p // 2
            q = (q-1) // 2
        if p == q:
            res = self.seg_func(res, self.dat[p])
        else:
            res = self.seg_func(self.seg_func(res, self.dat[p]), self.dat[q])

        return res


N = int(input())
hx = []
hy = []

for i in range(N):
    x, y = map(int, input().split())
    heappush(hx, (x, y))

y_list = [0] * N
for i in range(N):
    x, y = heappop(hx)
    y_list[i] = y
    heappush(hy, (y, i))

y_nor_list = [0] * N
for j in range(N):
    y, i = heappop(hy)
    y_nor_list[i] = j


def add(x, y):
    return x + y


sg_left = SegmentTree(init_array=[0]*N, seg_func=add, seg_func_null=0)
sg_right = SegmentTree(init_array=[0]*N, seg_func=add, seg_func_null=0)

left_lower = [0] * N
left_upper = [0] * N
right_lower = [0] * N
right_upper = [0] * N

for i in range(N):
    c = y_nor_list[i]
    left_lower[i] = sg_left.query(0, c)
    left_upper[i] = i - left_lower[i]
    sg_left.update(c, 1)

for i in range(N-1, -1, -1):
    c = y_nor_list[i]
    right_lower[i] = sg_right.query(0, c)
    right_upper[i] = N - 1 - i - right_lower[i]
    sg_right.update(c, 1)

# print(left_lower)
# print(left_upper)
# print(right_lower)
# print(right_upper)

LARGE = 998244353

res = 0
for i in range(N):
    r = pow(2, N, LARGE) - 1
    r -= pow(2, left_lower[i] + left_upper[i], LARGE) - 1
    r -= pow(2, right_lower[i] + right_upper[i], LARGE) - 1
    r -= pow(2, left_lower[i] + right_lower[i], LARGE) - 1
    r -= pow(2, left_upper[i] + right_upper[i], LARGE) - 1
    r += pow(2, left_lower[i], LARGE) - 1
    r += pow(2, left_upper[i], LARGE) - 1
    r += pow(2, right_lower[i], LARGE) - 1
    r += pow(2, right_upper[i], LARGE) - 1
    res += r

print(res % LARGE)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop


class SegmentTree(object):

    def __init__(self, init_array, seg_func=min, seg_func_null=10 ** 9 + 7):

        self.seg_func = seg_func
        self.seg_func_null = seg_func_null
        self.n = 1
        while self.n < len(init_array):
            self.n *= 2
        self.dat = [0] * (2 * self.n - 1)
        for i in range(len(init_array)):
            self.dat[self.n - 1 + i] = init_array[i]
        for i in range(self.n - 2, -1, -1):
            self.dat[i] = self.seg_func(self.dat[2 * i + 1], self.dat[2 * i + 2])

    def update(self, k, a):
        k += self.n - 1
        self.dat[k] = a
        while k > 0:
            k = (k - 1) // 2
            self.dat[k] = self.seg_func(self.dat[k * 2 + 1], self.dat[k * 2 + 2])

    def query(self, p, q):
        # [p, q)
        if q <= p:
            return self.seg_func_null

        p += self.n - 1
        q += self.n - 2
        res = self.seg_func_null

        while q - p > 1:
            if p & 1 == 0:
                res = self.seg_func(res, self.dat[p])
            if q & 1 == 1:
                res = self.seg_func(res, self.dat[q])
                q -= 1
            p = p // 2
            q = (q-1) // 2
        if p == q:
            res = self.seg_func(res, self.dat[p])
        else:
            res = self.seg_func(self.seg_func(res, self.dat[p]), self.dat[q])

        return res


N = int(input())
hx = []
hy = []

for i in range(N):
    x, y = map(int, input().split())
    heappush(hx, (x, y))

y_list = [0] * N
for i in range(N):
    x, y = heappop(hx)
    y_list[i] = y
    heappush(hy, (y, i))

y_nor_list = [0] * N
for j in range(N):
    y, i = heappop(hy)
    y_nor_list[i] = j


def add(x, y):
    return x + y


sg_left = SegmentTree(init_array=[0]*N, seg_func=add, seg_func_null=0)
sg_right = SegmentTree(init_array=[0]*N, seg_func=add, seg_func_null=0)

left_lower = [0] * N
left_upper = [0] * N
right_lower = [0] * N
right_upper = [0] * N

for i in range(N):
    c = y_nor_list[i]
    left_lower[i] = sg_left.query(0, c)
    left_upper[i] = sg_left.query(c, N)
    sg_left.update(c, 1)

for i in range(N-1, -1, -1):
    c = y_nor_list[i]
    right_lower[i] = sg_right.query(0, c)
    right_upper[i] = sg_right.query(c, N)
    sg_right.update(c, 1)

# print(left_lower)
# print(left_upper)
# print(right_lower)
# print(right_upper)

LARGE = 998244353

res = 0
for i in range(N):
    r = pow(2, N, LARGE) - 1
    r -= pow(2, left_lower[i] + left_upper[i], LARGE) - 1
    r -= pow(2, right_lower[i] + right_upper[i], LARGE) - 1
    r -= pow(2, left_lower[i] + right_lower[i], LARGE) - 1
    r -= pow(2, left_upper[i] + right_upper[i], LARGE) - 1
    r += pow(2, left_lower[i], LARGE) - 1
    r += pow(2, left_upper[i], LARGE) - 1
    r += pow(2, right_lower[i], LARGE) - 1
    r += pow(2, right_upper[i], LARGE) - 1
    res += r

print(res % LARGE)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

n = int(input())
xy = [tuple(map(int, input().split())) for _ in range(n)]
mod = 998244353

xs = [x for x, y in xy]
ys = [y for x, y in xy]
xs.sort()
ys.sort()
x_dict = dict(zip(xs, range(1, 1 + n)))
y_dict = dict(zip(ys, range(1, 1 + n)))

points = set((x_dict[x], y_dict[y]) for x, y in xy)

cum_array = [[0] * (n + 1) for _ in range(1 + n)]
for i in range(1, 1 + n):
    for j in range(1, 1 + n):
        cum_array[i][j] = (
            cum_array[i][j - 1]
            + cum_array[i - 1][j]
            - cum_array[i - 1][j - 1]
            + ((i, j) in points)
        )

ans = 0


for x, y in points:
    n0 = cum_array[x - 1][y - 1]
    n1 = cum_array[n][y] - cum_array[x][y]
    n2 = cum_array[x][n] - cum_array[x][y]
    n3 = n - cum_array[n][y] - cum_array[x][n] + cum_array[x][y]
    counts = [n0, n1, n2, n3]

    for exist in product([False, True], repeat=4):  # Trueならあり
        tmp = 1
        for e, c in zip(exist, counts):
            if e:
                tmp *= 2 ** c - 1
        if (exist[1] & exist[2]) | (exist[0] & exist[3]):
            tmp *= 2
        ans += tmp
        ans %= mod

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
p = 998244353
S = [[0, 0] for i in range(N)]
for i in range(N):
  S[i] = [int(x) for x in input().split()]
S.sort()
Y = sorted([i for i in range(N)], key=lambda y:S[y][1])
D = [[0, 0, 0, 0] for i in range(N)]
BIT = [0] * N
def query(y):
  _y = y + 1
  ans = 0
  while _y > 0:
    ans += BIT[_y - 1]
    _y -= _y & -_y
  return ans
def update(y):
  _y = y + 1
  while _y <= N:
    BIT[_y - 1] += 1
    _y += _y & -_y
  return 0
for i, y in enumerate(Y):
  D[y][0] = query(y)
  D[y][1] = i - D[y][0]
  update(y)
BIT = [0] * N
for i, y in enumerate(Y[::-1]):
  D[y][2] = query(y)
  D[y][3] = i - D[y][2]
  update(y)
cnt = 0
for i in range(N):
  a, b, c, d = [pow(2, d, p) - 1 for d in D[i]]
  cnt += pow(2, N - 1, p)
  cnt += a * d * (b + c + 1)
  cnt += b * c * (a + d + 1)
  cnt += a * b * c * d
  cnt %= p
print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
p = 998244353
S = [[0, 0] for i in range(N)]
for i in range(N):
  S[i] = [int(x) for x in input().split()]
S.sort()
Y = sorted([i for i in range(N)], key=lambda y:S[y][1])
D = [[0, 0, 0, 0] for i in range(N)]
BIT = [0] * N
def query(y):
  _y = y + 1
  ans = 0
  while _y > 0:
    ans += BIT[_y - 1]
    _y -= _y & -_y
  return ans
def update(y):
  _y = y + 1
  while _y <= N:
    BIT[_y - 1] += 1
    _y += _y & -_y
  return 0
for i, y in enumerate(Y):
  D[y][0] = query(y)
  D[y][1] = i - D[y][0]
  update(y)
BIT = [0] * N
for i, y in enumerate(Y[::-1]):
  D[y][2] = query(y)
  D[y][3] = i - D[y][2]
  update(y)
cnt = 0
for i in range(N):
  a, b, c, d = [pow(2, d, p) - 1 for d in D[i]]
  cnt += 2 ** (N - 1)
  cnt += a * d * (b + c + 1)
  cnt += b * c * (a + d + 1)
  cnt += a * b * c * d
  cnt %= p
print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
p = 998244353
S = [[0, 0] for i in range(N)]
for i in range(N):
  S[i] = [int(x) for x in input().split()]
S.sort()
Y = sorted([i for i in range(N)], key=lambda y:S[y][1])
D = [[0, 0, 0, 0] for i in range(N)]
BIT = [0] * N
def query(y):
  _y = y + 1
  ans = 0
  while _y > 0:
    ans += BIT[_y - 1]
    _y -= _y & -_y
  return ans
def update(y):
  _y = y + 1
  while _y <= N:
    BIT[_y - 1] += 1
    _y += _y & -_y
  return 0
for i, y in enumerate(Y):
  D[y][0] = query(y)
  D[y][1] = i - D[y][0]
  update(y)
BIT = [0] * N
for i, y in enumerate(Y[::-1]):
  D[y][2] = query(y)
  D[y][3] = i - D[y][2]
  update(y)
cnt = 0
for i in range(N):
  a, b, c, d = [(2 ** d - 1) % p for d in D[i]]
  cnt += 2 ** (N - 1)
  cnt += a * d * (b + c + 1)
  cnt += b * c * (a + d + 1)
  cnt += a * b * c * d
  cnt %= p
print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
p = 998244353
S = [[0, 0] for i in range(N)]
for i in range(N):
  S[i] = [int(x) for x in input().split()]
S.sort()
Y = sorted([i for i in range(N)], key=lambda y:S[y][1])
D = [[0, 0, 0, 0] for i in range(N)]
BIT = [0] * N
def query(y):
  _y = y + 1
  ans = 0
  while _y > 0:
    ans += BIT[_y - 1]
    _y -= _y & -_y
  return ans
def update(y):
  _y = y + 1
  while _y <= N:
    BIT[_y - 1] += 1
    _y += _y & -_y
  return 0
for i, y in enumerate(Y):
  D[y][0] = query(y)
  D[y][1] = i - D[y][0]
  update(y)
BIT = [0] * N
for i, y in enumerate(Y[::-1]):
  D[y][2] = query(y)
  D[y][3] = i - D[y][2]
  update(y)
cnt = 0
for i in range(N):
  a, b, c, d = [2 ** d - 1 for d in D[i]]
  cnt += 2 ** (N - 1)
  cnt += a * d * (b + c + 1)
  cnt += b * c * (a + d + 1)
  cnt += a * b * c * d
  cnt %= p
print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
class segTree:
  inf = 0
  def __init__(self, num):
    self.N = 1
    while self.N < num:
      self.N *= 2
    self.T = [self.inf] * (2 * self.N - 1)
  def do(self, l, r):
    #合計
    return l + r
  def set(self, L):
    for i in range(len(L)):
      self.update(i, L[i])
  def update(self, x):
    k = x + self.N - 1
    self.T[k] += 1
    while k > 0:
      k = (k - 1) // 2
      self.T[k] += 1
  def getV(self, l, r):
    return self.getVs(l, r, 0, 0, self.N - 1)
  def getVs(self, l, r, k, kl, kr):
    if l <= kl and r >= kr:
      return self.T[k]
    t = (kl + kr) // 2
    if l > t:
      return self.getVs(l, r, 2 * k + 2, t + 1,kr)
    if r < t + 1:
      return self.getVs(l, r , 2 * k + 1, kl, t)
    return self.do(self.getVs(l, r , 2 * k + 1, kl,t),
              self.getVs(l, r , 2 * k + 2, t + 1, kr))


N = int(input())
xy = [list(map(int, input().split())) for i in range(N)]
MOD = 998244353

xy.sort(key = lambda x:x[1])
R = {}
for i in range(N):
  R[xy[i][1]] = i
xy.sort()

LU = [0] * N
LD = [0] * N
RU = [0] * N
RD = [0] * N

Sl = segTree(N)
for i in range(N):
  t = Sl.getV(0, R[xy[i][1]])
  LU[i] = i - t
  LD[i] = t
  Sl.update(R[xy[i][1]])

Sr = segTree(N)
for i in range(N-1 , -1 , -1):
  t = Sr.getV(0, R[xy[i][1]])
  RU[i] = (N - 1 - i) - t
  RD[i] = t
  Sr.update(R[xy[i][1]])

ans = 0

PL = [1]
for i in range(N + 1):
  PL.append(PL[-1] * 2 % MOD)

def na(x):
  return PL[x] - 1
for i in range(N):
  t = PL[N - 1]
  t = (t + na(LU[i]) * na(RD[i])) % MOD
  t = (t + na(LD[i]) * na(RU[i])) % MOD
  t = (t + na(LU[i]) * na(LD[i]) * na(RU[i])) % MOD
  t = (t + na(LU[i]) * na(LD[i]) * na(RD[i])) % MOD
  t = (t + na(LU[i]) * na(RD[i]) * na(RU[i])) % MOD
  t = (t + na(LD[i]) * na(RD[i]) * na(RU[i])) % MOD
  t = (t + na(LU[i]) * na(LD[i]) * na(RD[i]) * na(RU[i])) % MOD
  ans = (ans + t) % MOD

print(ans)

Submission status: Time Limit Exceeded

Solution code:
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)

    def __iter__(self):
        psum = 0
        for i in range(self.size):
            csum = self.sum(i+1)
            yield csum - psum
            psum = csum
        raise StopIteration()

    def __str__(self):  # O(nlogn)
        return str(list(self))

    def sum(self, i):
        # [0, i) の要素の総和を返す
        if not (0 <= i <= self.size): raise ValueError("error!")
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        if not (0 <= i < self.size): raise ValueError("error!")
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    def __getitem__(self, key):
        if not (0 <= key < self.size): raise IndexError("error!")
        return self.sum(key+1) - self.sum(key)

    def __setitem__(self, key, value):
        # 足し算と引き算にはaddを使うべき
        if not (0 <= key < self.size): raise IndexError("error!")
        self.add(key, value - self[key])

from operator import itemgetter
import sys
input = sys.stdin.readline
mod = 998244353
N = int(input())
XY = [list(map(int, input().split())) for _ in range(N)]
XY.sort(key=itemgetter(1))
for i in range(N):
    XY[i][1] = i
XY.sort(key=itemgetter(0))
bitL = Bit(N)
bitR = Bit(N)
L = []  # 左下
R = [0]*N  # 右下
for _, y in XY:
    L.append(bitL.sum(y))
    bitL.add(y, 1)
for i, (_, y) in zip(range(N-1,-1,-1), XY[::-1]):
    R[i] = bitR.sum(y)
    bitR.add(y, 1)
ans = N * pow(2, N-1, mod) % mod
#print(ans)
for i, (ld, rd) in enumerate(zip(L, R)):
    lu = i - ld
    ru = N - 1 - i - rd
    #print(lu, ld, ru, rd)
    p_lu, p_ld, p_ru, p_rd = pow(2,lu,mod), pow(2,ld,mod), pow(2,ru,mod), pow(2,rd,mod)
    d = p_ru*p_ld*(p_lu-1)*(p_rd-1)\
           + p_lu*p_rd*(p_ru-1)*(p_ld-1)\
           - (p_lu-1)*(p_rd-1)*(p_ru-1)*(p_ld-1)
    #print(d)
    ans = (ans + d) % mod
#print(L)
#print(R)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


MOD = 998244353
n = int(input())
xys = []
ys = []
for line in sys.stdin:
    x, y = map(int, line.split())
    xys.append((x, y))
    ys.append(y)
ys.sort()
y_dict = {y: i + 1 for i, y in enumerate(ys)}
xys = [(x, y_dict[y]) for x, y in xys]
xys.sort()
bit = Bit(n)

p2d = [1]
for i in range(n):
    p2d.append(p2d[-1] * 2 % MOD)

ans = 0
for i, (x, y) in enumerate(xys):
    a = bit.sum(y)
    b = i - a
    c = y - a - 1
    d = (n - i - 1) - c
    bit.add(y, 1)
    # print(i, x, y, a, b, c, d)
    pa, pb, pc, pd = p2d[a], p2d[b], p2d[c], p2d[d]
    tmp = p2d[n - 1]
    tmp += (pa - 1) * (pd - 1) * (pb + pc - 1) % MOD
    tmp += (pb - 1) * (pc - 1) % MOD * pa * pd % MOD
    ans = (ans + tmp) % MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)

    def __iter__(self):
        psum = 0
        for i in range(self.size):
            csum = self.sum(i+1)
            yield csum - psum
            psum = csum
        raise StopIteration()

    def __str__(self):  # O(nlogn)
        return str(list(self))

    def sum(self, i):
        # [0, i) の要素の総和を返す
        if not (0 <= i <= self.size): raise ValueError("error!")
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        if not (0 <= i < self.size): raise ValueError("error!")
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    def __getitem__(self, key):
        if not (0 <= key < self.size): raise IndexError("error!")
        return self.sum(key+1) - self.sum(key)

    def __setitem__(self, key, value):
        # 足し算と引き算にはaddを使うべき
        if not (0 <= key < self.size): raise IndexError("error!")
        self.add(key, value - self[key])

from operator import itemgetter
mod = 998244353
N = int(input())
XY = [list(map(int, input().split())) for _ in range(N)]
XY.sort(key=itemgetter(1))
for i in range(N):
    XY[i][1] = i
XY.sort(key=itemgetter(0))
bitL = Bit(N)
bitR = Bit(N)
L = []  # 左下
R = [0]*N  # 右下
for _, y in XY:
    L.append(bitL.sum(y))
    bitL.add(y, 1)
for i, (_, y) in zip(range(N-1,-1,-1), XY[::-1]):
    R[i] = bitR.sum(y)
    bitR.add(y, 1)
ans = N * pow(2, N-1, mod) % mod
#print(ans)
for i, (ld, rd) in enumerate(zip(L, R)):
    lu = i - ld
    ru = N - 1 - i - rd
    #print(lu, ld, ru, rd)
    d = pow(2,ru,mod)*pow(2,ld,mod)*(pow(2,lu,mod)-1)*(pow(2,rd,mod)-1)\
           + pow(2,lu,mod)*pow(2,rd,mod)*(pow(2,ru,mod)-1)*(pow(2,ld,mod)-1)\
           - (pow(2,lu,mod)-1)*(pow(2,rd,mod)-1)*(pow(2,ru,mod)-1)*(pow(2,ld,mod)-1)
    #print(d)
    ans = (ans + d) % mod
#print(L)
#print(R)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


MOD = 998244353
n = int(input())
xys = []
ys = []
for line in sys.stdin:
    x, y = map(int, line.split())
    xys.append((x, y))
    ys.append(y)
ys.sort()
y_dict = {y: i + 1 for i, y in enumerate(ys)}
xys = [(x, y_dict[y]) for x, y in xys]
xys.sort()
bit = Bit(n)

p2d = [1]
for i in range(n):
    p2d.append(p2d[-1] * 2 % MOD)

ans = 0
for i, (x, y) in enumerate(xys):
    a = bit.sum(y)
    b = i - a
    c = y - a - 1
    d = (n - i - 1) - c
    bit.add(y, 1)
    # print(i, x, y, a, b, c, d)
    pa, pb, pc, pd = p2d[a], p2d[b], p2d[c], p2d[d]
    tmp = p2d[n - 1]
    tmp += (pa - 1) * (pd - 1) % MOD * pb * pc % MOD
    tmp += (pb - 1) * (pc - 1) % MOD * pa * pd % MOD
    tmp -= (pa - 1) * (pb - 1) % MOD * (pc - 1) * (pd - 1) % MOD
    ans = (ans + tmp) % MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


MOD = 998244353
n = int(input())
xys = []
ys = []
for line in sys.stdin:
    x, y = map(int, line.split())
    xys.append((x, y))
    ys.append(y)
ys.sort()
y_dict = {y: i + 1 for i, y in enumerate(ys)}
xys = [(x, y_dict[y]) for x, y in xys]
xys.sort()
bit = Bit(n)

p2d = [1]
for i in range(n):
    p2d.append(p2d[-1] * 2 % MOD)

ans = 0
for i, (x, y) in enumerate(xys):
    a = bit.sum(y)
    b = i - a
    c = y - a - 1
    d = (n - i - 1) - c
    bit.add(y, 1)
    # print(i, x, y, a, b, c, d)
    pa, pb, pc, pd = p2d[a], p2d[b], p2d[c], p2d[d]
    tmp = p2d[n - 1]
    tmp += (pa - 1) * (pd - 1) * pb * pc % MOD
    tmp += (pb - 1) * (pc - 1) * pa * pd % MOD
    tmp -= (pa - 1) * (pb - 1) * (pc - 1) * (pd - 1) % MOD
    ans = (ans + tmp) % MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


MOD = 998244353
n = int(input())
xys = []
ys = []
for line in sys.stdin:
    x, y = map(int, line.split())
    xys.append((x, y))
    ys.append(y)
ys.sort()
y_dict = {y: i + 1 for i, y in enumerate(ys)}
xys = [(x, y_dict[y]) for x, y in xys]
xys.sort()
bit = Bit(n)

p2d = [1]
for i in range(n):
    p2d.append(p2d[-1] * 2 % MOD)

ans = 0
for i, (x, y) in enumerate(xys):
    a = bit.sum(y)
    b = i - a
    c = y - a - 1
    d = (n - i - 1) - c
    bit.add(y, 1)
    # print(i, x, y, a, b, c, d)
    pa, pb, pc, pd = p2d[a], p2d[b], p2d[c], p2d[d]
    tmp = p2d[n - 1]
    tmp += (pa - 1) * (pd - 1) * pb * pc
    tmp += (pb - 1) * (pc - 1) * pa * pd
    tmp -= (pa - 1) * (pb - 1) * (pc - 1) * (pd - 1)
    ans = (ans + tmp) % MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


MOD = 998244353
n = int(input())
xys = []
ys = []
for line in sys.stdin:
    x, y = map(int, line.split())
    xys.append((x, y))
    ys.append(y)
ys.sort()
y_dict = {y: i + 1 for i, y in enumerate(ys)}
xys = [(x, y_dict[y]) for x, y in xys]
xys.sort()
f_bit = Bit(n)
b_bit = Bit(n)
for i in range(1, n + 1):
    b_bit.add(i, 1)

p2d = [1]
for i in range(n):
    p2d.append(p2d[-1] * 2 % MOD)

ans = 0
for i, (x, y) in enumerate(xys):
    b_bit.add(y, -1)
    a = f_bit.sum(y)
    b = i - a
    c = b_bit.sum(y)
    d = (n - i - 1) - c
    f_bit.add(y, 1)
    # print(i, x, y, a, b, c, d)
    tmp = p2d[n - 1]
    tmp += (p2d[a] - 1) * (p2d[d] - 1) * p2d[b] * p2d[c]
    tmp += (p2d[b] - 1) * (p2d[c] - 1) * p2d[a] * p2d[d]
    tmp -= (p2d[a] - 1) * (p2d[b] - 1) * (p2d[c] - 1) * (p2d[d] - 1)
    ans = (ans + tmp) % MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main(n, inp):
    idx = [0] * n
    total = 0
    while True:
        add1(idx)
        x = [inp[i][0] for i in range(n) if idx[i] == 1]
        y = [inp[i][1] for i in range(n) if idx[i] == 1]
        a = min(x)
        b = max(x)
        c = min(y)
        d = max(y)
        # print(a, b, c, d)
        count = 0
        for i in range(n):
            xx = inp[i][0]
            yy = inp[i][1]
            if (a <= xx <= b) and (c <= yy <= d):
                count += 1
        total += (count % 998244353)
        if all([i == 1 for i in idx]):
            break
    return total


def add1(idx):
    for i in range(len(idx)):
        if idx[i] == 0:
            idx[i] = 1
            return
        idx[i] = 0


if __name__ == '__main__':
    n = int(input())
    inp = []
    for i in range(n):
        inp.append(list(map(int, input().split())))
    print(main(n, inp))

Submission status: Time Limit Exceeded

Solution code:
import sys
from operator import itemgetter

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()

co = []
for i in range(n):
    co.append(na())

co.sort(key=itemgetter(1))
for i in range(n):
    co[i][1] = i

# print(co)
ft = [0] * (n + 5)

co.sort(key=itemgetter(0), reverse=True)

ups = [0] * n
downs = [0] * n

def addfenwick(ft, i, v):
    if v == 0 or i < 0: return
    i += 1
    while i < len(ft):
        ft[i] += v
        i += i&-i

def sumfenwick(ft, i):
    s = 0
    i += 1
    while i > 0:
        s += ft[i]
        i -= i&-i
    return s

for i in range(n):
    c = co[i]
    downs[n-1-i] = sumfenwick(ft, c[1])
    ups[n-1-i] = i - downs[n-1-i]
    addfenwick(ft, c[1], 1)

ft = [0] * (n+5)
co = co[::-1]

ans = 0
mod = 998244353
for i in range(n):
    c = co[i]
    d = sumfenwick(ft, c[1])
    u = i - d

    # print(d, u, ups[i], downs[i])
    ans += (pow(2, d, mod)-1) * (pow(2, ups[i], mod)-1) * pow(2, u+downs[i], mod)
    ans += (pow(2, u, mod)-1) * (pow(2, downs[i], mod)-1) * pow(2, d+ups[i], mod)
    ans -= (pow(2, d, mod)-1) * (pow(2, ups[i], mod)-1) * (pow(2, downs[i], mod)-1) * (pow(2, u, mod)-1)
    ans += pow(2, n-1, mod)
    addfenwick(ft, c[1], 1)

ans %= mod
if ans < 0:
    ans += mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 998244353

N = int(input())
XY = [[int(x) for x in input().split()] for _ in range(N)]
XY.sort() # X ascending
X,Y = zip(*XY)
Y_rank = {x:i for i,x in enumerate(sorted(Y),1)}

def BIT_update(tree,x):
    while x <= N:
        tree[x] += 1
        x += x & (-x)

def BIT_sum(tree,x):
    ret = 0
    while x:
        ret += tree[x]
        x -= x & (-x)
    return ret

pow2 = [1]
for _ in range(N):
    pow2.append(pow2[-1] * 2 % MOD)

answer = 0
tree = [0] * (N+1)
for L,(x,y) in enumerate(XY):
    y = Y_rank[y]
    R = N - 1 - L
    D = y - 1
    U = N - 1 - D
    LD = BIT_sum(tree,y)
    BIT_update(tree,y)
    LU = L - LD
    RD = D - LD
    RU = R - RD
    # bounding box が自分自身を含むような集合を数える
    # 全体から引く
    cnt = pow2[N]
    cnt -= pow2[L]
    cnt -= pow2[R]
    cnt -= pow2[D]
    cnt -= pow2[U]
    cnt += pow2[LD]
    cnt += pow2[LU]
    cnt += pow2[RD]
    cnt += pow2[RU]
    cnt -= 1
    answer += cnt

answer %= MOD

print(answer)

Submission status: Accepted

Solution code:
import sys
S=sys.stdin.readlines()
def Binit(B,siz):
  while len(B)<siz+1:
    B.append(0)
  while len(B)>siz+1:
    del B[-1]
  for i in range(siz+1):
    B[i]=0
  B.append(siz)

def Badd(B,a,x):
  z=a
  while z<=B[-1]:
    B[z]+=x
    z+=(z&(-z))

def Bsum(B,a):
  r=0
  z=a
  while z>0:
    r+=B[z]
    z-=(z&(-z))
  return r

def Bssum(B,a,b):
  return Bsum(B,max(a,b))-Bsum(B,min(a,b)-1)

N=int(S[0])
X=[list(map(int,S[i+1].split())) for i in range(N)]
X.sort()
for i in range(N):
  X[i][0]=i
D=dict()
Y=sorted([X[i][1] for i in range(N)])
for i in range(N):
  D[Y[i]]=i
for i in range(N):
  X[i][1]=D[X[i][1]]
B1,B2=[],[]
Binit(B1,N+1)
Binit(B2,N+1)
for i in range(N):
  Badd(B2,i+1,1)
mod=998244353
P=N*pow(2,N-1,mod)%mod
Z=[[1,1,1,1],[1,1,1,0],[1,1,0,1],[1,0,1,1],[1,0,0,1],[0,1,1,1],[0,1,1,0]]
A=0
for i in range(N):
  Badd(B2,X[i][1]+1,-1)
  V=[Bsum(B1,X[i][1]+1),Bsum(B2,X[i][1]+1)]
  W=[pow(2,V[0],mod)-1,pow(2,i-V[0],mod)-1,pow(2,V[1],mod)-1,pow(2,N-i-1-V[1],mod)-1]
  for j in range(7):
    A=1
    if Z[j][0]:
      A=A*W[0]%mod
    if Z[j][1]:
      A=A*W[1]%mod
    if Z[j][2]:
      A=A*W[2]%mod
    if Z[j][3]:
      A=A*W[3]%mod
    P=(P+A)%mod
  Badd(B1,X[i][1]+1,1)
print(P)