Problem:


Score : 600 points

Problem Statement

There are N robots numbered 1 to N placed on a number line. Robot i is placed at coordinate X_i. When activated, it will travel the distance of D_i in the positive direction, and then it will be removed from the number line. All the robots move at the same speed, and their sizes are ignorable.

Takahashi, who is a mischievous boy, can do the following operation any number of times (possibly zero) as long as there is a robot remaining on the number line.

Choose a robot and activate it. This operation cannot be done when there is a robot moving.

While Robot i is moving, if it touches another robot j that is remaining in the range [X_i, X_i + D_i) on the number line, Robot j also gets activated and starts moving. This process is repeated recursively.

How many possible sets of robots remaining on the number line are there after Takahashi does the operation some number of times? Compute this count modulo 998244353, since it can be enormous.

Constraints

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

-10^9 \leq X_i \leq 10^9

1 \leq D_i \leq 10^9

X_i \neq X_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 D_1
:
X_N D_N

Output

Print the number of possible sets of robots remaining on the number line, modulo 998244353.

Sample Input 1

2
1 5
3 3

Sample Output 1

3

There are three possible sets of robots remaining on the number line: \{1, 2\}, \{1\}, and \{\}.

These can be achieved as follows:

If Takahashi activates nothing, the robots \{1, 2\} will remain.

If Takahashi activates Robot 1, it will activate Robot 2 while moving, after which there will be no robots on the number line. This state can also be reached by activating Robot 2 and then Robot 1.

If Takahashi activates Robot 2 and finishes doing the operation, the robot \{1\} will remain.

Sample Input 2

3
6 5
-1 10
3 3

Sample Output 2

5

There are five possible sets of robots remaining on the number line: \{1, 2, 3\}, \{1, 2\}, \{2\}, \{2, 3\}, and \{\}.

Sample Input 3

4
7 10
-10 3
4 3
-4 3

Sample Output 3

16

None of the robots influences others.

Sample Input 4

20
-8 1
26 4
0 5
9 1
19 4
22 20
28 27
11 8
-3 20
-25 17
10 4
-18 27
24 28
-11 19
2 27
-2 18
-1 12
-24 29
31 29
29 7

Sample Output 4

110

Remember to print the count modulo 998244353.



Sample input:
2
1 5
3 3


Sample output:
3

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
from bisect import bisect

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

XD.sort()
data=[i for i in range(N)]
for i in range(N-2,-1,-1):
    Y=XD[i][0]+XD[i][1]
    b=bisect(XD,[Y])
    if i<b-1:
        data[i]=max(data[i+1],data[b-1])

ans=[0]*(N+1)
ans[N]=1
ans[N-1]=2

for i in range(N-2,-1,-1):
    ans[i]=(ans[i+1]+ans[data[i]+1])%mod

print(ans[0])

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect

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

XD.sort()
data=[i for i in range(N)]
for i in range(N-2,-1,-1):
    Y=XD[i][0]+XD[i][1]
    b=bisect(XD,[Y])
    if i<b-1:
        data[i]=max(data[i+1],data[b-1])

ans=[0]*(N+1)
ans[0]=1
ans[data[0]+1]+=1
ans[1]+=ans[0]

for i in range(1,N):
    
    ans[data[i]+1]=(ans[data[i]+1]+ans[i])%mod
    ans[i+1]=(ans[i+1]+ans[i])%mod

print(ans[-1])

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect

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

XD.sort()
data=[i for i in range(N)]
for i in range(N-2,-1,-1):
    Y=XD[i][0]+XD[i][1]
    b=bisect(XD,[Y])
    if i<b-1:
        data[i]=max(data[i+1],data[b-1])

print(data)

ans=[0]*(N+1)
ans[0]=1
ans[data[0]+1]+=1
ans[1]+=ans[0]

for i in range(1,N):
    
    ans[data[i]+1]=(ans[data[i]+1]+ans[i])%mod
    ans[i+1]=(ans[i+1]+ans[i])%mod

print(ans)
print(ans[-1])

Submission status: Wrong Answer

Solution code:
def main():
    mod = 998244353
    n = int(input())
    ab = [tuple(map(int, input().split())) for _ in [0]*n]
    ab.sort()
    next_a = [-1]*n
    dp = [0]*(n+1)
    dp[n] = 1
    for i in range(n-1, -1, -1):
        a0, b0 = ab[i]
        dist = a0+b0
        now = i
        while now < n-1:
            a1, b1 = ab[now+1]
            if a1 < dist:
                now = next_a[now+1]
            else:
                break
        next_a[i] = now
    for i in range(n-1, -1, -1):
        dp[i] = dp[i+1]+dp[next_a[i]+1] % mod
    print(dp[0])


