Source Code
import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        FastScanner in = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt();
        test:
        while (t-- > 0) {
            int n = in.nextInt(), k = in.nextInt();
            int[] arr = new int[n];
            HashMap<Integer, Integer> mp = new HashMap<>(), kmp = new HashMap<>();
            for (int i = 0; i < n; i++) {
                arr[i] = in.nextInt();
                mp.put(arr[i], mp.getOrDefault(arr[i], 0) + 1);
            }
            ArrayList<PairInt> pairs = new ArrayList<>();
            int min = 0;
            boolean wrong = false;
            for (int i = 0; i < k; i++) {
                pairs.add(new PairInt(in.nextInt(), in.nextInt()));
                kmp.put(pairs.get(i).first, kmp.getOrDefault(pairs.get(i).first, 0) + pairs.get(i).second);
                if (min <= pairs.get(i).first) min = pairs.get(i).first;
                else {
                    wrong = true;
                }
                if(mp.getOrDefault(pairs.get(i).first, 0) < kmp.get(pairs.get(i).first))
                    wrong = true;
                if (pairs.get(i).second > mp.get(pairs.get(i).first)) {
                    wrong = true;
                }
                if(pairs.get(pairs.size() - 1).first > pairs.get(i).first) wrong = true;
            }

            if (wrong) {
                out.println("no");
                continue test;
            }
            int l = 0;
            for (int i = 0; i < k; i++) {
                int temp = pairs.get(i).second;
                while (l < n) {
                    if (arr[l] == pairs.get(i).first) {
                        if(temp == 0)
                            break;
                        temp--;
                        mp.put(pairs.get(i).first, mp.getOrDefault(pairs.get(i).first, 1) - 1);
                        kmp.put(pairs.get(i).first, kmp.getOrDefault(pairs.get(i).first, 1) - 1);
                        l++;
                    } else if (arr[l] != pairs.get(i).first && mp.getOrDefault(arr[l], 0) >= kmp.getOrDefault(arr[l], 0)) {
                        if(mp.getOrDefault(arr[l], 0) == kmp.getOrDefault(arr[l], 0) && temp == 0)
                            break;
                        mp.put(arr[l], mp.get(arr[l]) - 1);
                        l++;
                    } else {
                        out.println("no");
                        continue test;
                    }
                }
                if ((l == n && i != k - 1) || (i == k - 1 && l != n)) {
                    out.println("no");
                    continue test;
                }
            }
            out.println("yes");
        }
        out.flush();
    }

    static int dp(int[] a, int h, int l, int r, int i, int value, int sum, int[][] dp) {
        sum = (sum + value) % h;
        if (dp[i][sum] != -1) {
            return dp[i][sum];
        }
        if (sum >= l && sum <= r) {
            try {
                int res = Math.max(
                        dp(a, h, l, r, i + 1, a[i + 1] - 1, sum, dp),
                        dp(a, h, l, r, i + 1, a[i + 1], sum, dp)
                ) + 1;
                dp[i][sum] = res;
                return res;
            } catch (IndexOutOfBoundsException e) {
                dp[i][sum] = 1;
                return 1;
            }
        }
        try {
            int res = Math.max(
                    dp(a, h, l, r, i + 1, a[i + 1] - 1, sum, dp),
                    dp(a, h, l, r, i + 1, a[i + 1], sum, dp)
            );
            dp[i][sum] = res;
            return res;
        } catch (IndexOutOfBoundsException e) {
            dp[i][sum] = 0;
            return 0;
        }
    }
    // 3 24 21 23
    // 12 11 22

    private static void db(int n, long[] cost, ArrayList<Pair<Long, Long>>[] graph) {
        PriorityQueue<Pair<Long, Long>> queue = new PriorityQueue<>();
        queue.add(new Pair<>(0L, (long) n));
        cost[n] = 0;
        while (!queue.isEmpty()) {
            int u = Math.toIntExact(queue.peek().second);
            long cost1 = -queue.peek().first;
            queue.poll();
            if (cost[(u)] < cost1) continue;
            for (int i = 0; i < graph[u].size(); i++) {
                int v = Math.toIntExact(graph[u].get(i).first);
                long cost2 = graph[u].get(i).second;
                if (cost1 + cost2 < cost[(v)]) {
                    cost[v] = cost2 + cost1;
                    queue.add(new Pair<>(-cost[(v)], (long) v));
                }
            }
        }
    }

    static boolean isPowerOfTwo(int n) {
        if (n == 0)
            return false;

        while (n != 1) {
            if (n % 2 != 0)
                return false;
            n = n / 2;
        }
        return true;
    }

    static boolean BRAVE(int x, int m, int n, ArrayList<Integer> arr) {
        long temp = m;
        for (int i = 0; i < n; i++) {
            temp = (long) (temp - Math.ceil(arr.get(i) / (double) x));
            if (temp < 0) {
                return false;
            }
        }
        return true;
    }

    static final long MOD = (int) (Math.pow(10, 9) + 7);

    private static class FastScanner {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        private FastScanner() throws IOException {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        private short nextShort() throws IOException {
            short ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do ret = (short) (ret * 10 + c - '0'); while ((c = read()) >= '0' && c <= '9');
            if (neg) return (short) -ret;
            return ret;
        }

        private int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9');
            if (neg) return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9');
            if (neg) return -ret;
            return ret;
        }

        private char nextChar() throws IOException {
            byte c = read();
            while (c <= ' ') c = read();
            return (char) c;
        }

        private String nextString() throws IOException {
            StringBuilder ret = new StringBuilder();
            byte c = read();
            while (c <= ' ') c = read();
            do {
                ret.append((char) c);
            } while ((c = read()) > ' ');
            return ret.toString();
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }

    }

    static class Pair<K, V> implements Comparable<Pair<K, V>> {

        K first;
        V second;

        Pair(K f, V s) {
            first = f;
            second = s;
        }

        @Override
        public String toString() {
            return "{" + first + ", " + second + "}";
        }

        @Override
        public int compareTo(Pair<K, V> other) {
            // First, compare the Integer values
            int firstComparison = ((Integer) this.first).compareTo((Integer) other.first);

            // If the Integer values are equal, compare the String values
            if (firstComparison == 0) {
                return 1 - ((String) this.second).compareTo((String) other.second);
            } else {
                return firstComparison;
            }
        }

    }

    static class PairInt implements Comparable<PairInt> {

        int first;
        int second;

        PairInt(int f, int s) {
            first = f;
            second = s;
        }

        @Override
        public int compareTo(PairInt o) {
            if (this.first > o.first) {
                return 1;
            } else if (this.first < o.first) return -1;
            else {
                if (this.second < o.second) return 1;
                else if (this.second == o.second) return -1;
                return 0;
            }
        }

        @Override
        public String toString() {
            return "<" + first + ", " + second + ">";
        }
    }

    static boolean sorted(int[] arr, int i, int n) {
        if (n >= arr.length)
            return false;
        for (; i < n; i++) {
            if (arr[i + 1] < arr[i]) return false;
        }
        return true;
    }

    static int sortedone(int[] arr, int i, int n) {
        if (sorted(arr, i, n)) {
            return n - i;
        }
        int x = sortedone(arr, i, n / 2);
//        System.out.println("x = " + x);
        int y = sortedone(arr, n / 2 + 1, n);
//        System.out.println("y = " + y);
        return Math.max(x, y);
    }

    static boolean iisPrime(long k, long n, long[] arr) {
        Map<Long, Long> mp = new HashMap<>();
        for (int i = 0; i < n; i++) {
            long index = arr[i] % k;
            mp.put(index, mp.getOrDefault(index, 0L) + 1);
        }
        for (long x : mp.values()) {
            if (x <= 1) return true;
        }
        return false;
    }

    static boolean isPrime(long n) {
        if (n < 2) return false;
        if (n % 2 == 0 || n % 3 == 0) return n == 3 || n == 2;
        for (long i = 5; i * i <= n; i += 6)
            if (n % i == 0 || n % (i + 2) == 0) return false;
        return true;
    }

    static ArrayList<Integer> printDivisors(int n) {
        ArrayList<Integer> arr = new ArrayList<>();
        for (int i = 1; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                if (n / i == i) arr.add(i);
                else arr.add(i);
                arr.add(n / i);
            }
        }
        return arr;
    }

    static int lower_bound(int[] array, int key) {
        int low = 0, high = array.length;
        int mid;
        while (low < high) {
            mid = low + (high - low) / 2;
            if (key <= array[mid]) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }
        if (low < array.length && array[low] < key) {
            low++;
        }
        return low;
    }

    static int lower_bound(ArrayList<Integer> array, int key) {
        int low = 0, high = array.size();
        int mid;
        while (low < high) {
            mid = low + (high - low) / 2;
            if (key <= array.get(mid)) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }
        if (low < array.size() && array.get(low) < key) {
            low++;
        }
        return low;
    }

    static int lcm(int a, int b) {
        return a / gcd(a, b) * b;
    }

    static int gcd(int a, int b) {
        if (a == 0) {
            return b;
        }
        return gcd(b % a, a);
    }

    //all primes
    static ArrayList<Integer> primes;
    static boolean[] primesB;

    static void sieve(int n) {
        primes = new ArrayList<>();
        primesB = new boolean[n + 1];
        for (int i = 2; i <= n; i++) {
            primesB[i] = true;
        }
        for (int i = 2; i * i <= n; i++) {
            if (primesB[i]) {
                for (int j = i * i; j <= n; j += i) {
                    primesB[j] = false;
                }
            }
        }
        for (int i = 0; i <= n; i++) {
            if (primesB[i]) {
                primes.add(i);
            }
        }
    }

    // Function to find gcd of array of
    // numbers
    static int findGCD(int[] arr, int n) {
        int result = arr[0];
        for (int element : arr) {
            result = gcd(result, element);

            if (result == 1) {
                return 1;
            }
        }
        return result;
    }

    private static int anInt(String s) {
        return Integer.parseInt(s);
    }

    private static long ll(String s) {
        return Long.parseLong(s);
    }

}
Copy
Sorted Array Partitioning Diaa12360
Java 11
846 ms
119.4 MB
Wrong Answer