# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Name |
---|
The algorithm basically reduces to this: the result for a position i = v[i-k] & v[i-k+1] & ... &v[i] & ... & v[i+k-1] & v[i+k], not taking into account the fact that the array is cyclical. The algorithm that you propose is O(log(maximum value) * N), as you do a pass for each bit in the values of the array. However, we can achieve O(N * log(N)) by using a interval tree which calculates the & value for arbitrary subsegments for the array. By using this, we can do a single pass through the results, and, for each result, calculate the value of v[i-k] & ... & v[i+k] (we have a special case in which either i-k or i+k reaches past the end of the array. Then we have v[0] & ... & v[(i+k)%n] & v[(i-k)&n] & ... & v[n-1], if we index the array v from 0). Since log(N) ~= 15, whereas log(maximum value) ~= 31, with this technique we can halve the execution time.
(for example source code you can look here http://pastebin.com/vD8renXr . I implemented the segment tree stupidly in this solution, so it might be a bit hard to understand -- sorry :( )