main()

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_right
input = sys.stdin.readline
ide = -1
def func(a,b):
  return max(a,b)
class SegmentTree:
  def __init__(self,ls,commutative = True):
    if commutative == True:
      self.n = len(ls)
      self.tree = [ide for i in range(self.n)]+ls
    else:
      self.n = 2**((len(ls)-1).bit_length())
      self.tree = [ide for i in range(self.n)]+ls+[ide for i in range(self.n-len(ls))]
    for i in range(1,self.n)[::-1]:
      self.tree[i] = func(self.tree[i<<1|0],self.tree[i<<1|1])
  def getall(self):
    return self.tree[1]
  def get(self,l,r):
    ret = ide
    l += self.n
    r += self.n
    while l < r:
      if l&1:
        ret = func(self.tree[l],ret)
        l += 1
      if r&1:
        ret = func(ret,self.tree[r-1])
      l >>= 1
      r >>= 1
    return ret
  def update(self,i,x):
    i += self.n
    self.tree[i] = x
    while i > 1:
      i >>= 1
      self.tree[i] = func(self.tree[i<<1|0],self.tree[i<<1|1])
n = int(input())
xd = [list(map(int,input().split())) for i in range(n)]
mod = 998244353
xd.sort()
go = [[] for i in range(n+1)]
come = [-1 for i in range(n+1)]
deg = [0]*(n+1)
move = [-1]*n
xdsum = [(sum(xd[i]),i) for i in range(n)]
xdsum.sort()
xdsump,xdsumi = list(zip(*xdsum))
xdsumi = list(xdsumi)
xdsumii = [0]*n
for i in range(n):
  t = xdsumi[i]
  xdsumii[t] = i
sol = SegmentTree(xdsumi)
for i in range(n-1,-1,-1):
  rng = bisect_right(xdsump,xd[i][0])
  sol.update(xdsumii[i],-1)
  move[i] = sol.get(rng,n-1)+1
  if not move[i]:
    move[i] = -1
for i in range(n):
  if move[i] == -1:
    continue
  come[i+1] = move[i]
  go[move[i]].append(i+1)
  deg[move[i]] += 1
leafls = []
ans = 1
dp = [1]*(n+1)
for i in range(1,n+1):
  if not go[i]:
    leafls.append(i)
    dp[i] = 2
while leafls:
  x = leafls.pop()
  if come[x] == -1:
    ans = ans*dp[x]%mod
    continue
  y = come[x]
  if deg[y] >= 1:
    deg[y] -= 1
    dp[y] = dp[y]*dp[x]%mod
    if deg[y] == 0:
      dp[y] += 1
      leafls.append(y)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]


mod = 998244353

inf = 1e10


