ZOJ - 4089 :Little Sub and Isomorphism Sequences (同构 set)

Posted hua-dong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ - 4089 :Little Sub and Isomorphism Sequences (同构 set)相关的知识,希望对你有一定的参考价值。

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

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

  1. ;
  2. Define as the number of times integer occur in sequence . For each integer in , always holds.

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

  • 1 x y: Change to (, );
  • 2: Query for the greatest () that there exist two integers and () and is isomorphic with . Specially, if there is no such , please output "-1" (without quotes) instead.

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 integers ().

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
",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;
}

 

以上是关于ZOJ - 4089 :Little Sub and Isomorphism Sequences (同构 set)的主要内容,如果未能解决你的问题,请参考以下文章

ZOJ Monthly, January 2019 I Little Sub and Isomorphism Sequences(set 妙用) ZOJ4089

ZOJ - 4082:Little Sub and his Geometry Problem (双指针)

ZOJ Monthly, January 2019 Little Sub and Isomorphism Sequences 离线离散化 + set + multiset

D.Little Sub and Balloons

A.Little Sub and Applese

石油大训练 Little Sub and Johann (博弈SG打表找规律)