Problem:


Score : 1000 points

Problem Statement

There are M chairs arranged in a line. The coordinate of the i-th chair (1 ≤ i ≤ M) is i.

N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i-th person wishes to sit in a chair whose coordinate is not greater than L_i, or not less than R_i. Naturally, only one person can sit in the same chair.

It may not be possible for all of them to sit in their favorite chairs, if nothing is done.
Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions.

Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs.

Constraints

1 ≤ N,M ≤ 2 × 10^5

0 ≤ L_i < R_i ≤ M + 1(1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
:
L_N R_N

Output

Print the minimum required number of additional chairs.

Sample Input 1

4 4
0 3
2 3
1 3
3 4

Sample Output 1

0

The four people can sit in chairs at the coordinates 3, 2, 1 and 4, respectively, and no more chair is needed.

Sample Input 2

7 6
0 7
1 5
3 6
2 7
1 6
2 6
3 7

Sample Output 2

2

If we place additional chairs at the coordinates 0 and 2.5, the seven people can sit at coordinates 0, 5, 3, 2, 6, 1 and 2.5, respectively.

Sample Input 3

3 1
1 2
1 2
1 2

Sample Output 3

2

Sample Input 4

6 6
1 6
1 6
1 5
1 5
2 6
2 6

Sample Output 4

2



Sample input:
4 4
0 3
2 3
1 3
3 4


Sample output:
0

Submission status: Wrong Answer

Solution code:
from collections import deque

class LazySegtree():
    def __init__(self,n,init_val,merge_func,ide_ele):
        self.n=n
        self.ide_ele=ide_ele
        self.merge_func=merge_func
        self.val=[0 for i in range(1<<n)]
        self.merge=[0 for i in range(1<<n)]
        self.parent=[-1 for i in range(1<<n)]
        self.lazy=[0 for i in range(1<<n)]
        deq=deque([1<<(n-1)])
        res=[]
        while deq:
            v=deq.popleft()
            res.append(v)
            if not v&1:
                gap=(v&-v)//2
                self.parent[v-gap]=v
                deq.append(v-gap)
                self.parent[v+gap]=v
                deq.append(v+gap)
        for v in res[::-1]:
            if v-1<len(init_val):
                self.val[v-1]=init_val[v-1]
            else:
                self.val[v-1]=10**18
            self.merge[v-1]=self.val[v-1]
            if not v&1:
                gap=(v&-v)//2
                self.merge[v-1]=self.merge_func(self.merge[v-1],self.merge[v-gap-1],self.merge[v+gap-1])

    def lower_kth_update(self,nd,k,x):
        if k==-1:
            return
        maxi=nd-(nd&-nd)+1+k
        ids=self.idgetter(nd,maxi)
        for pos in ids:
            gap=(pos&-pos)//2
            self.propagate(pos)
            if pos<=maxi:
                self.val[pos-1]+=x
                self.lazy[pos-gap-1]+=x*(gap&1)
        for pos in ids[::-1]:
            if pos&1:
                self.merge[pos-1]=self.val[pos-1]
            else:
                gap=(pos&-pos)//2
                self.merge[pos-1]=self.merge_func(self.val[pos-1],self.merge[pos-gap-1]+self.lazy[pos-gap-1],self.merge[pos+gap-1]+self.lazy[pos+gap-1])

    def upper_kth_update(self,nd,k,x):
        if k==-1:
            return
        mini=nd+(nd&-nd)-1-k
        ids=self.idgetter(nd,mini)
        for pos in ids:
            gap=(pos&-pos)//2
            self.propagate(pos)
            if pos>=mini:
                self.val[pos-1]+=x
                self.lazy[pos+gap-1]+=x*(gap&1)
        for pos in ids[::-1]:
            if pos&1:
                self.merge[pos-1]=self.val[pos-1]
            else:
                gap=(pos&-pos)//2
                self.merge[pos-1]=self.merge_func(self.val[pos-1],self.merge[pos-gap-1]+self.lazy[pos-gap-1],self.merge[pos+gap-1]+self.lazy[pos+gap-1])

    def update(self,l,r,x):
        pos=1<<(self.n-1)
        while True:
            gap=(pos&-pos)//2
            self.propagate(pos)
            if pos-1<l:
                pos+=(pos&-pos)//2
            elif pos-1>r:
                pos-=(pos&-pos)//2
            else:
                self.val[pos-1]+=x
                self.upper_kth_update(pos-gap,pos-1-l-1,x)
                self.lower_kth_update(pos+gap,r-pos,x)
                break

        while self.parent[pos]!=-1:
            if pos&1:
                self.merge[pos-1]=self.val[pos-1]
            else:
                gap=(pos&-pos)//2
                self.merge[pos-1]=self.merge_func(self.val[pos-1],self.merge[pos-gap-1]+self.lazy[pos-gap-1],self.merge[pos+gap-1]+self.lazy[pos+gap-1])
            pos=self.parent[pos]

    def lower_kth_merge(self,nd,k,debug=False):
        res=self.ide_ele
        if k==-1:
            return res
        maxi=nd-(nd&-nd)+1+k
        ids=self.idgetter(nd,maxi)
        for pos in ids:
            self.propagate(pos)

        stack=[self.ide_ele]
        for pos in ids[::-1]:
            gap=(pos&-pos)//2
            if pos&1:
                self.merge[pos-1]=self.val[pos-1]
            else:
                self.merge[pos-1]=self.merge_func(self.val[pos-1],self.merge[pos-gap-1]+self.lazy[pos-gap-1],self.merge[pos+gap-1]+self.lazy[pos+gap-1])
            if pos<=maxi:
                stack.append(self.val[pos-1])
                if not pos&1:
                    stack.append(self.merge[pos-gap-1]+self.lazy[pos-gap-1])

        return self.merge_func(stack)

    def upper_kth_merge(self,nd,k):
        res=self.ide_ele
        if k==-1:
            return res
        mini=nd+(nd&-nd)-1-k
        ids=self.idgetter(nd,mini)
        for pos in ids:
            self.propagate(pos)

        stack=[self.ide_ele]
        for pos in ids[::-1]:
            gap=(pos&-pos)//2
            if pos&1:
                self.merge[pos-1]=self.val[pos-1]
            else:
                self.merge[pos-1]=self.merge_func(self.val[pos-1],self.merge[pos-gap-1]+self.lazy[pos-gap-1],self.merge[pos+gap-1]+self.lazy[pos+gap-1])
            if pos>=mini:
                stack.append(self.val[pos-1])
                if not pos&1:
                    stack.append(self.merge[pos+gap-1]+self.lazy[pos+gap-1])

        return self.merge_func(stack)

    def query(self,l,r):
        pos=1<<(self.n-1)
        res=self.ide_ele
        while True:
            gap=(pos&-pos)//2
            self.propagate(pos)
            if pos-1<l:
                pos+=(pos&-pos)//2
            elif pos-1>r:
                pos-=(pos&-pos)//2
            else:
                left=self.upper_kth_merge(pos-gap,pos-1-l-1)
                right=self.lower_kth_merge(pos+gap,r-pos)
                res=self.merge_func(left,right,self.val[pos-1])
                return res

    def propagate(self,pos):
        if self.lazy[pos-1]:
            self.val[pos-1]+=self.lazy[pos-1]
            self.merge[pos-1]+=self.lazy[pos-1]
            if not pos&1:
                gap=(pos&-pos)//2
                self.lazy[pos-gap-1]+=self.lazy[pos-1]
                self.lazy[pos+gap-1]+=self.lazy[pos-1]
            self.lazy[pos-1]=0
        return

    def idgetter(self,start,goal):
        res=[]
        pos=goal
        while pos!=start:
            res.append(pos)
            pos=self.parent[pos]
        res.append(start)
        return res[::-1]

import sys

input=sys.stdin.buffer.readline

ide_ele=10**18

N,M=map(int,input().split())

init=[0 for i in range(M+2)]
for i in range(1,M+1):
    init[0]+=1
    init[i+1]-=1
for i in range(1,M+1):
    init[i]+=init[i-1]
init[-1]=0

LST=LazySegtree((M+2).bit_length(),init,merge_func=min,ide_ele=10**18)

hito=[tuple(map(int,input().split())) for i in range(N)]
hito.sort()

add=M-N
for l,r in hito:
    LST.update(0,r,-1)
    m=LST.query(l+1,M+1)+l
    add=min(m,add)

print(max(-add,0))

Submission status: Wrong Answer

Solution code:
from collections import deque

class LazySegtree():
    def __init__(self,n,init_val,merge_func,ide_ele):
        self.n=n
        self.ide_ele=ide_ele
        self.merge_func=merge_func
        self.val=[0 for i in range(1<<n)]
        self.merge=[0 for i in range(1<<n)]
        self.parent=[-1 for i in range(1<<n)]
        self.lazy=[0 for i in range(1<<n)]
        deq=deque([1<<(n-1)])
        res=[]
        while deq:
            v=deq.popleft()
            res.append(v)
            if not v&1:
                gap=(v&-v)//2
                self.parent[v-gap]=v
                deq.append(v-gap)
                self.parent[v+gap]=v
                deq.append(v+gap)
        for v in res[::-1]:
            if v-1<len(init_val):
                self.val[v-1]=init_val[v-1]
            self.merge[v-1]=self.val[v-1]
            if not v&1:
                gap=(v&-v)//2
                self.merge[v-1]=self.merge_func(self.merge[v-1],self.merge[v-gap-1],self.merge[v+gap-1])

    def lower_kth_update(self,nd,k,x):
        if k==-1:
            return
        maxi=nd-(nd&-nd)+1+k
        pos=nd
        while True:
            gap=(pos&-pos)//2
            if self.lazy[pos-1]:
                self.val[pos-1]+=self.lazy[pos-1]
                self.merge[pos-1]+=self.lazy[pos-1]
                if not pos&1:
                    self.lazy[pos-gap-1]+=self.lazy[pos-1]
                    self.lazy[pos+gap-1]+=self.lazy[pos-1]
                self.lazy[pos-1]=0
            if pos>maxi:
                pos-=gap
            elif pos==maxi:
                self.val[pos-1]+=x
                if not pos&1:
                    self.lazy[pos-gap-1]+=x
                break
            else:
                self.val[pos-1]+=x
                self.lazy[pos-gap-1]+=x
                pos+=gap

        while pos!=self.parent[nd]:
            if pos&1:
                self.merge[pos-1]=self.val[pos-1]
            else:
                gap=(pos&-pos)//2
                self.merge[pos-1]=self.merge_func(self.val[pos-1],self.merge[pos-gap-1]+self.lazy[pos-gap-1],self.merge[pos+gap-1]+self.lazy[pos+gap-1])
            pos=self.parent[pos]

    def upper_kth_update(self,nd,k,x):
        if k==-1:
            return
        mini=nd+(nd&-nd)-1-k
        pos=nd
        while True:
            gap=(pos&-pos)//2
            if self.lazy[pos-1]:
                self.val[pos-1]+=self.lazy[pos-1]
                self.merge[pos-1]+=self.lazy[pos-1]
                if not pos&1:
                    self.lazy[pos-gap-1]+=self.lazy[pos-1]
                    self.lazy[pos+gap-1]+=self.lazy[pos-1]
                self.lazy[pos-1]=0
            if pos>mini:
                self.val[pos-1]+=x
                self.lazy[pos+gap-1]+=x
                pos-=gap
            elif pos==mini:
                self.val[pos-1]+=x
                if not pos&1:
                    self.lazy[pos+gap-1]+=x
                break
            else:
                pos+=gap

        while pos!=self.parent[nd]:
            if pos&1:
                self.merge[pos-1]=self.val[pos-1]
            else:
                gap=(pos&-pos)//2
                self.merge[pos-1]=self.merge_func(self.val[pos-1],self.merge[pos-gap-1]+self.lazy[pos-gap-1],self.merge[pos+gap-1]+self.lazy[pos+gap-1])
            pos=self.parent[pos]

    def update(self,l,r,x):
        pos=1<<(self.n-1)
        while True:
            if self.lazy[pos-1]:
                self.val[pos-1]+=self.lazy[pos-1]
                self.merge[pos-1]+=self.lazy[pos-1]
                if not pos&1:
                    self.lazy[pos-gap-1]+=self.lazy[pos-1]
                    self.lazy[pos+gap-1]+=self.lazy[pos-1]
                self.lazy[pos-1]=0
            if pos-1<l:
                pos+=(pos&-pos)//2
            elif pos-1>r:
                pos-=(pos&-pos)//2
            else:
                self.val[pos-1]+=x
                gap=(pos&-pos)//2
                self.upper_kth_update(pos-gap,pos-1-l-1,x)
                self.lower_kth_update(pos+gap,r-pos,x)
                break

        while self.parent[pos]!=-1:
            if pos&1:
                self.merge[pos-1]=self.val[pos-1]
            else:
                gap=(pos&-pos)//2
                self.merge[pos-1]=self.merge_func(self.val[pos-1],self.merge[pos-gap-1]+self.lazy[pos-gap-1],self.merge[pos+gap-1]+self.lazy[pos+gap-1])
            pos=self.parent[pos]

    def lower_kth_merge(self,nd,k):
        res=self.ide_ele
        if k==-1:
            return res
        maxi=nd-(nd&-nd)+1+k
        pos=nd
        while True:
            gap=(pos&-pos)//2
            if self.lazy[pos-1]:
                self.val[pos-1]+=self.lazy[pos-1]
                self.merge[pos-1]+=self.lazy[pos-1]
                if not pos&1:
                    self.lazy[pos-gap-1]+=self.lazy[pos-1]
                    self.lazy[pos+gap-1]+=self.lazy[pos-1]
                self.lazy[pos-1]=0
            if pos>maxi:
                pos-=gap
            elif pos==maxi:
                break
            else:
                pos+=gap

        while self.parent[pos]!=self.parent[nd]:
            gap=(pos&-pos)//2
            if pos&1:
                self.merge[pos-1]=self.val[pos-1]
            else:
                self.merge[pos-1]=self.merge_func(self.val[pos-1],self.merge[pos-gap-1]+self.lazy[pos-gap-1],self.merge[pos+gap-1]+self.lazy[pos+gap-1])
            if pos<=maxi:
                res=self.merge_func(res,self.val[pos-1],self.merge[pos-gap-1])
            pos=self.parent[pos]
        return res

    def upper_kth_merge(self,nd,k):
        res=self.ide_ele
        if k==-1:
            return res
        mini=nd+(nd&-nd)-1-k
        pos=nd
        while True:
            gap=(pos&-pos)//2
            if self.lazy[pos-1]:
                self.val[pos-1]+=self.lazy[pos-1]
                self.merge[pos-1]+=self.lazy[pos-1]
                if not pos&1:
                    self.lazy[pos-gap-1]+=self.lazy[pos-1]
                    self.lazy[pos+gap-1]+=self.lazy[pos-1]
                self.lazy[pos-1]=0
            if pos>mini:
                pos-=gap
            elif pos==mini:
                break
            else:
                pos+=gap

        while pos!=self.parent[nd]:
            gap=(pos&-pos)//2
            if pos&1:
                self.merge[pos-1]=self.val[pos-1]
            else:
                self.merge[pos-1]=self.merge_func(self.val[pos-1],self.merge[pos-gap-1]+self.lazy[pos-gap-1],self.merge[pos+gap-1]+self.lazy[pos+gap-1])
            if pos>=mini:
                res=self.merge_func(res,self.val[pos-1],self.merge[pos+gap-1])
            pos=self.parent[pos]
        return res

    def query(self,l,r):
        pos=1<<(self.n-1)
        res=self.ide_ele
        step=0
        while True:
            gap=(pos&-pos)//2
            #print(step,self.val)
            #print(step,self.lazy)
            if self.lazy[pos-1]:
                self.val[pos-1]+=self.lazy[pos-1]
                self.merge[pos-1]+=self.lazy[pos-1]
                if not pos&1:
                    self.lazy[pos-gap-1]+=self.lazy[pos-1]
                    self.lazy[pos+gap-1]+=self.lazy[pos-1]
                self.lazy[pos-1]=0
            if pos-1<l:
                pos+=(pos&-pos)//2
            elif pos-1>r:
                pos-=(pos&-pos)//2
            else:
                left=self.upper_kth_merge(pos-gap,pos-1-l-1)
                right=self.lower_kth_merge(pos+gap,r-pos)
                res=self.merge_func(left,right,self.val[pos-1])
                return res

import sys

input=sys.stdin.buffer.readline

ide_ele=10**18

def segfunc(*args):
    return min(args)

N,M=map(int,input().split())

init=[0 for i in range(M+2)]
for i in range(1,M+1):
    init[0]+=1
    init[i+1]-=1
for i in range(1,M+1):
    init[i]+=init[i-1]
init[-1]=0

LST=LazySegtree((M+2).bit_length(),init,segfunc,ide_ele)

hito=[tuple(map(int,input().split())) for i in range(N)]
hito.sort()

add=M-N
for l,r in hito:
    LST.update(0,r,-1)
    m=LST.query(l+1,M+1)+l
    add=min(m,add)

print(max(-add,0))

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

N,M=map(int,input().split())

# N: 処理する区間の長さ
INF = 2**31-1

LV = (M+2-1).bit_length()
N0 = 2**LV
data = [0]*(2*N0)
lazy = [0]*(2*N0)

def gindex(l, r):
    L = (l + N0) >> 1; R = (r + N0) >> 1
    lc = 0 if l & 1 else (L & -L).bit_length()
    rc = 0 if r & 1 else (R & -R).bit_length()
    for i in range(LV):
        if rc <= i:
            yield R
        if L < R and lc <= i:
            yield L
        L >>= 1; R >>= 1

# 遅延伝搬処理
def propagates(*ids):
    for i in reversed(ids):
        v = lazy[i-1]
        if not v:
            continue
        lazy[2*i-1] += v; lazy[2*i] += v
        data[2*i-1] += v; data[2*i] += v
        lazy[i-1] = 0

# 区間[l, r)にxを加算
def update(l, r, x):
    *ids, = gindex(l, r)
    propagates(*ids)

    L = N0 + l; R = N0 + r
    while L < R:
        if R & 1:
            R -= 1
            lazy[R-1] += x; data[R-1] += x
        if L & 1:
            lazy[L-1] += x; data[L-1] += x
            L += 1
        L >>= 1; R >>= 1
    for i in ids:
        data[i-1] = min(data[2*i-1], data[2*i])

# 区間[l, r)内の最小値を求める
def query(l, r):
    propagates(*gindex(l, r))
    L = N0 + l; R = N0 + r

    s = INF
    while L < R:
        if R & 1:
            R -= 1
            s = min(s, data[R-1])
        if L & 1:
            s = min(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

for i in range(1,M+1):
    update(0,i+1,1)

add=0
hito=[]
for i in range(N):
    L,R=map(int,input().split())
    hito.append((L,R))
hito.sort()
#test=[query(i,i+1) for i in range(M+2)]
#print(test)
for l,r in hito:
    update(0,r+1,-1)
    #test=[query(i,i+1) for i in range(M+2)]
    #print(test)
    m=query(l+1,M+2)+l
    add=min(m,add)

print(max(-add,0))

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

N,M=map(int,input().split())

# N: 処理する区間の長さ
INF = 2**31-1

LV = (M+2-1).bit_length()
N0 = 2**LV
data = [0]*(2*N0)
lazy = [0]*(2*N0)

def gindex(l, r):
    L = (l + N0) >> 1; R = (r + N0) >> 1
    lc = 0 if l & 1 else (L & -L).bit_length()
    rc = 0 if r & 1 else (R & -R).bit_length()
    for i in range(LV):
        if rc <= i:
            yield R
        if L < R and lc <= i:
            yield L
        L >>= 1; R >>= 1

# 遅延伝搬処理
def propagates(*ids):
    for i in reversed(ids):
        v = lazy[i-1]
        if not v:
            continue
        lazy[2*i-1] += v; lazy[2*i] += v
        data[2*i-1] += v; data[2*i] += v
        lazy[i-1] = 0

# 区間[l, r)にxを加算
def update(l, r, x):
    *ids, = gindex(l, r)
    propagates(*ids)

    L = N0 + l; R = N0 + r
    while L < R:
        if R & 1:
            R -= 1
            lazy[R-1] += x; data[R-1] += x
        if L & 1:
            lazy[L-1] += x; data[L-1] += x
            L += 1
        L >>= 1; R >>= 1
    for i in ids:
        data[i-1] = min(data[2*i-1], data[2*i])

# 区間[l, r)内の最小値を求める
def query(l, r):
    propagates(*gindex(l, r))
    L = N0 + l; R = N0 + r

    s = INF
    while L < R:
        if R & 1:
            R -= 1
            s = min(s, data[R-1])
        if L & 1:
            s = min(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

for i in range(1,M+1):
    update(0,i+1,1)

add=0
hito=[]
for i in range(N):
    L,R=map(int,input().split())
    hito.append((L,R))
hito.sort()
#test=[query(i,i+1) for i in range(M+2)]
#print(test)
for l,r in hito:
    update(0,r+1,-1)
    #test=[query(i,i+1) for i in range(M+2)]
    #print(test)
    m=query(l+1,M+2)+l
    add=min(m,add)

print(-add)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
span = []
for _ in range(N):
    l,r = map(int,input().split())
    s = r
    t = M+l
    span.append((s,t))

span.sort()
cnt = 0
tmp = -1

for s,t in span:
    if tmp < s:
        tmp = s
    elif tmp+1<=t:
        tmp += 1
    else:
        cnt += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

# 区間加算、区間最大
class LazySeg:
    def __init__(self, aa):
        inf = 10 ** 16
        n = 1 << (len(aa) - 1).bit_length()
        tree = [-inf] * (2 * n - 1)
        lazy = [0] * (2 * n - 1)
        tree[n - 1:n - 1 + len(aa)] = aa
        for u in range(n - 2, -1, -1):
            tree[u] = max(tree[u * 2 + 1], tree[u * 2 + 2])
        self.tree = tree
        self.lazy = lazy
        self.n = n
        self.inf = inf

    def prop(self, u, l, r):
        self.tree[u] += self.lazy[u]
        if l + 1 < r:
            self.lazy[u * 2 + 1] += self.lazy[u]
            self.lazy[u * 2 + 2] += self.lazy[u]
        self.lazy[u] = 0

    def add(self, l, r, a, u=0, tl=0, tr=-1):
        if tr == -1: tr = self.n
        if r <= tl or tr <= l: return
        if l <= tl and tr <= r:
            self.lazy[u] += a
            self.prop(u, tl, tr)
            return
        tm = (tl + tr) // 2
        self.add(l, r, a, u * 2 + 1, tl, tm)
        self.add(l, r, a, u * 2 + 2, tm, tr)
        self.tree[u] = max(self.tree[u * 2 + 1], self.tree[u * 2 + 2])

    def max(self, l, r, u=0, tl=0, tr=-1):
        res = -self.inf
        if tr == -1: tr = self.n
        if r <= tl or tr <= l: return res
        self.prop(u, tl, tr)
        if l <= tl and tr <= r:
            return self.tree[u]
        tm = (tl + tr) // 2
        ret = self.max(l, r, u * 2 + 1, tl, tm)
        if ret > res: res = ret
        ret = self.max(l, r, u * 2 + 2, tm, tr)
        if ret > res: res = ret
        return res

def main():
    n,m=MI()
    lr=[LI() for _ in range(n)]
    lr.sort()
    #print(lr)
    aa=list(range(-m,1))
    st=LazySeg(aa)
    ans=max(n-m,0)
    for i,[l,r] in enumerate(lr):
        st.add(0,r,1)
        if i+1<n and lr[i+1][0]==l:continue
        #print(st.max(1,m+2),l)
        over=st.max(0,m+1)-l
        if over>ans:ans=over
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

# 区間加算、区間最大
class LazySeg:
    def __init__(self, aa):
        inf = 10 ** 16
        n = 1 << (len(aa) - 1).bit_length()
        tree = [-inf] * (2 * n - 1)
        lazy = [0] * (2 * n - 1)
        tree[n - 1:n - 1 + len(aa)] = aa
        for u in range(n - 2, -1, -1):
            tree[u] = max(tree[u * 2 + 1], tree[u * 2 + 2])
        self.tree = tree
        self.lazy = lazy
        self.n = n
        self.inf = inf

    def prop(self, u, l, r):
        self.tree[u] += self.lazy[u]
        if l + 1 < r:
            self.lazy[u * 2 + 1] += self.lazy[u]
            self.lazy[u * 2 + 2] += self.lazy[u]
        self.lazy[u] = 0

    def add(self, l, r, a, u=0, tl=0, tr=-1):
        if tr == -1: tr = self.n
        if r <= tl or tr <= l: return
        if l <= tl and tr <= r:
            self.lazy[u] += a
            self.prop(u, tl, tr)
            return
        tm = (tl + tr) // 2
        self.add(l, r, a, u * 2 + 1, tl, tm)
        self.add(l, r, a, u * 2 + 2, tm, tr)
        self.tree[u] = max(self.tree[u * 2 + 1], self.tree[u * 2 + 2])

    def max(self, l, r, u=0, tl=0, tr=-1):
        res = -self.inf
        if tr == -1: tr = self.n
        if r <= tl or tr <= l: return res
        self.prop(u, tl, tr)
        if l <= tl and tr <= r:
            return self.tree[u]
        tm = (tl + tr) // 2
        ret = self.max(l, r, u * 2 + 1, tl, tm)
        if ret > res: res = ret
        ret = self.max(l, r, u * 2 + 2, tm, tr)
        if ret > res: res = ret
        return res

def main():
    n,m=MI()
    lr=[LI() for _ in range(n)]
    lr.sort()
    #print(lr)
    aa=list(range(-m,1))
    st=LazySeg(aa)
    ans=n-m
    for i,[l,r] in enumerate(lr):
        st.add(0,r,1)
        if i+1<n and lr[i+1][0]==l:continue
        #print(st.max(1,m+2),l)
        over=st.max(0,m+1)-l
        if over>ans:ans=over
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

# 区間加算、区間最大
class LazySeg:
    def __init__(self, aa):
        inf = 10 ** 16
        n = 1 << (len(aa) - 1).bit_length()
        tree = [-inf] * (2 * n - 1)
        lazy = [0] * (2 * n - 1)
        tree[n - 1:n - 1 + len(aa)] = aa
        for u in range(n - 2, -1, -1):
            tree[u] = max(tree[u * 2 + 1], tree[u * 2 + 2])
        self.tree = tree
        self.lazy = lazy
        self.n = n
        self.inf = inf

    def prop(self, u, l, r):
        self.tree[u] += self.lazy[u]
        if l + 1 < r:
            self.lazy[u * 2 + 1] += self.lazy[u]
            self.lazy[u * 2 + 2] += self.lazy[u]
        self.lazy[u] = 0

    def add(self, l, r, a, u=0, tl=0, tr=-1):
        if tr == -1: tr = self.n
        if r <= tl or tr <= l: return
        if l <= tl and tr <= r:
            self.lazy[u] += a
            self.prop(u, tl, tr)
            return
        tm = (tl + tr) // 2
        self.add(l, r, a, u * 2 + 1, tl, tm)
        self.add(l, r, a, u * 2 + 2, tm, tr)
        self.tree[u] = max(self.tree[u * 2 + 1], self.tree[u * 2 + 2])

    def max(self, l, r, u=0, tl=0, tr=-1):
        res = -self.inf
        if tr == -1: tr = self.n
        if r <= tl or tr <= l: return res
        self.prop(u, tl, tr)
        if l <= tl and tr <= r:
            return self.tree[u]
        tm = (tl + tr) // 2
        ret = self.max(l, r, u * 2 + 1, tl, tm)
        if ret > res: res = ret
        ret = self.max(l, r, u * 2 + 2, tm, tr)
        if ret > res: res = ret
        return res

def main():
    n,m=MI()
    lr=[LI() for _ in range(n)]
    lr.sort()
    #print(lr)
    aa=list(range(-m-1,1))
    st=LazySeg(aa)
    ans=0
    for i,[l,r] in enumerate(lr):
        st.add(1,r+1,1)
        if i+1<n and lr[i+1][0]==l:continue
        #print(st.max(1,m+2),l)
        over=st.max(1,m+2)-l
        if over>ans:ans=over
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import heapq


INF = 10 ** 9

class SegTreeX:

    def __init__(self, n):
        k = 1
        while k < n:
            k *= 2
        self.n = k
        self.dat = [0] * (k * 2)
        self.pos = [i - (k - 1) for i in range(k * 2)]


    def update(self, i, x):
        i += self.n - 1
        self.dat[i] = x
        while 0 < i:
            i = (i - 1) // 2
            d1 = self.dat[i * 2 + 1]
            d2 = self.dat[i * 2 + 2]
            if d1 <= d2:
                self.dat[i] = d1
                self.pos[i] = self.pos[i * 2 + 1]
            else:
                self.dat[i] = d2
                self.pos[i] = self.pos[i * 2 + 2]


    def query(self, a, b, k = 0, l = 0, r = -1):
        if r < 0:
            r = self.n
        if r <= a or b <= l:
            return INF, -1
        if a <= l and r <= b:
            return self.dat[k], self.pos[k]
        else:
            vl, pl = self.query(a, b, k * 2 + 1, l, (l + r) // 2)
            vr, pr = self.query(a, b, k * 2 + 2, (l + r) // 2, r)
            if vl <= vr:
                return vl, pl
            else:
                return vr, pr


def solve(n, m, a):


    a.sort()
    ans = 0
    q = []
    ol = -1
    seg = SegTreeX(m + 1)
    for i in range(n):
        li, ri = a[i]
        if li != ol and 0 <= ol:
            while ol < len(q):
                rj = heapq.heappop(q)
                if m < rj:
                    ans += 1
                else:
                    v, p = seg.query(rj, m + 1)
                    if p < 0 or v == 1:
                        ans += 1
                    else:
                        seg.update(p, 1)
        heapq.heappush(q, ri)
        ol = li
    while ol < len(q):
        rj = heapq.heappop(q)
        if m < rj:
            ans += 1
        else:
            v, p = seg.query(rj, m + 1)
            if p < 0 or v == 1:
                ans += 1
            else:
                seg.update(p, 1)

    if ans < n - m:
        ans = n - m

    return ans


def main():
    n, m = input().split()
    n = int(n)
    m = int(m)
    a = []
    for _ in range(n):
        li, ri = input().split()
        li = int(li)
        ri = int(ri)
        a.append((li, ri))

    print(solve(n, m, a))


if __name__ == '__main__':
    main()