#solve
def solve():
    def init_min(init_min_val):
            #set_val
        for i in range(n):
            seg_min[i+num_min-1] = init_min_val[i]
        #built
        for i in range(num_min-2, -1, -1):
            seg_min[i] = min(seg_min[2*i+1], seg_min[2*i+2])


    def update_min(k, x):
        k += num_min-1
        seg_min[k] = x
        while k:
            k = (k-1)//2
            seg_min[k] = min(seg_min[k*2+1], seg_min[k*2+2])


    def query_min(p, q):
        if q <= p:
            return ide_ele_min
        p += num_min-1
        q += num_min-2
        res = ide_ele_min
        while q-p > 1:
            if p & 1 == 0:
                res = min(res, seg_min[p])
            if q & 1 == 1:
                res = min(res, seg_min[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = min(res, seg_min[p])
        else:
            res = min(min(res, seg_min[p]), seg_min[q])
        return res

    n = II()
    #####単位元######
    ide_ele_min = 10**10

    #num_min:n以上の最小の2のべき乗
    num_min = 2**(n-1).bit_length()
    seg_min = [ide_ele_min] * 2 * num_min
    xd = LIR(n)
    xd.sort()
    x = [x for x, d in xd]
    dp = [[0] * 2 for i in range(n + 1)]
    On = 0
    Off = 1
    dp[-1][On] =  1
    for i in range(n - 1, -1, -1):
        dp[i][Off] = (dp[i + 1][On] + dp[i + 1][Off]) % mod
        j = bisect_right(x, x[i] + xd[i][1] - 1) - 1
        if j == i:
            dp[i][On] = (dp[i + 1][On] + dp[i + 1][Off]) % mod
            update_min(i, (dp[i + 1][On] + dp[i + 1][Off]) % mod)
        else:
            j = query_min(i, j + 1)
            dp[i][On] = j
            update_min(i, j)
        
    print(sum(dp[0]) % mod)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
p = 998244353
n = int(input())
xd = [0]*n
for i in range(n):
  xd[i] = list(map(int, input().split()))

xd.sort(reverse = True)
X = [x[0] for x in xd]

cnt = [0]*n
rest = [-1]*n

for i,(x,d) in enumerate(xd):
  if i ==0:
    cnt[i] = 2
    continue
  next = i-1
  while X[next] < x + d:
    if next == -1:
      break
    else:
      next -= 1
  next += 1
  rest[i] = next
  rest[i] = min(rest[next:i+1])
  if rest[i] > 0:
    cnt[i] = cnt[rest[next]-1] + cnt[i-1]
  else:
    cnt[i] = 1 + cnt[i-1]
  cnt[i] %= p

  
print(cnt[-1])

Submission status: Wrong Answer

Solution code:
import bisect
n=int(input())
x=[]
d=[]
mod=998244353
l=[]
for i in range(n):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[0])
for i in range(n):
    a,b=l[i][0],l[i][1]
    x.append(a)
    d.append(b)

r=[0]*n
r[-1]=n-1
for i in range(n-2,-1,-1):
    xi=bisect.bisect_left(x,x[i]+d[i])
    xi-=1
    if xi==i:
        r[i]=i
    else:
        r[i]=r[xi]
dp=[0]*(n+1)
#dp[-2]=1
dp[-1]=1
for i in range(n-1,-1,-1):    
    dp[i]=(dp[i+1]+dp[r[i]+1])%mod
print((dp[0])%mod)

Submission status: Wrong Answer

Solution code:
import bisect
n=int(input())
x=[]
d=[]
mod=998244353
l=[]
for i in range(n):
    l.append(list(map(int,input().split())))
l.sort()
for i in range(n):
    a,b=l[i][0],l[i][1]#map(int,input().split())
    x.append(a)
    d.append(b)

r=[0]*n
r[-1]=n-1
for i in range(n-2,-1,-1):
    xi=bisect.bisect_left(x,x[i]+d[i])
    xi-=1
    r[i]=xi
#print(r,x,d)
dp1=[0]*n#動かさない
dp1[-1]=1
dp2=[0]*n#動かす
dp2[-1]=1
for i in range(n-2,-1,-1):    
    dp1[i]=dp1[i+1]+dp2[i+1]
    dp2[i]=dp1[r[i]]
    #print(dp1,dp2)
print((dp1[0]+dp2[0])%mod)

Submission status: Wrong Answer

Solution code:
import bisect
n=int(input())
x=[]
d=[]
mod=998244353
for i in range(n):
    a,b=map(int,input().split())
    x.append(a)
    d.append(b)
r=[0]*n
r[-1]=n-1
for i in range(n-2,-1,-1):
    xi=bisect.bisect_left(x,x[i]+d[i])
    xi-=1
    r[i]=xi
dp1=[0]*n
dp1[-1]=1
dp2=[0]*n
dp2[-1]=1
for i in range(n-2,-1,-1):
    dp2[i]=dp1[r[i]]
    dp1[i]=dp1[i+1]+dp2[i+1]
print((dp1[0]+dp2[0])%mod)

Submission status: Wrong Answer

Solution code:
N = int(input())
X = []
D = []
trees = []
for i in range(N):
    a = []
    x, d = input().split()
    x = int(x)
    d = int(d)
    trees.append([x, x+d])

trees.sort(key=lambda x: x[0],reverse=True)
print(trees)

Submission status: Wrong Answer

Solution code:
#reference Segment-Tree code from:https://ikatakos.com/pot/programming_algorithm/data_structure/segment_tree

def bisect(pos):
  l=-1
  r=n
  while r-l!=1:
    mid=(l+r)//2
    if arr[mid][0]>=pos:
      r=mid
    else:
      l=mid
  return r

mod=998244353
n=int(input())
arr=[list(map(int,input().split())) for _ in range(n)]
arr=sorted(arr,key=lambda x:x[0])
poss=[]
for i in range(n):
  poss.append(bisect(arr[i][0]+arr[i][1]))
poss.append(n)
print(poss)

offset=2**((n-1).bit_length())
st=[0]*(2*offset)

def update(pos,x):
  pos+=offset
  st[pos]=x
  while pos>1:
    y=st[pos^1]
    if x<=y:
      break
    pos>>=1
    st[pos]=x

def get_max(l,r):
  ret=0
  l+=offset
  r+=offset
  while l<r:
    if r&1:
      ret=max(ret,st[r-1])
    if l&1:
      ret=max(ret,st[l])
      l+=1
    l>>=1
    r>>=1
  return ret

for i in range(n):
  update(i,poss[i])
for i in range(n-1,-1,-1):
  poss[i]=get_max(i,poss[i])
  update(i,poss[i])

dp=[0]*(n+1)
dp[-1]=1
for i in range(n-1,-1,-1):
  dp[i]+=dp[i+1]
  dp[i]%=mod
  pos=poss[i]
  dp[i]+=dp[pos]
  dp[i]%=mod
print(dp[0]%mod)

Submission status: Wrong Answer

Solution code:
"""
F
"""
import sys
input=sys.stdin.readline

from bisect import bisect

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

XD.sort()
data=[i for i in range(N)]
for i in range(N-2,-1,-1):
    Y=XD[i][0]+XD[i][1]
    b=bisect(XD,[Y])
    if i<b-1:
        data[i]=max(data[i+1],data[b-1])

ans=[0]*(N+1)
ans[0]=1
ans[data[0]+1]+=1
ans[1]+=ans[0]

for i in range(1,N):
    ans[data[i]+1]=(ans[data[i]+1]+ans[i])%mod
    ans[i+1]=(ans[i+1]+ans[i])%mod


print(ans[-1])

Submission status: Wrong Answer

Solution code:
"""
F
"""
from bisect import bisect

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

XD.sort()
data=[i for i in range(N)]
for i in range(N-2,-1,-1):
    Y=XD[i][0]+XD[i][1]
    b=bisect(XD,[Y])
    if i<b-1:
        data[i]=max(data[i+1],data[b-1])

ans=[0]*(N+1)
ans[0]=1
ans[data[0]+1]+=1
for i in range(1,N):
    ans[i]+=ans[i-1]
    ans[data[i]+1]+=ans[i]

ans[-1]+=ans[-2]
print(ans[-1])

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
import bisect
mod=998244353
n=2*10**5
d=[]
N=1<<(n.bit_length())
t=[-1 for i in range(2*N-1)]

def ud(i,x):
  i+=N-1
  t[i]=x
  while i:
    i=(i-1)//2
    t[i]=max(t[i*2+1],t[i*2+2]) 
     

def qr(a,b):
    a+=N-1
    b+=N-1
    L = -1
    R = -1
    while a < b:
      if a&1==0:
         L = max(L, t[a])
      if b&1==0:
         b-=1
         R = max(t[b], R)
      
      a>>=1
      b>>=1
      
         
    return max(L, R)
 
for i in range(n):
  d.append([i*10,i*4])
d=sorted(d)
dx=[]
dd=[]
for i,j in d:
  dx.append(i)
  dd.append(j)
for i in range(n)[::-1]:
 j=bisect.bisect_left(dx,dx[i]+dd[i])
 tt=qr(i,j)
 ud(i,max(j-1,tt))
dp=[0 for i in range(n+1)]
dp[-1]=1
for i in range(n)[::-1]:
  dp[i]+=dp[i+1]
  dp[i]+=dp[t[i+N-1]+1]
  dp[i]%=mod
print(dp[0])

Submission status: Wrong Answer

Solution code:
import bisect


class SegTreeMax():
    def __init__(self, l):
        self.N = 1
        while self.N < len(l):
            self.N *= 2
        self.L = [0 for _ in range(self.N*2-1)]
        for i in range(len(l)):
            self.L[i+self.N-1] = l[i]
        for i in range(self.N-2, -1, -1):
            self.L[i] = max(self.L[i*2+1], self.L[i*2+2])

    def update(self, k, v):
        k += self.N-1
        self.L[k] = v
        while k > 0 and self.L[(k-1)//2] < v:
            k = (k-1)//2
            self.L[k] = v

    def query(self, a, b):
        return self.inquiry(0, self.N, 0, a, b)

    def inquiry(self, l, r, k, a, b):
        if r <= a or l >= b:
            return 0
        if a <= l and b >= r:
            return self.L[k]
        vl = self.inquiry(l, (l+r)//2, k*2+1, a, b)
        vr = self.inquiry((l+r)//2, r, k*2+2, a, b)
        return max(vl, vr)


MOD = 998244353
N = int(input())
R = [list(map(int, input().split())) for _ in range(N)]
R.sort(key=lambda x: x[0])
D = [R[i][0] for i in range(N)]
A = [i for i in range(N)]
for i in range(N-1, -1, -1):
    A[i] = bisect.bisect_left(D, R[i][0]+R[i][1]) - 1
S = SegTreeMax(A)

dp = [[0 for j in range(2)] for i in range(N)]
dp[N-1][0] = 1
dp[N-1][1] = 1
for i in range(N-1, 0, -1):
    dp[i-1][0] = (dp[i][0] + dp[i][1]) % MOD
    if A[i-1] != i-1:
        des = S.query(i-1, A[i-1]+1)
        dp[i-1][1] = (dp[des][1])% MOD
    else:
        dp[i-1][1] = (dp[i][0] + dp[i][1]) % MOD
print((dp[0][0]+dp[0][1]) % MOD)

Submission status: Wrong Answer

Solution code:
import bisect


class SegTreeMax():
    def __init__(self, l):
        self.N = 1
        while self.N < len(l):
            self.N *= 2
        self.L = [0 for _ in range(self.N*2-1)]
        for i in range(len(l)):
            self.L[i+self.N-1] = l[i]
        for i in range(self.N-2, -1, -1):
            self.L[i] = max(self.L[i*2+1], self.L[i*2+2])

    def update(self, k, v):
        k += self.N-1
        self.L[k] = v
        while k > 0 and self.L[(k-1)//2] < v:
            k = (k-1)//2
            self.L[k] = v

    def query(self, a, b):
        return self.inquiry(0, self.N, 0, a, b)

    def inquiry(self, l, r, k, a, b):
        if r <= a or l >= b:
            return 0
        if a <= l and b >= r:
            return self.L[k]
        vl = self.inquiry(l, (l+r)//2, k*2+1, a, b)
        vr = self.inquiry((l+r)//2, r, k*2+2, a, b)
        return max(vl, vr)


MOD = 998244353
N = int(input())
R = [list(map(int, input().split())) for _ in range(N)]
R.sort(key=lambda x: x[0])
D = [R[i][0] for i in range(N)]
A = [i for i in range(N)]
for i in range(N-1, -1, -1):
    A[i] = bisect.bisect_left(D, R[i][0]+R[i][1]) - 1
S = SegTreeMax(A)

dp = [[0 for j in range(2)] for i in range(N+1)]
dp[N-1][0] = 1
dp[N-1][1] = 1
dp[N][1] = 1
for i in range(N-1, 0, -1):
    dp[i-1][0] = (dp[i][0] + dp[i][1]) % MOD
    if A[i-1] != i-1:
        des = S.query(i-1, A[i-1]+1)
        dp[i-1][1] = dp[des+1][1]
    else:
        dp[i-1][1] = (dp[i][0] + dp[i][1]) % MOD
print((dp[0][0]+dp[0][1]) % MOD)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
import bisect
mod=998244353
n=i1()
d=[]
N=1<<(n.bit_length())
t=[-1 for i in range(2*N-1)]

def ud(i,x):
  i+=N-1
  t[i]=x
  while i:
    i=(i-1)//2
    t[i]=max(t[i*2+1],t[i*2+2]) 
     

def qr(a,b):
    a+=N-1
    b+=N-1
    L = -1
    R = -1
    while a < b:
      L = max(L, t[a])
      R = max(t[b], R)
      
      a>>=1
      b>>=1
      b-=1
         
    return max(L, R)
 
for i in range(n):
  d.append(i2())
d=sorted(d)
dx=[]
dd=[]
for i,j in d:
  dx.append(i)
  dd.append(j)

for i in range(n)[::-1]:
 j=bisect.bisect_left(dx,dx[i]+dd[i])
 tt=qr(i,j-1)
 ud(i,max(i,tt))
dp=[0 for i in range(n+1)]
dp[-1]=1
for i in range(n)[::-1]:
  dp[i]+=dp[i+1]
  dp[i]+=dp[t[i+N-1]+1]
  dp[i]%=mod
print(dp[0])

Submission status: Wrong Answer

Solution code:
import bisect


class SegTreeMax():
    def __init__(self, l):
        self.N = 1
        while self.N < len(l):
            self.N *= 2
        self.L = [0 for _ in range(self.N*2-1)]
        for i in range(len(l)):
            self.L[i+self.N-1] = l[i]
        for i in range(self.N-2, -1, -1):
            self.L[i] = max(self.L[i*2+1], self.L[i*2+2])

    def update(self, k, v):
        k += self.N-1
        self.L[k] = v
        while k > 0 and self.L[(k-1)//2] < v:
            k = (k-1)//2
            self.L[k] = v

    def query(self, a, b):
        return self.inquiry(0, self.N, 0, a, b)

    def inquiry(self, l, r, k, a, b):
        if r <= a or l >= b:
            return 0
        if a <= l and b >= r:
            return self.L[k]
        vl = self.inquiry(l, (l+r)//2, k*2+1, a, b)
        vr = self.inquiry((l+r)//2, r, k*2+2, a, b)
        return max(vl, vr)


MOD = 998244353
N = int(input())
R = [list(map(int, input().split())) for _ in range(N)]
R.sort(key=lambda x: x[0])
D = [R[i][0] for i in range(N)]
A = [i for i in range(N)]
for i in range(N-1, -1, -1):
    A[i] = bisect.bisect_left(D, R[i][0]+R[i][1]) - 1
S = SegTreeMax(A)

dp = [[0 for j in range(2)] for i in range(N)]
dp[N-1][0] = 1
dp[N-1][1] = 1
for i in range(N-1, 0, -1):
    dp[i-1][0] = (dp[i][0] + dp[i][1]) % MOD
    if A[i-1] != i-1:
        des = S.query(i-1, A[i-1]+1)
        dp[i-1][1] = dp[des][1]
    else:
        dp[i-1][1] = (dp[i][0] + dp[i][1]) % MOD
print((dp[0][0]+dp[0][1]) % MOD)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
import bisect
mod=998244353
n=i1()
d=[]
N=1<<(n.bit_length())
t=[-1 for i in range(2*N-1)]

def ud(i,x):
  i+=N-1
  t[i]=x
  while i:
    i=(i-1)//2
    t[i]=max(t[i*2+1],t[i*2+2]) 
     

def qr(a,b):
    a+=N-1
    b+=N-1
    L = -1
    R = -1
    while a < b:
      L = max(L, t[a])
      R = max(t[b], R)
      if b%2==0:
         b-=1
      a//=2
      b//=2
      
         
    return max(L, R)
 
for i in range(n):
  d.append(i2())
d=sorted(d)
dx=[]
dd=[]
for i,j in d:
  dx.append(i)
  dd.append(j)

for i in range(n)[::-1]:
 j=bisect.bisect_left(dx,dx[i]+dd[i])
 tt=qr(i,j-1)
 ud(i,max(i,tt))
dp=[0 for i in range(n+1)]
dp[-1]=1
for i in range(n)[::-1]:
  dp[i]+=dp[i+1]
  dp[i]+=dp[t[i+N-1]+1]
  dp[i]%=mod
print(dp[0])

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
import bisect
import sys
sys.setrecursionlimit(10**6)
mod=998244353
n=i1()
d=[]
N=1<<(n.bit_length())
t=[-1 for i in range(2*N-1)]

def ud(i,x):
  i+=N-1
  t[i]=x
  while i:
    i=(i-1)//2
    t[i]=max(t[i*2+1],t[i*2+2]) 
    
  
 
def qr(a,b):
    a+=N-1
    b+=N-1
    L = -1
    R = -1
    while a < b:
      L = max(L, t[a])
      R = max(t[b], R)
      if a%2==0:
         a+=1
      if b%2==0:
         b-=1
      a//=2
      b//=2
    return max(L,R)
for i in range(n):
  d.append(i2())
d=sorted(d)
dx=[]
dd=[]
for i,j in d:
  dx.append(i)
  dd.append(j)

for i in range(n)[::-1]:
 j=bisect.bisect_left(dx,dx[i]+dd[i])
 tt=qr(i,j-1)
 ud(i,max(i,tt))
dp=[0 for i in range(n+1)]
dp[-1]=1
for i in range(n)[::-1]:
  dp[i]+=dp[i+1]
  dp[i]%=mod
  dp[i]+=dp[t[i+N-1]+1]
  dp[i]%=mod
print(dp[0])

Submission status: Wrong Answer

Solution code:
import sys
import bisect
from collections import deque
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


N = int(readline())
MOD = 998244353

XD = [[int(x) for x in readline().split()] for _ in range(N)]
XD.sort()
#X = [x for x, _ in XD]

NXT = deque()
E = [[] for _ in range(N)]

for i in range(N - 1, -1, -1):
    x, d = XD[i]

    while NXT:
        j, nx = NXT.pop()

        if nx < x + d:
            E[i].append(j)
        else:
            NXT.append((j, nx))
            break

    NXT.append((i, x))


def dfs(i):
    rtn = 1
    for v in E[i]:
        rtn *= dfs(v)
    return rtn + 1


ans = 1
for i, x in NXT:
    ans *= dfs(i)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


class SegmentTree:
    """
    引数：
        N: 処理する区間の長さ
        operator: モノイド演算 (max, min, __add__,ラムダ式,関数定義など)
        UNIT: 単位元
        a: 長さNの配列 a で初期化（a を与えない場合、単位元で初期化）
    注意: 内部的には 1-indexed
    """

    def __init__(self, N, operator, unit, a=None):
        self.op = operator
        self.UNIT = unit

        self.N0 = 2**(N-1).bit_length()
        self.tree = [self.UNIT]*(2*self.N0)
        if a != None:
            self.tree[self.N0:self.N0+N] = a[:]
            for k in range(self.N0-1, 0, -1):
                self.tree[k] = self.op(self.tree[2*k], self.tree[2*k+1])

    # a_k の値を x に更新
    def update(self, k, x):
        k += self.N0
        self.tree[k] = x
        k //= 2
        while k:
            self.tree[k] = self.op(self.tree[2*k], self.tree[2*k+1])
            k //= 2

    # 区間[l,r)をopでまとめる
    def query(self, l, r):
        L = l + self.N0
        R = r + self.N0
        s = self.UNIT
        while L < R:
            if R & 1:
                R -= 1
                s = self.op(self.tree[R], s)
            if L & 1:
                s = self.op(s, self.tree[L])
                L += 1
            L >>= 1
            R >>= 1
        return s

    def get(self, k):  # k番目の値を取得。query[k,k]と同じ
        return self.tree[k + self.N0]

    def all(self):
        return self.tree[1]


N = int(readline())
MOD = 998244353

XD = [[int(x) for x in readline().split()] for _ in range(N)]
XD.sort()
X = [x for x, _ in XD]

B = [bisect.bisect_left(X, x + d) for x, d in XD] + [0]
seg = SegmentTree(N+1, max, 0, B)
#seg.update(N, N)

# print(X)
# print(B)
CNT = [0] * (N)
for i in range(N-1, -1, -1):
    m = seg.query(i, B[i])
    c = max(B[i], m)
    CNT[i] = c - i
    seg.update(i, c)


# print(CNT)
print(0)
exit()

s = 0
for c in CNT:
    m = pow(2, c - 1, MOD) - 1
    m %= MOD
    s += m
    s %= MOD

al = pow(2, N, MOD)
ans = al - s
ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


class SegmentTree:
    """
    引数：
        N: 処理する区間の長さ
        operator: モノイド演算 (max, min, __add__,ラムダ式,関数定義など)
        UNIT: 単位元
        a: 長さNの配列 a で初期化（a を与えない場合、単位元で初期化）
    注意: 内部的には 1-indexed
    """

    def __init__(self, N, operator, unit, a=None):
        self.op = operator
        self.UNIT = unit

        self.N0 = 2**(N-1).bit_length()
        self.tree = [self.UNIT]*(2*self.N0)
        if a != None:
            self.tree[self.N0:self.N0+N] = a[:]
            for k in range(self.N0-1, 0, -1):
                self.tree[k] = self.op(self.tree[2*k], self.tree[2*k+1])

    # a_k の値を x に更新
    def update(self, k, x):
        k += self.N0
        self.tree[k] = x
        k //= 2
        while k:
            self.tree[k] = self.op(self.tree[2*k], self.tree[2*k+1])
            k //= 2

    # 区間[l,r)をopでまとめる
    def query(self, l, r):
        L = l + self.N0
        R = r + self.N0
        s = self.UNIT
        while L < R:
            if R & 1:
                R -= 1
                s = self.op(self.tree[R], s)
            if L & 1:
                s = self.op(s, self.tree[L])
                L += 1
            L >>= 1
            R >>= 1
        return s

    def get(self, k):  # k番目の値を取得。query[k,k]と同じ
        return self.tree[k + self.N0]

    def all(self):
        return self.tree[1]


N = int(readline())
MOD = 998244353

XD = [[int(x) for x in readline().split()] for _ in range(N)]
XD.sort()
X = [x for x, _ in XD]

B = [bisect.bisect_left(X, x + d) for x, d in XD] + [0]
seg = SegmentTree(N+1, max, 0, B)
#seg.update(N, N)

# print(X)
# print(B)
CNT = [0] * (N)
for i in range(N-1, -1, -1):
    m = seg.query(i, B[i])
    c = max(B[i], m)
    CNT[i] = c - i
    seg.update(i, c)


# print(CNT)

s = 0
for c in CNT:
    m = pow(2, c - 1, MOD) - 1
    m %= MOD
    s += m
    s %= MOD

al = pow(2, N, MOD)
ans = al - s
ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

mod = 998244353

n = int(input())
robot = [tuple(map(int, input().split())) for _ in range(n)]
robot.sort(reverse=True)
parent = list(range(n))

root = []
for i in range(n):
  x, d = robot[i]
  while root:
    r = heapq.heappop(root)
    if r[0] < x + d:
      parent[r[1]] = i
    else:
      heapq.heappush(root, r)
      break
  heapq.heappush(root, (x, i))
  
count = 1
dp = [1] * n
for i in range(n):
  p = parent[i]
  if p == i:
    count = count * dp[i] % mod
  else:
    dp[p] = dp[p] * (dp[i] + 1) % mod
print(count)

Submission status: Wrong Answer

Solution code:
class SegTree:
    def __init__(self, init_val, ide_ele, seg_func):
        self.segfunc = seg_func
        n = len(init_val)
        self.num = 2**(n-1).bit_length()
        self.ide_ele = ide_ele
        self.seg=[self.ide_ele]*2*self.num
        for i in range(n):
            self.seg[i+self.num-1]=init_val[i]    
        for i in range(self.num-2,-1,-1) :
            self.seg[i]=self.segfunc(self.seg[2*i+1],self.seg[2*i+2]) 
        
    def update(self, k, x):
        k += self.num-1
        self.seg[k] = x
        # k+1ではなくkでは？
        while k+1:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[k*2+1],self.seg[k*2+2])
        
    def query(self, p, q):
        if q<=p:
            return self.ide_ele
        p += self.num-1
        q += self.num-2
        res=self.ide_ele
        while q-p>1:
            if p&1 == 0:
                res = self.segfunc(res,self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res
import sys;input=sys.stdin.readline
N=int(input())
X = []
Y = []
for _ in range(N):
    x, d = map(int, input().split())
    X.append((x,d))
X.sort(key=lambda x:x[0])
for i, (x, d) in enumerate(X):
    Y.append((2*x,i,0))
    Y.append((2*x+2*d-1,i,1))
Y.sort(key=lambda x:x[0])
C = [0]*N
for x, i, t in Y:
    if t==0:
        j = i
    elif t==1:
        C[i] = j
st = SegTree(C, -1, max)
dp1 = [0]*(N+1)
dp2 = [1]*(N+1)
for i in range(N-1, -1, -1):
    x, d = X[i]
    y=st.query(i, C[i]+1)
    st.update(i, y)
    dp1[i] = dp1[i+1]+dp2[i+1]
    dp2[i] = dp1[y+1]+dp2[y+1]
print(dp1[0]+dp2[0])

Submission status: Wrong Answer

Solution code:
import bisect
n=int(input())
XD=[]
for _ in range(n):
    x,d=map(int,input().split())
    XD.append([x,d])
XD.sort()
X=[]
D=[]
for i in range(n):
    X.append(XD[i][0])
    D.append(XD[i][1])
l=[]
for i in range(n):
    l.append(bisect.bisect_left(X,X[i]+D[i])-1)
l_=[0]*n
for i in range(n):
    l[n-i-1]=max([l[j] for j in range(n-i-1,l[n-i-1]+1)])
dp=[0]*(n+1)
dp[n]=1
for i in range(n):
    dp[n-i-1]=dp[n-i]+dp[l[n-i-1]+1]
print(dp[0])

Submission status: Wrong Answer

Solution code:


import glob
import numpy as np

# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = 'ABC\\158\\F'

# テスト用ファイル置き場のトップ
TOP_PATH = 'C:\\AtCoder'

class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class Solver(Common):

    def resolve(self):

        N = int(self.input_data())
        robots = []
        for j in range(N):
            robots.append([int(i) for i in self.input_data().split()])

        robots.sort()

        result = [[0,0] for i in range(N)]

        for i in range(N-1, -1, -1):
            if i == N-1:
                result[i][0] = 1
                result[i][1] = 1
                continue

            result[i][0] = result[i+1][0] + result[i+1][1]
            if robots[i][0] + robots[i][1] <= robots[i+1][0]:
                result[i][1] = result[i+1][0] + result[i+1][1]
            else:
                for j in range(i+1,N):
                    if robots[i][0] + robots[i][1] > robots[j][0]:
                        result[i][1] = result[j][1]
                    else:
                        break

        print(str(result[0][0] + result[0][1]))

solver = Solver(REL_PATH)
solver.exec_resolve()