hdu 4638 Group (线段树+离线处理)

Group

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


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] 之间有多少段连续的数。

思路:离线处理,每个查询按照r从小到大排序.从1-n开始,考虑每个点,首先每个位置都设为1.

表示先不管其他的,i这个位置上的数可以成为一个组,然后在看 c[i]-1 和 c[i+1]的位置,如果c[i]-1

或c[i]+1的位置在i的前方,那么可以与c[i]合并了,即就在对应的位置上减1最后的答案就是区间的和。

 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100010;

struct tree
{
    int l,r,num;
}a[maxn*4];
struct node
{
    int l,r,index;
    void fun(int ll,int rr,int inde)
    {
        l=ll,r=rr;
        index=inde;
    }
}b[maxn];
int pre[maxn],c[maxn],ans[maxn],n,m;
bool visited[maxn];

bool cmp(node p,node q)
{
    if(p.r==q.r)  return p.l<q.l;
    return p.r<q.r;
}

void initial()
{
    memset(pre,-1,sizeof(pre));
    memset(visited,0,sizeof(visited));
}

void input()
{
    int x,y;
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&c[i]);
        pre[c[i]]=i;
    }
    for(int i=0;i<m;i++)
    {
         scanf("%d %d",&x,&y);
         b[i].fun(x-1,y-1,i);
    }
    sort(b,b+m,cmp);
}

void build(int l,int r,int k)
{
    a[k].l=l,a[k].r=r;
    a[k].num=0;
    if(l==r)   return ;
    int mid=(l+r)>>1;
    build(l,mid,2*k);
    build(mid+1,r,2*k+1);
}

void update(int l,int r,int add,int k)
{
    if(a[k].l==l && a[k].r==r) a[k].num+=add;
    else
    {
        int mid=(a[k].l+a[k].r)>>1;
        if(r<=mid)  update(l,r,add,2*k);
        else update(l,r,add,2*k+1);
        a[k].num=a[2*k].num+a[2*k+1].num;
    }
}

int query(int l,int r,int k)
{
    if(a[k].l==l && a[k].r==r) return a[k].num;
    else
    {
        int mid=(a[k].l+a[k].r)>>1;
        if(r<=mid)      return query(l,r,2*k);
        else if(l>mid)  return query(l,r,2*k+1);
        else  return query(l,mid,2*k)+query(mid+1,r,2*k+1);
    }
}

void solve()
{
    build(0,n-1,1);
    int t=0;
    for(int i=0;i<n;i++)
    {
        int tmp=c[i],Min=c[i]-1,Max=c[i]+1;
        if(Max<=n && visited[Max])  update(pre[Max],pre[Max],-1,1);
        if(Min>=1 && visited[Min])  update(pre[Min],pre[Min],-1,1);
        update(i,i,1,1);
        visited[tmp]=1;
        while(b[t].r==i)
        {
            ans[b[t].index]=query(b[t].l,b[t].r,1);
            t++;
        }
    }
    for(int i=0;i<m;i++)   printf("%d\n",ans[i]);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        initial();
        input();
        solve();
    }
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值