zoukankan      html  css  js  c++  java
  • HDU 4638 莫队算法

    Group

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


    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
     
    Source
     题意:t组数据 给你一个长度为n的序列 m个询问[l,r] 问 l到r 的值可以组成多少个连续的段 
     题解:例如序列3 1 2 5 4  
    查询 [1,5]  值为{1,2,3,4,5}  只有一个连续的段
    查询 [2,4] 值为{1,2} {5} 有两个连续的段
    莫队处理;
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<map>
     7 #include<queue>
     8 #include<stack>
     9 #include<vector>
    10 #include<set>
    11 #define ll __int64
    12 using namespace std;
    13 int n,m;
    14 struct node
    15 {
    16     int l,r,id;
    17 } N[100005];
    18 int p[100005];
    19 int block;
    20 int a[100005];
    21 int x[100005];
    22 int mp[100005];
    23 ll ans=0;
    24 ll re[100005];
    25 int cmp(struct node aa,struct node bb)
    26 {
    27     if(p[aa.l]==p[bb.l])
    28         return  aa.r<bb.r;
    29     else
    30         return p[aa.l]<p[bb.l];
    31 }
    32 void update(int w,int h)
    33 {
    34     if(h==1)
    35     {
    36         mp[a[w]]=1;
    37         if(mp[a[w]-1]==0&&mp[a[w]+1]==0)
    38             ans++;
    39         if(mp[a[w]-1]==1&&mp[a[w]+1]==1)
    40             ans--;
    41     }
    42     else
    43     {
    44         mp[a[w]]=0;
    45         if(mp[a[w]-1]==1&&mp[a[w]+1]==1)
    46             ans++;
    47         if(mp[a[w]-1]==0&&mp[a[w]+1]==0)
    48             ans--;
    49     }
    50 }
    51 int t;
    52 int main()
    53 {
    54     scanf("%d",&t);
    55     for(int o=1;o<=t;o++)
    56     {
    57         for(int i=0;i<100004;i++)
    58             mp[i]=0;
    59         scanf("%d %d",&n,&m);
    60         for(int i=1; i<=n; i++)
    61             scanf("%d",&a[i]);
    62         for(int i=1; i<=m; i++)
    63         {
    64             scanf("%d %d",&N[i].l,&N[i].r);
    65             N[i].id=i;
    66         }
    67         block=(int)sqrt((double)n);
    68         for(int i=1; i<=n; i++)
    69             p[i]=(i-1)/block+1;
    70         sort(N+1,N+1+m,cmp);
    71         ans=0;
    72         for(int i=1,l=1,r=0; i<=m; i++)
    73         {
    74             for(; r<N[i].r; r++) update(r+1,1);
    75             for(; l>N[i].l; l--) update(l-1,1);
    76             for(; r>N[i].r; r--) update(r,-1);
    77             for(; l<N[i].l; l++) update(l,-1);
    78             re[N[i].id]=ans;
    79         }
    80         for(int i=1; i<=m; i++)
    81             printf("%I64d
    ",re[i]);
    82     }
    83     return 0;
    84 }
  • 相关阅读:
    ecshop里的$_CFG从哪来的
    高效PHP程序必知的53个技巧
    Jquery结合datagrid框架
    PHP数据类型转换(字符转数字,数字转字符)
    php 操作数组 (合并,拆分,追加,查找,删除等)
    css3制作导航栏
    php日期转时间戳,指定日期转换成时间戳
    PHP 时间与字符串的相互转化
    php 生成.txt文件
    linux PHP yum 安装phpzie
  • 原文地址:https://www.cnblogs.com/hsd-/p/7050493.html
Copyright © 2011-2022 走看看