BZOJ3809,是权限题。

我永远喜欢莫队。

先莫队一下移下左右指针,然后用一个数据结构维护一下区间$[a, b]$中的颜色的值,跟着指针移动一起修改修改,每一次$query$的时候就相当于查询一下$[a, b]$中的和。

其实可以直接对颜色进行分块,维护一下块内的值以及每一个位置的答案,每一次修改是$O(1)$的,每一次查询是$O(\sqrt{n})$的,因为总共要进行$m$次查询,所以总的时间复杂度是$O((n + m)\sqrt{n})$,这样写可以比树状数组以及其他的数据结构少一个$log$。

然而这个$log$在你谷上跑的飞快……

最近真是颓了。

Code:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 1e5 + ;
const int M = 1e6 + ; int n, qn, a[N], blo, ans[N];
int cnt[N], bel[N], ln[N], rn[N]; struct Querys {
int l, r, st, ed, id, ans;
} q[M]; bool cmp(const Querys &x, const Querys &y) {
if(bel[x.l] == bel[y.l]) return x.r < y.r;
else return x.l < y.l;
} inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int query(int st, int ed) {
int res = ;
if(bel[st] == bel[ed]) {
for(int i = st; i <= ed; i++)
if(cnt[i]) res++;
} else {
for(int i = st; i <= rn[bel[st]]; i++)
if(cnt[i]) res++;
for(int i = ln[bel[ed]]; i <= ed; i++)
if(cnt[i]) res++;
for(int i = bel[st] + ; i <= bel[ed] - ; i++)
res += ans[i];
}
return res;
} inline void add(int x) {
++cnt[a[x]];
if(cnt[a[x]] == ) ans[bel[a[x]]]++;
} inline void del(int x) {
--cnt[a[x]];
if(cnt[a[x]] == ) ans[bel[a[x]]]--;
} inline void solve() {
sort(q + , q + + qn, cmp);
for(int l = , r = , i = ; i <= qn; i++) {
for(; l < q[i].l; del(l++));
for(; l > q[i].l; add(--l));
for(; r < q[i].r; add(++r));
for(; r > q[i].r; del(r--));
q[q[i].id].ans = query(q[i].st, q[i].ed);
}
} int main() {
read(n), read(qn);
for(int i = ; i <= n; i++) read(a[i]); blo = sqrt(n);
for(int i = ; i <= blo; i++) {
ln[i] = (i - ) * blo + ;
rn[i] = i * blo;
}
if(rn[blo] < n)
++blo, ln[blo] = rn[blo - ] + , rn[blo] = n;
for(int i = ; i <= blo; i++) {
for(int j = ln[i]; j <= rn[i]; j++)
bel[j] = i;
} for(int i = ; i <= qn; i++) {
read(q[i].l), read(q[i].r), read(q[i].st), read(q[i].ed);
q[i].id = i;
} solve(); for(int i = ; i <= qn; i++)
printf("%d\n", q[i].ans);
return ;
}

Luogu 4867 Gty的二逼妹子序列的更多相关文章

  1. 【题解】Luogu P4867 Gty的二逼妹子序列

    原题传送门 同Luogu P4396 [AHOI2013]作业 询问多了10倍,但还能跑过(smog #include <bits/stdc++.h> #define N 100005 # ...

  2. BZOJ 3809: Gty的二逼妹子序列

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1387  Solved: 400[Submit][Status][Di ...

  3. 【BZOJ-3809】Gty的二逼妹子序列 分块 + 莫队算法

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1072  Solved: 292[Submit][Status][Di ...

  4. Bzoj 3809: Gty的二逼妹子序列 莫队,分块

    3809: Gty的二逼妹子序列 Time Limit: 35 Sec  Memory Limit: 28 MBSubmit: 868  Solved: 234[Submit][Status][Dis ...

  5. 3809: Gty的二逼妹子序列

    3809: Gty的二逼妹子序列 链接 分析: 和这道AHOI2013 作业差不多.权值是1~n的,所以对权值进行分块.$O(1)$修改,$O(\sqrt n)$查询. 代码: #include< ...

  6. 【BZOJ 3809】 3809: Gty的二逼妹子序列 (莫队+分块)

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1728  Solved: 513 Description Autumn ...

  7. 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块

    [BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...

  8. [AHOI2013]作业 & Gty的二逼妹子序列 莫队

    ---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...

  9. [bzoj3809]Gty的二逼妹子序列_莫队_分块

    Gty的二逼妹子序列 bzoj-3809 题目大意:给定一个n个正整数的序列,m次询问.每次询问一个区间$l_i$到$r_i$中,权值在$a_i$到$b_i$之间的数有多少个. 注释:$1\le n\ ...

随机推荐

  1. python虚拟开发环境搭建(virtualenv和virtualenvwrapper)

    虚拟开发环境的搭建 (0) 搭建虚拟环境的意义 使不同的开发环境独立 环境升级不影响其他开发环境,也不影响全局 防止包管理的混乱 (1) 指定 虚拟环境的创建目录 环境变量设置 创建 WORKON_H ...

  2. Brackets (区间DP)

    个人心得:今天就做了这些区间DP,这一题开始想用最长子序列那些套路的,后面发现不满足无后效性的问题,即(,)的配对 对结果有一定的影响,后面想着就用上一题的思想就慢慢的从小一步一步递增,后面想着越来越 ...

  3. 转:django关于csrf防止跨站的ajax请求403处理

    http://blog.csdn.net/wjy397/article/details/49078099

  4. flask之python3 虚拟环境及使用dotnv来永久保存环境变量

    Python 3 comes bundled with the venv module to create virtual environments Create an environment Cre ...

  5. 蓝桥杯 算法训练 ALGO-149 5-2求指数

     算法训练 5-2求指数   时间限制:1.0s   内存限制:256.0MB 问题描述 已知n和m,打印n^1,n^2,...,n^m.要求用静态变量实现.n^m表示n的m次方.已知n和m,打印n^ ...

  6. FLASH和EEROM使用【转】

    最近在看代码的时候,遇到了一个使用FLASH模拟EEPROM的情况,看到这个我当时是一脸蒙蔽啊,对于一个有时候连FLASH和EEPROM都分不清的人来说,怎么可能读懂用FLASH来模拟EEPROM呢? ...

  7. thrift rpc 使用常见问题解答和经验

    Thrift是一个非常棒的工具,是Facebook的开源项目,目前的开发非常的活跃,由Apache管理,所以用的是Apache Software License,这非常重要,因为可以放心的对其修改并用 ...

  8. Oracle data guard 10g 搭建

    Oracle data guard 10g 搭建 1系统常规参数检查 硬盘 [root@localhost ~]# df -h 内核 [root@localhost ~]# uname -a [roo ...

  9. 应用HTMLTestRunner整合测试报告

    为了便于测试脚本的维护,以及更多测试用例的管理,于是根据上次学习的HTMLTestRunner生成的测试报告,今天将对其进行整理.我们之前使用 TestSuite 只是在一个.py 文件里添加多个测试 ...

  10. Python函数(六)-嵌套函数

    嵌套函数就是在一个函数里再嵌套一个或多个函数 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" def First(): pri ...