累加器(河南萌新2024)

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> PII;
#define pb emplace_back
//#define int ll
#define all(a) a.begin(),a.end()
#define x first
#define y second
#define ps push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

void solve();

const int N = 2e6 + 10;

signed main() {
    IOS;
    solve();
    return 0;
}

ll sb(ll a)
{
    ll ans = 0;
    ll a1 = a-1,a2 = a;
    for(int i = 0; i <= 30; ++ i)
    {
        if((a1&(1<<i)) != (a2&(1<<i)))
            ans ++;
    }
    return ans;
}

void solve() {
    vector<ll> a(N);
    for(int i = 1; i < N; ++ i)
    {
        a[i] = sb(i);
        a[i] += a[i-1];
    }
    ll t;
    cin >> t;
    while(t--)
    {
        ll x,y;
        cin >> x >> y;
        y = x + y;
        cout << a[y] - a[x] << endl;
    }
}
相关推荐
iuu_star8 分钟前
C语言数据结构-顺序查找、折半查找
c语言·数据结构·算法
Yzzz-F15 分钟前
P1558 色板游戏 [线段树 + 二进制状态压缩 + 懒标记区间重置]
算法
漫随流水22 分钟前
leetcode算法(515.在每个树行中找最大值)
数据结构·算法·leetcode·二叉树
mit6.8241 小时前
dfs|前后缀分解
算法
扫地的小何尚1 小时前
NVIDIA RTX PC开源AI工具升级:加速LLM和扩散模型的性能革命
人工智能·python·算法·开源·nvidia·1024程序员节
千金裘换酒2 小时前
LeetCode反转链表
算法·leetcode·链表
byzh_rc3 小时前
[认知计算] 专栏总结
线性代数·算法·matlab·信号处理
qq_433554544 小时前
C++ manacher(求解回文串问题)
开发语言·c++·算法
歌_顿4 小时前
知识蒸馏学习总结
人工智能·算法
闲看云起4 小时前
LeetCode-day6:接雨水
算法·leetcode·职场和发展