-
Notifications
You must be signed in to change notification settings - Fork 1
/
drAnsVortisNo.cpp
70 lines (52 loc) · 1 KB
/
drAnsVortisNo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
using namespace std;
#define MAX 1000001
bool isVortisNo(long n) {
int prev = n%2;
n /= 2;
while(n > 0) {
int b = n%2;
if(b == prev && b == 1) {
return true;
}
prev = b;
n /= 2;
}
return false;
}
int* getVortisCount(long *ar, int n) {
int *vortisNo = new int[n];
vortisNo[0] = isVortisNo(ar[0]) ? 1 : 0;
for(int i = 1; i < n; i++) {
vortisNo[i] = isVortisNo(ar[i]) ? vortisNo[i-1] + 1 : vortisNo[i-1];
}
for(int i = 0; i < n; i++) {
cout << vortisNo[i] << " ";
}
cout << endl;
return vortisNo;
}
int vortisNosInGivenRange(int *vortisNo, int l, int r) {
if(l == 0) {
return vortisNo[r];
}
return vortisNo[r] - vortisNo[l-1];
}
int main () {
int n, q;
cin >> n >> q;
long *ar = new long[n];
for(int i = 0; i < n; i++) {
cin >> ar[i];
}
int *vortisNo = getVortisCount(ar, n);
while(q--) {
int l, r;
cin >> l >> r;
int ans = vortisNosInGivenRange(vortisNo, l-1, r-1);
cout << ans << endl;
}
delete [] ar;
delete [] vortisNo;
return 0;
}