Problem 2059 MM

Accept: 109    Submit: 484
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

There is a array contain N(1<N<=100000) numbers. Now give you M(1<M<10000) query.

Every query will be:

1 x : ask longest substring which every number no less than x

2 y x : change the A[y] to x. there are at most change 10 times.

For each ask can you tell me the length of longest substring.

 Input

There are multiple tests.

each test first line contain two integer numbers N M,second line contain N integer numbers.

Next M lines each line will be:

1 x : ask longest substring which every number no less than x

2 y x : change the A[y] to x. there are at most change 10 times.

0 < N <= 100000, 0 < M <= 10000, -1000000000 <= A[i] <= 1000000000

 Output

Each ask output the length of longest substring .

 Sample Input

5 5
1 2 3 2 1
1 2
1 3
2 3 1
1 2
1 3

 Sample Output

3
1
1
0
 
题意:  给一个数列,两种操作,第一种操作是输入一个x,查找一个子串满足每个元素的值都不小于x且长度最大,输出这个最大值,第二个操作是输入x和y,修改a[y]=x,第二个操作最多只有十次
 
思路: o(nlogn)预处理,o(logn)查询。由于修改操作只有10次,所以每次修改都直接暴力预处理一遍,然后考虑没有修改操作的情况。
预处理方法:把数列值排序一下,然后从大到小逐个插入,用并查集维护插入的这个点能连接的最长长度,在这个过程中维护一个最大值,最大值就是答案了。
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1e9;
const double eps = 1e-;
const int N = ;
int cas = ; struct _node{
int pos,val,ans;
friend bool operator < (const _node &a, const _node &b)
{
return a.val < b.val;
}
};
int n,m;
int fa[N],sum[N],b[N],mxval;
_node a[N]; int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
} void un(int u,int v)
{
u=find(u), v=find(v);
fa[u]=v;
sum[v]+=sum[u];
} void pre()
{
memset(sum,,sizeof(sum));
mxval = -INF-;
for(int i=;i<n;i++) fa[i]=i;
for(int i=;i<n;i++)
{
a[i].val=b[i],a[i].pos=i;
if(a[i].val > mxval) mxval = a[i].val;
}
sort(a,a+n);
int mx = ;
for(int i=n-;i>=;i--)
{
sum[a[i].pos]=;
if(sum[a[i].pos-]) un(a[i].pos-,a[i].pos);
if(sum[a[i].pos+]) un(a[i].pos+,a[i].pos);
if(mx < sum[find(a[i].pos)]) mx = sum[fa[a[i].pos]];
a[i].ans = mx;
}
} int solve(int x)
{
_node t;
t.val=x;
int id = lower_bound(a,a+n,t) - a;
return a[id].ans;
} void run()
{
for(int i=;i<n;i++)
scanf("%d",b+i);
pre();
int x,y,op;
while(m--)
{
scanf("%d",&op);
if(op==)
{
scanf("%d",&x);
if(x>mxval) puts("");
else printf("%d\n",solve(x));
}
else
{
scanf("%d%d",&y,&x);
b[y-]=x;
pre();
}
}
} int main()
{
#ifdef LOCAL
freopen("case.txt","r",stdin);
#endif
while(scanf("%d%d",&n,&m)!=EOF)
run();
return ;
}

FZU 2059 MM (并查集+排序插入)的更多相关文章

  1. SPOJ:Lexicographically Smallest(并查集&排序)

    Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...

  2. FZU 2059 MM

    Description There is a array contain N(1<N<=100000) numbers. Now give you M(1<M<10000) q ...

  3. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  4. HDU——1272小希的迷宫(并查集+拓扑排序)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  5. FZU 2112 并查集、欧拉通路

    原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...

  6. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  7. 拓扑排序 - 并查集 - Rank of Tetris

    Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...

  8. hdu 1811Rank of Tetris (并查集 + 拓扑排序)

    /* 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B ...

  9. ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线

    hdu 1811 Rank of Tetris Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

随机推荐

  1. maven scope runtime

    https://blog.csdn.net/ningbohezhijunbl/article/details/25818069 There are 6 scopes available: compil ...

  2. 编译安装Heartbeat常见错误

    -----------那些需要升级包还有少包的错误就不写了---------- <b>1</b>. Reusable-Cluster-Components-glue-glue- ...

  3. 在调用Response.End()时,会执行Thread.CurrentThread.Abort()操作

    在调用Response.End()时,会执行Thread.CurrentThread.Abort()操作. 如果将Response.End()放在try...catch中,catch会捕捉Thread ...

  4. Mac版 Intellij IDEA 激活

    第一步:修改这两个文件夹 进入跳转路径,输入 /private/etc/ 点击[前往] 同时加上: # Intellij IDEA0.0.0.0 account.jetbrains.com 第二部:在 ...

  5. php函数: set_include_path

    <?php $p =get_include_path(); $p.=PATH_SEPARATOR.'./bp/'; $p.=PATH_SEPARATOR.'./CLI/'; $p.=PATH_S ...

  6. linux下编译安装python

    从官网下载指定的源码包 https://www.python.org/downloads/source/ 把源码文件以二进制方式上传到linux服务器 安装python需要用到gcc工具,首先查看gc ...

  7. leetcode 890. Possible Bipartition

    Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of ...

  8. WPF区时浏览小程序

    在深圳已经工作了一个月了,之前做WinForm的,现在做WPF,每天加班到晚上10点,比之前累.学习新技术也是有个过程的,我就从基础的语法和 界面布局做起.这是我仿着做一个小软件. 效果图赏析 在原基 ...

  9. git删除远程分支【转】

    本文转载自:https://my.oschina.net/tsingxu/blog/84601 如果不再需要某个远程分支了,比如搞定了某个特性并把它合并进了远程的 master 分支(或任何其他存放  ...

  10. codeforces 之 Little Pigs and Wolves

    B-Little Pigs and Wolvestime limit per test2 secondsmemory limit per test256 megabytesinputstandard ...