Luogu P2709 小B的询问
https://www.luogu.com.cn/problem/P2709#submit
小B的询问
题目描述
小B 有一个长为
他一共有
其中
小B请你帮助他回答询问。
输入格式
第一行三个整数
第二行
接下来的
输出格式
输出
样例 #1
样例输入 #1
6 4 3 1 3 2 1 1 3 1 4 2 6 3 5 5 6
样例输出 #1
6 9 5 2
提示
【数据范围】
对于
//标准莫队板子 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; int n, m, k, block; int l = 1, r = 0, out[5211314], ans; int a[5211314], pos[5211314], cnt[5211314]; struct ASK { int l, r, id; }ask[5211314]; inline bool cmp(ASK a, ASK b) { if (pos[a.l] != pos[b.l]) return pos[a.l] < pos[b.l]; else return a.r < b.r; } inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch - '0'); ch = getchar(); } return x * f; } inline void Add(int x) { ans = ans + 2 * cnt[x] * 1 + 1; cnt[x] ++; return; } inline void Del(int x) { ans = ans - 2 * cnt[x] * 1 + 1; cnt[x] --; return; } int main() { n = read(); m = read(); k = read(); block = sqrt(n); for (int i = 1; i <= n; ++ i) { a[i] = read(); pos[i] = (i - 1) / block + 1; } for (int i = 1; i <= m; ++ i) { ask[i].l = read(); ask[i].r = read(); ask[i].id = i; } sort(ask + 1, ask + 1 + m, cmp); for (int i = 1; i <= m; ++ i) { while (ask[i].l < l) Add(a[-- l]); while (l < ask[i].l) Del(a[l ++]); while (r < ask[i].r) Add(a[++ r]); while (ask[i].r < r) Del(a[r --]); out[ask[i].id] = ans; } for (int i = 1; i <= m; ++ i) { printf("%d\n", out[i]); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现