累加器(河南萌新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;
    }
}
相关推荐
2401_8414956410 小时前
【计算机视觉】基于数学形态学的保留边缘图像去噪
人工智能·python·算法·计算机视觉·图像去噪·数学形态学·边缘保留
十八岁讨厌编程10 小时前
【算法训练营Day30】动态规划part6
算法·动态规划
CoderYanger10 小时前
优选算法-双指针:2.复写零
java·后端·算法·leetcode·职场和发展
charlie11451419111 小时前
理解C++20的革命特性——协程支持2:编写简单的协程调度器
c++·学习·算法·设计模式·c++20·协程·调度器
hadage23311 小时前
--- 常见排序算法汇总 ---
算法·排序算法
Mrs.Gril11 小时前
目标检测:yolov7算法在RK3588上部署
算法·yolo·目标检测
WWZZ202512 小时前
ORB_SLAM2原理及代码解析:单应矩阵H、基础矩阵F求解
线性代数·算法·计算机视觉·机器人·slam·基础矩阵·单应矩阵
2401_8414956413 小时前
【计算机视觉】分水岭实现医学诊断
图像处理·人工智能·python·算法·计算机视觉·分水岭算法·医学ct图像分割
liulilittle13 小时前
网络编程基础算法剖析:从字节序转换到CIDR掩码计算
开发语言·网络·c++·算法·通信
Kent_J_Truman13 小时前
【第几小 / 分块】
算法·蓝桥杯