Description

"Hey! I have an awesome task with chameleons, 5 th task for Saturday’s competition."

"Go ahead. . . "

(...)

“That’s too difficult, I have an easier one, they won’t even solve that one.”

“You are given an array of N integers from the interval [1, K]. You need to process M queries. The first type of query requires you to change a number in the array to a different value, and the second type of query requires you to determine the length of the
shortest contiguous subarray of the current array that contains all numbers from 1 to K.”

“Hm, I can do it in O(N^6 ). What’s the limit for N?”

Input

The first line of input contains the integers N, K and M (1 <= N, M <= 100 000, 1 <= K <= 50). The second line of input contains N integers separated by space, the integers from the array. After that, M queries follow, each in one of the following two forms:

• “1 p v” - change the value of the p th number into v (1 <= p <= N, 1 <= v <= K)

• “2” - what is the length of the shortest contiguous subarray of the array containing all the integers from 1 to K

Output

The output must consist of the answers to the queries of the second type, each in its own line. If the required subarray doesn’t exist, output −1.

Sample Input

4 3 5

2 3 1 2

2

1 3 3

2

1 1 1

2

6 3 6

1 2 3 2 1 1

2

1 2 1

2

1 4 1

1 6 2

2

Sample Output

3

-1

4

3

3

4

题意:给你一段长为n的区间,区间内有k(k<=50)种颜色,让你每次更新后找到最短的连续区间,使得这个区间包含全部k种颜色,颜色可以有重复。

思路:一开始的思路是每次更新都用set维护,维护每一种颜色到区间两端点的最近距离,但是这样已更新所有数据都乱了= =,而且时间复杂度也爆了,所以要用别的方法。我们可以在线段树中开两个vector<pair<ll,int> >lo,re,分别表示左端点往右走的最多50种状态,以及右端点往左走的最多50种状态,那么b[i].mindis=min(b[i*2].mindis,b[i*2+1].mindis),然后就是要中间区间的合并了,我们用两个指针,一个p1指向左子树离b[i*2].r的最远的那种状态,然后另一个p2指向右子树离b[i*2+1].l最近的那种状态,每一次判断它们或起来是不是包含全部k种颜色,如果没有的话,p2往右移,如果恰好包含,那么再使得p1向右移。

ps:用pair写代码减少了很多,看着也舒服..

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 600000
#define maxn 100050
int a[maxn],k; struct node{
int l,r;
vector< pair<ll,int> >lo,re;
ll state;
int mindis;
}b[4*maxn]; void pushup(int th)
{
int i,j,siz;
b[th].lo=b[th*2].lo;
b[th].re=b[th*2+1].re; siz=b[th*2].lo.size();
ll now=b[th*2].lo[siz-1].first;
for(i=0;i<b[th*2+1].lo.size();i++ ){
if((now|b[th*2+1].lo[i].first )==now )continue;
now=(now|b[th*2+1].lo[i].first);
b[th].lo.push_back(make_pair(now,b[th*2+1].lo[i].second) );
} siz=b[th*2+1].re.size();
now=b[th*2+1].re[siz-1].first;
for(i=0;i<b[th*2].re.size();i++ ){
if((now|b[th*2].re[i].first )==now )continue;
now=(now|b[th*2].re[i].first );
b[th].re.push_back(make_pair(now,b[th*2].re[i].second) );
} b[th].state=(b[th*2].state|b[th*2+1].state);
b[th].mindis=min(b[th*2].mindis,b[th*2+1].mindis); ll state=(1LL<<k)-1;
int weizhi; j=0;
for(i=b[th*2].re.size()-1;i>=0;i--){
now=b[th*2].re[i].first;
weizhi=b[th*2].re[i].second;
while(j<b[th*2+1].lo.size()){
if((b[th*2+1].lo[j].first|now)==state ){
b[th].mindis=min(b[th].mindis,b[th*2+1].lo[j].second-weizhi+1 );break;
}
j++;
}
}
} void build(int l,int r,int th)
{
int mid;
b[th].l=l;b[th].r=r;
if(l==r){
b[th].lo.clear();
b[th].lo.push_back(make_pair(1LL<<a[l],l) ); b[th].re.clear();
b[th].re.push_back(make_pair(1LL<<a[l],r)); b[th].state=(1LL<<a[l]);
b[th].mindis=inf;return;
}
mid=(l+r)/2;
build(l,mid,th*2);
build(mid+1,r,th*2+1);
pushup(th);
} void update(int idx,int num,int th)
{
int mid;
if(b[th].l==idx && b[th].r==idx){
b[th].lo.clear();
b[th].lo.push_back(make_pair(1LL<<num,b[th].l) ); b[th].re.clear();
b[th].re.push_back(make_pair(1LL<<num,b[th].r)); b[th].state=(1LL<<num);
b[th].mindis=inf;return; }
mid=(b[th].l+b[th].r)/2;
if(idx<=mid)update(idx,num,th*2);
else update(idx,num,th*2+1);
pushup(th);
} int main()
{
int n,m,i,j,f,c,d;
while(scanf("%d%d%d",&n,&k,&m)!=EOF) //k=1的情况特殊考虑
{
if(k==1){
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(i=1;i<=m;i++){
scanf("%d",&f);
if(f==1){
scanf("%d%d",&c,&d);
}
else printf("1\n");
}
continue; } for(i=1;i<=n;i++){
scanf("%d",&a[i]);
a[i]--;
}
build(1,n,1);
for(i=1;i<=m;i++){
scanf("%d",&f);
if(f==1){
scanf("%d%d",&c,&d);
d--;
update(c,d,1);
}
else{
if(b[1].mindis>500000)printf("-1\n");
else printf("%d\n",b[1].mindis);
}
}
}
return 0;
}

