牛客第四场 子段乘积

线段树的运用 线段树真np 真好用,真强

题目链接

https://ac.nowcoder.com/acm/contest/3005/C

直接扫一遍维护区间 会出现 遇见 0 处理麻烦的问题

直接用线段树 这一问题将会不复存在 将普通的区间加 变成 区间 * 就可!

#include <iostream>
#include <malloc.h>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#define IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
typedef long long LL;
int dis[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
const int maxn = 200000 + 10;
const LL mod = 998244353;
const int inf = 0x3f3f3f3f;
LL a[maxn];
LL c[maxn << 2];
void creat(int node, int L, int R)
{
    if (L == R)
    {
        c[node] = a[L];
        return;
    }
    int mid = (L + R) >> 1;
    creat(node << 1, L, mid);
    creat(node << 1 | 1, mid + 1, R);
    c[node] = c[node << 1 | 1] * c[node << 1] % mod;
}
LL query(int node, int L, int R, int start, int end) // L, R  为查询区间
{
    if (start > R || end < L)
    {
        return 1;
    }
    if (start >= L && end <= R)
    {
        return c[node];
    }
    else
    {
        int mid = (start + end) >> 1;
        LL lans = 1;
        LL rans = 1;
        if (mid >= L)
            lans = query(node << 1, L, R, start, mid) % mod;
        if (mid + 1 <= R)
            rans = query(node << 1 | 1, L, R, mid + 1, end) % mod;
        return (lans * rans) % mod;
    }
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    IO;
    int n, k;
    LL ans = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    creat(1, 1, n);
    for (int i = 1; i + k - 1 <= n; i++)
    {
        int x = i;
        int y = i + k - 1;
        ans = max(ans, query(1, x, y, 1, n) % mod) % mod;
    }
    cout << ans;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值