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:
# Enclosed Points
import bisect
from collections import defaultdict


class SegTree():
    # 1-indexed
    def __init__(self, N, function, basement):
        self.n = N
        self.K = (self.n-1).bit_length()
        self.f = function
        self.b = basement
        self.seg = [basement]*(2**(self.K+1)+1)
        X = 2**self.K

    def update(self, k, value):
        X = 2**self.K
        k += X
        self.seg[k] += value
        while k:
            k = k >> 1
            self.seg[k] = self.f(self.seg[k << 1], self.seg[(k << 1) | 1])

    def query(self, L, R):
        num = 2**self.K
        L += num
        R += num
        vL = self.b
        vR = self.b
        while L < R:
            if L & 1:
                vL = self.f(vL, self.seg[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.f(self.seg[R], vR)
            L >>= 1
            R >>= 1
        return self.f(vL, vR)


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


def Press(lists):
    lists.sort()
    d = defaultdict(int)
    now = -float("inf")
    index = 0
    for i in range(len(lists)):
        if lists[i] > now:
            index += 1
            d[lists[i]] = index
            now = lists[i]
        elif lists[i] == now:
            d[lists[i]] = index
    return d


mod = 998244353
N = int(input())
Point = []
for i in range(N):
    x, y = map(int, input().split())
    Point.append((x, y, i))


def modifie():
    Point.sort(key=lambda x: (x[1], x[0]))
    X = []
    for x, y, i in Point:
        X.append(x)
    modified_point_x = Press(X)
    return modified_point_x


modified_point_x = modifie()
segtree = SegTree(N+1, aggfunc, 0)
below_below = defaultdict(int)

for i in range(N):
    x, y, index = Point[i]
    segtree.update(modified_point_x[x], 1)
    below_below[Point[i]] = segtree.query(0, modified_point_x[x]+1)

# print(below_below)

X, Y = [], []
for x, y, index in Point:
    X.append(x)
    Y.append(y)
X.sort()
Y.sort()

ans = 0

for i in range(N):
    x, y, index = Point[i]
    ZZ = below_below[Point[i]]-1
    XX = bisect.bisect_right(X, x)-ZZ-1
    WW = bisect.bisect_right(Y, y)-ZZ-1
    YY = N-1-XX-ZZ-WW
    ss= pow(2, N, mod)-1-pow(2,XX,mod)-pow(2,YY,mod)-pow(2,ZZ,mod)-pow(2,WW,mod)+4-(pow(2,XX,mod)-1)*(pow(2,YY,mod)-1)-(pow(2,YY,mod)-1)*(pow(2,WW,mod)-1)-(pow(2,ZZ,mod)-1)*(pow(2,WW,mod)-1)-(pow(2,ZZ,mod)-1)*(pow(2,XX,mod)-1)
    ans+=ss
print(ans % mod)

Submission status: Time Limit Exceeded

Solution code:
I = [int(_) for _ in open(0).read().split()]
N, X, Y = I[0], I[1::2], I[2::2]
mod = 998244353


def compress_coord(raw):
    i_v = {}
    v_i = {}
    for i, v in enumerate(sorted(set(raw))):
        i_v[i] = v
        v_i[v] = i
    return v_i, i_v


class BinaryIndexedTree():
    def __init__(self, n):
        """
        constructs binary indexed tree

        Parameters
        ----------
        n : int
            maximum index value
        """
        self.n = n
        self.dat = [0] * (n + 1)

    def sum(self, i):
        """
        returns the sum of [1, i]

        Parameters
        ----------
        i : int
            right end of the interval
        """
        s = 0
        while i:
            s += self.dat[i]
            i -= i & -i
        return s

    def add(self, i, x):
        """
        adds x to i-th element

        Parameters
        ----------
        i : int
            index
        x : int
            additional value
        """
        while i <= self.n:
            self.dat[i] += x
            i += i & -i


xi, _ = compress_coord(X)
yj, _ = compress_coord(Y)
X = [xi[x] for x in X]
Y = [yj[y] for y in Y]
LL = {}
LR = {}
RL = {}
RR = {}
sortXY = sorted(zip(X, Y))
bit = BinaryIndexedTree(N + 10)
for x, y in sortXY:
    LL[(x, y)] = bit.sum(y + 1)
    LR[(x, y)] = bit.sum(N + 1) - bit.sum(y + 1)
    bit.add(y + 1, 1)
bit = BinaryIndexedTree(N + 10)
for x, y in sortXY[::-1]:
    RL[(x, y)] = bit.sum(y + 1)
    RR[(x, y)] = bit.sum(N + 1) - bit.sum(y + 1)
    bit.add(y + 1, 1)
ans = N * (pow(2, N, mod) - 1) % mod
for x, y in zip(X, Y):
    ll = LL[(x, y)]
    lr = LR[(x, y)]
    rl = RL[(x, y)]
    rr = RR[(x, y)]
    a = pow(2, ll + lr, mod) - 1
    b = pow(2, rl + rr, mod) - 1
    c = pow(2, ll + rl, mod) - 1
    d = pow(2, lr + rr, mod) - 1
    e = pow(2, ll, mod) - 1
    f = pow(2, lr, mod) - 1
    g = pow(2, rl, mod) - 1
    h = pow(2, rr, mod) - 1
    ans -= a + b + c + d
    ans += e + f + g + h
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
I = [int(_) for _ in open(0).read().split()]
N, X, Y = I[0], I[1::2], I[2::2]
mod = 998244353


class SegmentTree():
    def __init__(self, array, f, ti):
        """
        Parameters
        ----------
        array : list
            to construct segment tree from
        f : func
            binary operation of the monoid
        ti : 
            identity element of the monoid
        """
        self.f = f
        self.ti = ti
        self.n = n = 2**(len(array).bit_length())
        self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
        for i in range(n - 1, 0, -1):  # build
            dat[i] = f(dat[i << 1], dat[i << 1 | 1])

    def update(self, p, v):  # set value at position p (0-indexed)
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = v
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def query(self, l, r):  # result on interval [l, r) (0-indexed)
        f, ti, n, dat = self.f, self.ti, self.n, self.dat
        vl = vr = ti
        l += n
        r += n
        while l < r:
            if l & 1:
                vl = f(vl, dat[l])
                l += 1
            if r & 1:
                r -= 1
                vr = f(dat[r], vr)
            l >>= 1
            r >>= 1
        return f(vl, vr)


def compress_coord(raw):
    i_v = {}
    v_i = {}
    for i, v in enumerate(sorted(set(raw))):
        i_v[i] = v
        v_i[v] = i
    return v_i, i_v


xi, _ = compress_coord(X)
yj, _ = compress_coord(Y)
X = [xi[x] for x in X]
Y = [yj[y] for y in Y]
LL = {}
LR = {}
RL = {}
RR = {}
sty = SegmentTree([0] * N, lambda x, y: x + y, 0)
sortXY = sorted(zip(X, Y))
for x, y in sortXY:
    LL[(x, y)] = sty.query(0, y)
    LR[(x, y)] = sty.query(y, N)
    sty.update(y, 1)
sty = SegmentTree([0] * N, lambda x, y: x + y, 0)
for x, y in sortXY[::-1]:
    RL[(x, y)] = sty.query(0, y)
    RR[(x, y)] = sty.query(y, N)
    sty.update(y, 1)
ans = N * (pow(2, N, mod) - 1) % mod
for x, y in zip(X, Y):
    ll = LL[(x, y)]
    lr = LR[(x, y)]
    rl = RL[(x, y)]
    rr = RR[(x, y)]
    a = pow(2, ll + lr, mod) - 1
    b = pow(2, rl + rr, mod) - 1
    c = pow(2, ll + rl, mod) - 1
    d = pow(2, lr + rr, mod) - 1
    e = pow(2, ll, mod) - 1
    f = pow(2, lr, mod) - 1
    g = pow(2, rl, mod) - 1
    h = pow(2, rr, mod) - 1
    ans -= a + b + c + d
    ans += e + f + g + h
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
XY = [tuple(map(int,input().split())) for i in range(N)]
XY.sort(key=lambda x:x[0])

ys = []
for _,y in XY:
    ys.append(y)
ys.sort()
yd = {y:i for i,y in enumerate(ys)}

class BinaryIndexedTree:
    def __init__(self,size):
        self.N = size
        self.bit = [0]*(size+1)
    def add(self,x,w): # 0-indexed
        x += 1
        while x <= self.N:
            self.bit[x] += w
            x += (x & -x)
    def _sum(self,x): # 1-indexed
        ret = 0
        while x > 0:
            ret += self.bit[x]
            x -= (x & -x)
        return ret
    def sum(self,l,r): # [l,r)
        return self._sum(r) - self._sum(l)
    def __str__(self): # for debug
        arr = [self.sum(i,i+1) for i in range(self.N)]
        return str(arr)

lbit = BinaryIndexedTree(N)
lu = []
ld = []
for i,(_,y) in enumerate(XY):
    d = lbit._sum(yd[y])
    u = i - d
    lu.append(u)
    ld.append(d)
    lbit.add(yd[y],1)

rbit = BinaryIndexedTree(N)
ru = []
rd = []
for i,(_,y) in enumerate(XY[::-1]):
    d = rbit._sum(yd[y])
    u = i - d
    ru.append(u)
    rd.append(d)
    rbit.add(yd[y],1)

MOD = 998244353
ans = 0
for a,b,c,d in zip(ld,lu,rd[::-1],ru[::-1]):
    pa,pb,pc,pd = pow(2,a,MOD),pow(2,b,MOD),pow(2,c,MOD),pow(2,d,MOD)
    ans += (pa-1) * (pd-1) * pb * pc
    ans += (pb-1) * (pc-1) * pa * pd
    ans -= (pa-1) * (pb-1) * (pc-1) * (pd-1)
    ans %= MOD
ans += N * pow(2,N-1,MOD)
ans %= MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
mod = 998244353
a = []
ys = []
for _ in range(N):
  x, y = map(int, input().split())
  a.append((x, y))
  ys.append(y)

def compress(init_val):
  t = sorted(set(init_val))
  d = dict([[t[i], i] for i in range(len(t))])
  res = [0] * len(init_val)
  for i in range(len(init_val)): res[i] = d[init_val[i]]
  return res

ys = compress(ys)
a = [(a[i][0], ys[i] + 1) for i in range(N)]
a.sort()

class BIT:
  def __init__(self, n):
    self.n = n
    self.data = [0] * (n + 1)
    self.el = [0] * (n + 1)
  def sum(self, i):
    s = 0
    while i > 0:
      s += self.data[i]
      i -= i & -i
    return s
  def add(self, i, x):
    self.el[i] += x
    while i <= self.n:
      self.data[i] += x
      i += i & -i
  def get(self, i, j = None):
    if j is None:
      return self.el[i]
    return self.sum(j) - self.sum(i)
  def lowerbound(self, s):
    x = 0
    y = 0
    for i in range(self.n.bit_length(), -1, -1):
      k = x + (1 << i)
      if k <= self.n and (y + self.data[k] < s):
        y += self.data[k]
        x += 1 << i
    return x + 1

fwkl = BIT(N)
fwkr = BIT(N)
ls = [0] * N
rs = [0] * N
for i in range(N):
  ls[i] = fwkl.sum(a[i][1])
  fwkl.add(a[i][1], 1)
for i in range(N - 1, -1, -1):
  rs[i] = fwkr.sum(a[i][1])
  fwkr.add(a[i][1], 1)

pw = lambda x: pow(2, x, mod) - 1
#print(ls, rs)
res = 0
for i in range(N):
  ld, rd = ls[i], rs[i]
  lu = i - ld
  ru = N - i - rd - 1
  for k in range(16):
    d = [0] * 4
    t = 1
    if k & 1:
      t *= pw(ld)
      t %= mod
      d[0] = 1
      d[2] = 1
    if k & 2:
      t *= pw(lu)
      t %= mod
      d[0] = 1
      d[3] = 1
    if k & 4:
      t *= pw(rd)
      t %= mod
      d[1] = 1
      d[2] = 1
    if k & 8:
      t *= pw(ru)
      t %= mod
      d[1] = 1
      d[3] = 1
    res += t * (2 ** (sum(d) == 4))
    res %= mod
print(res)

Submission status: Time Limit Exceeded

Solution code:
# ref: https://atcoder.jp/contests/abc136/submissions/6696160
# ref: https://atcoder.jp/contests/abc136/submissions/6708692

n = int(input())
XY = sorted(list(map(int, input().split())) for _ in range(n))
X, Y = zip(*XY)
Y_rank = {z: i for i, z in enumerate(sorted(Y), 1)}

mod = 998244353


class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)

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

    def add(self, i, j):
        while i <= self.n:
            self.data[i] += j
            i += i & -i


po2 = [1]
for i in range(200200):
    po2.append(po2[-1] * 2 % mod)

b = BIT(n)

res = 0
for L, (x, y) in enumerate(XY):
    y = Y_rank[y]
    A = b.sum(y)
    B = L - A
    D = y - 1 - A
    C = n - 1 - A - B - D
    res += po2[n] - (po2[A+B] + po2[B+C] + po2[C+D] + po2[D+A]) + (po2[A] + po2[B] + po2[C] + po2[D]) - 1
    res %= mod
    b.add(y, 1)
print(res)

Submission status: Time Limit Exceeded

Solution code:
def main():
    mod = 998244353
    n = int(input())
    xy = [list(map(int, input().split())) for _ in [0]*n]
    ans = 0
    pow_2 = [1]
    j = 1
    for i in range(n):
        j = j*2 % mod
        pow_2.append(j)
    p2 = pow_2[n-1]

    xy.sort(key=lambda x: x[1])
    xy = [(x, i+1) for i, (x, y) in enumerate(xy)]
    xy.sort()

    class BIT():
        def __init__(self, n):
            self.n = n
            self.maxbit = 2**(len(bin(self.n))-3)
            self.bit = [0]*(self.n+1)
            self.allsum = 0

        # 全ての要素に1を足す
        def all_plus(self):
            self.bit = [0]+[i & (-i) for i in range(1, self.n+1)]
            self.allsum = self.n

        # 要素iにvを追加する
        def add(self, i, v):
            x = i
            self.allsum += v
            while x < self.n + 1:
                self.bit[x] += v
                x += x & (-x)

        # 位置0からiまでの和(sum(bit[:i]))を計算する
        def sum(self, i):
            ret = 0
            x = i
            while x > 0:
                ret += self.bit[x]
                x -= x & (-x)
            return ret

    bit_left = BIT(n)
    bit_right = BIT(n)
    bit_right.all_plus()

    for i, (x, y) in enumerate(xy):
        bit_right.add(y, -1)
        A = bit_left.sum(y)
        B = i-A
        C = bit_right.sum(y)
        D = n-i-1-C
        bit_left.add(y, 1)
        ans += (pow_2[A]-1)*(pow_2[D]-1)*(pow_2[B+C])
        ans += (pow_2[B]-1)*(pow_2[C]-1)*(pow_2[A+D])
        ans -= (pow_2[B]-1)*(pow_2[C]-1)*(pow_2[A]-1)*(pow_2[D]-1)
        ans += p2
        ans = ans % mod
    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    mod = 998244353
    n = int(input())
    xy = [list(map(int, input().split())) for _ in [0]*n]
    ans = 0
    pow_2 = [1]
    j = 1
    for i in range(n):
        j = j*2 % mod
        pow_2.append(j)
    p2 = pow_2[n-1]
    xy.sort()

    class BIT():
        def __init__(self, member):
            self.member_list = sorted(member)
            self.member_dict = {v: i+1 for i, v in enumerate(self.member_list)}
            self.n = len(member)
            self.maxmember = self.member_list[-1]
            self.minmember = self.member_list[0]
            self.maxbit = 2**(len(bin(self.n))-3)
            self.bit = [0]*(self.n+1)
            self.allsum = 0

        # 全ての要素に1を足す
        def all_plus(self):
            self.bit = [0]+[i & (-i) for i in range(1, self.n+1)]
            self.allsum = self.n

        # 要素iにvを追加する
        def add(self, i, v):
            x = self.member_dict[i]
            self.allsum += v
            while x < self.n + 1:
                self.bit[x] += v
                x += x & (-x)

        # 位置0からiまでの和(sum(bit[:i]))を計算する
        def sum(self, i):
            ret = 0
            x = self.member_dict[i]
            while x > 0:
                ret += self.bit[x]
                x -= x & (-x)
            return ret

    member = [y for x, y in xy]
    bit_left = BIT(member)
    bit_right = BIT(member)
    bit_right.all_plus()

    for x, y in xy:
        bit_right.add(y, -1)
        A = bit_left.sum(y)
        B = bit_left.allsum-bit_left.sum(y)
        C = bit_right.sum(y)
        D = bit_right.allsum-bit_right.sum(y)
        bit_left.add(y, 1)
        ans += (pow_2[A]-1)*(pow_2[D]-1)*(pow_2[B+C])
        ans += (pow_2[B]-1)*(pow_2[C]-1)*(pow_2[A+D])
        ans -= (pow_2[B]-1)*(pow_2[C]-1)*(pow_2[A]-1)*(pow_2[D]-1)
        ans += p2
        ans = ans % mod
    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
import sys; input = sys.stdin.buffer.readline
from collections import defaultdict
con = 998244353; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

class SegmentTree(object):
	def __init__(self, N):
		self.N0 = 2 ** (N - 1).bit_length()
		self.data = [0] * (2 * self.N0)

	def update(self, k, x):
		k += self.N0 - 1
		self.data[k] = x
		while k > 0:
			k = (k - 1) // 2
			self.data[k] = self.data[2 * k + 1] + self.data[2 * k + 2]

	def query(self, l, r):
		L = l + self.N0; R = r + self.N0 + 1
		m = 0
		while L < R:
			if R & 1:
				R -= 1
				m += self.data[R - 1]
			if L & 1:
				m += self.data[L - 1]
				L += 1
			L >>= 1; R >>= 1

		return m

#処理内容
def main():
	N = int(input())
	XY = [None] * N
	X = [None] * N
	Y = [None] * N
	for i in range(N):
		x, y = getlist()
		XY[i] = [x, y]
		X[i] = x; Y[i] = y

	#座圧
	X.sort(); Y.sort()
	Dx = defaultdict(int)
	Dy = defaultdict(int)
	for i in range(N):
		Dx[X[i]] = i
		Dy[Y[i]] = i

	for i in range(N):
		XY[i] = Dx[XY[i][0]], Dy[XY[i][1]]

	XY.sort()
	YY = [XY[i][1] for i in range(N)]
	# print(YY)
	# print(XY)
	
	Segleft = SegmentTree(N)
	Segright = SegmentTree(N)

	#関係なく決まっている値
	ans = pow(2, N - 1, con) * N
	ans %= con

	yLU = [0] * N
	yLD = [0] * N
	yRU = [0] * N
	yRD = [0] * N

	#左初期化
	Segleft.update(YY[0], 1)

	for i in range(1, N - 1):
		y = YY[i]
		yLD[i] = Segleft.query(0, y)
		yLU[i] = Segleft.query(y, N - 1)
		Segleft.update(y, 1)

	#右初期化
	Segright.update(YY[N - 1], 1)

	for i in range(N - 2, 0, -1):
		y = YY[i]
		yRD[i] = Segright.query(0, y)
		yRU[i] = Segright.query(y, N - 1)
		Segright.update(y, 1)

	# print(yLD)
	# print(yLU)
	# print(yRD)
	# print(yRU)

	for i in range(1, N - 1):
		lu = yLU[i]; ru = yRU[i]
		ld = yLD[i]; rd = yRD[i]
		a = pow(2, lu, con)
		b = pow(2, ru, con)
		c = pow(2, ld, con)
		d = pow(2, rd, con)
		if rd != 0 and lu != 0:
			ans += (d - 1) * (a - 1) * b * c
		if ld != 0 and ru != 0:
			ans += (c - 1) * (b - 1) * a * d
		#包除
		if rd != 0 and lu != 0 and ld != 0 and ru != 0:
			ans -= (a - 1) * (b - 1) * (c - 1) * (d - 1)
		ans %= con

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
import sys; input = sys.stdin.buffer.readline
from collections import defaultdict
con = 998244353; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

class SegmentTree(object):
	def __init__(self, N):
		self.N0 = 2 ** (N - 1).bit_length()
		self.data = [0] * (2 * self.N0)

	def update(self, k, x):
		k += self.N0 - 1
		self.data[k] = x
		while k > 0:
			k = (k - 1) // 2
			self.data[k] = self.data[2 * k + 1] + self.data[2 * k + 2]

	def query(self, l, r):
		L = l + self.N0; R = r + self.N0 + 1
		m = 0
		while L < R:
			if R & 1:
				R -= 1
				m += self.data[R - 1]
			if L & 1:
				m += self.data[L - 1]
				L += 1
			L >>= 1; R >>= 1

		return m

#処理内容
def main():
	N = int(input())
	XY = [None] * N
	X = [None] * N
	Y = [None] * N
	for i in range(N):
		x, y = getlist()
		XY[i] = [x, y]
		X[i] = x; Y[i] = y

	#座圧
	X.sort(); Y.sort()
	Dx = defaultdict(int)
	Dy = defaultdict(int)
	for i in range(N):
		Dx[X[i]] = i
		Dy[Y[i]] = i

	for i in range(N):
		XY[i] = Dx[XY[i][0]], Dy[XY[i][1]]

	XY.sort()
	# print(XY)
	
	Segleft = SegmentTree(N)
	Segright = SegmentTree(N)

	#関係なく決まっている値
	ans = pow(2, N - 1, con) * N
	ans %= con

	yLU = [0] * N
	yLD = [0] * N
	yRU = [0] * N
	yRD = [0] * N

	#左初期化
	Segleft.update(XY[0][1], 1)

	for i in range(1, N - 1):
		y = XY[i][1]
		yLD[i] = Segleft.query(0, y)
		yLU[i] = Segleft.query(y, N - 1)
		Segleft.update(y, 1)

	#右初期化
	Segright.update(XY[N - 1][1], 1)

	for i in range(N - 2, 0, -1):
		y = XY[i][1]
		yRD[i] = Segright.query(0, y)
		yRU[i] = Segright.query(y, N - 1)
		Segright.update(y, 1)

	# print(yLD)
	# print(yLU)
	# print(yRD)
	# print(yRU)

	for i in range(1, N - 1):
		lu = yLU[i]; ru = yRU[i]
		ld = yLD[i]; rd = yRD[i]
		a = pow(2, lu, con)
		b = pow(2, ru, con)
		c = pow(2, ld, con)
		d = pow(2, rd, con)
		if rd != 0 and lu != 0:
			ans += (d - 1) * (a - 1) * b * c
		if ld != 0 and ru != 0:
			ans += (c - 1) * (b - 1) * a * d
		#包除
		if rd != 0 and lu != 0 and ld != 0 and ru != 0:
			ans -= (a - 1) * (b - 1) * (c - 1) * (d - 1)
		ans %= con

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
import sys; input = sys.stdin.buffer.readline
from collections import defaultdict
con = 998244353; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

class SegmentTree(object):
	def __init__(self, N):
		self.N0 = 2 ** (N - 1).bit_length()
		self.data = [0] * (2 * self.N0)

	def update(self, k, x):
		k += self.N0 - 1
		self.data[k] = x
		while k > 0:
			k = (k - 1) // 2
			self.data[k] = self.data[2 * k + 1] + self.data[2 * k + 2]

	def query(self, l, r):
		L = l + self.N0; R = r + self.N0 + 1
		m = 0
		while L < R:
			if R & 1:
				R -= 1
				m += self.data[R - 1]
			if L & 1:
				m += self.data[L - 1]
				L += 1
			L >>= 1; R >>= 1

		return m

#処理内容
def main():
	N = int(input())
	XY = [None] * N
	X = [None] * N
	Y = [None] * N
	for i in range(N):
		x, y = getlist()
		XY[i] = [x, y]
		X[i] = x; Y[i] = y

	#座圧
	XY.sort()
	X.sort(); Y.sort()
	Dx = defaultdict(int)
	Dy = defaultdict(int)
	for i in range(N):
		Dx[X[i]] = i
		Dy[Y[i]] = i

	for i in range(N):
		XY[i] = Dx[XY[i][0]], Dy[XY[i][1]]

	XY.sort()
	# print(XY)
	
	Segleft = SegmentTree(N)
	Segright = SegmentTree(N)

	#関係なく決まっている値
	ans = pow(2, N - 1, con) * N
	ans %= con

	yLU = [0] * N
	yLD = [0] * N
	yRU = [0] * N
	yRD = [0] * N

	#左初期化
	Segleft.update(XY[0][1], 1)

	for i in range(1, N - 1):
		y = XY[i][1]
		yLD[i] = Segleft.query(0, y)
		yLU[i] = Segleft.query(y, N - 1)
		Segleft.update(y, 1)

	#右初期化
	Segright.update(XY[N - 1][1], 1)

	for i in range(N - 2, 0, -1):
		y = XY[i][1]
		yRD[i] = Segright.query(0, y)
		yRU[i] = Segright.query(y, N - 1)
		Segright.update(y, 1)

	# print(yLD)
	# print(yLU)
	# print(yRD)
	# print(yRU)

	for i in range(1, N - 1):
		lu = yLU[i]; ru = yRU[i]
		ld = yLD[i]; rd = yRD[i]
		a = pow(2, lu, con)
		b = pow(2, ru, con)
		c = pow(2, ld, con)
		d = pow(2, rd, con)
		if rd != 0 and lu != 0:
			ans += (d - 1) * (a - 1) * b * c
		if ld != 0 and ru != 0:
			ans += (c - 1) * (b - 1) * a * d
		#包除
		if rd != 0 and lu != 0 and ld != 0 and ru != 0:
			ans -= (a - 1) * (b - 1) * (c - 1) * (d - 1)
		ans %= con

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
import sys; input = sys.stdin.buffer.readline
from collections import defaultdict
con = 998244353; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

class SegmentTree(object):
	def __init__(self, N):
		self.N0 = 2 ** (N - 1).bit_length()
		self.data = [0] * (2 * self.N0)

	def update(self, k, x):
		k += self.N0 - 1
		self.data[k] = x
		while k > 0:
			k = (k - 1) // 2
			self.data[k] = self.data[2 * k + 1] + self.data[2 * k + 2]

	def query(self, l, r):
		L = l + self.N0; R = r + self.N0 + 1
		m = 0
		while L < R:
			if R & 1:
				R -= 1
				m += self.data[R - 1]
			if L & 1:
				m += self.data[L - 1]
				L += 1
			L >>= 1; R >>= 1

		return m

#処理内容
def main():
	N = int(input())
	XY = [None] * N
	X = [None] * N
	Y = [None] * N
	for i in range(N):
		x, y = getlist()
		XY[i] = [x, y]
		X[i] = x; Y[i] = y

	#座圧
	XY.sort()
	X.sort(); Y.sort()
	Dx = defaultdict(int)
	Dy = defaultdict(int)
	for i in range(N):
		Dx[X[i]] = i
		Dy[Y[i]] = i

	for i in range(N):
		XY[i] = Dx[XY[i][0]], Dy[XY[i][1]]

	XY.sort()
	# print(XY)
	
	Segleft = SegmentTree(N)
	Segright = SegmentTree(N)

	#関係なく決まっている値
	ans = pow(2, N - 1, con) * N
	ans %= con

	yLU = [0] * N
	yLD = [0] * N
	yRU = [0] * N
	yRD = [0] * N

	#左初期化
	Segleft.update(XY[0][1], 1)

	for i in range(1, N - 1):
		y = XY[i][1]
		yLD[i] = Segleft.query(0, y)
		yLU[i] = Segleft.query(y, N - 1)
		Segleft.update(y, 1)

	#右初期化
	Segright.update(XY[N - 1][1], 1)

	for i in range(N - 2, 0, -1):
		y = XY[i][1]
		yRD[i] = Segright.query(0, y)
		yRU[i] = Segright.query(y, N - 1)
		Segright.update(y, 1)

	# print(yLD)
	# print(yLU)
	# print(yRD)
	# print(yRU)

	for i in range(1, N - 1):
		lu = yLU[i]; ru = yRU[i]
		ld = yLD[i]; rd = yRD[i]
		if rd != 0 and lu != 0:
			ans += (pow(2, rd, con) - 1) * (pow(2, lu, con) - 1) * pow(2, ru, con) * pow(2, ld, con)
		if ld != 0 and ru != 0:
			ans += (pow(2, ld, con) - 1) * (pow(2, ru, con) - 1) * pow(2, lu, con) * pow(2, rd, con)
		#包除
		if rd != 0 and lu != 0 and ld != 0 and ru != 0:
			ans -= (pow(2, ld, con) - 1) * (pow(2, ru, con) - 1) * (pow(2, lu, con) - 1) * (pow(2, rd, con) - 1)
		ans %= con

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input=sys.stdin.readline

N=int(input())
p=[]
for i in range(N):
    x,y=map(int,input().split())
    p.append([x,y])
mod=998244353

pow=[1]
for i in range(N):
    pow.append((pow[-1]*2)%mod)

p.sort(key=lambda x:x[1])
for i in range(N):
    p[i][1]=i+1


data=[[0 for i in range(4)] for i in range(N)]
#A1 ... AnのBIT(1-indexed)
n=N
BIT = [0]*(n+1)

#A1 ~ Aiまでの和 O(logN)
def BIT_query(idx):
    if idx==0:
        return 0
    res_sum = 0
    while idx > 0:
        res_sum += BIT[idx]
        idx -= idx&(-idx)
    return res_sum

#Ai += x O(logN)
def BIT_update(idx,x):
    while idx <= n:
        BIT[idx] += x
        idx += idx&(-idx)
    return

p.sort()
for i in range(N):
    x,y=p[i]
    data[i][0]=BIT_query(N)-BIT_query(y)
    data[i][3]=BIT_query(y-1)
    BIT_update(y,1)

BIT=[0]*(n+1)
for i in range(N-1,-1,-1):
    x,y=p[i]
    data[i][1]=BIT_query(N)-BIT_query(y)
    data[i][2]=BIT_query(y-1)
    BIT_update(y,1)

ans=0
for i in range(N):
    ban=0
    for j in range(4):
        ban+=(pow[data[i][j]]-1)*pow[data[i][j-1]]
        ban%=mod
    ans+=pow[N]-ban-1
    ans%=mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

input=sys.stdin.readline

N=int(input())
p=[]
for i in range(N):
    x,y=map(int,input().split())
    p.append([x,y])

p.sort()
for i in range(N):
    p[i][0]=i+1

p.sort(key=lambda x:x[1])
for i in range(N):
    p[i][1]=i+1


data=[[0 for i in range(4)] for i in range(N)]
#A1 ... AnのBIT(1-indexed)
n=N
BIT = [0]*(n+1)

#A1 ~ Aiまでの和 O(logN)
def BIT_query(idx):
    res_sum = 0
    while idx > 0:
        res_sum += BIT[idx]
        idx -= idx&(-idx)
    return res_sum

#Ai += x O(logN)
def BIT_update(idx,x):
    while idx <= n:
        BIT[idx] += x
        idx += idx&(-idx)
    return

p.sort()
for i in range(N):
    x,y=p[i]
    data[i][0]=BIT_query(N)-BIT_query(y)
    if y==1:
        data[i][1]=0
    else:
        data[i][1]=BIT_query(y-1)
    BIT_update(y,1)

BIT=[0]*(n+1)
for i in range(N-1,-1,-1):
    x,y=p[i]
    data[i][2]=BIT_query(N)-BIT_query(y)
    if y==1:
        data[i][3]=0
    else:
        data[i][3]=BIT_query(y-1)
    BIT_update(y,1)

ans=0
mod=998244353
for i in range(N):
    ban=0
    for j in range(4):
        ban+=pow(2,data[i][j],mod)-1
        ban%=mod
    ban+=(pow(2,data[i][0],mod)-1)*(pow(2,data[i][2],mod)-1)
    ban+=(pow(2,data[i][2],mod)-1)*(pow(2,data[i][3],mod)-1)
    ban+=(pow(2,data[i][3],mod)-1)*(pow(2,data[i][1],mod)-1)
    ban+=(pow(2,data[i][1],mod)-1)*(pow(2,data[i][0],mod)-1)
    ban%=mod
    ans+=pow(2,N,mod)-ban-1
    ans%=mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
mod=998244353
from operator import itemgetter
l.sort(key=itemgetter(1))
for i in range(n):
    l[i][1]=i
l.sort(key=itemgetter(0))
for i in range(n):
    l[i][0]=i

No=2
while No<=n:
    No*=2
tree=[0]*(2*No)

def add(k,x):
    k+=No-1
    while k>=0:
        tree[k]+=x
        k=(k-1)//2
def query(l,r):
    L=l+No-1
    R=r+No-1
    s=0
    while L<=R:
        if R&1:
            s+=tree[R]
            R-=2
        else:
            R-=1
        if L&1:
            L-=1
        else:
            s+=tree[L]
        L>>=1;R>>=1
    return s
pow2=[1 for i in range(n+2)]
t=2
for i in range(1,n+2):
    pow2[i]=t
    t*=2
    t%=mod
ans=0
for x,y in l:
    lu=query(y,n)
    ld=query(0,y)
    ru=n-1-y-lu
    rd=y-ld
    ans+=pow2[n-1]+(pow2[lu]-1)*(pow2[rd]-1)*(pow2[ru]+pow2[ld]-1+(pow2[ld]-1)*(pow2[ru]-1))%mod+(pow2[ld]-1)*(pow2[ru]-1)*(pow2[rd]+pow2[lu]-1)%mod
    ans%=mod
    add(y,1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
MOD = 998244353
import bisect
N = int(input())
P = [list(map(int,input().split())) for i in [0]*N]

class SegmentTree:
    def __init__(self,data):
        N = len(data)
        self.l = 2**((N-1).bit_length())
        self.data = [0]*self.l + data + [0]*(self.l-N)
        for i in range(self.l-1,0,-1):
            self.data[i] = self.data[2*i] + self.data[2*i+1]

    def set(self,i,val):
        i += self.l
        self.data[i] = val
        i = i//2
        while i > 0:
            self.data[i] = self.data[2*i] + self.data[2*i+1]
            i = i//2

    def get(self,i,j):
        i += self.l
        j += self.l
        s = 0
        while j-i > 0:
            if i & 1:
                s += self.data[i]
                i += 1
            if j & 1:
                s += self.data[j-1]
                j -= 1
            i = i//2
            j = j//2
        return s

X,Y = map(list,zip(*P))
X.sort()
Y.sort()
compY = {y:cy for cy,y in enumerate(Y)}
Psorted = sorted(P)
compP = [[n,compY[y]] for n,(_,y) in enumerate(Psorted)]

n1s = []
seg = SegmentTree([0]*N)
for _,y in compP[::-1]:
    n1s.append(seg.get(y,N))
    seg.set(y,1)
n1s = n1s[::-1]

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

ans = (-4*sum(pows)- N + N * pow(2,N,MOD)) % MOD

for n,(x,y) in enumerate(Psorted):
    lower = bisect.bisect_left(Y,y)
    upper = N-1-lower
    left = n
    right = N-n-1
    n1 = n1s[n]
    n2 = upper - n1
    n3 = left - n2
    n4 = right - n1
    ans += pows[n1] + pows[n2] + pows[n3] + pows[n4]
    ans %= MOD

print(ans)