vjudge上莫队专题

真的是要吐槽自己(自己的莫队手残写了2个bug)

  • s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n))
  • 在重载<是将q[i].l/s<q2[i].l/s 写成q1[i].l<s<q2[i].l<s 导致一下午都在调bug疯了

Sona

  • 比小Z的袜子简单,直接维护区间频度的^3
  • 需要离散化(离散化化的标号应提前准备好,如果用时在二分查找会增加复杂度)
  • 还有比较坑的一点是多case,但样例只有一个case;多case一定要注意那些结构需要清空

cf:221D Little Elephant and Array

  • 维护x=freq(x)的个数

Little Elephant and Array

  • 维护区间相异元素的个数(通过出现次数便可很好写[L,R]->[L-1,R]...的转移)
  • 转移有一种非常简单的写法:先减去关于元素q的信息,然后更新(可能删除或加上q),然后重新加上元素q相关信息

D-query

  • 维护区间\(s*freq(s)^2\),freq(s)是s出现的次数
  • 这里有个int溢出的坑(如注释所示)
inline void move(ll& now,int p,int v){

    now-=(ll)num[c[p]]*num[c[p]]*tmp[p];//不强制转换为ll会WA
num[c[p]]+=v;
now+=(ll)num[c[p]]*num[c[p]]*tmp[p];
//printf("db1 %lld %d %d %d %d\n",now,c[p],p,num[c[p]],v); /*
if(v==1){
now+=(1+2*num[c[p]])*tmp[p];//这种从数学上简化一步缩小增量大小,也可以
num[c[p]]++;
}
else{
now+=(1-2*num[c[p]])*tmp[p];
num[c[p]]--;
}
*/
/*
printf("db1 %lld %d %d %d\n",now,p,num[c[p]],v);
for(int i=1;i<=n;i++){
printf("%d\t",num[c[i]]);
}
printf("\n");
*/
}

Fast Queries

  • 比较卡时间,经时间证明 ,下面写法能卡过
struct Query{
int l,r,id;
bool operator < (const Query&a) const{
if(l / s != a.l / s) return l / s < a.l / s;
return r < a.r;
}
/*
void read(int i){
scanf("%d %d",&l,&r);
id = i;
}
*/
}ques[maxm];
  • 对运算符号重载的另一种写法不能卡过
struct Query{
int l,r,id;
inline friend bool operator <(const Query& q1,const Query&q2){
if(q1.l/s!=q2.l/s) return q1.l/s<q2.l/s;
return q1.r<q2.r;
}
}ques[maxn];

总结一下自己对这种裸的模板题的坑:

  • n,m的含义不要混淆
  • 分块s=sqrt(n)
  • 注意离散化开的数组含义,一起询问的l,r是下标,需要通过数组才能得到真正信息
  • 防止整形溢出
  • 运算重载的写法能优化速度

加油不要手残呀

Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)的更多相关文章

  1. Little Elephant and Array CodeForces - 220B (莫队)

    The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...

  2. codeforces 86D : Powerful array

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

  3. CodeForces 86D Powerful array(莫队+优化)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  4. NBUT 1457 Sona(莫队算法+离散化)

    [1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...

  5. D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力

    莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...

  6. codefroce D. Powerful array[初识块状数组]

    codefroce D. Powerful array[初识块状数组] 由于是初始所以,仅仅能先用别人的分析.囧... 题目: 给定一个数列:A1, A2,--,An,定义Ks为区间(l,r)中s出现 ...

  7. D. Powerful array

    D. Powerful array 题意 给定一个数列:a[i] (1<= i <= n) K[j]表示 在区间 [l,r]中j出现的次数.有t个查询,每个查询l,r,对区间内所有a[i] ...

  8. VS2013 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array<int>::operator [](int)”只是在返回类型上不同

    1,VS2013 错误 1 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array ...

  9. CF86D Powerful array

    题意翻译 题意:给出一个n个数组成的数列a,有t次询问,每次询问为一个[l,r]的区间,求区间内每种数字出现次数的平方×数字的值 的和. 输入:第一行2个正整数n,t. 接下来一行n个正整数,表示数列 ...

随机推荐

  1. Xpath--使用Xpath爬取糗事百科成人版图片

    #!usr/bin/env python#-*- coding:utf-8 _*-"""@author:Hurrican@file: 爬取糗事百科.py@time: 20 ...

  2. 数据结构(1) 第一天 算法时间复杂度、线性表介绍、动态数组搭建(仿Vector)、单向链表搭建、企业链表思路

    01 数据结构基本概念_大O表示法 无论n是多少都执行三个具体步骤 执行了12步 O(12)=>O(1) O(n) log 2 N = log c N / log c N (相当于两个对数进行了 ...

  3. [模板]Matrix Tree定理

    结论:一个图的生成树个数等于它的度数矩阵减邻接矩阵得到的矩阵(基尔霍夫矩阵)的任意一个n-1阶主子式的行列式的绝对值 证明:不会 求法:高斯消元 例题:[HEOI2013]小Z的房间 #include ...

  4. 使用maven创建web项目时后面多出来Maven Webapp如何删除

    类似这样: 解决办法:

  5. Myeclipse学习总结(6)——MyEclipse断点调试

    当程序写好之后,如何调试呢? 我们在MyEclipse中jav添加断点,运行debug as-->open debug Dialog,然后在对话框中选类后--> Run在debug视图下. ...

  6. MyBatis-Spring-SqlSessionFactoryBean(转)

    SqlSessionFactoryBean 在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建.而在 MyBatis-Spring 中 ...

  7. nodejs-EventEmitter

    addListener(event, listener)为指定事件添加一个监听器到监听器数组的尾部. on(event, listener)为指定事件注册一个监听器,接受一个字符串 event 和一个 ...

  8. 又一次发现Oracle太美之glogin.sql

    又一次发现Oracle太美之glogin.sql 刚開始接触Oracle的时候,有时候一登陆一个生产环境.常常会出现以下的情况: [oracle@rh64 app]$ sqlplus / as sys ...

  9. 2017第34周复习Java总结

    从上周日开始对工作中遇到的Java相关的知识进行总结整理.先是回顾了Java关键字,重点说了static关键字的用法:修饰变量.程序块.内部类.方法.还静态导包:重点说了final关键字可以修饰类.方 ...

  10. HTML iframe 和 frameset 的区别

    转自:http://www.cnblogs.com/polk6/archive/2013/05/24/3097430.html HTML iframe 和 frameset 的区别 iframe 和 ...