【BZOJ2038】[2009国家集训队]小Z的袜子(hose)【莫队】
来源:互联网 发布:别人的生活 知乎 编辑:程序博客网 时间:2024/06/02 19:01
裸的模板。
/* Footprints In The Blood Soaked Snow */#include <cstdio>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 50005, maxm = maxn, maxsqrtn = 224;int n, m, num[maxn], cnt[maxn];LL ans[maxm], deno[maxm];struct _query {int id, l, r;} q[maxm];inline int iread() {int f = 1, x = 0; char ch = getchar();for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';return f * x;}inline bool cmp(_query a, _query b) {return a.l / maxsqrtn < b.l / maxsqrtn || (a.l / maxsqrtn == b.l / maxsqrtn && a.r < b.r);}inline LL gcd(LL a, LL b) {for(; b; b ^= a ^= b ^= a %= b);return a;}int main() {n = iread(); m = iread();for(int i = 1; i <= n; i++) num[i] = iread();for(int i = 1; i <= m; i++) {int l = iread(), r = iread();q[i] = (_query){i, l, r};deno[i] = (LL)(q[i].r - q[i].l + 1) * (q[i].r - q[i].l);}sort(q + 1, q + 1 + m, cmp);LL tmp = 0;int l = 1, r = 0;for(int i = 1; i <= m; i++) {while(q[i].l < l) {l--;tmp += cnt[num[l]] << 1LL;cnt[num[l]]++;}while(l < q[i].l) {cnt[num[l]]--;tmp -= cnt[num[l]] << 1LL;l++;}while(q[i].r < r) {cnt[num[r]]--;tmp -= cnt[num[r]] << 1LL;r--;}while(r < q[i].r) {r++;tmp += cnt[num[r]] << 1LL;cnt[num[r]]++;}ans[q[i].id] = tmp;}for(int i = 1; i <= m; i++) {LL div = gcd(ans[i], deno[i]);printf("%lld/%lld\n", ans[i] / div, deno[i] / div);}return 0;}
0 0
- 【BZOJ2038】[2009国家集训队]小Z的袜子(hose)【莫队】
- 【bzoj2038】[2009国家集训队]小Z的袜子(hose) 莫队
- bzoj2038: [2009国家集训队]小Z的袜子(hose) 莫队
- [莫队] BZOJ2038: [2009国家集训队]小Z的袜子(hose)
- BZOJ2038 [2009国家集训队]小Z的袜子(hose) 莫队
- bzoj2038 [2009国家集训队]小Z的袜子(hose) (莫队)
- bzoj2038: [2009国家集训队]小Z的袜子(hose)
- bzoj2038 [2009国家集训队]小Z的袜子(hose) (分块)
- bzoj2038: [2009国家集训队]小Z的袜子(hose)
- bzoj2038【2009国家集训队】小Z的袜子(hose)
- BZOJ2038: [2009国家集训队]小Z的袜子(hose)
- bzoj2038 [2009国家集训队]小Z的袜子(hose)
- bzoj2038: [2009国家集训队]小Z的袜子(hose)
- 【bzoj2038】 [2009国家集训队]小Z的袜子(hose)
- 【bzoj2038】 [2009国家集训队]小Z的袜子(hose)
- BZOJ2038: [2009国家集训队]小Z的袜子(hose)
- 【bzoj2038】[2009国家集训队]小Z的袜子(hose)
- BZOJ2038 [2009国家集训队][小Z的袜子(hose)]
- kali windows双系统安装!
- 内存条相关学习
- Huawei 2014 机试题(续)
- (个人开源)nginx源码注释
- 多路IO复用
- 【BZOJ2038】[2009国家集训队]小Z的袜子(hose)【莫队】
- TCP连接:“三次握手,四次挥手”
- 前端不为人知的一面--前端冷知识集锦
- STL vector
- ViewTreeObserver.OnPreDrawListener()第一次无法监听或View第一次加载无图像;
- java中形参String的可变参数的定义(String...)
- 查找关键词----indexOf
- css3 media响应式
- 我该知道的技术