传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5868

Little Sub and Isomorphism Sequences


Time Limit: 3 Seconds      Memory Limit: 65536 KB

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

题意概括:

给出 一个长度为 N 的序列, 和 M 次操作.

有两种操作:

1 id y : 把序列中第 id 个位置的值 修改成 y

2 : 查询这个序列中能满足两个子串“相同”的最大长度。

两个子串相同的条件:子串的数字种类和每种种类的数量相同,当然两个子串的起点不同(理所当然,否则这道题就没意义了)。

解题思路:

很明显就是维护每种 相同的值的 下标(位置)的 最大差值。

因为除了两端的值,中间公共部分 无论是数量还是种类肯定相同。

那么问题就转换成了如何维护每种值 value 的位置最大差值。

用 set 存每种 value 的下标,最多需要 20w个(最坏的情况就是每次操作都是插入新的值)

用 multiset 维护 最大差值。

值 val 的范围是 【1, 1e9】,需要离散化。

在线 map 离散化 RE。

离线 lower_bound() AC。

AC code:

 #include <set>
#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 2e5+;
const int MAXM = 2e5+;
int a[MAXN]; //存实际序列
int b[MAXN]; //存所有序列包括原序列的值和修改的值,用于离散化
int c[MAXN]; //存操作
int x[MAXN]; //存修改的下标
int y[MAXN]; //存修改的值
set<int>ss[MAXM]; ///维护每一种值的起点和终点
multiset<int>ans; ///维护两点距离差
//map<int, int>mmp;
int N, M; int main()
{
int cnt = ;
int T_case;
scanf("%d", &T_case);
while(T_case--){ int cnt = ;
ans.clear();
scanf("%d %d", &N, &M);
for(int i = ; i <= N; i++){
scanf("%d", &a[i]);
b[++cnt] = a[i];
} for(int i = ; i <= M; i++){
scanf("%d", &c[i]);
if(c[i] == ){
scanf("%d %d",&x[i], &y[i]);
b[++cnt] = y[i];
}
}
sort(b+, b++cnt);
cnt = unique(b+, b+cnt+)-(b+); ///离散化后的数据总量 for(int i = ; i <= cnt; i++){ ///初始化
ss[i].clear();
} for(int i = ; i <= N; i++){
a[i] = lower_bound(b+, b+cnt+, a[i])-b; ///对原序列的值进行离散化
ss[a[i]].insert(i);
} for(int i = ; i <= cnt; i++){
if(!ss[i].empty()) ans.insert(*--ss[i].end() - *ss[i].begin()); ///初始化两点距离差
} for(int i = ; i <= M; i++){
if(c[i] == ){ ///删除操作
int no = a[x[i]];
ans.erase(ans.find( *--ss[no].end()-*ss[no].begin()) );
ss[no].erase(ss[no].find(x[i]));
if(!ss[no].empty()) ans.insert(*--ss[no].end()-*ss[no].begin()); y[i] = lower_bound(b+, b+cnt+, y[i])-b; ///增加操作
a[x[i]] = y[i];
no = y[i];
if(!ss[no].empty()) ans.erase(ans.find( *--ss[no].end()-*ss[no].begin()) );
ss[no].insert(x[i]);
ans.insert(*--ss[no].end()-*ss[no].begin());
}
else{
int res = -;
if(!ans.empty()){
res = *--ans.end();
}
if(res == ) res = -;
printf("%d\n", res);
}
} }
return ;
}

