Zero Query SPOJ - ZQUERY

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 }

 


转载于:https://www.cnblogs.com/zgglj-com/p/8952434.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值