BZOJ 2038 小Z的袜子(hose)(莫队算法)
来源:互联网 发布:外卖送餐软件 编辑:程序博客网 时间:2024/04/28 18:18
题意:
中文题面
分析:
对于[L,R]的询问,设其中颜色为x,y,z,...的袜子的个数为a,b,c,...
那么ans(L,R)=C2a+C2b+C2c+...C2R−L+1=(a∗(a−1)/2+b∗(b−1)/2+c∗(c−1)/2+...)(R−L+1)∗(R−L)/2)
=a2+b2+c2+...−(a+b+c+...)(R−L+1)∗(R−L)=a2+b2+c2+...−(R−L+1)(R−L+1)∗(R−L)
所以这道题目的关键是求一个区间内每种颜色数目的平方和
关于莫队具体的可以看这里, 戳我查看
莫队均摊复杂度为O(n1.5),证明如下:
对于每一块内的所有询问,r是单调递增的,总变化范围为0∼n,这部分总复杂度为O(n);
l的变化范围为0∼n−−√,每块内平均有n−−√个询问,这部分复杂度为O(n−−√⋅n−−√)=O(n)
总共有n−−√块询问,总体均摊复杂度为O(n−−√⋅(n+n))=O(2nn−−√)=O(n1.5) ■
代码(区间表示为
//// Created by TaoSama on 2016-01-25// Copyright (c) 2015 TaoSama. All rights reserved.//#pragma comment(linker, "/STACK:1024000000,1024000000")#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;#define pr(x) cout << #x << " = " << x << " "#define prln(x) cout << #x << " = " << x << endlconst int N = 5e4 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;const int B = 250;typedef long long LL;int n, q;int c[N], cnt[N];LL sum, up[N], dw[N];struct Query { int l, r, id; bool operator< (const Query& q) const { return r < q.r; }};void update(int i, int delta) { sum -= cnt[c[i]] * cnt[c[i]]; cnt[c[i]] += delta; sum += cnt[c[i]] * cnt[c[i]];}int main() {#ifdef LOCAL freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);#endif ios_base::sync_with_stdio(0); while(scanf("%d%d", &n, &q) == 2) { for(int i = 1; i <= n; ++i) scanf("%d", c + i); memset(cnt, 0, sizeof cnt); vector<vector<Query> > Q(n / B + 2, vector<Query>()); for(int i = 1; i <= q; ++i) { int l, r; scanf("%d%d", &l, &r); Q[l / B].push_back(Query {l, ++r, i}); } for(int i = 0; i <= n / B; ++i) sort(Q[i].begin(), Q[i].end()); sum = 0; for(int i = 0; i < Q.size(); ++i) { int l, r; l = r = i * B; for(int j = 0; j < Q[i].size(); ++j) { Query &q = Q[i][j]; while(r < q.r) update(r++, 1); while(l < q.l) update(l++, -1); while(l > q.l) update(--l, 1); up[q.id] = sum - r + l; dw[q.id] = 1LL * (r - l) * (r - l - 1); LL g = __gcd(up[q.id], dw[q.id]); up[q.id] /= g; dw[q.id] /= g; } for(int j = l; j < r; ++j) update(j, -1); } for(int i = 1; i <= q; ++i) printf("%lld/%lld\n", up[i], dw[i]); } return 0;}
0 0
- 小Z的袜子(hose) - bzoj 2038 莫队算法
- BZOJ 2038 小Z的袜子(hose) 莫队算法
- BZOJ-2038 小Z的袜子(hose) (莫队算法)
- BZOJ 2038 小Z的袜子(hose) [莫队算法]
- (莫队算法)2038: 小Z的袜子(hose)
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)(莫队算法)
- BZOJ 2038 小Z的袜子(hose)(莫队算法)
- BZOJ-2038 小Z的袜子(hose) 莫队算法
- BZOJ 2038 小Z的袜子(hose)(莫队算法)
- BZOJ 2038 小Z的袜子(hose) (莫队算法入门题)
- BZOJ 2038 小Z的袜子(hose)( 莫队算法)
- bzoj 2038: [2009国家集训队]小Z的袜子(hose)(莫队算法)
- BZOJ 2038([2009国家集训队]小Z的袜子(hose)-莫队算法序列)
- BZOJ 2038 2009国家集训队 小Z的袜子(hose) 莫队算法
- bzoj 2038: [2009国家集训队]小Z的袜子(hose) (莫队算法)
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)|分块|莫队算法
- bzoj 2038: [2009国家集训队]小Z的袜子(hose) 【莫队算法】
- bzoj 2038: [2009国家集训队]小Z的袜子(hose)(莫队算法)
- 如何阅读别人写的项目
- RabbitMQ消息分发模式----"Work Queues"模式
- EXTJS日期时间选择控件(精确到秒)
- virtualbox中centos用yum安装的mysql关于权限出现的问题
- spark开发中一些错误
- BZOJ 2038 小Z的袜子(hose)(莫队算法)
- kafka运行环境的搭配与实践
- mac中 Android framework源码阅读步骤
- leetcode209-Minimum Size Subarray Sum(最小长度子数组和)
- poj_1002 487-3279 快速排序
- windows下面安装Python和pip终极教程
- 【闲着没事儿干】Spring Aop例子
- extjs 中的afterRender, onDestroy等函数什么时候调用啊
- debian下安装Theano(附pydot问题解决方法)