Cattle off 3005C- product sub-segment - segment tree

Link: https: //ac.nowcoder.com/acm/contest/3005/C
Source: Cattle-off network

Subject description:

A given length n number of rows a1, a2, ..., an, is the product of its length continuously seeking subsegment k modulo the maximum value of 998,244,353 remainders.

Enter a description:

The first line of two integers n, k.
The second row of n integer, a1, a2, ..., an .

Output Description:

Output an integer representing the largest remainder.

Sample input:

5 3
1 2 3 0 8

Sample output:

6

main idea:

Segment tree, when the contribution assignment, no update, n-k + 1 sub-interval query.

code show as below:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=2e5+20;
const ll mo=998244353;
struct node{
	int l,r;
	ll v;
}tr[N<<2];
void pushup(int m)
{
	tr[m].v=tr[m<<1].v*tr[m<<1|1].v%mo;
	return;
}
void build(int m,int l,int r)
{
	tr[m].l=l;
	tr[m].r=r;
	if(l==r)
	{
		scanf("%lld",&tr[m].v);
		return;
	}
	int mid=(l+r)>>1;
	build(m<<1,l,mid);
	build(m<<1|1,mid+1,r);
	pushup(m);
	return;
}
ll query(int m,int l,int r)
{
	if(tr[m].l==l&&tr[m].r==r)
		return tr[m].v;
	int mid=(tr[m].l+tr[m].r)>>1;
	if(r<=mid)
		return query(m<<1,l,r);
	if(l>mid)
		return query(m<<1|1,l,r);
	return query(m<<1,l,mid)*query(m<<1|1,mid+1,r)%mo;
}
int main()
{
	int n,k;
	cin>>n>>k;
	build(1,1,n);
	ll ans=0;
	for(int i=1;i+k-1<=n;i++)
		ans=max(ans,query(1,i,i+k-1));
	cout<<ans<<endl;
	return 0;
}
Published 144 original articles · won praise 135 · views 10000 +

Guess you like

Origin blog.csdn.net/Nothing_but_Fight/article/details/104267766