Group (Mo + Team block)

 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

The meaning of problems:
Q sequence given number of consecutive sections, such as: 2,6 3 1 7, 9 are considered contiguous sequence

Thinking:
n-maximum 1e5, m maximum 1E5, if pure violence, 1E10, will burst, it is clear that the ordinary block can not be resolved,
so the team with Mo, the first block, and then to the sorting section,
the use of L, the movement r an array of all the results obtained to go again offline intervals, with the final output

Offline is known in advance and known to all the requirements of the situation, which is essential for the team Mo conditions of use, or take what sort it

About ordering interval :
range of sequencing, look at the left are in the same block,
if the piece that is left from the past, then left on moving big of a deal, then press right from small to large
if not in one, that is, from left far left have moved to remove the risk, you press the left from small to large

 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      the else  return x.left < y.left;
 15  }
 16  
. 17  int the Add ( int n-, int SUM)
 18 is  {
 . 19      coun- [n-] = . 1 ;
 20 is      IF (coun- [N- . 1 ] && coun- [n-+ . 1 ]) SUM -;           // connect two sequence into a sequence of 
21 is      the else  IF (coun- [N-! . 1 ] && coun- [n-+! . 1 SUM ++;])      // no before or since, in itself is a sequence of 
22 is      return SUM;
 23 is  }
 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 is          {
 58              the while (R & lt < STR [I] .right)
 59              {
 60                  R & lt ++;                   // ultimate right end point should include the required range right end point 
61 is                  SUM = the Add (A [R & lt ], SUM);
 62 is              }
 63 is              the while (R & lt> STR [I] .right)
 64              {
 65                  SUM = Subtract (A [R & lt], SUM);
 66                  r-- ;
 67              }
 68              the 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\n", re[i]);
83         }
84     }
85     return 0;
86 }

 

Guess you like

Origin www.cnblogs.com/0xiaoyu/p/11298104.html