累加器(河南萌新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;
    }
}
相关推荐
liulilittle16 分钟前
IP校验和算法:从网络协议到SIMD深度优化
网络·c++·网络协议·tcp/ip·算法·ip·通信
bkspiderx2 小时前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法
中华小当家呐3 小时前
算法之常见八大排序
数据结构·算法·排序算法
沐怡旸4 小时前
【算法--链表】114.二叉树展开为链表--通俗讲解
算法·面试
一只懒洋洋4 小时前
K-meas 聚类、KNN算法、决策树、随机森林
算法·决策树·聚类
方案开发PCBA抄板芯片解密5 小时前
什么是算法:高效解决问题的逻辑框架
算法
songx_995 小时前
leetcode9(跳跃游戏)
数据结构·算法·游戏
小白狮ww6 小时前
RStudio 教程:以抑郁量表测评数据分析为例
人工智能·算法·机器学习
AAA修煤气灶刘哥6 小时前
接口又被冲崩了?Sentinel 这 4 种限流算法,帮你守住后端『流量安全阀』
后端·算法·spring cloud
kk”7 小时前
C语言快速排序
数据结构·算法·排序算法