用于乘法、替换和乘积的数组查询
这是我们提供的范围查询问题和大小为 N 的数组。给定 3 种类型的查询,您必须回答 M 个指定的查询。
类型 1 查询:将以 LRX 的形式为您提供 3 个值,在这种类型的查询中,必须将 x 乘以 L 到 R 范围内的数组元素。
类型 2 查询:在此查询中,您还将获得 3 个 LRY 形式的值,执行此类查询后,您将以第一个元素替换为 Y 的形式替换数组元素,第二个元素替换为2*Y 并且如下所示,在 L 到 R 的范围内。
类型 3 查询:在此您将获得 2 个值 L 和 R,在此您必须
求范围内所有数字的乘积。由于该数字可能非常大,因此您只需找到以十进制表示法表示的该数字的尾随零的数量。
例子:
Input : arr[] = {2, 4, 3, 5, 5|
queries[] = {{3 2 4}, {3 2 5}, {2 2 4 1},
{1 3 3 10}, {3 1 5}}
Output : 5
Explanation :
Since the first query is of type 3 so we multiply
the elements 4 * 3 * 5 = 60.
Since the second query is of type 3 so we multiply
the elements 4 * 3 * 5 * 5 = 300.
Since the third query is of type 2 and the value of
Y is 1 so after execution of this query the array
becomes [2, 1, 2, 3, 5].
Since the fourth query is of type 1 and the value of
x is 10 so after execution of this query the array
becomes [2, 1, 20, 3, 5].
Now the last query is of type 3 then we simply multiply
all the elements inclusive in the given range i.e.
2 * 1 * 20 * 3 * 5 = 600.
Now our task is to calculate the trailing zeros obtained
in the type 3 query i.e. 60 has 1 trailing zero, 300 has
2 trailing zeros and 600 has 2 trailing zeros so the
answer of this given input is 5.
方法一:
在这种情况下,我们可以简单地应用蛮力方法。在蛮力方法中,我们将在数组元素中应用所有操作,对于每个类型 3 查询,我们将获得的结果存储在一个新数组中,然后我们将计算由此获得的每个结果的尾随零的数量,然后计算所需的和。
该方法的复杂度为 O(m*n),因为我们将对给定的 m 个查询操作整个数组 m 次,并且需要一个大小为 m 的额外空间来保存在类型 3 查询中获得的结果以计算执行 m 个查询后尾随零的数量。
因此,时间复杂度为 O(m*n),空间复杂度为 O(m)。
方法二:
在这种方法中,我们有 2 个向量,因为尾随零的数字可以是 10 的倍数,而 10 是 2 和 5 的倍数,因此为此目的维护了两个单独的向量。其余的已在下面解释。
C++
// C++ program to solve three types of queries.
#include
using namespace std;
//vector of 1000 elements,
//all set to 0
vector twos(1000,0);
//vector of 1000 elements,
//all set to 0
vector fives(1000,0);
int sum = 0;
// Function to check number of
// trailing zeros in multiple of 2
int returnTwos(int val)
{
int count = 0;
while (val % 2 == 0 && val != 0) {
val = val / 2;
count++;
}
return count;
}
// Function to check number of
// trailing zeros in multiple of 5
int returnFives(int val)
{
int count = 0;
while (val % 5 == 0 && val != 0) {
val = val / 5;
count++;
}
return count;
}
// Function to solve the queries received
void solve_queries(int arr[], int n)
{
int type, ql, qr, x, y;
cin >> type;
// If the query is of type 1.
if (type == 1) {
cin >> ql >> qr >> x;
// Counting the number of
// zeros in the given value of x
int temp = returnTwos(x);
int temp1 = returnFives(x);
for (int i = ql - 1; i < qr; i++) {
// The value x has been multiplied
// to their respective indices
arr[i] = arr[i] * x;
// The value obtained above has been
// added to their respective vectors
twos[i] += temp;
fives[i] += temp1;
}
}
// If the query is of type 2.
if (type == 2) {
cin >> ql >> qr >> y;
// Counting the number of
// zero in the given value of x
int temp = returnTwos(y);
int temp1 = returnFives(y);
for (int i = ql - 1; i < qr; i++) {
// The value y has been replaced
// to their respective indices
arr[i] = (i - ql + 2) * y;
// The value obtained above has been
// added to their respective vectors
twos[i] = returnTwos(i - ql + 2) + temp;
fives[i] = returnFives(i - ql + 2) + temp1;
}
}
// If the query is of type 2
if (type == 3) {
cin >> ql >> qr;
int sumtwos = 0;
int sumfives = 0;
for (int i = ql - 1; i < qr; i++) {
// as the number of trailing zeros for
// each case has been found for each array
// element then we simply add those to
// the respective index to a variable
sumtwos += twos[i];
sumfives += fives[i];
}
// Compare the number of zeros
// obtained in the multiples of five and two
// consider the minimum of them and add them
sum += min(sumtwos, sumfives);
}
}
// Driver code
int main()
{
int n, m;
// Input the Size of array
// and number of queries
cin >> n >> m;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
twos[i] = returnTwos(arr[i]);
fives[i] = returnFives(arr[i]);
}
// Running the while loop
// for m number of queries
while (m--) {
solve_queries(arr, n);
}
cout << sum << endl;
return 0;
}
Java
// Java program to solve three types of queries.
import java.io.*;
import java.util.*;
import java.util.Arrays;
class GFG{
static Scanner sc= new Scanner(System.in);
// Vector of 1000 elements,
// all set to 0
static int twos[] = new int[1000];
// Vector of 1000 elements,
// all set to 0
static int fives[] = new int[1000];
static int sum = 0;
// Function to check number of
// trailing zeros in multiple of 2
static int returnTwos(int val)
{
int count = 0;
while (val % 2 == 0 && val != 0)
{
val = val / 2;
count++;
}
return count;
}
// Function to check number of
// trailing zeros in multiple of 5
static int returnFives(int val)
{
int count = 0;
while (val % 5 == 0 && val != 0)
{
val = val / 5;
count++;
}
return count;
}
// Function to solve the queries received
static void solve_queries(int arr[], int n)
{
int type = sc.nextInt();
// If the query is of type 1.
if (type == 1)
{
int ql = sc.nextInt();
int qr = sc.nextInt();
int x = sc.nextInt();
// Counting the number of
// zeros in the given value of x
int temp = returnTwos(x);
int temp1 = returnFives(x);
for(int i = ql - 1; i < qr; i++)
{
// The value x has been multiplied
// to their respective indices
arr[i] = arr[i] * x;
// The value obtained above has been
// added to their respective vectors
twos[i] += temp;
fives[i] += temp1;
}
}
// If the query is of type 2.
if (type == 2)
{
int ql = sc.nextInt();
int qr = sc.nextInt();
int y = sc.nextInt();
// Counting the number of
// zero in the given value of x
int temp = returnTwos(y);
int temp1 = returnFives(y);
for(int i = ql - 1; i < qr; i++)
{
// The value y has been replaced
// to their respective indices
arr[i] = (i - ql + 2) * y;
// The value obtained above has been
// added to their respective vectors
twos[i] = returnTwos(i - ql + 2) + temp;
fives[i] = returnFives(i - ql + 2) + temp1;
}
}
// If the query is of type 2
if (type == 3)
{
int ql = sc.nextInt();
int qr = sc.nextInt();
int sumtwos = 0;
int sumfives = 0;
for(int i = ql - 1; i < qr; i++)
{
// As the number of trailing zeros for
// each case has been found for each array
// element then we simply add those to
// the respective index to a variable
sumtwos += twos[i];
sumfives += fives[i];
}
// Compare the number of zeros
// obtained in the multiples of five and two
// consider the minimum of them and add them
sum += Math.min(sumtwos, sumfives);
}
}
// Driver code
public static void main(String[] args)
{
// Input the Size of array
// and number of queries
int n = sc.nextInt();
int m = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
twos[i] = returnTwos(arr[i]);
fives[i] = returnFives(arr[i]);
}
// Running the while loop
// for m number of queries
while (m-- != 0)
{
solve_queries(arr, n);
}
System.out.println(sum);
}
}
// This code is contributed by SHUBHAMSINGH10
Python3
# Python3 program to solve three types of queries.
# vector of 1000 elements,
# all set to 0
twos = [0] * 1000
# vector of 1000 elements,
# all set to 0
fives = [0] * 1000
sum = 0
# Function to check number of
# trailing zeros in multiple of 2
def returnTwos(val):
count = 0
while (val % 2 == 0 and val != 0):
val = val // 2
count += 1
return count
# Function to check number of
# trailing zeros in multiple of 5
def returnFives(val):
count = 0
while (val % 5 == 0 and val != 0):
val = val // 5
count += 1
return count
# Function to solve the queries received
def solve_queries(arr, n):
global sum
arrr1 = list(map(int,input().split()))
type = arrr1[0]
# If the query is of type 1.
if (type == 1):
ql, qr, x = arrr1[1], arrr1[2], arrr1[3]
# Counting the number of
# zeros in the given value of x
temp = returnTwos(x)
temp1 = returnFives(x)
i = ql - 1
while(i < qr):
# The value x has been multiplied
# to their respective indices
arr[i] = arr[i] * x
# The value obtained above has been
# added to their respective vectors
twos[i] += temp
fives[i] += temp1
i += 1
# If the query is of type 2.
if (type == 2):
ql, qr, y = arrr1[1], arrr1[2], arrr1[3]
# Counting the number of
# zero in the given value of x
temp = returnTwos(y)
temp1 = returnFives(y)
i = ql - 1
while(i < qr):
# The value y has been replaced
# to their respective indices
arr[i] = (i - ql + 2) * y
# The value obtained above has been
# added to their respective vectors
twos[i] = returnTwos(i - ql + 2) + temp
fives[i] = returnFives(i - ql + 2) + temp1
i += 1
# If the query is of type 2
if (type == 3):
ql, qr = arrr1[1], arrr1[2]
sumtwos = 0
sumfives = 0
i = ql - 1
while(i < qr):
# As the number of trailing zeros for
# each case has been found for each array
# element then we simply add those to
# the respective index to a variable
sumtwos += twos[i]
sumfives += fives[i]
i += 1
# Compare the number of zeros
# obtained in the multiples of five and two
# consider the minimum of them and add them
sum += min(sumtwos, sumfives)
# Driver code
# Input the Size of array
# and number of queries
n, m = map(int, input().split())
arr = list(map(int, input().split()))
for i in range(n):
twos[i] = returnTwos(arr[i])
fives[i] = returnFives(arr[i])
# Running the while loop
# for m number of queries
while (m):
m -= 1
solve_queries(arr, n)
print(sum)
# This code is contributed by SHUBHAMSINGH10
输入:
5 5
2 4 3 5 5
3 2 4
3 2 5
2 2 4 1
1 3 3 10
3 1 5
输出:
5
这段代码的复杂度是 O(n*q)。