import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import java.util.StringTokenizer;

import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Math.*;
import static java.lang.System.in;
import static java.lang.System.out;

import java.util.*;

public class Main {
    static FastScanner in = new FastScanner();
    static PrintWriter out = new PrintWriter(System.out);

    static class Segment_Tree {
        int[] sgt;
        int[] ar;
        int N;

        Segment_Tree(int n, int[] a) {
            sgt = new int[1500005];
            ar = new int[n];
            Arrays.fill(sgt, 0);
            N = n;
            for (int i = 0; i < a.length; ++i) {
                ar[i] = a[i];
            }
        }

        void build(int v, int lt, int rt) {
            if (lt == rt) {
                sgt[v] = ar[lt];
                return;
            }
            int mid = (lt + rt) / 2;
            build(2 * v, lt, mid);
            build((2 * v) + 1, mid + 1, rt);
            sgt[v] = max(sgt[2 * v], sgt[(2 * v) + 1]);
        }

        void update(int v, int lt, int rt, int pos, int value) {
            if (lt == rt) {
                sgt[v] = value;
                return;
            }
            int mid = (lt + rt) / 2;
            if (pos > mid) update((2 * v) + 1, mid + 1, rt, pos, value);
            else update((2 * v), lt, mid, pos, value);
            sgt[v] = max(sgt[2 * v], sgt[(2 * v) + 1]);
        }

        int query(int v, int lt, int rt, int lq, int rq) {
            //out.println(lq + " " + rq);
            if (lq > rq) {
                return 0;
            }
            if (lt == lq && rt == rq) {
                return sgt[v];
            }
            int mid = (lt + rt) / 2;
            return max(query(2 * v, lt, mid, lq, min(rq, mid)), query((2 * v) + 1, mid + 1, rt, max(mid + 1, lq), rq));
        }
    }

    public static void main(String[] args) {
        int n = in.nextInt(), k = in.nextInt();
        int[] a = ria(n);
        Segment_Tree sgt = new Segment_Tree(n, a);
        //out.println(sgt.query(1, 0, n - 1, 2, 5));
        //sgt.update(1, 0, n-1, 4, 2);
        //out.println(sgt.query(1, 0, n - 1, 4, 4));
        a = reverse(a);
        int ans = 1;
        for (int i = 0; i < n; ++i) {
            int x = a[i];
            int left = max(0, x - k);
            int right = min(700000, x + k);
            //out.println("The left and right limits are : " + left + " " + right);
            int max_value = sgt.query(1, 0, 700000, left, right);
            ans = max(ans, max_value + 1);
            sgt.update(1, 0, 700000, x, ans);
            //out.println("The answer right now : " + ans);
        }
        out.println(ans);
        out.flush();
        out.close();
    }

    static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        String next() {
            while (!st.hasMoreTokens())
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return parseInt(next());
        }

        int[] readArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        long nextLong() {
            return parseLong(next());
        }

