Yet Another Data Structure Problem (ZOJ - 3998,双懒惰标记线段树)

一.题目链接:

ZOJ-3998

二.题目大意:

给你 n 个数 a[1 ~ n].

现有 3 种 m 次操作:

① l r v 将 a[l ~ r] 的数都乘以 v

② l r v 将 a[l ~ r] 的数变为 a[l]^v....a[r]^v

③ l r 查询 a[l] ×......× a[r].

三.分析:

双懒惰标记的线段树.

详见代码吧...

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-6
#define pi acos(-1.0)
#define ll long long int
using namespace std;

const int M = (int)1e5;
const int mod = 1000000007;

struct node
{
    int l;
    int r;
    ll w;
    ll f1;
    ll f2;
}tree[M * 4 + 5];

ll ans;

void build(int k, int l, int r)
{
    tree[k].l = l;
    tree[k].r = r;
    tree[k].f1 = tree[k].f2 = 1;
    if(l == r)
    {
        scanf("%lld", &tree[k].w);
        return;
    }
    int mid = (l + r) / 2;
    build(k * 2, l, mid);
    build(k * 2 + 1, mid + 1, r);
    tree[k].w = tree[k * 2].w * tree[k * 2 + 1].w % mod;
}

ll quick(ll a, ll b)
{
    ll sum = 1;
    while(b)
    {
        if(b & 1)
            sum = sum * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return sum % mod;
}

void push(int k)
{
    tree[k * 2].f2 = tree[k * 2].f2 * tree[k].f2 % (mod - 1);
    tree[k * 2 + 1].f2 = tree[k * 2 + 1].f2 * tree[k].f2 % (mod - 1);
    tree[k * 2].f1 = quick(tree[k * 2].f1, tree[k].f2) * tree[k].f1 % mod;
    tree[k * 2 + 1].f1 = quick(tree[k * 2 + 1].f1, tree[k].f2) * tree[k].f1 % mod;
    tree[k * 2].w = quick(tree[k * 2].w, tree[k].f2) * quick(tree[k].f1, tree[k * 2].r - tree[k * 2].l + 1) % mod;
    tree[k * 2 + 1].w = quick(tree[k * 2 + 1].w, tree[k].f2) * quick(tree[k].f1, tree[k * 2 + 1].r - tree[k * 2 + 1].l + 1) % mod;
    tree[k].f1 = tree[k].f2 = 1;
}

void interver(int k, int l, int r, int a, int b, ll c, bool flag)
{
    if(l >= a && r <= b)
    {
        if(!flag)
        {
            tree[k].f1 = tree[k].f1 * c % mod;
            tree[k].w = tree[k].w * quick(c, tree[k].r - tree[k].l + 1ll) % mod;
            return;
        }
        else
        {
            tree[k].w = quick(tree[k].w, c);
            tree[k].f1 = quick(tree[k].f1, c);
            tree[k].f2 = tree[k].f2 * c % (mod - 1);
            return;
        }
    }
    push(k);
    int mid = (l + r) / 2;
    if(a <= mid)
        interver(k * 2, l, mid, a, b, c, flag);
    if(mid < b)
        interver(k * 2 + 1, mid + 1, r, a, b, c, flag);
    tree[k].w = tree[k * 2].w * tree[k * 2 + 1].w % mod;
}

void query(int k, int l, int r, int a, int b)
{
    if(l >= a && r <= b)
    {
        ans = ans * tree[k].w % mod;
        return;
    }
    push(k);
    int mid = (l + r) / 2;
    if(a <= mid)
        query(k * 2, l, mid, a, b);
    if(mid < b)
        query(k * 2 + 1, mid + 1, r, a, b);
    tree[k].w = tree[k * 2].w * tree[k * 2 + 1].w % mod;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n, m;
        scanf("%d %d", &n, &m);
        build(1, 1, n);
        while((m--) > 0)
        {
            int dir, l, r;
            scanf("%d %d %d", &dir, &l, &r);
            if(dir <= 2)
            {
                ll v;
                scanf("%lld", &v);
                interver(1, 1, n, l, r, v, dir - 1);
            }
            else
            {
                ans = 1;
                query(1, 1, n, l, r);
                printf("%lld\n", ans);
            }
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值