题面传送门 首先肯定将所有物品排个序. 考虑暴力做法,对于每个询问,枚举所有物品,能买就买.不过扫一眼就知道无法直接优化. 不妨换个角度,暴力做法是枚举询问,这次我们枚举物品.从左到右依次枚举所有物品,将所有买得起当前物品的询问答案 \(+1\),钱数减去当前物品的价格.这样就貌似与 DS 能够搭得上边了. 于是题目变为:\(n\) 次操作,每次操作将序列所有值 \(\geq c\) 的数减去 \(c\),问每个数被操作了几次. 可还是不好维护啊,平衡树 split 出 \(\geq c\) 的…
[bzoj3211]花神游历各国 2014年3月17日2,7230 Description   Input   Output 每次x=1时,每行一个整数,表示这次旅行的开心度 Sample Input 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 2 3 1 1 4 Sample Output 101 11 11 HINT 对于100%的数据, n ≤ 100000,m≤200000 ,data[i]非负且小于10^9 题解 这道题目记录,因为一个数经过较少的次数就会被开根…
传送门 区间位赋值.区间求最大值似乎是不能够像一般的线段树一样直接打标记的,但是直接暴力也太没有面子了. 我们考虑优化一下暴力:如果说线段树的一段区间内在当前修改的所有位置上所有数都是相同的,那么这个最大值就是可以直接维护的,在上面打上标记:如果这个条件不满足就暴力向下递归. 然后交一发发现过了!然而这并不是数据水. 考虑势能分析计算复杂度.设每一个节点的势能函数为当前区间的所有数在位置上不全相同的位置个数.每一次在找到覆盖区间之后暴力向下递归,则势能函数必定减小至少\(1\),而势能的增加量最…
Codeforces 题目传送门 & 洛谷题目传送门 这个 \(42\) 的条件非常奇怪,不过注意到本题 \(a_i\) 范围的最大值为 \(10^{14}\),而在值域范围内 \(42\) 的幂的个数最多只有 \(13\) 个,故考虑用类似于 seg-beats 的方法解决这道题. 首先不考虑操作 \(2\)(区间赋值操作),我们很容易发现一个性质,那就是我们对一个区间进行区间加的操作,每额外进行一次,都是因为存在某个数 \(a_i\) 达到了 \(42\) 的整数次幂,而我们的 \(a_i\…
大意:有$n$个格子, 初始$i$位置的颜色为$i$, 美丽值为0, 有两种操作 将区间$[l,r]$内的元素全部改为$x$, 每个元素的美丽值增加$|x-y|$, $y$为未改动时的值 询问区间$[l,r]$所有元素的美丽值之和 现在给定$m$个操作, 让你输出所有操作2的询问结果. 直接线段树暴力修改, 操作2复杂度显然$O(logn)$, 考虑操作1复杂度的证明. 操作1可以看成先区间增加贡献, 之后再区间赋值, 会产生额外复杂度的只有杂色区间, 考虑杂色区间的势能. 将初值看做n次赋值操…
目录 @description@ @solution@ @accepted code@ @details@ @description@ 有 n 件 T-shirt,第 i 件 T-shirt 有一个 ci 和 qi,分别表示费用与质量. 同时有 k 个顾客,第 j 个顾客准备了 bj 的金钱去购买 T-shirt. 每个顾客的购买策略是相同的: 他会买他的资金范围内 q 值最大的一件,如果有多个选 c 最小的一件,每种 T-shirt 只买1 次. 重复购买,直到所有的 T-shirt 他都买不…
定义 第\(x\)次操作后,势能为\(\phi(x)\),该操作实际复杂度\(c(x)\),均摊复杂度\(a(x)\). 定义\(a(x)=c(x)+\phi(x)-\phi(x-1)\). 那么总复杂度为$\phi(n)-\phi(0)+\sum c(x) $. 简单应用 Q:对于一个初始为0的二进制数,每次+1,求n次操作复杂度. A:定义\(\phi(x)\)为\(i\)次操作后1的个数,对于一次+1 ,1个0->1,x个1->0,那么\(a(x)= (1+x) + (1-x)=2\),…
Cup Trick 平衡树维护一下位置. #include<bits/stdc++.h> #include <bits/extc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int>…
题目大意: 提供n个对象,分别编号为1,...,n.每个对象都可能是某个编号小于自己的对象的特例或是成分.认为某个对象的特例的特例依旧是该对象的特例,即特例关系传递,同样一个对象的成分的成分依旧是该对象的成分.但是还需要注意一个对象的成分是该对象的所有特例的成分.每个对象都不可能是自己的特例或成分.要求解答之后的q个谜题,每个谜题提问两个对象是否是特例关系或成分关系. 输入级别:n,q<1e5 花了一个晚上才想到思路解了题目... 首先我们要先解决一个简单的问题:对于一株多叉树,如何进行预处理,…
简介 二叉搜索树, 可以维护一个集合/序列, 同时维护节点的 \(size\), 因此可以支持 insert(v), delete(v), kth(p,k), rank(v)等操作. 另外, prev(v) == kth(rt,rank(v)-1); next(v) == kth(rt,rank(v)+1). 平衡树通过各种方法保证二叉搜索树的平衡, 从而达到 \(O(\log n)\) 的均摊复杂度. Splay Splay 不仅可以实现一般平衡树的操作, 还可以实现序列的翻转/旋转等操作.…
[替罪羊树]bzoj3224&luogu3369&cogs1829 [Tyvj 1728]普通平衡树 bzoj 洛谷 cogs 先长点芝士 替罪羊树也是一种很好写的平衡树qwq..替罪羊树的核心思想就是重构.即当一棵子树的平衡被破坏,那么就把这棵树拍平,也就是树高为O(logn)的完美二叉树形态.这样看似复杂度很高,实则不然.可以证明,替罪羊树每次重构的复杂度都是均摊O(logn)的. --蒯自WFJdalao的博客 插入和普通BST类似,只需要判断插入操作是否导致了这一条链上结点的大小平…
题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要填的那个数填进去,然后循环比较每行每列以及对角线的和是否相等,题目提交上去卡了几次要注意几点 注意:1.答案数据范围$1\leq x \leq 1e18$,要用 long long 2.特殊情况,$n \equiv 1$时,由于一定有要填的数,所以一定有解 3.反正就是要注意看清楚题目和数据边界情况…
http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问的步数,之后遍历区间找到被访问次数达到n(所有数都可以变成这个数)并且标记的需要步数最少即可. 注意:当是奇数的时候,例如11(11/2=5 5*2=10),按照这么算(除2后再乘2)回重新得到一个新的数 #include <cstdio> #include <cstring> #in…
题目链接:CF Round #254 div1 C 题目分析 这道题目是要实现区间赋值的操作,同时还要根据区间中原先的值修改区间上的属性权值. 如果直接使用普通的线段树区间赋值的方法,当一个节点表示的区间完全被要求修改的区间包含时,就直接打上赋值的标记然后 return .但是这样这个节点中每个位置原先的值不同,需要进行的属性权值修改也就不同,是不能直接实现的.如果我们在节点表示的区间被修改的区间包含时,并不直接打标记 return ,而是当节点表示的区间被修改的区间完全包含而且这个节点中的每个…
题目链接:  http://codeforces.com/contest/335/problem/B 分析: 第一眼看上去串的长度为5*10^4, 冒似只能用O(n)的算法可解. 而这样的算法从来没见过...... 其实不然, 注意一个条件"如果有存在长度为100的回文子串则输出长度为100的,否则输出最长的", 可以发现: 一个长度大于100的回文串都可以变成长度为100的 如: 长度为101的只要删除中间的一个就变为长度为100的, 长度为102的删除中间两个也变为长度为100的.…
500pt, 题目链接:http://codeforces.com/problemset/problem/371/A 分析:k-periodic说明每一段长度为k,整个数组被分成这样长度为k的片段,要使得修改最少,求出k长度的片段中每个位置出现次数最多的数就行. 代码: #include <vector> #include <list> #include <map> #include <set> #include <deque> #include…
Description Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positi…
B. Anatoly and Cockroaches time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there tog…
B. Code For 1 time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him t…
C. Alyona and Spreadsheet time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit table…
B. Game of Credit Cards time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and deci…
A. Shell Game time limit per test:0.5 seconds memory limit per test:256 megabytes input:standard input output:standard output Bomboslav likes to look out of the window in his room and watch lads outside playing famous shell game. The game is played b…
A. Oath of the Night's Watch time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output "Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands,…
C. Did you mean... time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search…
B. Which floor? time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output In a building where Polycarp lives there are equal number of flats on each floor. Unfortunately, Polycarp don't remember how…
B. Gleb And Pizza time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pieces of sausage lay on…
A. Kirill And The Game time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output Kirill plays a new computer game. He came to the potion store where he can buy any potion. Each potion is characteriz…
A. Theatre Square time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the ci…
E. Mother of Dragons time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output There are n castles in the Lannister's Kingdom and some walls connect two castles, no two castles are connected by more…
C. Journey time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other…