zjnu1716 NEKAMELEONI (线段树)的更多相关文章

  1. 校内模拟赛T5:连续的“包含”子串长度( nekameleoni?) —— 线段树单点修改,区间查询 + 尺取法合并

    nekameleoni 区间查询和修改 给定N,K,M(N个整数序列,范围1~K,M次查询或修改) 如果是修改,则输入三个数,第一个数为1代表修改,第二个数为将N个数中第i个数做修改,第三个数为修改成 ...

  2. bzoj3932--可持久化线段树

    题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...

  3. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  4. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  5. codevs 1080 线段树点修改

    先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...

  6. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  7. PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树

    #44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...

  8. CF719E(线段树+矩阵快速幂)

    题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...

  9. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

随机推荐

  1. Nginx配置请求头

    最近发现一个问题: IOS访问后台接口是,总是application/json;charset=utf-8 但是后台接口只支持大写的UTF-8,修改了Nginx的请求头之后正常. proxy_set_ ...

  2. Spark学习进度11-Spark Streaming&Structured Streaming

    Spark Streaming Spark Streaming 介绍 批量计算 流计算 Spark Streaming 入门 Netcat 的使用 项目实例 目标:使用 Spark Streaming ...

  3. python模块详解 | unittest(单元测试框架)(持续更新中)

    目录: why unittest? unittest的四个重要概念 加载测试用例的三个方法 自动加载测试用例 忽略测试和预期失败 生成html测试报告 why unittest? 简介: Unitte ...

  4. 总结下MySql优化。防止数据灾难的发生。

    在PHP开发中用到的数据库中MySql是最牛逼的数据库,没有之一--^_^ 相比Sqlite个人最喜欢的特性就是"支持多线程,充分利用 CPU 资源",不像Sqlite那样,动不动 ...

  5. MyISAM与InnoDB两者之间区别与选择(转)

    Mysql在V5.1之前默认存储引擎是MyISAM:在此之后默认存储引擎是InnoDB MyISAM:默认表类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Acces ...

  6. 映泰主板H100系列安装win7的各种坑

    自100系列主板发布以来,windows7好像就被遗弃一样,原因就在于安装win7的时候,会出现USB设备无法使用导致无法安装的问题.主要在于Win7系统没有整合USB的XHCI驱动,而100系列芯片 ...

  7. [SSL]在线检查服务器HTTPS安全

    https://myssl.com/ SSL/TLS安全评估报告 https://www.ssllabs.com/ssltest/ SSL Server Test HTTPS开启工具(IIS) htt ...

  8. 私有镜像仓库Harbor基础介绍与部署

    企业级私有镜像仓库Harbor 一:介绍 Harbor,是一个英文单词,意思是港湾,港湾是干什么的呢,就是停放货物的,而货物呢,是装在集装箱中的,说到集装箱,就不得不提到Docker容器,因为dock ...

  9. 关于请求接口报4XX错误,给广大前端同胞进行伸冤澄清,请相信它不一定都是前端的错

    关于请求接口报4XX错误,给广大前端同胞进行伸冤澄清,请相信它不一定都是前端的错 首先确保接口没有写错,参数按照后台要的写,确保自己也没有写错,若页面还是报4xx错误,请站出来大胆的质疑后端,干什么吃 ...

  10. Markdown特殊字符、数学公式汇总

    引自:https://blog.csdn.net/weixin_39653948/article/details/104621249