Q 范围查询的数组中偶数和三元组的计数
给定一个大小为N的数组arr[]和(L, R)形式的Q个查询,任务是计算每个查询的L和R范围内的元素的偶数和的三元组数。
例子:
Input: N = 6, arr[ ] = {1, 2, 3, 4, 5, 6}, Q[ ] = {{1, 3}, {2, 5}}
Output: 1 2
Explanation:
For Query (1, 3): only triplet (1, 2, 3) exists with even sum
For Query (2, 5): Triplets (2, 3, 5) and (3, 4, 5) have even sum.
Hence the output is 1 and 2 respectively
Input: N = 3, arr[ ] = {1, 2, 5}, Q[ ] = {{1, 2}}
Output: 0
方法:上述问题可以通过观察来解决,即对于三元组和是偶数,元素必须是以下模式之一:
- 偶数+偶数+偶数=偶数
- 奇数 + 奇数 + 偶数 = 偶数
请按照以下步骤解决问题:
- 初始化两个数组,比如说arr_even[]和arr_odd[]长度为size + 1 。
- 初始化变量,比如even = 0和odd = 0 ,以存储偶数和奇数的计数。
- 遍历数组arr[]并为每个i存储偶数直到arr_even[i]中的索引i和奇数直到arr_odd[i]中的索引i 。
- 遍历查询Q[]和每一对(l, r) :
- 求(l, r)中的偶数计数为arr_even[r] – arr_even[l-1] ,求(l, r)中的奇数计数为arr_odd[r] – arr_odd[l-1]。
- 在变量 say ans中找到三元组的计数,作为(even*(even-1)*(even-2))/6和(odd*(odd-1)/2)*even 的总和。
- 最后,打印每个查询的答案。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to count number of triplets
// with even sum in range l, r for each
// query
void countTriplets(int size, int queries,
int arr[], int Q[][2])
{
// Initialization of array
int arr_even[size + 1], arr_odd[size + 1];
// Initialization of variables
int even = 0, odd = 0;
arr_even[0] = 0;
arr_odd[0] = 0;
// Traversing array
for (int i = 0; i < size; i++) {
// If element is odd
if (arr[i] % 2) {
odd++;
}
// If element is even
else {
even++;
}
// Storing count of even and odd
// till each i
arr_even[i + 1] = even;
arr_odd[i + 1] = odd;
}
// Traversing each query
for (int i = 0; i < queries; i++) {
int l = Q[i][0], r = Q[i][1];
// Count of odd numbers in l to r
int odd = arr_odd[r] - arr_odd[l - 1];
// Count of even numbers in l to r
int even = arr_even[r] - arr_even[l - 1];
// Finding the ans
int ans = (even * (even - 1)
* (even - 2))
/ 6
+ (odd * (odd - 1) / 2)
* even;
// Printing the ans
cout << ans << " ";
}
}
// Driver Code
int main()
{
// Given Input
int N = 6, Q = 2;
int arr[] = { 1, 2, 3, 4, 5, 6 };
int queries[][2] = { { 1, 3 }, { 2, 5 } };
// Function Call
countTriplets(N, Q, arr, queries);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// Function to count number of triplets
// with even sum in range l, r for each
// query
static void countTriplets(int size, int queries,
int[] arr, int[][] Q)
{
// Initialization of array
int[] arr_even = new int[size + 1];
int []arr_odd = new int[size + 1];
// Initialization of variables
int even = 0;
int odd = 0;
arr_even[0] = 0;
arr_odd[0] = 0;
// Traversing array
for (int i = 0; i < size; i++) {
// If element is odd
if (arr[i] % 2 == 1) {
odd++;
}
// If element is even
else {
even++;
}
// Storing count of even and odd
// till each i
arr_even[i + 1] = even;
arr_odd[i + 1] = odd;
}
// Traversing each query
for (int i = 0; i < queries; i++) {
int l = Q[i][0], r = Q[i][1];
// Count of odd numbers in l to r
odd = arr_odd[r] - arr_odd[l - 1];
// Count of even numbers in l to r
even = arr_even[r] - arr_even[l - 1];
// Finding the ans
int ans = (even * (even - 1) * (even - 2)) / 6
+ (odd * (odd - 1) / 2) * even;
// Printing the ans
System.out.print(ans + " ");
}
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int N = 6, Q = 2;
int[] arr = { 1, 2, 3, 4, 5, 6 };
int[][] queries = { { 1, 3 }, { 2, 5 } };
countTriplets(N, Q, arr, queries);
}
}
// This code is contributed by maddler.
Python3
# Python 3 program for above approach
# Function to count number of triplets
# with even sum in range l, r for each
# query
def countTriplets(size, queries, arr, Q):
# Initialization of array
arr_even = [0 for i in range(size + 1)]
arr_odd = [0 for i in range(size + 1)]
# Initialization of variables
even = 0
odd = 0
arr_even[0] = 0
arr_odd[0] = 0
# Traversing array
for i in range(size):
# If element is odd
if (arr[i] % 2):
odd += 1
# If element is even
else:
even += 1
# Storing count of even and odd
# till each i
arr_even[i + 1] = even
arr_odd[i + 1] = odd
# Traversing each query
for i in range(queries):
l = Q[i][0]
r = Q[i][1]
# Count of odd numbers in l to r
odd = arr_odd[r] - arr_odd[l - 1]
# Count of even numbers in l to r
even = arr_even[r] - arr_even[l - 1]
# Finding the ans
ans = (even * (even - 1)*(even - 2)) // 6 + (odd * (odd - 1) // 2)* even
# Printing the ans
print(ans,end = " ")
# Driver Code
if __name__ == '__main__':
# Given Input
N = 6
Q = 2
arr = [1, 2, 3, 4, 5, 6]
queries = [[1, 3],[2, 5]]
# Function Call
countTriplets(N, Q, arr, queries)
# This code is contributed by ipg2016107.
C#
// C# program for above approach
using System;
class GFG
{
// Function to count number of triplets
// with even sum in range l, r for each
// query
static void countTriplets(int size, int queries,
int[] arr, int[, ] Q)
{
// Initialization of array
int[] arr_even = new int[size + 1];
int[] arr_odd = new int[size + 1];
// Initialization of variables
int even = 0;
int odd = 0;
arr_even[0] = 0;
arr_odd[0] = 0;
// Traversing array
for (int i = 0; i < size; i++) {
// If element is odd
if (arr[i] % 2 == 1) {
odd++;
}
// If element is even
else {
even++;
}
// Storing count of even and odd
// till each i
arr_even[i + 1] = even;
arr_odd[i + 1] = odd;
}
// Traversing each query
for (int i = 0; i < queries; i++) {
int l = Q[i, 0], r = Q[i, 1];
// Count of odd numbers in l to r
odd = arr_odd[r] - arr_odd[l - 1];
// Count of even numbers in l to r
even = arr_even[r] - arr_even[l - 1];
// Finding the ans
int ans = (even * (even - 1) * (even - 2)) / 6
+ (odd * (odd - 1) / 2) * even;
// Printing the ans
Console.Write(ans + " ");
}
}
// Driver Code
public static void Main()
{
// Given Input
int N = 6, Q = 2;
int[] arr = { 1, 2, 3, 4, 5, 6 };
int[, ] queries = { { 1, 3 }, { 2, 5 } };
countTriplets(N, Q, arr, queries);
}
}
// This code is contributed by subham348.
Javascript
输出:
1 2
时间复杂度: O(N*Q)
辅助空间: O(N)