2021“MINIEYE杯”中国大学生算法设计超级联赛(8)Counting Stars

Counting Stars

官方题解是这样的
这是官方题解

题目类似于求根号的和,就是对于第一种操作每一个数最多减少lon(a[i])次,那么总体用线段树单点修改的话就只要
修改n*lon(a[i])^2次,对于第二种操作,只对最高位有用,所以可以考虑开两个线段树来维护最高和除了最高位的其他数,最高位可以
区间修改,最低为位单点修改。

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define ll long long int
#define ms(a, b) memset(a, b, sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define se second
#define ull unsigned long long
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define endl "\n"
#define bug cout << "----acac----" << endl;
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
ll ksm(ll a, ll b, ll mod)
{
    ll ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = (a % mod * ans % mod) % mod;
        }
        b >>= 1;
        a = (a % mod * a % mod) % mod;
    }
    return ans;
}
ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}
const ll mod = 998244353;
const int maxn = 2e5 + 10;
const int maxm = 2e3 + 50;
const double eps = 1e-8;
const ll inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1);
ll b[maxn], c[maxn];
int n;
struct node
{
    int l, r;
    ll lazy;
    ll sum;
} a[maxn << 2][3];
void push_up(int rt)
{
    a[rt][1].sum = (a[lson][1].sum + a[rson][1].sum) % mod;
    a[rt][2].sum = (a[lson][2].sum + a[rson][2].sum) % mod;
}
void push_down(int rt)
{
    if (a[rt][2].lazy == 1)
        return;
    a[lson][2].lazy = a[lson][2].lazy * a[rt][2].lazy % mod;
    a[rson][2].lazy = a[rson][2].lazy * a[rt][2].lazy % mod;
    a[lson][2].sum = a[lson][2].sum * a[rt][2].lazy % mod;
    a[rson][2].sum = a[rson][2].sum * a[rt][2].lazy % mod;
    a[rt][2].lazy = 1;
}
void build(int l, int r, int rt)
{
    a[rt][1].l = a[rt][2].l = l;
    a[rt][1].r = a[rt][2].r = r;
    a[rt][1].lazy = a[rt][2].lazy = 1;
    if (l == r)
    {
        a[rt][1].sum = b[l]; //1表示低位
        a[rt][2].sum = c[l]; //2表示最高位
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, lson);
    build(mid + 1, r, rson);
    push_up(rt);
}
void update(int l, int r, int op, int rt)
{
    if (a[rt][2].sum == 0)
        return;
    if (op == 2 && a[rt][1].l == a[rt][1].r)
    {
        if (a[rt][1].sum == 0)
        {
            a[rt][2].sum = 0;
        }
        else
        {
            a[rt][1].sum -= lowbit(a[rt][1].sum);
        }
        return;
    }
    if(op==3)
    {
        if(l<=a[rt][1].l&&a[rt][1].r<=r)
        {
            a[rt][2].sum = a[rt][2].sum * 2 % mod;
            a[rt][2].lazy = a[rt][2].lazy * 2 % mod;
            return;
        }
    }
    push_down(rt);
    int mid = (a[rt][1].l + a[rt][1].r) >> 1;
    if (l <= mid)
        update(l, r, op, lson);
    if (r > mid)
        update(l, r, op, rson);
    push_up(rt);
}
ll query(int l,int r,int rt)
{
    if(a[rt][1].l>=l&&r>=a[rt][1].r)
    {
        return (a[rt][1].sum + a[rt][2].sum)%mod;
    }
    ll ans = 0;
    push_down(rt);
    int mid = (a[rt][1].l + a[rt][1].r) >> 1;
    if(l<=mid)
    {
        ans = (ans + query(l, r, lson)) % mod;
    }
    if(r>mid)
    {
        ans = (ans + query(l, r, rson)) % mod;
    }
    return ans;
}
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            scanf("%lld", &b[i]);
            for (int j = 30; j >= 0; j--)
            {
                if (((b[i] >> j) & 1))
                {
                    c[i] = (1 << j);
                    break;
                }
            }
            b[i] -= c[i];
        }
        build(1, n, 1);
        int m;
        scanf("%d", &m);
        while(m--)
        {
            int op, l, r;
            scanf("%d%d%d", &op, &l, &r);
            if(op==1)
            {
                printf("%lld\n", query(l, r, 1));
            }
            else
            {
                update(l, r, op, 1);
            }
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值