Little Sub has a sequence . Now he has a problem for you.

Two sequences are considered isomorphic when they meet all the following two conditions:

  1. ;
  2. Define always holds.

Now we have . and there are two kinds of operations:

  • 1 x y: Change );
  • 2: Query for the greatest

Input

There are multiple test cases. The first line of the input contains an integer ), indicating the number of test cases. For each test case:

The first line ontains two integers .

The second line contains ).

In the following lines, each line contains one operation. The format is described above.

Output

For each operation 2, output one line containing the answer.

Sample Input
1
3 5
1 2 3
2
1 3 2
2
1 1 2
2
Sample Output
-1
1
2

题意:给定你个数组,以及一些单点修改,以及询问,每次询问需要求得,最长的字串长度,它在其他位置存在同构。

思路:最长同构子串对总是会重叠的,然后两个子串不重叠的部分必须是同构。 那么一定有,最优之一就是“x+公共”与“公共+x”这两个同构最长。

这个不难反证。 那么实现就是每种数离散后建立set,然后保存每个set的最远距离即可。

#include<bits/stdc++.h>
#define rep(i,w,v) for(int i=w;i<=v;i++)
using namespace std;
const int maxn=200010;
int a[maxn],b[maxn],c[maxn],x[maxn],y[maxn],tot;
set<int>s[maxn];
multiset<int>S;
multiset<int>::iterator it;
int main()
{
    int T,N,M,ans;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M); tot=0;
        rep(i,1,N) scanf("%d",&a[i]),b[++tot]=a[i];
        rep(i,1,M){
            scanf("%d",&c[i]);
            if(c[i]==1) {
                scanf("%d%d",&x[i],&y[i]);
                b[++tot]=y[i];
            }
        }
        sort(b+1,b+tot+1);
        tot=unique(b+1,b+tot+1)-(b+1);
        S.clear(); rep(i,1,tot) s[i].clear();
        rep(i,1,N) {
            a[i]=lower_bound(b+1,b+tot+1,a[i])-b;
            s[a[i]].insert(i);
        }
        rep(i,1,tot) if(!s[i].empty())
          S.insert(*(--s[i].end())-*s[i].begin());
        rep(i,1,M){
            if(c[i]==2){
                ans=-1;
                if(!S.empty()) ans=*(--S.end());
                if(ans==0) ans=-1;
                printf("%d\n",ans);
            }
            else {
                it=S.find(*(--s[a[x[i]]].end())-*s[a[x[i]]].begin());
                s[a[x[i]]].erase(x[i]);
                S.erase(it);
                if(!s[a[x[i]]].empty()) S.insert(*(--s[a[x[i]]].end())-*s[a[x[i]]].begin());

                y[i]=lower_bound(b+1,b+tot+1,y[i])-b; a[x[i]]=y[i];
                if(!s[y[i]].empty())  S.erase(S.find(*(--s[y[i]].end())-*s[y[i]].begin()));
                s[y[i]].insert(x[i]);
                S.insert(*(--s[y[i]].end())-*s[y[i]].begin());
            }
        }
    }
    return 0;
}

 

相关文章:

  • 2021-11-28
  • 2022-01-18
  • 2021-07-25
  • 2021-11-17
  • 2021-06-16
  • 2022-12-23
  • 2021-08-08
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-26
  • 2021-08-11
  • 2021-12-21
  • 2021-09-04
  • 2022-12-23
相关资源
相似解决方案