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: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
"""
Binary indexed tree
0-indexed, 関数は閉区間
0からの区間加算、1点更新
"""
class BIT: #0-indexed
    def __init__(self, n):
        self.tree = [0]*(n+1)
        self.tree[0] = None
#        self.element = [0]*(n+1)
    def sum(self, i): #a_0 + ... + a_{i} #閉区間
        s = 0; i += 1
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    def range(self,l,r): #a_l + ... + a_r 閉区間
        return sum(r) - sum(l-1) 
    def add(self, i, x):
        i += 1
        while i <= n:
            self.tree[i] += x
            i += i & -i
        # self.element[i] += x
    #def get(self,i): return element[i]        
    
###########################################
# http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=3791366
###########################################

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

n = int(input())
xyi = []
for i in range(n):
    a = [int(i) for i in readline().split()]
    xyi.append(a+[i])

dame_x = [0]*n
dame_y = [0]*n
dame_xy = [0]*n

MOD = 998244353

sxyi = sorted(xyi, key = lambda x: x[1]) # zaatu の逆写像
modosu = [i for x,y,i in sxyi]
zaatu = [0]*n
for j,zj in enumerate(modosu):
    zaatu[zj] = j


for j, (x,y,i) in enumerate(sxyi):
    dame_y[i] = pow(2,j,MOD)-1 + pow(2,n-j-1,MOD)-1

xyi.sort()

from itertools import accumulate
pow2 = list(accumulate([1]+[2]*n, lambda x,y: (x*y)%MOD))
#print(pow2)

bit = BIT(n)
for j, (x,y,i) in enumerate(xyi):
    dame_x[i] = pow2[j] -1 + pow2[n-j-1]-1
    c = bit.sum(zaatu[i])
    dame_xy[i] += pow2[c]-1 + pow2[j-c]-1
    bit.add(zaatu[i],1)
        
bit = BIT(n)
for j, (x,y,i) in enumerate(reversed(xyi)):
    c = bit.sum(zaatu[i])
    dame_xy[i] += pow2[c]-1 + pow2[j-c]-1
    bit.add(zaatu[i],1)
        
        
#print(dame_x,"\n",dame_y,"\n",dame_xy)

ans = n*(pow2[n]-1)%MOD
for dx,dy,dxy in zip(dame_x,dame_y,dame_xy):
    ans = (ans - dx - dy + dxy)%MOD


print(ans)

Submission status: Runtime Error

Solution code:
def bitsum(bit,i):
    s = 0
    while i > 0:
        s += bit[i]
        i -= i&(-i)
    return s
    
def bitadd(bit,i,x):
    n = len(bit)
    while i <= n:
        bit[i] += x
        i += i&(-i)
    return bit

mod = 998244353

N = int(input())
xy = [list(map(int,input().split())) for _ in range(N)]
xy.sort(key=lambda x:x[1])
for i in range(N):
    xy[i][1] = i+1
xy.sort()
for i in range(N):
    xy[i][0] = i

# x<xi,y<yi
l1 = [0]*N
# x<xi,y>yi
l2 = [0]*N
# x>xi,y<yi
r1 = [0]*N
# x>xi,y>yi
r2 = [0]*N

bitl = [0]*(N+1)
for i in range(N):
    l1[i] = bitsum(bitl,xy[i][1])
    l2[i] = i-l1[i]
    bitadd(bitl,xy[i][1],1)

bitr = [0]*(N+1)
for i in range(N):
    r1[N-i-1] = bitsum(bitr,xy[N-i-1][1])
    r2[N-i-1] = i-r1[N-i-1]
    bitadd(bitr,xy[N-i-1][1],1)

ans = pow(2,N-1,mod)*N%mod
for i in range(N):
    L1 = pow(2,l1[i],mod)
    L2 = pow(2,l2[i],mod)
    R1 = pow(2,r1[i],mod)
    R2 = pow(2,r2[i],mod)
    ans += L1*L2%mod*R1%mod*R2%mod
    ans -= L1*R1%mod
    ans -= L2*R2%mod
    ans -= L1*L2%mod
    ans -= R1*R2%mod
    ans += L1+L2+R1+R2-1
    ans %= mod

print(ans)

Submission status: Runtime Error

Solution code:
from itertools import product
import numpy as np
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)
#         )

cum_array = np.zeros((n+1, n+1), dtype=np.int)
for x, y in points:
    cum_array[x, y] = 1
cum_array = np.cumsum(cum_array, 0)
cum_array = np.cumsum(cum_array, 1)


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) % mod - 1
        if (exist[1] & exist[2]) | (exist[0] & exist[3]):
            tmp *= 2
        ans += tmp
        ans %= mod

print(ans)

Submission status: Runtime Error

Solution code:
from bisect import bisect_left as bl
N = int(input())
P = 998244353
NN = 17
XX = [0] * (2**(NN+1)-1)
pow2 = [1]
for _ in range(1, 202020):
    pow2.append(pow2[-1]*2%P)

def addvalue(j):
    i = 2**NN + j - 1
    while i >= 0:
        XX[i] += 1
        i = (i-1) // 2

def rangesum(a, b):
    l = a + (1<<NN)
    r = b + (1<<NN)
    s = 0
    while l < r:
        if l%2:
            s += XX[l-1]
            l += 1
        if r%2:
            r -= 1
            s += XX[r-1]
        l >>= 1
        r >>= 1
    return s

