Contest 3 problem in "Yet another partition problem"

This code is able to pass Sub-Task 1 but fails in subtask 2. I wrote this code using the official hints but still getting WA and cant find the bug. Please if anyone can help.

#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main()
{
    //INPUT;
    ll n, q, input;
    cin >> n >> q;
    vector<ll> arr;
    set<ll> s;

    for (ll i = 0; i < n; i++)
    {
        cin >> input;
        arr.push_back(input);
    }
    
    s.insert(1);
    for (ll i = 1; i < n; i++)
    {
        if (arr[i] % arr[i - 1] != 0)
            s.insert(i + 1);
    }

    while (q--)
    {
        cin >> input;

        if (input == 1)
        {
            ll num, i;
            cin >> i >> num;
            arr[i - 1] = num;
            s.insert(i);
            s.insert(i + 1);
            if (arr[i] % arr[i - 1] == 0 && i != n)
                s.erase(i + 1);
            else if (arr[i - 1] % arr[i - 2] == 0 && (i - 1) != 0)
                s.erase(i);
        }
        else if (input == 2)
        {
            ll i;
            cin >> i;
            auto it = s.upper_bound(i);
            it--;
            cout
                << *(it) << endl;
        }
    }
}