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. OpenCV - win7+vs2013(2012)+opencv3.0.0 环境配置 (以及配置技巧)

    1. opencv 3.0.0 库下载地址, 这里的版本是3.0.0,其他的版本配置可能不一样,请大家注意. http://sourceforge.net/projects/opencvlibrary ...

  2. Jupyterhub Error 503: Proxy Target Missing

    Jupyterhub Error 503: Proxy Target Missing 请求太频繁

  3. Springboot演示小Demo

    模拟数据库演示springboot小测试 1.编写一个实体类:user package com.wisezone.test; import java.io.Serializable; public c ...

  4. IntelliJ IDEA 12创建Maven管理的Java Web项目(图解)

    转:http://blog.csdn.net/zht666/article/details/8673609/ 本文主要使用图解介绍了使用IntelliJIDEA 12创建Maven管理的JavaWeb ...

  5. c++11之一: 基于范围的for循环

    #include <iostream> using namespace std; int main(){ ]{,,,,}; for (int& e: ary) e *= ; for ...

  6. MongoDB 3.4 高可用集群搭建(二)replica set 副本集

    转自:http://www.lanceyan.com/tech/mongodb/mongodb_repset1.html 在上一篇文章<MongoDB 3.4 高可用集群搭建(一):主从模式&g ...

  7. 四川第七届 I Travel(bfs)

    Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n. Amo ...

  8. Errors occurred during the build. Errors occurred during the build. Errors running builder 'JavaScript Validator' on XXX

    选择项目--右键Properties--Builders--如果有则取消第一项“JavaScript Validator”的勾. http://blog.csdn.net/error_case/art ...

  9. RPCServiceClient-调用webservice客户端

    import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.address ...

  10. expected declaration specifiers or '...' before string constant

    /work/platform_bus_dev_drv/led_dev.c:52: error: expected declaration specifiers or '...' before stri ...