ZOJ Monthly, January 2019 Little Sub and Isomorphism Sequences 【离线离散化 + set + multiset】的更多相关文章

  1. ZOJ Monthly, January 2019

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

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

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

  3. ZOJ Monthly, January 2019 Little Sub and his Geometry Problem 【推导 + 双指针】

    传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5861 Little Sub and his Geometry Prob ...

  4. ZOJ Monthly, January 2019 Little Sub and his Geometry Problem ZOJ4082(模拟 乱搞)

    在一次被自己秀死... 飞机 题目: 给出N,K, Q; 给出一个N*N的矩阵  , 与K个特殊点 , 与Q次查询 , 每次查询给出一个C , 问 在这个N*N矩阵中 , 有多少的点是满足这样的一个关 ...

  5. ZOJ Monthly, January 2018 训练部分解题报告

    A是水题,此处略去题解 B - PreSuffix ZOJ - 3995 (fail树+LCA) 给定多个字符串,每次询问查询两个字符串的一个后缀,该后缀必须是所有字符串中某个字符串的前缀,问该后缀最 ...

  6. matrix_2015_1 138 - ZOJ Monthly, January 2015

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3844 第一个,n个数,每次操作最大数和最小数都变成他们的差值,最后n个数相 ...

  7. ZOJ Monthly, January 2018

    A 易知最优的方法是一次只拿一颗,石头数谁多谁赢,一样多后手赢 #include <map> #include <set> #include <ctime> #in ...

  8. ZOJ Monthly, January 2018 Solution

    A - Candy Game 水. #include <bits/stdc++.h> using namespace std; #define N 1010 int t, n; int a ...

  9. ZOJ Monthly, January 2019-Little Sub and Pascal's Triangle

    这个题的话,它每行奇数的个数等于该行行号,如果是0开始的,就该数的二进制中的1的个数,设为k,以它作为次数,2k就是了. #include <stdio.h> int main() { i ...

随机推荐

  1. Silverlight & Blend动画设计系列五:故事板(StoryBoards)和动画(Animations)

    正如你所看到的,Blend是一个非常强大的节约时间的设计工具,在Blend下能够设计出很多满意的动画作品,或许他具体是怎么实现的,通过什么方式实现的我们还是一无所知.本篇将续前面几篇基础动画之上,详细 ...

  2. Node.js学习笔记(四) --- fs模块的使用

    目录 . fs.stat 检测是文件还是目录 . fs.mkdir 创建目录 . fs.writeFile 创建写入文件 . fs.appendFile 追加文件 . fs.readFile 读取文件 ...

  3. 十三、nginx 强制下载txt等文件

    当前的浏览器能够识别文件格式,如果浏览器本身能够解析就会默认打开,如果不能解析就会下载该文件. 那么使用nginx做资源服务器的时候,如何强制下载文件呢? location /back/upload/ ...

  4. 给div加上padding和border,如何不让div整体改变

    最近要入门H5,遇到很多困惑,所以,每解决一个,我就要写在博客里,以防忘记! 给div加上padding和border,如何不让div整体改变? 如果想要实现这样的效果,只需要在这个div块中写入 b ...

  5. es6 import笔记

    export输出: // profile.js var firstName = 'Michael'; var lastName = 'Jackson'; var year = 1958; export ...

  6. 排序算法lowb三人组-冒泡排序

    冒泡排序:顾名思义就是像气泡从水里浮出来一样 把列表立起来如上图所示,从列表的第0项开始循环(把最大的数想成此次循环的气泡) 要把最大的放到上面那那就用第0项开始一次和剩下的开始比较,只要比第0项小就 ...

  7. java中如何使用BigDecimal使得Double类型保留两位有效数字

    一.场景:从数据表中读出Decimal类型的数据直接塞给Double类型的对象时,并不会有什么异常. 如果要再此基础上计算,就会发生异常. 比如:读出数据为0.0092,将其乘以100,则变成了0.9 ...

  8. 【Udacity】解决:字幕遮挡视频内容怎么办?Udacity字幕大小调整

    字幕有没有办法显示在最下方,否则把内容挡住了,根本看不清老师所指的内容 Chrome 的小插件,能方便地调节视频的字幕大小,譬如:Udacity 的学习视频字幕大小不能调节,有时候不想它占太多位置,而 ...

  9. 【Android】Warning :uninstalling will remove the application data!

    最近从Android Studio向手机发布项目过程中经常出现, 问题虽小,但是开发过程中确实浪费时间. It is possible that issue is resolved by uninst ...

  10. idea output 消失找不到

    今天一不小心把idea的 debug的控制台output 搞丢了, 半天找不到,原来是在这里隐藏着 Restore layout