传送门 解题思路: 这道题给了我们一个崭新的角度来看线段树. 我们常常使用的线段树是维护区间的函数的. 这里呢,提示我们线段树其实还可以维护递推. 美好的矩阵递推性质支持了这一功能. 或者说,对于递推项求和,可以使用线段树维护矩阵. 区间向前递推可以用懒惰标记记录递推矩阵. 区间的查询可以是子节点矩阵和. 这其实也是满足分配率的. 最后pushup,pushdown搞一搞就好了. 代码(闲来无事卡常码风突变): #include<cstdio> #include<cstring>…
题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types:…
E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types: 1 l r x - increase all integers on the segment from l…
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard input output: standard output Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types: 1 l…
线段树. 线段树维护区间矩阵和,操作都是最简单的线段树.$lazy$标记不要记录乘了几次,直接记录乘了几次之后的矩阵就可以了,不然每次下传的时候再算一遍时间复杂度会提高. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include…
CF719E. Sasha and Array 题意: 对长度为 n 的数列进行 m 次操作, 操作为: a[l..r] 每一项都加一个常数 C, 其中 0 ≤ C ≤ 10^9 求 F[a[l]]+F[a[l+1]]+...F[a[r]] mod 1e9+7 的余数 矩阵快速幂求斐波那契 矩阵满足乘法分配律和结合律! 所以可以每个节点维护矩阵/矩阵和,区间加相当于区间乘矩阵 注意:不要把快速幂写在里面,复杂度平添一个log.把\(B^C\)算出来之后传进去就好了 #include <iostr…
正解:线段树 解题报告: 传送门! 首先这种斐波拉契,又到了1e9的范围,又是求和什么的,自然而然要想到矩阵加速昂 然后这里主要是考虑修改操作,ai+=x如果放到矩阵加速中是什么意思呢QAQ? 那不就是,乘以转移矩阵的x次方嘛 然后再放到线段树上,每个lazytag都是一个加速矩阵 然后就做完辣! 听起来很简单的样子但jio得好像代码挺复杂的,,,QAQ 所以等下放代码QAQ…
有两个操作: 将 $[l,r]$所有数 + $x$ 求 $\sum_{i=l}^{r}fib(i)$ $n=m=10^5$   直接求不好求,改成矩阵乘法的形式:  $a_{i}=M^x\times fib_{1}$直接用线段树维护 $M^x$ 即可. 因为矩阵乘法是满足结合律的: $A*B+A*C=A*(B+C)$ #include <cstdio> #include <algorithm> #include <cstring> #define lson (now &…
我们考虑线性代数上面的矩阵知识 啊呸,是基础数学 斐波那契的矩阵就不讲了 定义矩阵 \(f_x\) 是第 \(x\) 项的斐波那契矩阵 因为 \(f_i * f_j = f_{i+j}\) 然后又因为 \(\texttt{AB+AC=A(B+C)}\) 所以 \(\sum_{i=l}^{r} f(a_i+x) = f(x)\sum_{i=l}^{r} f(a_i)\) 线段树板子题,维护一个矩阵,这题没了 // by Isaunoya #include <bits/stdc++.h> usin…
题目链接:http://codeforces.com/contest/719/problem/E 题意:操作1将[l, r] + x; 操作2求f[l] + ... + f[r]; 题解:注意矩阵可以是a*(b + c) = a*b + a*c ,还有update的时候传入矩阵 //#pragma comment(linker, "/STACK:102400000, 102400000") #include <algorithm> #include <iostream…
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\(\sum_{i=l}^r a_i(i-l+1)^k \mod 10^9+7\) \(n,m \leq 10^5,k \leq 5\) 分析 根据二项式定理 \[(i-l+1)^k=\sum_{j=0}^k (-1)^{k-j} C_{k}^j i^j(l-1)^{k-j}\] 那么 \(\begi…
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问,最少删除多少个字符,使得串中符合ugly串? 思路:定义dp(i, j),其中i=5,j=5,因为只需要删除2016当中其中一个即可,所以一共所需要删除的字符和需要的字符为20176,因此i和j只要5就够了. 然后转移就是dp(i,i) = 0, 如果说区间大小为1的话,那么如果是2017中的一个…
题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 否有满足的数列. 解题思路:线段树维护.每条限制等于是对l~r之间的数或上q(取且的性质,对应二进制位一定为1).那么处理全然部的 限制.在进行查询.查询相应每一个l~r之间的数取且是否还等于q.所以用线段树维护取且和.改动为或操作. #include <cstdio> #include <…
Please, another Queries on Array? 利用欧拉函数的计算方法, 用线段树搞一搞就好啦. #include<bits/stdc++.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, i…
题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的按位与和至少为多少. 更新时,如果val的pos位为1,那么整个区间的按位与和pos位也应该为1,否则与出来就不对了.(这是本题解题的核心) 那么此时更新 sum[rt] |= val 即可.然后再check一遍看是否满足所有条件即可. 代码: #include <iostream> #inclu…
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 secondsmemory limit per test 256 megabytes 问题描述 You've got an array a, consisting of n integers a1, a2, ..., an. You are allowed to perform two operations on…
传送门 题目 Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types: 1 l r x — increase all integers on the segment from l to r by values x; 2 l r — find , where f(x) is the x-th Fibonacci number.…
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵线段树,类似计数排序思想. #include <bits/stdc++.h> using namespace std; ; struct SegTree { ], sum[], l, r; }T[N << ]; ][N]; void pushup(int p, int c) { T[p…
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consisting only of parentheses '(' and ')' is called balanced if it is one of the following. • A string "()" is balanced. • Concatenation of two balance…
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description When selecting files in an application dialog, Vasya noted that he can get the same selection in different ways. A simple mouse click selects a sing…
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int maxn = 5*1e5; typedef long lo…
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 = 3的元素累加求和. 解题思路:线段树单点更新,每一个点维护5个值.分别表示从该段区间中i % 5 = t的和.然后两端区间合并时仅仅须要依据左孩子中元素的个数合并.所以有一个c表示区间上元素的个数. 由于有同样的数.所以要离线操做,将全部的数映射成位置,可是对于del则不须要映射,由于集合中肯定…
[Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You should perform n queries. There are three different types of queries: 1 l r — Add all missing numbers from the interval [l, r] 2 l r — Remove all present…
原文链接http://www.cnblogs.com/zhouzhendong/p/8990745.html 题目传送门 - CodeForces 516C 题意 在一个环上,有$n$棵树. 给出每一个树的高度$h_i$以及每一个树距离他顺时针方向后一个树的距离$d_i$. 有$m$次询问,每次,都会有一段连续区间内的树萎掉.请你找两棵树$x,y$,最大化$2(h_x+h_y)+dist(x,y)$.其中$dist(x,y)$为这两棵树的距离,这个距离不能包含萎掉的树. $n,m\leq 10^…
题目链接:https://codeforces.com/problemset/problem/1099/F Mitya and Vasya are playing an interesting game. They have a rooted tree with $n$ vertices, and the vertices are indexed from $1$ to $n$. The root has index $1$. Every other vertex $i≥2$ has its p…
题目传送门 快速的vjudge通道 快速的Codeforces通道 题目大意 有$n$个火车站,第$i$个火车站出售第$i + 1$到第$a_{i}$个火车站的车票,特殊地,第$n$个火车站不出售车票. 记$\rho_{i, j}$表示从第$i$个火车站出发,到第$j$个火车站最少要购买的车票数. 求$\sum_{i = 1}^{n - 1}\sum_{j=i + 1}^{n}\rho_{i,j}$. 对于在$[i + 1, a_{i}]$中的火车站,肯定直接购买一张从$i$出发的火车票,然后就…
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredients and a wonder-oven which can bake several types of cakes, and opened the bakery. Soon the expenses started to overcome the income, so Slastyona decid…
https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒消耗b[i]电量,充电器每秒可以给一台电脑充x电,假如有一台电脑在某一秒末电量<0,则会关机,问最小的x使得在k秒内没有任何电脑关机 题解 二分答案x,线段树维护区间[1,n]最小天数,枚举k天每天单点修改天数最小的点 代码 #include<bits/stdc++.h> #define M…
题目:http://codeforces.com/problemset/problem/220/B 题意 给定一组数据,多次询问区间内某数字出现次数与该数字数值相同的数的个数 思路 一看到区间查询,就会想到线段树,有木有! 单点或区间的修改.查询等可是线段树的强项嘞√ 而我们今天的线段树类型为: 离线处理.区间更新.单点查询 给定一个数字序列,线段树每个节点所要记录的状态是 :从此处以及到正在处理的元素处满足题意的ans 上面一句话什么意思嘞 我们做的是 边建树,边查询,当对应状态的线段树建立完…
Codeforces 834D The Bakery LINK 题目大意是给你一个长度为n的序列分成k段,每一段的贡献是这一段中不同的数的个数,求最大贡献 是第一次做线段树维护DP值的题 感觉还可以,虽然看了一下这题是用线段树维护DP值 然后说思路 有一个很显然的思路是这样的: dpi,j表示前i个数分成j段的最大贡献 dpi,j=max(dpk,j−1+calc(k+1,i)) 然后我们就可以对于每一层j用线段树维护起来 然后就非常愉快地发现dp[i][p]只会从dp[i−1][p−1]之前的…