• Group(分块+莫队)


     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

    题意:
    问所给区间的连续的序列个数,如:3 1 2,6 7, 9都算连续的序列

    思路:
    n最大1e5,m最大1e5,若纯暴力,1e10,会爆,很显然普通分块解决不了,
    因此用了莫队,先分块,再把所给区间排序,
    利用l,r的移动,数组走一遍求出所有离线区间的结果,最后一起输出

    离线就是预先已经知道了已知和所有要求的情况,这是莫队必不可少的使用条件,不然拿啥排序呀

    关于区间的排序
    区间的排序,先看left是否在同一块,
    若在一块,即left距离近,那么左边移来移去没啥大不了,就按right从小到大排
    若不在一块,即left距离远,左边移来移去有风险,就按left从小到大排

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int a[100005], belong[100005], coun[100005];
     6 struct node
     7 {
     8     int left, right, num;
     9 }str[100005];
    10 
    11 bool cmp(struct node x, struct node y)
    12 {
    13     if(belong[x.left]==belong[y.left]) return x.right < y.right;
    14     else return x.left < y.left;
    15 }
    16 
    17 int add(int n, int sum)
    18 {
    19     coun[n] = 1;
    20     if(coun[n-1]&&coun[n+1]) sum--;          //连接两序列成为一个序列
    21     else if(!coun[n-1]&&!coun[n+1]) sum++;     //前无古人后无来者,本身是一个序列
    22     return sum;
    23 }
    24 
    25 int subtract(int n, int sum)
    26 {
    27     coun[n] = 0;
    28     if(coun[n-1]&&coun[n+1]) sum++;
    29     else if(!coun[n-1]&&!coun[n+1]) sum--;
    30     return sum;
    31 }
    32 
    33 int main()
    34 {
    35     int n, m, t, re[100005], i, temp, l, r, sum;
    36     scanf("%d", &t);
    37     while(t--)
    38     {
    39         scanf("%d %d", &n, &m);
    40         temp = sqrt(n);
    41         for(i=1; i<=n; i++)
    42         {
    43             scanf("%d", &a[i]);
    44             belong[i] = (i-1) / temp + 1;
    45         }
    46         for(i=1; i<=m; i++)
    47         {
    48             str[i].num = i;
    49             scanf("%d %d", &str[i].left, &str[i].right);
    50         }
    51         sort(str+1, str+m+1, cmp);
    52         l = 1;      //先走右,再走左
    53         r = 0;
    54         sum = 0;
    55         memset(coun, 0, sizeof(coun));
    56         for(i=1; i<=m; i++)
    57         {
    58             while(r < str[i].right)
    59             {
    60                 r++;                  //最终的右端点应该包括所求区间的右端点
    61                 sum = add(a[r], sum);
    62             }
    63             while(r > str[i].right)
    64             {
    65                 sum = subtract(a[r], sum);
    66                 r--;
    67             }
    68             while(l < str[i].left)
    69             {
    70                 sum = subtract(a[l], sum);
    71                 l++;
    72             }
    73             while(l > str[i].left)
    74             {
    75                 l--;
    76                 sum = add(a[l], sum);
    77             }
    78             re[str[i].num] = sum;
    79         }
    80         for(i=1; i<=m; i++)
    81         {
    82             printf("%d
    ", re[i]);
    83         }
    84     }
    85     return 0;
    86 }
  • 相关阅读:
    Java类 初始化块
    Java中 equals和==
    Raft算法
    OLTP与OLAP
    数据库事务机制ACID
    乐观锁和悲观锁
    SparkStreaming + Flume
    如何提高深度睡眠质量,科学解决睡眠问题
    centos 7 下安装mysql 远程连接出现10038错误如何解决
    CentOS 7.0 yum安装Apache、PHP和MySQL
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11298104.html
Copyright © 2020-2023  润新知