3809: Gty的二逼妹子序列

链接

分析:

  和这道AHOI2013 作业差不多。权值是1~n的,所以对权值进行分块。$O(1)$修改,$O(\sqrt n)$查询。

代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int bel[N], sum[N], a[N], Ans[N * ], cnt[N], B;
struct Que{
int l, r, a, b, id;
bool operator < (const Que &A) const {
return bel[l] == bel[A.l] ? r < A.r : bel[l] < bel[A.l];
}
}Q[N * ]; inline void add(int x) {
cnt[x] ++;
if (cnt[x] == ) sum[bel[x]] ++;
}
inline void del(int x) {
cnt[x] --;
if (cnt[x] == ) sum[bel[x]] --;
}
inline int query(int l,int r) {
int res = ;
for (int i = l, lim = min(r, bel[l] * B); i <= lim; ++i) res += (cnt[i] > );
if (bel[l] != bel[r])
for (int i = (bel[r] - ) * B + ; i <= r; ++i) res += (cnt[i] > );
for (int i = bel[l] + ; i <= bel[r] - ; ++i) res += sum[i];
return res;
}
int main() {
int n = read(), m = read(); B = sqrt(n);
for (int i = ; i <= n; ++i) a[i] = read();
for (int i = ; i <= m; ++i)
Q[i].l = read(), Q[i].r = read(), Q[i].a = read(), Q[i].b = read(), Q[i].id = i;
for (int i = ; i <= n; ++i) bel[i] = (i - ) / B + ;
sort(Q + , Q + m + );
int L = , R = ;
for (int i = ; i <= m; ++i) {
while (L > Q[i].l) add(a[--L]);
while (R < Q[i].r) add(a[++R]);
while (L < Q[i].l) del(a[L++]);
while (R > Q[i].r) del(a[R--]);
Ans[Q[i].id] = query(Q[i].a, Q[i].b);
}
for (int i = ; i <= m; ++i) printf("%d\n",Ans[i]);
return ;
}

3809: Gty的二逼妹子序列的更多相关文章

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

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

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

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

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

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

  4. BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块

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

  5. BZOJ 3809 Gty的二逼妹子序列(莫队+分块)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3809 [题目大意] 给定一个长度为n(1<=n<=100000)的正整数序 ...

  6. 【BZOJ】3809: Gty的二逼妹子序列

    http://www.lydsy.com/JudgeOnline/problem.php?id=3809 题意:n个元素(1<=n<=100000)每个元素有一权值<=n.q个询问, ...

  7. bzoj 3809 Gty的二逼妹子序列——莫队+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...

  8. bzoj 3809 Gty的二逼妹子序列 —— 莫队+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 据说一开始应该想到莫队+树状数组,然而我想的却是莫队+权值线段树... 如果用权值线段 ...

  9. bzoj 3809 Gty的二逼妹子序列(莫队算法,块状链表)

    [题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基 ...

随机推荐

  1. Centos7 之目录处理命令(八)

    linux中 关于目录 有几个重要概念 一个是 / 根目录  还有一个当前用户的家目录 比如 root用户的家目录是 /root  普通用户的家目录是/home/xxx 下 root登录 默认家目录 ...

  2. POJ3907 Build Your Home

    嘟嘟嘟 题意:按逆时针或顺时针给出一个多边形,求面积. 解法:直接套用公式:\(S = \frac{1}{2}|\sum _ {i = 1} ^ {n} {v_i \times v_{i + 1}}| ...

  3. xss实现获取网站源码

    当网站cookie设置了httponly,xss获取不到到网站的cookie.但是我们是可以获取到网站后台的url. 这时候我们可以xss得到网站后台源码,从而找到网站后台的一些敏感操作:添加用户,删 ...

  4. No.4 - 3D 空间的卡片翻转动效

    参考 ①张鑫旭http://www.zhangxinxu.com/wordpress/2012/09/css3-3d-transform-perspective-animate-transition/ ...

  5. 13.56Mhz SI522兼容MFRC522的资料以及对比性能

    (13.56Mhz芯片) SI522是一颗专门替代MFRC522/FM17522,PIN对PIN 完全软硬件兼容.相对于MFRC522,SI522完全替换,不需要做任何更改,同时接受模式下功耗低10m ...

  6. Eclipse查看.properties文件中文乱码

    在中文操作系统中,Eclipse中的Java类型文件的编码的默认设置是GBK,但是对Properties资源文件的编码的默认设置是ISO-8859-1.所以编辑Java文件中的中文不会出现问题,但编辑 ...

  7. PAT——1039. 到底买不买

    小红想买些珠子做一串自己喜欢的珠串.卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖.于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子 ...

  8. iOS的AssetsLibrary框架访问所有相片

    该框架下有几个类,ALAssetsLibrary,ALAssetsGroup,ALAsset,ALAssetsFilter,ALAssetRepresentation. ALAssetsLibrary ...

  9. CentOS查看卸载openjdk

    1.查看openjdk版本 java -versionjava version "1.7.0_51" OpenJDK Runtime Environment (rhel-2.4.5 ...

  10. What to do next to activate this settings for already existing users

    Link: http://sharepoint.stackexchange.com/questions/89284/sharepoint-2013-mysite-increase-quota Cent ...