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 ;
}

最新文章

  1. C语言末
  2. 深入.net(文件操作)
  3. [PCL]1 PCL点云库安装
  4. WPF 位置转化和动画
  5. mongoDB查询及游标
  6. Python垃圾回收机制 总结
  7. 再学ajax--第一天
  8. Unity TimeLine
  9. charles重复发送一个网络请求&amp;同时发送N次
  10. 学习笔记CB002:词干提取、词性标注、中文切词、文档分类
  11. Python面向对象6:抽象类和自定义类
  12. vue的Vuex
  13. [Windows]查看运行进程的参数【wmic】
  14. mongodb副本集与分片结合
  15. linux随机数生成
  16. 2019/4/8 wen text
  17. Shell-16--函数
  18. VCL界面控件DevExpress VCL Controls发布v18.2.5|附下载
  19. codeforces820B Mister B and Angle in Polygon 2017-06-28 09:42 123人阅读 评论(0) 收藏
  20. python shlex 模块

热门文章

  1. 字符集、字符编码、XML中的中文编码
  2. boost库 bind/function的使用
  3. bzoj 4710 分特产
  4. Python 2.7_爬取CSDN单页面博客文章及url(二)_xpath提取_20170118
  5. BZOJ4605:崂山白花蛇草水
  6. boost_1.61.0编译安装
  7. VirtualBox-Debian7.2-share
  8. MySQL最新版本 MySQL5.7.11 批量自动化一键式安装(转)
  9. Jmeter &amp; TICK
  10. CentOS7网卡设置为桥接模式静态IP配置方法详解