码迷,mamicode.com
首页 > 其他好文 > 详细

Zero Query SPOJ - ZQUERY

时间:2018-04-26 15:33:23      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:color   input   spoj   having   string   HERE   代码   sub   sqrt   

Given an array having N elements, each element is either -1 or 1.

You have M queries, each query has two numbers L and R, you have to answer the length of the longest subarray in range L to R (inclusive) that its sum is equal to 0.

Input

The first line contains two numbers N and M (1 <= N, M <= 50000) - the number of elements and the number of queries.

The second line contains N numbers - the elements of the array, each element is either -1 or 1.

In the next M lines, each line contains two numbers L and R (1 <= L <= R <= N).

Output

For each query, print the length of the longest subarray that satisfies the query in one line. If there isn‘t any such subarray, print 0.

Note

Subarray in an array is like substring in a string, i.e. subarray should contain contiguous elements.

Example

Input:
6 4
1 1 1 -1 -1 -1
1 3
1 4
1 5
1 6
Output:
0
2
4
6
题解:。。。真不知道咋说, 具体看代码吧,分块好懂,主要是找满足题意的subarry时,想到二分,最后注意一下lower_bound的细节。
  1 #pragma warning(disable:4996)
  2 #include<vector>
  3 #include<cmath>
  4 #include<cstdio>
  5 #include<cstring>
  6 #include<iostream>
  7 #include<algorithm>
  8 using namespace std;
  9 
 10 const int maxb = 300;
 11 const int maxm = 50010;
 12 const int maxn = 100020;
 13 
 14 int n, m;
 15 int a[maxm], Be[maxm], ans[maxb][maxb], use[maxn];
 16 
 17 vector<int> add[maxn];
 18 
 19 int main()
 20 {
 21     while (scanf("%d%d", &n, &m) != EOF) {
 22 
 23         for (int i = 0; i < maxn; i++) if (!add[i].empty()) add[i].clear();
 24 
 25         int unit = sqrt(n);  //每个块里面有多少个数
 26         int cnt;
 27         if (n % unit) cnt = n / unit + 1;
 28         else cnt = n / unit;
 29 
 30         a[0] = 50010;
 31         add[a[0]].push_back(0);
 32 
 33         for (int i = 1; i <= n; i++) {
 34             scanf("%d", a + i);
 35             a[i] += a[i - 1];        //前缀和
 36             add[a[i]].push_back(i);
 37             if (i % unit) Be[i] = i / unit + 1;     //每个元素属于哪个块
 38             else Be[i] = i / unit;
 39         }
 40         //预处理块与块之间的最长Subarray
 41         for (int i = 1; i <= n; i += unit) {
 42 
 43             memset(use, -1, sizeof(use));
 44             use[a[i - 1]] = i - 1;
 45 
 46             int temp = 0;
 47             int block = i / unit + 1;
 48 
 49             for (int j = i; j <= n; j++) {
 50                 if (use[a[j]] == -1) use[a[j]] = j;
 51                 else temp = max(temp, j - use[a[j]]);
 52                 if (j % unit == 0) ans[block][j / unit] = temp;
 53             }
 54             ans[block][cnt] = temp;
 55         }
 56 
 57         while (m--) {
 58 
 59             int x, y;
 60             scanf("%d%d", &x, &y);
 61             x--;
 62             //属于相邻的两块或者在同一块
 63             if (Be[x + 1] + 1 >= Be[y]) {
 64                 int len = 0;
 65                 for (int i = x; i < y; i++) {
 66                     if (add[a[i]].size() == 1) continue;
 67 
 68                     int pos = lower_bound(add[a[i]].begin(), add[a[i]].end(), y) - add[a[i]].begin();
 69 
 70                     if (pos == add[a[i]].size()) len = max(len, add[a[i]][pos - 1] - i);
 71                     else if (add[a[i]][pos] == y) len = max(len, add[a[i]][pos] - i);
 72                     else len = max(len, add[a[i]][pos - 1] - i);
 73                 }
 74                 printf("%d\n", len);
 75             }
 76             else {
 77                 int l = min(Be[x + 1] * unit, n);
 78                 int r = (Be[y] - 1) * unit + 1;
 79                 int len = ans[Be[x + 1] + 1][Be[y] - 1];
 80 
 81                 for (int i = x; i < l; i++) {
 82                     if (add[a[i]].size() == 1) continue;
 83 
 84                     int pos = lower_bound(add[a[i]].begin(), add[a[i]].end(), y) - add[a[i]].begin();
 85 
 86                     if (pos == add[a[i]].size()) len = max(len, add[a[i]][pos - 1] - i);
 87                     else if (add[a[i]][pos] == y) len = max(len, add[a[i]][pos] - i);
 88                     else len = max(len, add[a[i]][pos - 1] - i);
 89                 }
 90 
 91                 for (int i = r; i <= y; i++) {
 92                     int pos = lower_bound(add[a[i]].begin(), add[a[i]].end(), x) - add[a[i]].begin();
 93                     len = max(len, i - add[a[i]][pos]);
 94                 }
 95 
 96                 printf("%d\n", len);
 97             }
 98         }
 99     }
100     return 0;
101 }

 


Zero Query SPOJ - ZQUERY

标签:color   input   spoj   having   string   HERE   代码   sub   sqrt   

原文地址:https://www.cnblogs.com/zgglj-com/p/8952434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!