【codeforces 719E】Sasha and Array
【题目链接】: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]就是答案;(即第一行第二列)
写个线段树的成段更新;
用懒惰标记记录加上的数字x对应的A^x
维护区间的矩阵和就好;
新增加的A^x不要每次都重新算,不然会T
【Number Of WA】
9
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
const int G = 2; //�����С
const int MOD = 1e9 + 7; //�
struct MX
{
LL v[G+1][G+1];
void O() { ms(v, 0); }
void E() { ms(v, 0); for (int i = 1; i <= G; ++i)v[i][i] = 1; }
MX operator * (const MX &b) const
{
MX c; c.O();
for (int k = 1; k <= G; ++k)
{
for (int i = 1; i <= G; ++i) if (v[i][k])
{
for (int j = 1; j <= G; ++j)
{
c.v[i][j] = (c.v[i][j] + (LL)v[i][k] * b.v[k][j]) % MOD;
}
}
}
return c;
}
MX operator + (const MX &b) const
{
MX c; c.O();
for (int i = 1; i <= G; ++i)
{
for (int j = 1; j <= G; ++j)
{
c.v[i][j] = (v[i][j] + b.v[i][j]) % MOD;
}
}
return c;
}
MX operator ^ (LL p) const
{
MX y; y.E();
MX x; memcpy(x.v, v, sizeof(v));
while (p)
{
if (p&1) y = y*x;
x = x*x;
p>>=1;
}
return y;
}
}A,v,cur;
int n,m,flag[N<<2];
MX sum[N<<2],lazy_tag[N<<2];
LL a[N];
inline void push_up(int rt)
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
lazy_tag[rt].E();
if (l==r)
{
sum[rt] = A^a[l];
return;
}
int m = (l+r)>>1;
build(lson),build(rson);
push_up(rt);
}
inline void push_down(int rt)
{
if (!flag[rt]) return;
flag[rt<<1] = flag[rt<<1|1] = 1;
flag[rt] = 0;
sum[rt<<1]=sum[rt<<1]*lazy_tag[rt];
sum[rt<<1|1]=sum[rt<<1|1]*lazy_tag[rt];
lazy_tag[rt<<1] = lazy_tag[rt<<1]*lazy_tag[rt];
lazy_tag[rt<<1|1] = lazy_tag[rt<<1|1]*lazy_tag[rt];
lazy_tag[rt].E();
}
void up_data(int L,int R,int x,int l,int r,int rt)
{
if (L<= l && r <= R)
{
lazy_tag[rt]=lazy_tag[rt]*cur;
sum[rt] = sum[rt]*cur;
flag[rt] = 1;
return;
}
push_down(rt);
int m = (l+r)>>1;
if (L <= m) up_data(L,R,x,lson);
if (m < R) up_data(L,R,x,rson);
push_up(rt);
}
MX Q(int L,int R,int l,int r,int rt)
{
if (L <= l && r <= R)
return sum[rt];
push_down(rt);
int m = (l+r)>>1;
MX temp1,temp2;
temp1.O(),temp2.O();
if (L <= m) temp1 = Q(L,R,lson);
if (m < R) temp2 = Q(L,R,rson);
temp1 = temp1 + temp2;
return temp1;
}
LL query(int l,int r)
{
MX temp = Q(l,r,1,n,1);
temp = temp*v;
return temp.v[1][2];
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
cin >> n >> m;
rep1(i,1,n) cin >> a[i];
A.v[1][1] = 0,A.v[1][2] = A.v[2][1] = A.v[2][2] = 1;
v.v[1][1] = v.v[1][2] = v.v[2][1] = 0,v.v[2][2] = 1;
build(1,n,1);
rep1(i,1,m)
{
int type;
cin >> type;
if (type==1)
{
int l,r,x;
cin >> l >> r >> x;
cur = A^x;
up_data(l,r,x,1,n,1);
}
else
{
int l,r;
cin >> l >> r;
cout << query(l,r) << endl;
}
}
return 0;
}
【codeforces 719E】Sasha and Array的更多相关文章
- 【24.17%】【codeforces 721D】Maxim and Array
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 754A】Lesha and array splitting
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 258B】 Sort the Array
[题目链接] http://codeforces.com/contest/451/problem/B [算法] 模拟 在序列中找到一段单调递增的子序列,将这段序列反转,然后判断序列是否变得单调递增,即 ...
- codeforces 719E E. Sasha and Array(线段树)
题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...
- 【44.19%】【codeforces 727C】Guess the Array
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【Codeforces 1042D】Petya and Array
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 把a[i]处理成前缀和 离散化. 枚举i从1..n假设a[i]是区间和的a[r] 显然我们需要找到a[r]-a[l]<t的l的个数 即a ...
- 【codeforces 1109B】Sasha and One More Name
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 如果这个回文串的左半部分,字母全是一样的. 那么显然不可能再分出来了,因为不管怎么分怎么排列,最后肯定都只能和原串一样. 所以无解 其他情况下 ...
- 【Codeforces 1114B】Yet Another Array Partitioning Task
[链接] 我是链接,点我呀:) [题意] 让你把数组分成k个连续的部分 使得每个部分最大的m个数字的和最大 [题解] 把原数组降序排序 然后选取前m*k个数字打标记 然后对于原数组 一直贪心地取 直到 ...
- 【Codeforces 1109C 】Sasha and a Patient Friend
Codeforces 1109 C 题意:现在有个碗,每时每刻其中的水量都会加一个整数(可以为负). 给\(n\)个询问,询问有\(3\)种类型: \(1\ t\ s\):将从第\(t\)秒开始水量增 ...
随机推荐
- 时序图与状态图(Rose) - Windows XP经典软件系列
以CAN转UART(串口)为例. 功能简单介绍: 当主程序收到CAN口的数据后:依据按键的不同来选择是使用CAN口发送,还是使用UART口发送. 一.图片 时序图 watermark/2/text/a ...
- UVa 12525 Boxes and Stones (dp 博弈)
Boxes and Stones Paul and Carole like to play a game with S stones and B boxes numbered from 1 to B. ...
- 编写MyLayer,2 锚点,3 精灵的创建,4 zorder
1 编写MyLayer 头文件:MyLayer.h #include "cocos2d.h" USING_NS_CC; //代表的是: using namespace c ...
- Android之——ContentProvider操作XML
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47682559 不多说,不废话,直接上代码,大家一看都懂得 /** * Conten ...
- beisen
#include <stdio.h> #include <pthread.h> #include <windows.h> #define N 100 #define ...
- Php learn note
Php learn note 1. Between two part of ECHO, there is , sign rather than + sign. echo 'Hello World!!' ...
- EOJ 3031 二进制倒置
题目描述 给定一个整数 n(0≤n≤10100).将 n 的 334 位二进制表示形式(不包括开头可能的值为 0 的位,n=0 表示为 1 位 0)前后倒置,输出倒置后的二进制数对应的整数. 例如:n ...
- Tool-杂项-建模:犀牛(3D造型软件)
ylbtech-Tool-杂项-建模:犀牛(3D造型软件) 犀牛(Rhino)是美国Robert McNeel & Assoc.开发的PC上强大的专业3D造型软件,它可以广泛地应用于三维动画制 ...
- [javascript] jQuery系列之目录汇总
最近一个月写了些关于jQuery的文章,谢谢大家的支持.文章仅我个人观点,也许有不对的地方,请指出.这个系列还在更新中 一:jQuery基础系列: jQuery温习篇---强大的JQuery选择器 j ...
- @synthesize和@dynamic区别
在声明property属性后,有2种实现选择 @synthesize 编译器期间,让编译器自动生成getter/setter方法. 当有自定义的存或取方法时,自定义会屏蔽自动生成该方法 @dynami ...