当前位置: 首页 > news >正文

普通莫队板子

时间复杂度为:\(O(n * \sqrt{m})\), n为数组长度,m为查询次数。

板子代码

int n, m, k;/*
a[]记录原数组。
B为块长。
res记录当前区间的答案。
c[]为辅助数组,帮助O(1)转移区间答案。
ans[]记录查询答案。
*/
int a[N];
int B, res, c[N], ans[N];/*
记录查询,以左端点所在块的块号为第一关键字,升序排序;
以右端点为第二关键字,根据块号奇偶性优化排序:(块号为奇数,升序;块号为偶数,降序)。
*/
struct query{int l, r, id;bool operator <(const query& x) const{if(l / B != x.l / B) return l < x.l;if((l / B) & 1) return r < x.r;else return r > x.r;}
}q[N];void add(int x){/* 区间外扩1格对答案的改变 */
}void del(int x){/* 区间收缩1格对答案的改变 */
}void solve(){/* 输入 */cin >> n >> m >> k;for(int i = 1; i <= n; i ++) cin >> a[i];/* 记录查询,离线处理。 */for(int i = 1; i <= m; i ++){cin >> q[i].l >> q[i].r;q[i].id = i;}/* 块长最优为 n / sqrt(m), 能保证无论数据n,m是什么,时间复杂度都是O(n * sqrt(m))。 */B = n / sqrt(m);/* 边界判断,防止B为0的特殊情况,这个非常重要。 */if(B == 0) B = 1;/* 排序 */sort(q + 1, q + 1 + m);/*处理询问。 */for(int i = 1, l = 0, r = 0; i <= m; i ++){/* 顺序很重要,要先扩张区间,然后再收缩区间,防止l > r。 */while(l > q[i].l) add(a[--l]);while(r < q[i].r) add(a[++r]);while(l < q[i].l) del(a[l++]);while(r > q[i].r) del(a[r--]);ans[q[i].id] = res - 1;}/* 输出 */for(int i = 1; i <= m; i ++) cout << ans[i] << endl;
}

示例题目

示例代码

#include <bits/stdc++.h>
#define int long long 
using namespace std;#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define PII pair<int, int>
#define lowbit(x) ((x) & (-(x)))
#define all(a) a.begin(), a.end() 
#define debug(x) cout << #x << " = " << (x) << "\n";
#define vdebug(a) cout << #a << " = "; for(auto& x: a) cout << x << " "; cout << "\n";
#define vlrdebug(a, l, r) cout << #a << " = "; for(auto i = l; i <= r; i ++) cout << a[i] << " "; cout << "\n";
#define lc ((p) << 1)
#define rc ((p) << 1 | 1)const int N = 1e6 + 10, M = 1010;
const int mod = 1e9 + 7, MOD = 998244353;
const int INF = 0x3f3f3f3f;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};int n, m, k;
int a[N];
int B, res, c[N], ans[N];struct query{int l, r, id;bool operator <(const query& x) const{if(l / B != x.l / B) return l < x.l;if((l / B) & 1) return r < x.r;else return r > x.r;}
}q[N];void add(int x){res += 2 * c[x] + 1;c[x] ++;
}void del(int x){res -= 2 * c[x] - 1;c[x] --;
}void solve(){cin >> n >> m >> k;for(int i = 1; i <= n; i ++) cin >> a[i];for(int i = 1; i <= m; i ++){cin >> q[i].l >> q[i].r;q[i].id = i;}B = n / sqrt(m);if(B == 0) B = 1;sort(q + 1, q + 1 + m);for(int i = 1, l = 0, r = 0; i <= m; i ++){while(l > q[i].l) add(a[--l]);while(r < q[i].r) add(a[++r]);while(l < q[i].l) del(a[l++]);while(r > q[i].r) del(a[r--]);ans[q[i].id] = res - 1;}for(int i = 1; i <= m; i ++) cout << ans[i] << endl;
}signed main(){ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int _ = 1;// cin >> _;while(_ --) solve();return 0;
} 
http://www.rkmt.cn/news/73355.html

相关文章:

  • 2025最新推荐!AI写作工具测评榜单,学术价值最大化
  • 2025年面包培训正规厂商推荐,专业面包培训公司与学校排名全
  • 基于MATLAB的最小生成树求解
  • 2025年钛棒过滤器权威榜单揭晓!上海青上过滤以技术革新领跑行业
  • 冒号排序
  • 微孔板恒温振荡器哪家性价比高?瑞诚仪器产品质优价廉
  • AI真的太好用啦!Aspire Dashboard集成GitHub Copilot。
  • 2025年酒精定制源头口碑排行,高复购率无水乙醇推荐,目前酒精源头厂家技术实力与市场典范解析
  • 外贸出海企业必看:上海、苏州、无锡地区优秀海外营销推广代运营公司盘点(2025年12月新版)
  • 2025年度不锈钢衣柜加盟TOP5权威推荐:甄选代理项目抢占
  • 最大似然优化与交叉熵(CE)多高斯混合估计算法的应用
  • Git 提交规范
  • 2025年下半年江苏徐州油浸式变压器品牌综合评估与选购指南
  • 2025年军用正射成图:无人机蜂群系统的关键价值与优选供应商
  • 2025年江苏保冷柜生产厂家综合评述与推荐
  • 根据某张表更新另一张表字段
  • 习题解析之:快餐数据查询
  • 软件工程日报
  • 2025年下半年江苏加温柜生产厂家综合评估与推荐
  • 2025年度不锈钢防刮花台面厂家推荐,专业强的不锈钢防刮花台
  • 2025年下半年江苏徐州干式变压器品牌综合推荐与选购指南
  • 2025年下半年徐州永磁工业风扇工厂综合推荐榜单与选择指南
  • 2025年厂房装修TOP推荐排行榜,上海天澜:厂房装修设计/食品厂房装修/化妆品厂房装修/工厂厂房装修/车间厂房装修权威企业
  • Redis大Key问题怎么解决
  • 2025 锁紧螺母公司TOP5权威排名
  • 西城微科的体重秤方案开发之路-方案开发商
  • 2025年下半年北京央国企就业服务机构精选推荐:五家优质服务商深度解析
  • 上海GEO公司十大排名推荐:优扬集团以全域智造引领行业变革
  • 2025年下半年江苏徐州工业吊扇厂家综合评估与选购指南
  • 国标GB28181算法算力平台EasyGBS全终端实时视频监控方案解析