Beautiful Segments
You are given an array, , consisting of integers.
A segment, , is beautiful if and only if the bitwise AND of all numbers in with indices in the inclusive range of is not greater than . In other words, segment is beautiful if .
You must answer queries. Each query, , consists of integers: , , and . The answer for each is the number of beautiful segments such that and .
Input Format
The first line contains two space-separated integers, (the number of integers in ) and (the number of queries).
The second line contains space-separated integers, where the integer denotes the element of array .
Each line of the subsequent lines contains space-separated integers, , , and , respectively, describing query .
Constraints
- holds for test cases worth at least of the problem's score.
- holds for test cases worth at least of the problem's score.
Output Format
Print lines, where the line contains the number of beautiful segments for query .
Sample Input
5 3
1 2 7 3 4
1 5 3
2 4 6
3 5 2
Sample Output
13
5
2
Explanation
The beautiful segments for all queries are listed below.
Query 0: The beautiful segments are .
Query 1: The beautiful segments are .
Query 2: The beautiful segments are .
xxxxxxxxxx
using namespace std;
vector<string> split_string(string);
// Complete the solve function below.
vector<int> solve(vector<int> a, vector<vector<int>> queries) {
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string nq_temp;
getline(cin, nq_temp);
vector<string> nq = split_string(nq_temp);
int n = stoi(nq[0]);
int q = stoi(nq[1]);
string a_temp_temp;
getline(cin, a_temp_temp);
vector<string> a_temp = split_string(a_temp_temp);
vector<long> a(n);
for (int a_itr = 0; a_itr < n; a_itr++) {
long a_item = stol(a_temp[a_itr]);
a[a_itr] = a_item;
}
vector<vector<long>> queries(q);
for (int queries_row_itr = 0; queries_row_itr < q; queries_row_itr++) {
queries[queries_row_itr].resize(3);
for (int queries_column_itr = 0; queries_column_itr < 3; queries_column_itr++) {
cin >> queries[queries_row_itr][queries_column_itr];
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
vector<int> result = solve(a, queries);
for (int result_itr = 0; result_itr < result.size(); result_itr++) {
fout << result[result_itr];
if (result_itr != result.size() - 1) {
fout << "\n";
}
}
fout << "\n";
fout.close();
return 0;
}
vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector<string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
return splits;
}