X = []
Y = []
XY = []
for _ in range(N):
    x, y = map(int, input().split())
    X.append(x)
    Y.append(y)
    XY.append((x, y))

X = sorted(X)
Y = sorted(Y)
XY = sorted(XY, key = lambda x: x[1])

DX = {}
for i in range(N):
    DX[X[i]] = i

ans = 0
U = 0
for x, y in XY:
    a = rangesum(0, DX[x])
    L = bl(X, x)
    b = U-a
    d = L-a
    c = N-a-b-d-1
    ans = (ans + pow2[N] - (pow2[a+b] + pow2[b+c] + pow2[c+d] + pow2[d+a] - pow2[a] - pow2[b] - pow2[c] - pow2[d] + 1)) % P
    U += 1
    addvalue(DX[x])
    
print(ans)

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 998244353
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

P points[216000];
int n;

int bit[216000];
void init(){
    fill(bit, bit + 216000, 0);
}

Int get_sum(int pos){
    pos++;
    Int sum = 0;
    while(pos){
        sum += bit[pos];
        pos -= pos & -pos;
    }
    return sum;
}

void add_val(int pos, int val){
    pos++;
    while(pos < 216000){
        bit[pos] += val;
        pos += pos & -pos;
    }
}

Int mod_pow(Int x, Int a, Int m = MOD){
    if(a == 0)return 1;
    Int res = mod_pow(x, a / 2, m);
    res = res * res % m;
    if(a % 2)res *= x;
    return res % m;
}

Int inv(Int x, Int m = MOD){
    return mod_pow(x, m-2, m);
}


vector<Int> xs, ys;

int main(){
    cin >> n;
    for(int i = 0;i < n;i++){
        cin >> points[i].first >> points[i].second;
        xs.push_back(points[i].first);
        ys.push_back(points[i].second);
    }
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    sort(ys.begin(), ys.end());
    ys.erase(unique(ys.begin(), ys.end()), ys.end());
    for(int i = 0;i < n;i++){
        points[i].first = lower_bound(xs.begin(), xs.end(), points[i].first) - xs.begin();
        points[i].second = lower_bound(ys.begin(), ys.end(), points[i].second) - ys.begin();
    }
    xs.clear();
    ys.clear();
    for(int i = 0;i < n;i++){
        xs.push_back(points[i].first);
        ys.push_back(points[i].second);
    }
    sort(xs.begin(), xs.end());
    sort(ys.begin(), ys.end());

    Int sum = mod_pow(2, n) - 1;
    sum *= n;sum %= MOD;
    sort(points, points + n);
    for(int i = 0;i < n;i++){
        Int x = points[i].first, y = points[i].second;
        Int l = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
        Int r = xs.end() - upper_bound(xs.begin(), xs.end(), x);
        Int u = lower_bound(ys.begin(), ys.end(), y) - ys.begin();
        Int d = ys.end() - upper_bound(ys.begin(), ys.end(), y);
        sum -= mod_pow(2, l) - 1;sum %= MOD;
        sum -= mod_pow(2, r) - 1;sum %= MOD;
        sum -= mod_pow(2, u) - 1;sum %= MOD;
        sum -= mod_pow(2, d) - 1;sum %= MOD;
//        cout << i << ":" << l << " "<< r << " " << u << " " << d << endl;
    }
    int done_cnt = 0;
    queue<P> q;
    init();
    for(int i = 0;i < n;i++){
        Int x = points[i].first, y = points[i].second;
        while(!q.empty() && q.front().first < x){
            add_val(q.front().second, 1);
            done_cnt++;
//            cout << "add:" << q.front().second << endl;
            q.pop();
        }
        Int ld = get_sum(y-1);
        Int lu = done_cnt - get_sum(y);
        sum += pow(2, ld) - 1;sum %= MOD;
        sum += pow(2, lu) - 1;sum %= MOD;
        q.push(P(x,y));
        
//        cout << i << ":" << lu << " "<< ld << endl;
    }
    done_cnt = 0;
    while(!q.empty())q.pop();
    init();
    for(int i = 0;i < n;i++){
        Int x = points[n-i-1].first, y = points[n-i-1].second;
        while(!q.empty() && q.front().first > x){
            add_val(q.front().second, 1);
            done_cnt++;
//            cout << "add:" << q.front().second << endl;
            q.pop();
        }
        Int ld = get_sum(y-1);
        Int lu = done_cnt - get_sum(y);
        sum += pow(2, ld) - 1;sum %= MOD;
        sum += pow(2, lu) - 1;sum %= MOD;
        q.push(P(x,y));
//        cout << i << ":" << lu << " "<< ld << endl;
    }
    sum %= MOD;
    if(sum < 0)sum += MOD;
    cout << sum << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)
    divisors.sort()
    divisors.reverse()
    return divisors

n, k = map(int, input().split())
dat = list(map(int, input().split()))
dat = list(map(abs, dat))
total = sum(dat)
divs = make_divisors(total)
for ds in divs:
    t = 0
    for i in range(n):
        t += min( (dat[i] % ds) , abs((dat[i] % ds) - ds))
        #print(" = {0} ... {1}".format(dat[i], min( (dat[i] % ds) , abs((dat[i] % ds) - ds))))
    #print("end: {0} {1}".format(ds, t))
    t = t // 2
    if t < k:
        res = ds
        break
print(ds)