题意:给定一棵树,每个节点有颜色,对于每个询问(u,k)询问以u为根节点的子树下有多少种颜色出现次数>=k 因为是子树,跟dfs序有关,转化为一段区间,可以用莫队算法求解 直接用一个数组统计出现次数>=k的颜色 Code #include <cstdio> #include <algorithm> #include <cmath> #define N 100010 using namespace std; int n,m,A[N],bl[N],Ans[N],…
正解:莫队/分块 解题报告: 传送门 ummm这题耗了我一天差不多然后我到现在还没做完:D 而同机房的大佬用了一个小时没有就切了?大概这就是大佬和弱鸡的差距趴QAQ 然后只是大概写下思想好了因为代码我还没打出来QAQ 先说莫队? 莫队似乎有俩方式能过呢 首先是个暴力,就只对l1r1排序让l2r2乱跳,然后神仙hl过去了?然后我就过不去?委屈:D 然后可以前缀和优化一波,这个大概是最稳的,然后文佬的博客似乎写了qwq有时间再研究下趴QAQ大概思路知道一下就成立就是个二维前缀和的玩意儿qwq 然后说…
题目传送门 /* 题意:从i开始,之前出现过的就是之前的值,否则递增,问第p个数字是多少 莫队算法:先把a[i+p-1]等效到最前方没有它的a[j],问题转变为求[l, r]上不重复数字有几个,裸莫队:) */ #include <cstdio> #include <algorithm> #include <map> #include <set> #include <vector> #include <cmath> #include…
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: standard input output: standard output Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pa…
Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)Total Submission(s): 2808    Accepted Submission(s): 826 Problem Description In this problem we consider a rooted tree with N vertices. The vertices ar…
E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the n…
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pai…
任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Bob has a favorite number k and ai of length n. Now he asks yo…
D. Powerful array time limit per test seconds memory limit per test megabytes input standard input output standard output An array of positive integers a1, a2, ..., an ..., ar,  ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occ…
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pai…
题目链接: E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by…
题目链接:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Bob has a favorite number k and ai of length n. Now he asks y…
Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 1457 Appoint description:  Description Sona, Maven of the Strings. Of cause, she can play the zither. Sona can't speak but she can make fancy music. Her…
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Submit][Status][Discuss] Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只…
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description NPY's girlfriend blew him out!His honey doesn't love him any more!However, he has so many girlfriend candidates.Because there are too many girls and…
题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬.你的任务便是告诉小…
3052: [wc2013]糖果公园 Time Limit: 200 Sec  Memory Limit: 512 MBSubmit: 883  Solved: 419[Submit][Status][Discuss] Description Input Output Sample Input Sample Output 84 131 27 84 HINT Source Solution 树上带修莫队 本质还是树上莫队,详情可以转 BZOJ-3757苹果树 但是这里需要修改,就需要一些特殊的地方…
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 6475  Solved: 3004[Submit][Status][Discuss] Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜…
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减少对答案的贡献. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; #define MAXN (1<<20) int…
http://acm.fzu.edu.cn/problem.php?pid=2226 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以轻易实现的神奇数据结构,可以处理一类无修改的离线区间查询问题(PS:暂时还没有遇到莫队解决更新区间查询的问题) 莫队算法可以在O(1),实现[l, r]到[l, r±1] / [l±1, r]的转移,然后我们就可以对所有查询分sqrt(n)块,把每个查询所在的块号当做第一关键字,右端点作为第二关键字…
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 分析:莫队算法 莫队算法是一种思想…… 处理问题:不带修改的区间询问 使用要求:[l-1,r] [l,r-1]的结果可由[l,r]的答案在O(1)或O(logn)的时间内推出 具体步骤: 1.对整个区间轴分成根号n块 2.以l所在的块的编号为第一关键字,r为第二关键字给所有询问排序方便处理 3.对于每个块内的询问单独处理 ①对于当前块里的第一个询问暴力求解 ②剩下的在第一个询问基础上递…
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MB Submit: 5573 Solved: 2568 [Submit][Status][Discuss] Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命-- 具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两…
题目是区间逆序数查询. 莫队算法..左或右区间向左或右延伸时加或减这个区间小于或大于新数的数的个数,这个个数用树状数组来统计,我用线段树超时了.询问个数和数字个数都记为n,数字范围不确定所以离散化,这样时间复杂度就是$O(n^{\frac32}\log{n})$ #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; #define…
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orzzzz 首先推公式的话很简单吧... 看的题解是从http://foreseeable97.logdown.com/posts/158522-233333来的 对于查询$[l,r]$ $$ans=\frac{\sum \dbinom{x_i}{2}}{\dbinom{r-l+1}{2}}$$ 其中$x_i$表示当前区间颜色为i的出现次数 这个还用解释么.... 然后我…
[1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the zither. Sona can't speak but she can make fancy music. Her music can attack, heal, encourage and enchant. There're an ancient score(乐谱). But because i…
莫队算法,具体的可以看10年莫涛的论文. 大题思路就是假设对于区间l,r我们有了一个答案,那么对于区间l,r+1,我们 可以暴力的转移一个答案,那么对于区间l1,r1和区间l2,r2,需要暴力处理 的部分就是|r1-r2|+|l1-l2|如果将l看成x,r看成r,得到的暴力部分就是manhattan距离 那么我们将所有的询问,构成一张二维图,可以从一个点转移到另一个点,且总manhattan距离 尽可能的小,所以可以建立一颗manhattan mst,这样的话就可以得到最优的转移,但是实际来说…
今天学了一下传说中的解决离线询问不修改的一种算法.题目的意思非常简单,就是询问在一个[L,R]区间里的取两个物品,然后这两个物品颜色相同的概率.其实就是对于每种颜色i,这个区间里对应的个数cnt[i],那么答案就应该是  sigma (cnt[i]*cnt[i-1]) / (R-L+1)*(R-L). 问题是要是每次询问我都遍历一遍的话必T无疑.这个时候莫队算法就给出了其中一种非常重要的离线处理方法,通过合适的安排询问的次序降低一定的复杂度. 举个例子,假如我询问的时候是询问[1,2],[1,3…
[题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基于权值的块状链表,每个块维护一个区间信息sum,表示权值在该块的数的种数. 这样插入与删除只需要O(1)的时间,查询需要O(sqrt(n))的时间,总的时间复杂度为O(n^1.5+qn^0.5) [代码] #include<set> #include<cmath> #include&l…
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3289 [题意] 回答若干个询问:[l,r]区间内的逆序对个数. [思路] 莫队算法,BIT维护插入删除. 使用BIT维护当前指针l,r内的所有的数.考虑转移部分: l<q[i].l 删出区间中比a[l]小的数与之构成的逆序对,Q(a[l]-1) l>q[i].l l++,添加区间中比a[l]小的数与之构成的逆序对,Q(a[l]-1) r>q[i].r 删除区间中比a[r]大的…
[题意] 若干个询问sigma{ cnt[i]^2 } cnt[i]表示i在[l,r]内的出现次数. [思路] 莫队算法,裸题. 一个cnt数组即可维护插入与删除. [代码] #include<set> #include<cmath> #include<queue> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include…