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=;
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=;
rep(i,,N) scanf("%d",&a[i]),b[++tot]=a[i];
rep(i,,M){
scanf("%d",&c[i]);
if(c[i]==) {
scanf("%d%d",&x[i],&y[i]);
b[++tot]=y[i];
}
}
sort(b+,b+tot+);
tot=unique(b+,b+tot+)-(b+);
S.clear(); rep(i,,tot) s[i].clear();
rep(i,,N) {
a[i]=lower_bound(b+,b+tot+,a[i])-b;
s[a[i]].insert(i);
}
rep(i,,tot) if(!s[i].empty())
S.insert(*(--s[i].end())-*s[i].begin());
rep(i,,M){
if(c[i]==){
ans=-;
if(!S.empty()) ans=*(--S.end());
if(ans==) ans=-;
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+,b+tot+,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 ;
}

ZOJ - 4089 :Little Sub and Isomorphism Sequences (同构 set)的更多相关文章

  1. Little Sub and Isomorphism Sequences ZOJ - 4089

    ZOJ - 4089 思路:可以反正 最长重构序列必然符合  此模式 x  +  {   }  与  {   }  +  x 那么 题意转化为了  找两个距离最长的相同的数.eeee 先离散化 然后 ...

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

    传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5868 Little Sub and Isomorphism Seque ...

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

    写这篇博客来证明自己的愚蠢 ...Orz  飞机 题意:给定你个数组,以及一些单点修改,以及询问,每次询问需要求得,最长的字串长度,它在其他位置存在同构 题解:经过一些奇思妙想后 ,你可以发现问题是传 ...

  4. ZOJ-4089-Little Sub and Isomorphism Sequences

    给定你个数组,以及一些单点修改,以及询问,每次询问需要求得,最长的字串长度,它在其他位置存在同构. 当存在两个不相交的区间同构时,如: 1.2.…….n -1.n.n + 1.…….m.m + 1.m ...

  5. ZOJ Monthly, January 2019

    A: Little Sub and Pascal's Triangle Solved. 题意: 求杨辉三角第n行奇数个数 思路: 薛聚聚说找规律,16说Lucas 答案是 $2^p \;\;p 为 n ...

  6. Isomorphism 同构

    小结: 1.两个有限维度的向量空间,在同一数域下,是同构的 等价于 它们维数相等. Isomorphism 同构 0.1.8 Isomorphism. If U and V are vector sp ...

  7. 【BZOJ4474】isomorphism(树的同构,哈希)

    题意:一个无向树的度数为 2的结点称为假结点,其它结点称为真结点.一个无向树的简化树其结点由原树的全体真结点组成,两个真结点之间有边当且仅当它们在原树中有边,或者在原树中有一条联结这两个结点的路,其中 ...

  8. ZOJ Problem Set - 1338 Up and Down Sequences 解释 ac代码

    这道题目我一开始一头雾水,怎么都数不对,参考了下网上的博文,才弄懂. 题意是这样的,如果是上升序列,上升序列的长度不是所有上升数字的,是这么规定的,如果它与前一个数字构成上升,那么这个数字算上长度.所 ...

  9. Python数据类型之“序列概述与基本序列类型(Basic Sequences)”

    序列是指有序的队列,重点在"有序". 一.Python中序列的分类 Python中的序列主要以下几种类型: 3种基本序列类型(Basic Sequence Types):list. ...

随机推荐

  1. jeasyUI DataGrid 根据屏幕宽度自适应, 改变右侧滚动条Size

    PC浏览器的Datagrid可以显示多几列,但是在手机浏览器时,只能有选择性的显示前几列. $(window).resize(function () { if (document.body.clien ...

  2. axios API速查表

    原来jq自带ajax方法,但是近期项目用vue,在vue项目中发送ajax请求原来用vue resource,现在更推荐使用axios,因为axios和vue更配! GET 请求 // Make a ...

  3. vs2015 出现Lc.exe 已退出,代码为-1的问题,如何解决

    今天在代码运行时,出现lc.exe已退出,代码为-1 的问题

  4. 联想E431 安装ubuntu16.04

    http://jingyan.baidu.com/article/3c48dd348bc005e10be358eb.html 按照这个教程安装成功!!

  5. 常用6种type的form表单的input标签分析及示例

    <input> 标签用于搜集用户信息. 根据不同的 type 属性值,输入字段拥有很多种形式.输入字段可以是文本字段.复选框.掩码后的文本控件.单选按钮.按钮等等. 在这里博主介绍6中ty ...

  6. day21 MRO和C3算法

    核能来袭 --MRO和C3算法 1. python的多继承 2.python经典类的MRO 3.python新式类的MRO, C3算法 4.super 是什么鬼? 一.python的多继承 在前面的学 ...

  7. centos7 新装系统网络配置

    [root@localhost ~]# cat /etc/sysconfig/grub GRUB_TIMEOUT= GRUB_DISTRIBUTOR="$(sed 's, release . ...

  8. spring有关jar包的作用

    参考的是spring官网spring4.3版本. 链接:https://docs.spring.io/spring/docs/4.3.19.RELEASE/spring-framework-refer ...

  9. easyui弹框后销毁当前tab弹框不显示的解决方式

    var id=$("#pageId").val(); var message = "{\"id\":" + id+ ",\&quo ...

  10. freemarker中的null异常处理以及!与??的使用(转)

    原文链接: https://blog.csdn.net/mexican_jacky/article/details/50638062 阅读数:6304 如工程包含: 在user中我们有个角色,那么我们 ...