2013 Multi-University Training Contest 4 Group(离线+BIT)

Group

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2203    Accepted Submission(s): 1147


Problem Description
There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interval of men to make some group. K men in a group can create K*K value. The value of an interval is sum of these value of groups. The people of same group's id must be continuous. Now we chose an interval of men and want to know there should be how many groups so the value of interval is max.
 

Input
First line is T indicate the case number.
For each case first line is n, m(1<=n ,m<=100000) indicate there are n men and m query.
Then a line have n number indicate the ID of men from left to right.
Next m line each line has two number L,R(1<=L<=R<=n),mean we want to know the answer of [L,R].
 

Output
For every query output a number indicate there should be how many group so that the sum of value is max.
 

Sample Input
  
  
1 5 2 3 1 2 5 4 1 5 2 4
 

Sample Output
  
  
1 2
 

【题意】给定1~n的一个排列,每次查询一个区间(l,r),将所求区间内的数进行分组,每一组内的数的值必须是连着的,该组的价值为元素个数的平方和 。问在区间价值最大的情况下,要分成几组 ?

【解题方法&&分析】1e5的数据,感觉莫队可以莽一下,试了一下无奈TLE。那么这道题还可以怎么做呢?我们可以这样,用树状数组离线维护,把询问按照右区间递增排序,然后从1~n维护,维护到i时注意找寻前i个是不是有a[i]-1,和a[i]+1,有的话直接去掉,因为  这两个数必定要和a[i]分到一组,留一个就好了 。

【AC 代码】

//
//Created by just_sort 2016/9/16 14:25
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e6+7;
int a[maxn],p[maxn],ans[maxn];
struct node1{
    int l,r,id;
    bool operator<(const node1 &rhs) const{
        return r<rhs.r;
    }
}q[maxn];
struct BIT{
    int c[maxn];int n;
    void init(int _n){
        n = _n;
        memset(c,0,sizeof(c));
    }
    void add(int i,int v){
        while(i<=n){
            c[i]+=v;
            i+=i&-i;
        }
    }
    int getans(int i){
        int ret=0;
        while(i>0){
            ret+=c[i];
            i-=i&-i;
        }
        return ret;
    }
}bit;

int  main()
{
    int T,n,m;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++){
            scanf("%d",&a[i]);
            p[a[i]]=i;
        }
        for(int i=0; i<m; i++){
            scanf("%d%d",&q[i].l,&q[i].r);
            q[i].id=i;
        }
        sort(q,q+m);
        bit.init(n);
        int j = 0;
        for(int i=1; i<=n; i++){
            bit.add(i,1);
            if(a[i]>1 && p[a[i]-1]<i) bit.add(p[a[i]-1],-1);
            if(a[i]<n && p[a[i]+1]<i) bit.add(p[a[i]+1],-1);
            while(j<m && q[j].r==i){
                ans[q[j].id] = bit.getans(q[j].r)-bit.getans(q[j].l-1);
                j++;
            }
        }
        for(int i=0; i<m; i++){
            printf("%d\n",ans[i]);
        }
    }
    return 0;
}

【TLE 莫队】如果哪位大牛可以用莫队过,一定要告诉小弟。不过感觉这复杂度好像的确不够QAQ

//
//Created by just_sort 2016/9/16 14:25
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e6+7;
int a[maxn],pos[maxn],vis[maxn],ans[maxn];
int block,sum;
struct node{
    int l,r,id;
    //node(int l,int r,int id):l(l),r(r),id(id){}
}q[maxn];
bool cmp(node a,node b)
{
    if(pos[a.l]==pos[b.l]) return a.r<b.r;
    else return pos[a.l]<pos[b.l];
}
void add(int x)
{
    vis[x]=1;
    sum += 1-vis[x-1]-vis[x+1];
}
void del(int x)
{
    vis[x]=0;
    sum += vis[x-1]+vis[x+1]-1;
}
int main()
{
    int T,n,m;
    scanf("%d",&T);
    while(T--)
    {
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        block = ceil(sqrt(n));
        for(int i=1; i<=n; i++){
            scanf("%d",&a[i]);
            pos[i] = i/block;
        }
        for(int i=1; i<=m; i++){
            scanf("%d%d", &q[i].l,&q[i].r);
            q[i].id = i;
        }
        sort(q+1,q+m+1,cmp);
        int L=1,R=0;
        sum = 0;
        for(int i=1; i<=m; i++){
            if(R<q[i].r){
                for(int j=R+1; j<=q[i].r; j++) add(a[j]);
            }
            else{
                for(int j=R; j>q[i].r; j--)   del(a[j]);
            }
            if(L<q[i].l){
                for(int j=L; j<q[i].l; j++)    del(a[j]);
            }
            else{
                for(int j=L-1; j>=q[i].l; j--) add(a[j]);
            }
            ans[q[i].id] = sum;
        }
        for(int i=1; i<=m; i++){
            printf("%d\n",ans[i]);
        }
    }
    return 0;
}


【解题方法2 莫队】

【代码】

const int M=100005;
int s;
struct Query{
    int L,R,id;
    void input(int x){
        scanf("%d%d",&L,&R);
        id=x;
    }
    bool operator <(const Query&tmp)const{
        if(L/s==tmp.L/s) return R<tmp.R;
        return L/s<tmp.L/s;
    }
}q[M];
int A[M],res,ans[M];
bool mark[M];
void del(int x){
    if(mark[A[x]-1]&&mark[A[x]+1]) res++;
    if(!mark[A[x]-1]&&!mark[A[x]+1]) res--;
    mark[A[x]]=0;
}
void add(int x){
    if(!mark[A[x]-1]&&!mark[A[x]+1]) res++;
    if(mark[A[x]-1]&&mark[A[x]+1]) res--;
    mark[A[x]]=1;
}
void solve(){
    int n,m;
    scanf("%d%d",&n,&m);
    s=(int)sqrt(n);
    for(int i=0;i<=n+1;i++)
        mark[i]=0;
    for(int i=1;i<=n;i++)
        scanf("%d",&A[i]);
    for(int i=0;i<m;i++)
        q[i].input(i);
    sort(q,q+m);
    int L=1,R=1;
    res=1;
    mark[A[1]]=1;
    for(int i=0;i<m;i++){
        for(;R<q[i].R;R++) add(R+1);
        for(;R>q[i].R;R--) del(R);
        for(;L<q[i].L;L++) del(L);
        for(;L>q[i].L;L--) add(L-1);
        ans[q[i].id]=res;
    }
    for(int i=0;i<m;i++)
        printf("%d\n",ans[i]);
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--) solve();
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值