        double nextDouble() {
            return parseDouble(next());
        }
    }

    static final int IBIG = 1000000007;
    static final int IMAX = 2147483647;
    static final int IMIN = -2147483648;
    static final long LMAX = 9223372036854775807L;
    static final long LMIN = -9223372036854775808L;

    static int ri() {
        return in.nextInt();
    }

    static double rd() {
        return in.nextDouble();
    }

    static long rl() {
        return in.nextLong();
    }

    static String rs() {
        return in.next();
    }

    static double[] rda(int n) {
        double[] a = new double[n];
        for (int i = 0; i < n; ++i) a[i] = in.nextDouble();
        return a;
    }

    static int[] ria(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; ++i) a[i] = in.nextInt();
        return a;
    }

    static long[] rla(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; ++i) a[i] = in.nextLong();
        return a;
    }

    static char[] rca() {
        return in.next().toCharArray();
    }

    static long powmod(long a, long b, long mod) {
        long res = 1;
        while (b > 0) {
            if ((b & 1) != 0) res *= a;
            a *= a;
            res %= mod;
            a %= mod;
            b = b >> 2;
        }
        return res;
    }

    static long powmod(int a, int b, long mod) {
        long res = 1;
        long a_copy = a;
        long b_copy = b;
        while (b_copy > 0) {
            if ((b_copy & 1) != 0) res *= a_copy;
            a_copy *= a_copy;
            res %= mod;
            a_copy %= mod;
            b_copy = b_copy >> 2;
        }
        return res;
    }

    static void pr(int n) {
        out.print(n);
    }

    static void pr(long n) {
        out.print(n);
    }

    static void pr(String n) {
        out.print(n);
    }

    static void pr(char n) {
        out.print(n);
    }

    static void pr(char[] a) {
        out.print(a.toString());
    }

    static void prln(int n) {
        out.println(n);
    }

    static void prln(long n) {
        out.println(n);
    }

    static void prln(String n) {
        out.println(n);
    }

    static void prln(char n) {
        out.println(n);
    }

    static void prln(char[] a) {
        out.println(a.toString());
    }

    static int[] copy(int[] a) {
        int[] b = new int[a.length];
        for (int i = 0; i < a.length; ++i) {
            b[i] = a[i];
        }
        return b;
    }

    static long[] copy(long[] a) {
        long[] b = new long[a.length];
        for (int i = 0; i < a.length; ++i) {
            b[i] = a[i];
        }
        return b;
    }

    static char[] copy(char[] a) {
        char[] b = new char[a.length];
        for (int i = 0; i < a.length; ++i) {
            b[i] = a[i];
        }
        return b;
    }

    static int[] reverse(int[] a) {
        int[] b = new int[a.length];
        for (int i = 0; i < a.length; ++i) {
            b[a.length - 1 - i] = a[i];
        }
        return b;
    }

    static long[] reverse(long[] a) {
        long[] b = new long[a.length];
        for (int i = 0; i < a.length; ++i) {
            b[a.length - 1 - i] = a[i];
        }
        return b;
    }

    static char[] reverse(char[] a) {
        char[] b = new char[a.length];
        for (int i = 0; i < a.length; ++i) {
            b[a.length - 1 - i] = a[i];
        }
        return b;
    }
}

Score : 400 points

Problem Statement

You are given a sequence A_1, A_2, ..., A_N and an integer K.

Print the maximum possible length of a sequence B that satisfies the following conditions:

B is a (not necessarily continuous) subsequence of A.

For each pair of adjacents elements of B, the absolute difference of the elements is at most K.

Constraints

1 \leq N \leq 300,000

0 \leq A_i \leq 300,000

0 \leq K \leq 300,000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1
A_2
:
A_N

Output

Print the answer.

Sample Input 1

10 3
1
5
4
3
8
6
9
7
2
4

Sample Output 1

7

For example, B = (1, 4, 3, 6, 9, 7, 4) satisfies the conditions.

It is a subsequence of A = (1, 5, 4, 3, 8, 6, 9, 7, 2, 4).

All of the absolute differences between two adjacent elements (|1-4|, |4-3|, |3-6|, |6-9|, |9-7|, |7-4|) are at most K = 3.


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;

public class Main {
	static final long MOD1=1000000007;
	static final long MOD2=998244353;
	public static void main(String[] args) {
		PrintWriter out = new PrintWriter(System.out);
		InputReader sc=new InputReader(System.in);
		int N=sc.nextInt();
		int Q=sc.nextInt();
		S[] dat=new S[N];
		Arrays.setAll(dat, i->new S(modPow(10L, N-i-1),i,i));
		LazySegTree<S, F> lazySegTree=new LazySegTree<S, F>(dat, S::op, S.E,S::map,F::composite,F.I);
		for (int j = 0; j < Q; j++) {
			int l=sc.nextInt()-1;
			int r=sc.nextInt()-1;
			long D=sc.nextLong();
			lazySegTree.apply(l,r+1,new F(D,N));
			out.println(lazySegTree.allProd().sum);
		}
		out.flush();
	}
	//LazySegTree<S, F> lazySegTree=new LazySegTree<S, F>(dat, S::op, S.E,S::map,F::composite,F.I);
	//f(a*b)!=f(a)*f(b)の場合Sを集合その作用素をmergeとみなして集合の各要素に対して作用するものをfとすればf(a*b)=f(a)*f(b)となる。
	//その場合Sには必要な情報だけを持たせる（和だったらsumと要素数があれば十分）
	static class S {
	        static final S E = new S(0,Long.MAX_VALUE/2,Long.MIN_VALUE/2);
	        long sum;
	        long l;
	        long r;
	        public S (long sum,long l,long r) {
	            super();
	            this.sum = sum;
	            this.l=l;
	            this.r=r;
	        }
	        public static S op(S s1, S s2) {
	            return new S((s1.sum+s2.sum)%MOD2,Math.min(s1.l, s2.l),Math.max(s1.r, s2.r));
	        }
	        static S map(F f,  S s) {
	            return new S(((f.D*((modPow(10,f.N-s.l)-modPow(10, f.N-s.r-1)+MOD2)%MOD2))%MOD2*modInv(9))%MOD2, s.l,s.r);
	        }
	    }
	static class F {
	        static final F I = new F(0,0);
	        long D;
	        long N;
	        public F (long D,long N) {
	            super();
	            this.D = D;
	            this.N= N;
	        }
	        public static F composite(F f, F g) {
	            return new F(f.D,f.N);
	        }
	    }
	static long modInv(long x) {
        return modPow(x, MOD2 - 2);
    }
	static long modPow(long x, long y) {
        long z = 1;
        while (y > 0) {
            if (y % 2 == 0) {
                x = (x * x) % MOD2;
                y /= 2;
            } else {
                z = (z * x) % MOD2;
                y--;
            }
        }
        return z;
    }
	static class LazySegTree<S, F> {
	    final int MAX;

