赛氪-《二进制》

原题链接:

SaikrVj | 二进制

 

 把十进制数转换为若干 2的k次方 的和  k则为对应层数

如 3 = 2的0次方 + 2的1次方  所以在第0层和第1层

因为ai最大为1024,所以可以开十二个线段树,套线段树板子

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int,int>pii;
const int mod = 1e9+7 , INF = 0x3f3f3f3f , N = 1e5 + 10;


struct Node
{
    int l,r;
    LL s;
    LL add;
}tr[13][4 * N];
int n,m;

int lowbit(int x)
{
    return x & -x;
}

void push_down(int u,int x)
{
    Node &root = tr[u][x];
    Node &left = tr[u][x << 1];
    Node &right = tr[u][x << 1 | 1];
    
    left.add += root.add;
    left.s += (LL)(left.r - left.l + 1) * root.add;
    right.add += root.add;
    right.s += (LL)(right.r - right.l + 1) * root.add;
    root.add = 0;
}

void push_up(int u,int x)
{
    tr[u][x].s = tr[u][x << 1].s + tr[u][x << 1 | 1].s;
}

void build(int u,int x,int l,int r)
{
    tr[u][x] = {l,r};
    if (l == r)
        return;
    int mid = l + r >> 1;
    build(u,x << 1,l,mid);
    build(u,x << 1 | 1,mid + 1,r);
    push_up(u,x);
}

void modify(int u,int x,int l,int r,int v)
{
    if (tr[u][x].l >= l && tr[u][x].r <= r)
    {
        tr[u][x].s += (LL)(tr[u][x].r - tr[u][x].l + 1) * v;
        tr[u][x].add += v;
    }
    else
    {
        push_down(u,x);   
        int mid = tr[u][x].l + tr[u][x].r >> 1;
        if (l <= mid)
            modify(u,x << 1,l,r,v);
        if (r > mid)
            modify(u,x << 1 | 1,l,r,v);
        push_up(u,x);
    }
}

LL query(int u,int x,int l,int r)
{
    if (tr[u][x].l >= l && tr[u][x].r <= r)
        return tr[u][x].s; 
    push_down(u,x);
    int mid = tr[u][x].l + tr[u][x].r >> 1;
    
    LL res = 0;
    if (l <= mid)
        res = query(u,x << 1,l,r);
    if (r > mid)
        res += query(u,x << 1 | 1,l,r);
        
    return res;
}

int main()
{
    cin >> n >> m;
    for (int i = 0 ; i <= 12 ; i ++)
        build(i,1,1,n);
    
    while (m --)
    {
        int type;
        cin >> type;
        if (type == 1)
        {
            int x,l,r,c;
            cin >> x >> l >> r >> c;
            while (x)
            {
                modify(log2(lowbit(x)),1,l,r,c);
                x -= lowbit(x);
            }
        }
        else
        {
            int x,l,r;
            cin >> x >> l >> r;
            LL res = 0;
            while (x)
            {
                res += query(log2(lowbit(x)),1,l,r);
                x -= lowbit(x);
            }
            cout << res << endl;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值