【题解】[CF718C Sasha and Array]】的更多相关文章

[题解]CF718C Sasha and Array 对于我这种喜欢写结构体封装起来的选手这道题真是太对胃了\(hhh\) 一句话题解:直接开一颗线段树的矩阵然后暴力维护还要卡卡常数 我们来把\(2 \times 2\)看做之后时间复杂度就是\(O(nlogn)\). 写了一点点这种线段树维护除了数字之外的东西的题目,一个最大的感受就是递归用\(void\),传答案什么的一个全局变量,这样比较快. 需要注意的点是\(lazy \ \ tag\)要随着\(seg\)一起初始化一下. #includ…
题解 (不会矩阵加速的先去学矩阵加速) 反正我想不到线段树维护矩阵.我太菜了. 我们在线段树上维护一个区间的斐波那契的列矩阵的和. 然后询问时提取每个符合题意列矩阵的答案项(不是列矩阵存了两项吗,一个是当前项,一个是用来递推的) 因为矩阵乘有结合律所以区间加这个操作就直接区间乘变换矩阵的x次方就行. 然后记得开long long #include<iostream> #include<cstring> #include<cstdio> #include<algor…
Description 给定一个数列,维护两种操作 操作 \(1\),将区间 \([l,r]\) 的数字统一加 \(x\). 操作 \(2\),求 \(\sum \limits_{i=l}^r f(val[i])\),其中 \(f(i)\) 表示斐波那契数列的第 \(i\) 项.' 答案对 \(10^9+7\) 取模. Solution 线段树维护矩阵. 因为是斐波那契数列,容易想到用矩阵快速幂来求这个东西. 想这样做的话,要想清楚两个问题: 因为题目中求的是和,那么知道 \([l,mid]\)…
正解:线段树 解题报告: 传送门! 首先这种斐波拉契,又到了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…
题目链接: 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:…
C. Sasha and Array 题目大意&题目链接: http://codeforces.com/problemset/problem/718/C 长度为n的正整数数列,有m次操作,$opt==1$时,对$[L,R]$全部加x,$opt==2$时,对$[L,R]$求$\sum_{i=L}^{R}Fibonacc(a_{i})$. 题解: 线段树+矩阵快速幂. 在每个线段树存一个转移矩阵,然后YY即可. 代码: #include<cstdio> #include<cstrin…
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…
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 r x …
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…
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: 1 l r…
E. Sasha and Array 这个题目没有特别难,需要自己仔细想想,一开始我想了一个方法,不对,而且还很复杂,然后lj提示了我一下说矩阵乘,然后再仔细想想就知道怎么写了. 这个就是直接把矩阵放到线段树里面去了. 注意优化,降低复杂度. #include <cstring> #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #inclu…
Description 有一个长为\(n\)的数列\(a_{1},a_{2}...a_{n}\),你需要对这个数列维护如下两种操作: \(1\space l \space r\space x\) 表示将数列中的\(a_{l},a_{l+1}...a_{r-1},a_{r}\)加上\(x\) \(2\space l\space r\) 表示要你求出\(\sum_{i=l}^{r}fib(a_{i})\)对\(10^9+7\)取模后的结果 fib(x)fib(x)表示的是斐波那契的第\(x\)项,\…
传送门 题目 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/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…
题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B ar…
[题目链接]:http://codeforces.com/contest/719/problem/E [题意] 给你一个数列,有两种操作1 l r x 给[l,r]区间上的数加上x, 2 l r 询问[l,r]区间fibonacci数列的和(f[l]+f[l+1]+--f[r]) [题解] 斐波那契数列有个矩阵乘法公式 f[n]= [0 1] ^n× [0 0] [1 1] [0 1] 最后得到的矩阵A A[1][2]就是答案;(即第一行第二列) 写个线段树的成段更新; 用懒惰标记记录加上的数字…
题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例#1 10 1 11 1 2 3 4 5 6 7 8 9 10 2 8 6 10 输出样例#1 1 2 3 4 5 6 7 8 9 6 题意简述 输入一个数组\(a[1,...,n]\)和\(m\)条指令,你的任务是对数组进行变换,输出最终结果. 每条指令形如\((L,R,v,p)\),表示先统计出\(a[L],a[L+1],...,a[R]\)中严格小于\(v\)的元素个数\(k\),然后把\(a[p]\)修改成\(u \tim…
大佬们的题解都太深奥了,直接把转移方程放出来让其他大佬们感性理解,蒟蒻们很难理解,所以我就写了一篇让像我一样的蒟蒻能看懂的题解 原题传送门 动态规划三部曲:确定状态,转移方程,初始状态和答案. --神仙 @akicc 第一步 确定状态 \(f_{i,j,k}(k\in\{0,1,2\})\)表示第 \(i\) 个数选为 \(j\) 且和前一个数是小于/等于/大于(\(k=0\) 是大于,\(k=1\) 是等于,\(k=2\) 是小于)的关系时的方案数. 第二步 转移方程 把三种关系分开讨论: \…
题目链接 分析:矩阵快速幂+线段树 斐波那契数列的计算是矩阵快速幂的模板题,这个也没什么很多好解释的,学了矩阵快速幂应该就知道的东西= =这道题比较巧妙的在于需要用线段树来维护矩阵,达到快速查询区间斐波那契数列和的目的.这道题极为卡常数,我TLE了不知道多少发,才在赛后过了这道题. 我尝试下来,发现矩阵乘法的写法极为重要,我就是因为用了三层循环来写矩阵乘法导致了悲剧的TLE,一直卡在了第17组数据.我百度了网上别的矩阵快速幂的写法才过了这道题. 还有涨智识的地方是不要随意memset.我原来的矩…
线段树. 线段树维护区间矩阵和,操作都是最简单的线段树.$lazy$标记不要记录乘了几次,直接记录乘了几次之后的矩阵就可以了,不然每次下传的时候再算一遍时间复杂度会提高. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include…
1.题目描述 2.问题分析 使用二分法即可. 3.代码 TreeNode* sortedArrayToBST(vector<int>& nums) { ) return NULL; ; ; TreeNode *node = ); BST(node->left,nums, left, right); return node->left; } void BST(TreeNode * &parent, vector<int> &nums,int lef…
1.题目描述 2.题目分析 每个元素对应的积应该是 它 前面的每个元素的积,和后面的每个元素的积 3.代码 vector<int> productExceptSelf(vector<int>& nums) { vector<int> res( nums.size() ); ; ; i< nums.size() ; i++) { res[i] = p; p *= nums[i]; } p = ; ; i >= ; i--) { res[i] *= p;…
传送门 解题思路: 这道题给了我们一个崭新的角度来看线段树. 我们常常使用的线段树是维护区间的函数的. 这里呢,提示我们线段树其实还可以维护递推. 美好的矩阵递推性质支持了这一功能. 或者说,对于递推项求和,可以使用线段树维护矩阵. 区间向前递推可以用懒惰标记记录递推矩阵. 区间的查询可以是子节点矩阵和. 这其实也是满足分配率的. 最后pushup,pushdown搞一搞就好了. 代码(闲来无事卡常码风突变): #include<cstdio> #include<cstring>…
传送门 Solution 用线段树维护矩阵 第一个操作相当于区间乘 第二个操作相当于区间求和 Code  #include<bits/stdc++.h> #define ll long long #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'…
用了两场比赛上Div 1感觉自己好腊鸡的说...以下是这两场比赛的部分题解(不得不说有个黄学长来抱大腿还是非常爽的) Round #372 : Div 2 A:Crazy Computer 题意:给定N个输入和一个时间长度M,每次输入屏幕上增加一个字符,若两个输入间隔大于M则屏幕上的字符会被清空,问结束时屏幕上还有多少个字符 直接模拟没有什么好说的 代码: #include<cstdio> #include<iostream> #include<cstring> #in…
拖了一周才完成的题解,抛出一个可爱的表情 (っ'-')╮ =͟͟͞͞❤️.对我来说E.F比较难,都是线段树的题,有点久没写了. A - Infinite Sequence CodeForces - 675A 公差为c,首项为a的等差数列是否有一项等于b. 注意c为0的情况. #include<cstdio> long long a,b,c; int main() { scanf("%lld%lld%lld",&a,&b,&c); if(c==0&am…
数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺序访问数组.按下标取值是对数组的常见操作. 相关LeetCode题: 905. Sort Array By Parity  题解 922. Sort Array By Parity II  题解 977. Squares of a Sorted Array  题解 1150. Check If a…