	    final int N;
	    final int Log;
	    final java.util.function.BinaryOperator<S> Op;
	    final S E;
	    final java.util.function.BiFunction<F, S, S> Mapping;
	    final java.util.function.BinaryOperator<F> Composition;
	    final F Id;

	    final S[] Dat;
	    final F[] Laz;

	    @SuppressWarnings("unchecked")
	    public LazySegTree(int n, java.util.function.BinaryOperator<S> op, S e, java.util.function.BiFunction<F, S, S> mapping, java.util.function.BinaryOperator<F> composition, F id) {
	        this.MAX = n;
	        int k = 1;
	        while (k < n) k <<= 1;
	        this.N = k;
	        this.Log = Integer.numberOfTrailingZeros(N);
	        this.Op = op;
	        this.E = e;
	        this.Mapping = mapping;
	        this.Composition = composition;
	        this.Id = id;
	        this.Dat = (S[]) new Object[N << 1];
	        this.Laz = (F[]) new Object[N];
	        java.util.Arrays.fill(Dat, E);
	        java.util.Arrays.fill(Laz, Id);
	    }

	    public LazySegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e, java.util.function.BiFunction<F, S, S> mapping, java.util.function.BinaryOperator<F> composition, F id) {
	        this(dat.length, op, e, mapping, composition, id);
	        build(dat);
	    }

	    private void build(S[] dat) {
	        int l = dat.length;
	        System.arraycopy(dat, 0, Dat, N, l);
	        for (int i = N - 1; i > 0; i--) {
	            Dat[i] = Op.apply(Dat[i << 1 | 0], Dat[i << 1 | 1]);
	        }
	    }

	    private void push(int k) {
	        if (Laz[k] == Id) return;
	        int lk = k << 1 | 0, rk = k << 1 | 1;
	        Dat[lk] = Mapping.apply(Laz[k], Dat[lk]);
	        Dat[rk] = Mapping.apply(Laz[k], Dat[rk]);
	        if (lk < N) Laz[lk] = Composition.apply(Laz[k], Laz[lk]);
	        if (rk < N) Laz[rk] = Composition.apply(Laz[k], Laz[rk]);
	        Laz[k] = Id;
	    }

	    private void pushTo(int k) {
	        for (int i = Log; i > 0; i--) push(k >> i);
	    }

	    private void pushTo(int lk, int rk) {
	        for (int i = Log; i > 0; i--) {
	            if (((lk >> i) << i) != lk) push(lk >> i);
	            if (((rk >> i) << i) != rk) push(rk >> i);
	        }
	    }

	    private void updateFrom(int k) {
	        k >>= 1;
	        while (k > 0) {
	            Dat[k] = Op.apply(Dat[k << 1 | 0], Dat[k << 1 | 1]);
	            k >>= 1;
	        }
	    }

