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. 关于软件架构中的b/s

    **B/S架构 b/s只需要一个浏览器,用户就可以通过不同的网址访问不同的服务器程序. 优点:开发,安装,部署,维护简单 缺点:对硬件要求过高,用户的体验会受到影响 首先是资源分类:**可以分为静态资 ...

  2. 什么是开发中经常说的'POCO'

    什么是开发中经常说的'POCO'Posted By : 蓝狐Updated On : 2015-07-19在看一些EF的文章,经常提到POCO这个词,但是,有没有比较详细的说这个POCO是什么意思呢? ...

  3. wmic process进程管理

    process    进程管理工具 示例:1.列举当前的进程.进程路径.命令行.进程ID.父进程ID.线程数,内存使用::wmic process get name,executablepath,co ...

  4. DOI技术扫盲一

    DOI:  desktop office intergration   桌面办公软件集成简单的将,就是我们在Windows桌面中打开的办公软件(如:word,excel,pdf等等)可以在SAP系统进 ...

  5. SpringBoot快速掌握(1):核心技术

    SpringBoot快速掌握(1):核心技术 SpringBoot快速掌握(1):核心技术 SpringBoot快速掌握(1):核心技术 SpringBoot快速掌握(1):核心技术 SpringBo ...

  6. LeetCode-P53题解【动态规划】

    本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: https://leetcode.com/problems/maximum-subarray/ ...

  7. Python执行程序实可视化_heartrate

    最近发现了一个Python程序执行的简单实时可视化神器,名字叫 heartrate,安装完运行可以看到下面这样的炫酷过程. 虽然很炫酷,但有点看不懂. 来解释下,左边的动态数字代表每行被触发的次数.变 ...

  8. [APUE] 进程环境

    APUE 一书的第七章学习笔记. 进程终止 有 8 种方式可以使得进程终止,5 种为正常方式: Return from main Calling exit() Calling _exit or _Ex ...

  9. 转 1 认识开源性能测试工具jmeter

    1 认识开源性能测试工具jmeter   典型的性能测试工具主要有2个,Load Runner和jmeter.Load Runner是商业化的,Jmeter是开源的.下面我们认识一下开源性能测试工具j ...

  10. Python+Selenium+Unittest实现PO模式web自动化框架(3)

    1.Outputs目录下的具体目录功能 2.logs目录 logs目录是用于存放log日志的一个目录. 2.reports目录 reports目录是用于存放测试报告的. 3.screenshots目录 ...