牛客第四场 子段乘积

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

题目链接

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

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

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

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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;
}

 

 

你可能感兴趣的:(数据结构)