码迷,mamicode.com
首页 > 其他好文 > 详细

【FZU】Problem 2059 MM(离线处理并查集)

时间:2015-05-15 22:55:32      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

离线处理,并查集


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100005;
struct Node{
    int id,value;
}node[maxn],input[maxn];
bool cmp(Node p,Node q){
    return p.value > q.value;
}
int p[maxn],vis[maxn],fa[maxn],num[maxn],ans[maxn];
int n,m,cnt;
int find_father(int u){
    return fa[u] == u ? u : fa[u] = find_father(fa[u]);
}
void init(){
    for(int i = 1; i <= n; i++){
        fa[i] = i;
        num[i] = 1;
    }
    memset(vis,0,sizeof(vis));
    sort(node + 1,node + 1 + n, cmp);
    for(int i = 1; i <= n; i++)
        p[node[i].id] = i;
    cnt = 0;
}
void solve(){
    sort(input,input + cnt,cmp);
    int ret = 0;
    int   j = 1;
    for(int i = 0; i < cnt; i++){
        int t = input[i].id;
        for(;j <= n && node[j].value >= input[i].value; j++){
            ret = max(ret,1);
            int pos = node[j].id;
            vis[pos] = 1;
            int lpos = pos - 1,rpos = pos + 1;
            if(lpos >= 1 && vis[lpos]){
                int f1 = find_father(pos);
                int f2 = find_father(lpos);
                if(f1 != f2){
                    if(f1 > f2) swap(f1,f2);
                    num[f1] += num[f2];
                    fa[f2] = f1;
                    ret = max(ret,num[f1]);
                }
            }
            if(rpos <= n && vis[rpos]){
                int f1 = find_father(pos);
                int f2 = find_father(rpos);
                if(f1 != f2){
                    if(f1 > f2) swap(f1,f2);
                    num[f1] += num[f2];
                    fa[f2] = f1;
                    ret = max(ret,num[f1]);
                }
            }
        }
        ans[t] = ret;
    }
    for(int i = 0; i < cnt; i++)
        printf("%d\n",ans[i]);
}
void read(){
    int op,a,b;
    init();
    for(int i = 0; i < m; i++){
        scanf("%d",&op);
        if(op == 1){
            input[cnt].id = cnt;
            scanf("%d",&input[cnt++].value);
        }
        else{
            solve();
            scanf("%d%d",&a,&b);
            node[p[a]].value = b;
            init();
        }
    }
    solve();
}
int main(){
    while(scanf("%d%d",&n,&m) != EOF){
        for(int i = 1; i <= n; i++){
            node[i].id = i;
            scanf("%d",&node[i].value);
        }
        read();
    }
    return 0;
}

【FZU】Problem 2059 MM(离线处理并查集)

标签:

原文地址:http://blog.csdn.net/u013451221/article/details/45750137

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!