	    private void updateFrom(int lk, int rk) {
	        for (int i = 1; i <= Log; i++) {
	            if (((lk >> i) << i) != lk) {
	                int lki = lk >> i;
	                Dat[lki] = Op.apply(Dat[lki << 1 | 0], Dat[lki << 1 | 1]);
	            }
	            if (((rk >> i) << i) != rk) {
	                int rki = (rk - 1) >> i;
	                Dat[rki] = Op.apply(Dat[rki << 1 | 0], Dat[rki << 1 | 1]);
	            }
	        }
	    }

	    public void set(int p, S x) {
	        exclusiveRangeCheck(p);
	        p += N;
	        pushTo(p);
	        Dat[p] = x;
	        updateFrom(p);
	    }

	    public S get(int p) {
	        exclusiveRangeCheck(p);
	        p += N;
	        pushTo(p);
	        return Dat[p];
	    }

	    public S prod(int l, int r) {
	        if (l > r) {
	            throw new IllegalArgumentException(
	                String.format("Invalid range: [%d, %d)", l, r)
	            );
	        }
	        inclusiveRangeCheck(l);
	        inclusiveRangeCheck(r);
	        if (l == r) return E;
	        l += N; r += N;
	        pushTo(l, r);
	        S sumLeft = E, sumRight = E;
	        while (l < r) {
	            if ((l & 1) == 1) sumLeft = Op.apply(sumLeft, Dat[l++]);
	            if ((r & 1) == 1) sumRight = Op.apply(Dat[--r], sumRight);
	            l >>= 1; r >>= 1;
	        }
	        return Op.apply(sumLeft, sumRight);
	    }

	    public S allProd() {
	        return Dat[1];
	    }

	    public void apply(int p, F f) {
	        exclusiveRangeCheck(p);
	        p += N;
	        pushTo(p);
	        Dat[p] = Mapping.apply(f, Dat[p]);
	        updateFrom(p);
	    }

	    public void apply(int l, int r, F f) {
	        if (l > r) {
	            throw new IllegalArgumentException(
	                String.format("Invalid range: [%d, %d)", l, r)
	            );
	        }
	        inclusiveRangeCheck(l);
	        inclusiveRangeCheck(r);
	        if (l == r) return;
	        l += N; r += N;
	        pushTo(l, r);
	        for (int l2 = l, r2 = r; l2 < r2;) {
	            if ((l2 & 1) == 1) {
	                Dat[l2] = Mapping.apply(f, Dat[l2]);
	                if (l2 < N) Laz[l2] = Composition.apply(f, Laz[l2]);
	                l2++;
	            }
	            if ((r2 & 1) == 1) {
	                r2--;
	                Dat[r2] = Mapping.apply(f, Dat[r2]);
	                if (r2 < N) Laz[r2] = Composition.apply(f, Laz[r2]);
	            }
	            l2 >>= 1; r2 >>= 1;
	        }
	        updateFrom(l, r);
	    }

	    public int maxRight(int l, java.util.function.Predicate<S> g) {
	        inclusiveRangeCheck(l);
	        if (!g.test(E)) {
	            throw new IllegalArgumentException("Identity element must satisfy the condition.");
	        }
	        if (l == MAX) return MAX;
	        l += N;
	        pushTo(l);
	        S sum = E;
	        do {
	            l >>= Integer.numberOfTrailingZeros(l);
	            if (!g.test(Op.apply(sum, Dat[l]))) {
	                while (l < N) {
	                    push(l);
	                    l = l << 1;
	                    if (g.test(Op.apply(sum, Dat[l]))) {
	                        sum = Op.apply(sum, Dat[l]);
	                        l++;
	                    }
	                }
	                return l - N;
	            }
	            sum = Op.apply(sum, Dat[l]);
	            l++;
	        } while ((l & -l) != l);
	        return MAX;
	    }

	    public int minLeft(int r, java.util.function.Predicate<S> g) {
	        inclusiveRangeCheck(r);
	        if (!g.test(E)) {
	            throw new IllegalArgumentException("Identity element must satisfy the condition.");
	        }
	        if (r == 0) return 0;
	        r += N;
	        pushTo(r - 1);
	        S sum = E;
	        do {
	            r--;
	            while (r > 1 && (r & 1) == 1) r >>= 1;
	            if (!g.test(Op.apply(Dat[r], sum))) {
	                while (r < N) {
	                    push(r);
	                    r = r << 1 | 1;
	                    if (g.test(Op.apply(Dat[r], sum))) {
	                        sum = Op.apply(Dat[r], sum);
	                        r--;
	                    }
	                }
	                return r + 1 - N;
	            }
	            sum = Op.apply(Dat[r], sum);
	        } while ((r & -r) != r);
	        return 0;
	    }

