Yet Another Partition Problem

wrong answer on test case 4.
codechef.com/viewsolution/33937833

Try simplify your code. Here is my solution.

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
	// your code goes here
        int n,q;
        cin >> n >> q;
        int a[n+1]; a[0] = INT_MAX;
        for(int i=1; i<=n; i++)
            cin >> a[i];
        set <int> mp;
        mp.insert(1);
        for(int i=2; i<=n; i++)
        {
            if(a[i]%a[i-1] != 0)
                mp.insert(i);
        }

        for(int i=1; i<=q; i++)
        {
            int x;
            cin >> x;
            if(x==1)
            {
                int ind, val;
                cin >> ind >> val;
                a[ind] = val;
                if(a[ind]%a[ind-1] != 0 && ind != 1)
                    mp.insert(ind);
                else
                    if(ind != 1)
                        mp.erase(ind);
                if(a[ind+1]%a[ind] != 0)
                    mp.insert(ind+1);
                else
                    mp.erase(ind+1);
                /*for(auto x : mp)
                    cout<<x<<" ";
                cout<<endl;*/
            }
            else
            {
                int ind;
                cin >> ind;
                set <int> :: iterator itr;
                itr = mp.upper_bound(ind);
                itr--;
                cout<<*itr<<endl;
            }
        }

        return 0;
}