Codeforces 85D Sum of Medians】的更多相关文章

题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 = 3的元素累加求和. 解题思路:线段树单点更新,每一个点维护5个值.分别表示从该段区间中i % 5 = t的和.然后两端区间合并时仅仅须要依据左孩子中元素的个数合并.所以有一个c表示区间上元素的个数. 由于有同样的数.所以要离线操做,将全部的数映射成位置,可是对于del则不须要映射,由于集合中肯定…
传送门 D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five con…
D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecu…
Sum of Medians 题解: 对于这个题目,先想到是建立5棵Splay,然后每次更新把后面一段区间的树切下来,然后再转圈圈把切下来的树和别的树合并. 但是感觉写起来太麻烦就放弃了. 建立5棵线段树. 然后 seg[rt][i]代表的是只考虑当前所管辖的区间中的情况下, 下标对5取余之后为 i 的那些值的和. 最重要的一点是更新. ; i < ; ++i) seg[i][rt] = seg[i][rt<<] + seg[((i-sz[rt<<]%)+)%][rt<…
传送门 题目 In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third l…
http://codeforces.com/problemset/problem/85/D 题意: 给你N(0<N<1e5)次操作,每次操作有3种方式, 1.向集合里加一个数a(0<a<1e9)(增加数a之前的集合一定不存在a) 2.向集合里删一个数a(0<a<1e9)(删除数a之前的集合一定存在a) 3.将下标i%5==3的数相加,计算总和并相加. 分析: 先离散,然后线段树,sum[i]用来标识前i项有几个数,ans[rt][i]表示以rt为根的子树中第i大数的和…
D. Sum of Medians Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/85/D Description In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive el…
D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecu…
题目链接: Sum of Medians Time Limit:3000MSMemory Limit:262144KB 问题描述 In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is cal…
D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecu…
CF85D Sum of Medians 题意翻译 一个集合,初始为空.现有三个操作: 1.add:向集合里加入数x,保证加入前集合中没有数x: 2.del:从集合中删除数x,保证删除前集合中有x: 3.sum:询问将集合里的数从小到大排序后,求下标i模5余3的数的和. 现有n次操作,对于每个查询操作,输出答案 题解Here! 一开始感觉好不可做啊... 然后发现,线段树好像可以搞一搞. 线段树每个节点维护$5$个值,即区间中所有$\text{下标}\mod5$后结果相同的位置的值的和. 即:在…
Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five.…
题目链接:http://codeforces.com/contest/85/problem/D 做法果然男默女泪啊..... 大概就是直接开了一个$vector$每次插入删除都用自带的$insert$和$erase$,然后查询也是暴力搞. 那么为啥么过得很有理有据呢? 1.首先考虑如果没有修改我就能继承上一次的答案... 2.修改我们假设(就是)${O(logn)}$的. 3.每次暴力查询是$5$个数字一步. 4.显然一开始并不是上来就有${100000}$个数字 所以大概复杂度会是${O(n^…
Codeforces 题面传送门 & 洛谷题面传送门 智商掉线/ll 本来以为是个奇怪的反悔贪心,然后便一直往反悔贪心的方向想就没想出来,看了题解才发现是个 nb 结论题. Conclusion. 在最优方案中,至多只有一个数组只有部分被选,其余数组要么全选要么全都不选. 证明:考虑调整.假设存在两个数组 \(x,y\) 分别选了前 \(p,q\) 个元素,这里不妨假设 \(a_{x,p+1}\ge a_{y,q+1}\),那么考虑从 \(y\) 数组中拎 \(l=\min(len_x-p,q)…
Codeforces 题面传送门 & 洛谷题面传送门 个人感觉这题称不上毒瘤. 首先看到选一条路径之类的字眼可以轻松想到点分治,也就是我们每次取原树的重心 \(r\) 并将路径分为经过重心和不经过重心两类路径.对于第二类路径我们肯定可以在对 \(r\) 所有子树进一步分治的过程中将其变成第一类路径,因此我们只用考虑怎样计算第一类路径的贡献即可. 显然对于一条第一类路径 \(x\to y\),我们要将其拆成 \(x\to r\) 和 \(r\to y\) 两部分分别处理,考虑怎样合并这两部分的贡献…
E. Sum of Remainders time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + ... + n mod m. As the result can be very large, you shoul…
616E Sum of Remainders Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + - + n mod m. As the result can be very large, you should print the value modulo 109 + 7 (the remainder when divided by 109 + 7). The modulo operator a mod b stands f…
<题目链接> 题目大意: 给你一个序列,有两个操作,一个是求区间 l - r 的和,另一个是对区间l-r的元素修改值,x=d(x),d(x)为x的因子个数. 解题分析: 因为可能有多次修改操作,并且修改的范围可能比较大,所以提前将1~1e6范围内的数的因子个数全部打表进行处理.但是仅仅这样还是不行的,因为如果每次区间更新都暴力更新到叶子节点的话,区间更新 $O(nlog(n))$ ,然后m次询问,时间复杂度就达到了$O(n*mlog(n))$,而本题n给到了3e5,毫无疑问这样暴力更新是会超时…
920F - SUM and REPLACE 思路1: 线段树(982 ms) 每个点最多更新6次 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define ls rt<<1,l,m #define rs rt<<1|1,m+1,r #define mem(a,b) memset(a,b,sizeof(a)) ; const int…
传送门 线段树入门题. 给你一个序列:支持区间修改成自己的约数个数,区间求和. 实际上跟区间开方一个道理. 2的约数个数为2,1的约数个数为1,因此只要区间的最大值小于3就不用修改否则就暴力修改. 因此要做的就是预处理一个数的约数个数,这个可以nlnnnln_nnlnn​预处理. 代码: #include<bits/stdc++.h> #define lc (p<<1) #define rc (p<<1|1) #define mid (T[p].l+T[p].r>…
847C - Sum of Nestings 思路:简单的递归. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) void c(ll n,ll k) { ios::sync_with_stdio(false); cin.tie(); )return ; ) { cout<<…
题意:给N个数M次操作,(1<=N,M<=3e5, 1<=ai<=1e6),1是使[L,R]中的每个元素变成其因子的个数之和:2是求[L,R]区间之和 分析:看上去就很线段树的一题,但是却思考了很久.发现1和2即使对其,也不会改变二者的值.而且一个大于2的数进行多次1操作,也最终会退化到2. 先预处理筛出1e6以内各数的质因子个数和.在线段树的节点中维护两个值:区间和以及区间最大值.在update函数中,如果该区间的最大值不超过2,那么该区间没有更新的必要:若超过2,则递归向下找到…
题目大意: 一个数列 支持两种操作 1 把区间内的数变成他们自己的约数个数 2 求区间和 思路: 可以想到每个数最终都会变成2或1 然后我们可以线段树 修改的时候记录一下每段有没有全被修改成1或2 是的话就不修改了 不是就暴力修改 因为每个数被修改的次数很小 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #includ…
Description 题库链接 给你 \(k\) 个盒子,第 \(i\) 个盒子中有 \(n_i\) 个数,第 \(j\) 个数为 \(x_{i,j}\).现在让你进行 \(k\) 次操作,第 \(i\) 次操作要求从第 \(i\) 个盒子中取出一个元素(这个元素最开始就在该盒子中),放入任意一个你指定的盒子中,要求经过 \(k\) 次操作后 所有盒子元素个数和最开始相同: 所有盒子元素总和相等 询问是否存在一种操作方式使之满足,若存在,输出任意一种方案即可. \(1\leq k\leq 15…
题意:给你x个集合的数,然后根据求y集合的数. 思路:根据对称性,先找出对称出现的个数cnt,然后对称位置的中如果出现一个的把另一个加入到y集合中,再找出cnt个对应位置都不出现的加入到y集合中. #include <cstdio> #include <cstring> #include <set> #include <algorithm> #define ll long long #define maxn 5000100 using namespace s…
题目链接 给两个数n, m. 求n%1+n%2+.......+n%m的值. 首先, n%i = n-n/i*i, 那么原式转化为n*m-sigma(i:1 to m)(n/i*i). 然后我们可以发现  1/4 = 2/4 = 3/4 = 0, 4/4 = 5/4 = 6/4 = 7/4 = 1. 所以可以将这些结果分成很多块, 按块算结果. 注意计算过程中时刻避免爆longlong. #include <iostream> #include <vector> #include…
比赛链接:https://codeforces.com/contest/1440 A. Buy the String 题解 枚举字符串中 \(0\) 或 \(1\) 的个数即可. 代码 #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n, c0, c1…
Solved   CodeForces 920A Water The Garden   Solved   CodeForces 920B Tea Queue   Solved   CodeForces 920C Swap Adjacent Elements       CodeForces 920D Tanks       CodeForces 920E Connected Components?       CodeForces 920F SUM and REPLACE       CodeF…
原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等我晚上再说23333 我大概的看了一下所讲内容及实例,果不愧是大牛级别的人,讲的很详细,所以决定转载过来. 对原文作者致以崇高的敬意. 以下是转载内容. [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思…
线段树完全版  ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉得当时的代码风格实在是太丑了,很多线段树的初学者可能就是看着这篇文章来练习的,如果不小心被我培养出了这么糟糕的风格,实在是过意不去,正好过几天又要给集训队讲解线段树,所以决定把这些题目重新写一遍,顺便把近年我接触到的一些新题更新上去~;并且学习了splay等更高级的数据结构后对线段树的体会有更深了一…