	    private void exclusiveRangeCheck(int p) {
	        if (p < 0 || p >= MAX) {
	            throw new IndexOutOfBoundsException(
	                String.format("Index %d is not in [%d, %d).", p, 0, MAX)
	            );
	        }
	    }

	    private void inclusiveRangeCheck(int p) {
	        if (p < 0 || p > MAX) {
	            throw new IndexOutOfBoundsException(
	                String.format("Index %d is not in [%d, %d].", p, 0, MAX)
	            );
	        }
	    }

	    // **************** DEBUG **************** //

	    private int indent = 6;

	    public void setIndent(int newIndent) {
	        this.indent = newIndent;
	    }

	    @Override
	    public String toString() {
	        return toString(1, 0);
	    }

	    private String toString(int k, int sp) {
	        if (k >= N) return indent(sp) + Dat[k];
	        String s = "";
	        s += toString(k << 1 | 1, sp + indent);
	        s += "\n";
	        s += indent(sp) + Dat[k] + "/" + Laz[k];
	        s += "\n";
	        s += toString(k << 1 | 0, sp + indent);
	        return s;
	    }

	    private static String indent(int n) {
	        StringBuilder sb = new StringBuilder();
	        while (n --> 0) sb.append(' ');
	        return sb.toString();
	    }
	}
		static class InputReader {
		private InputStream in;
		private byte[] buffer = new byte[1024];
		private int curbuf;
		private int lenbuf;
		public InputReader(InputStream in) {
			this.in = in;
			this.curbuf = this.lenbuf = 0;
		}

		public boolean hasNextByte() {
			if (curbuf >= lenbuf) {
				curbuf = 0;
				try {
					lenbuf = in.read(buffer);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return false;
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[curbuf++];
			else
				return -1;
		}

		private boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		private void skip() {
			while (hasNextByte() && isSpaceChar(buffer[curbuf]))
				curbuf++;
		}

		public boolean hasNext() {
			skip();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (!isSpaceChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public int nextInt() {
			if (!hasNext())
				throw new NoSuchElementException();
			int c = readByte();
			while (isSpaceChar(c))
				c = readByte();
			boolean minus = false;
			if (c == '-') {
				minus = true;
				c = readByte();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res = res * 10 + c - '0';
				c = readByte();
			} while (!isSpaceChar(c));
			return (minus) ? -res : res;
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			int c = readByte();
			while (isSpaceChar(c))
				c = readByte();
			boolean minus = false;
			if (c == '-') {
				minus = true;
				c = readByte();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res = res * 10 + c - '0';
				c = readByte();
			} while (!isSpaceChar(c));
			return (minus) ? -res : res;
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		public long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}

		public char[][] nextCharMap(int n, int m) {
			char[][] map = new char[n][m];
			for (int i = 0; i < n; i++)
				map[i] = next().toCharArray();
			return map;
		}
	}
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 500 points

Problem Statement

You have a string S of length N.
Initially, all characters in S are 1s.

You will perform queries Q times.
In the i-th query, you are given two integers L_i, R_i and a character D_i (which is a digit).
Then, you must replace all characters from the L_i-th to the R_i-th (inclusive) with D_i.

After each query, read the string S as a decimal integer, and print its value modulo 998,244,353.

Constraints

1 \leq N, Q \leq 200,000

1 \leq L_i \leq R_i \leq N

1 \leq D_i \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
L_1 R_1 D_1
:
L_Q R_Q D_Q

Output

Print Q lines.
In the i-th line print the value of S after the i-th query, modulo 998,244,353.

Sample Input 1

8 5
3 6 2
1 4 7
3 8 3
2 2 2
4 5 1

Sample Output 1

11222211
77772211
77333333
72333333
72311333

Sample Input 2

200000 1
123 456 7

Sample Output 2

641437905

Don't forget to take the modulo.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.