【题解】CF718C Sasha and Array

对于我这种喜欢写结构体封装起来的选手这道题真是太对胃了\(hhh\)

一句话题解:直接开一颗线段树的矩阵然后暴力维护还要卡卡常数

我们来把\(2 \times 2\)看做之后时间复杂度就是\(O(nlogn)\)。

写了一点点这种线段树维护除了数字之外的东西的题目,一个最大的感受就是递归用\(void\),传答案什么的一个全局变量,这样比较快。

需要注意的点是\(lazy \ \ tag\)要随着\(seg\)一起初始化一下。

#include<bits/stdc++.h>

using namespace std;typedef long long ll;
#define DRP(t,a,b) for(register int t=(a),edd=(b);t>=edd;--t)
#define RP(t,a,b) for(register int t=(a),edd=(b);t<=edd;++t)
#define ERP(t,a) for(register int t=head[a];t;t=e[t].nx)
#define midd register int mid=(l+r)>>1
#define TMP template < class ccf >
#define lef l,mid,pos<<1
#define rgt mid+1,r,pos<<1|1 TMP inline ccf qr(ccf b){
register char c=getchar();register int q=1;register ccf x=0;
while(c<48||c>57)q=c==45?-1:q,c=getchar();
while(c>=48&&c<=57)x=x*10+c-48,c=getchar();
return q==-1?-x:x;}
TMP inline ccf Max(ccf a,ccf b){return a<b?b:a;}
TMP inline ccf Min(ccf a,ccf b){return a<b?a:b;}
TMP inline ccf Max(ccf a,ccf b,ccf c){return Max(a,Max(b,c));}
TMP inline ccf Min(ccf a,ccf b,ccf c){return Min(a,Min(b,c));}
TMP inline ccf READ(ccf* _arr,int _n){RP(t,1,_n)_arr[t]=qr((ccf)1);}
//----------------------template&IO---------------------------
const int mod=1e9+7;
const int maxn=1e5+5;
struct MTX{
ll data[2][2];
MTX(){data[0][0]=data[0][1]=data[1][0]=data[1][1]=0;}
inline ll* operator[](int x){return data[x];}
inline void operator *=(MTX a){
MTX ret;
RP(k,0,1)RP(i,0,1)RP(t,0,1)ret[t][i]=(ret[t][i]+data[t][k]*a[k][i])%mod;
*this=ret;}
inline void unis(){data[0][1]=data[1][0]=data[1][1]=1;data[0][0]=0;}
inline void cle() {*this=MTX();RP(t,0,1)data[t][t]=1;}
inline void operator ^=(int x){
MTX base=*this,ret;RP(t,0,1) ret[t][t]=1;
for(register int t=x;t;t>>=1,base*=base)if(t&1)ret*=base;
*this=ret;}
inline void operator +=(MTX x){RP(t,0,1)RP(i,0,1)data[t][i]=(data[t][i]+x[t][i])%mod;}
inline void pr(){RP(t,0,1){RP(i,0,1)cout<<data[t][i]<<' ';cout<<endl;}cout<<endl;}
}seg[maxn<<2],laz[maxn<<2]; bool lz[maxn<<2];
MTX ret,md;
int n,m; inline void pd(int pos){
if(not lz[pos]) return;
seg[pos<<1]*=laz[pos];
seg[pos<<1|1]*=laz[pos];
laz[pos<<1]*=laz[pos];
laz[pos<<1|1]*=laz[pos]; lz[pos<<1]=lz[pos<<1|1]=1;
laz[pos].cle();lz[pos]=0;
} inline void pp(int pos){
seg[pos]=MTX();
seg[pos]+=seg[pos<<1];
seg[pos]+=seg[pos<<1|1];
} void build(int l,int r,int pos){
if(l==r){
seg[pos].unis();
laz[pos].cle();
seg[pos]^=qr(1)-1;
return;
}
midd;laz[pos].cle();
build(lef);build(rgt);
pp(pos);
} void upd(int L,int R,int l,int r,int pos){
if(L<=l&&r<=R){
laz[pos]*=md;
seg[pos]*=md;
lz[pos]=1;
return;
}midd;pd(pos);
if(L<=mid) upd(L,R,lef);
if(mid< R) upd(L,R,rgt);
pp(pos);
} void que(int L,int R,int l,int r,int pos){
if(L<=l&&r<=R){
ret+=seg[pos];
return;
}midd;pd(pos);
if(L<=mid) que(L,R,lef);
if(mid< R) que(L,R,rgt);
pp(pos);
} int main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
#endif
n=qr(1);m=qr(1);
build(1,n,1);
RP(t,1,m){
register int t1,t2;
if(qr(1)==1){
t1=qr(1);t2=qr(1);
md.unis();md^=qr(1);
upd(t1,t2,1,n,1);
}
else{
t1=qr(1);t2=qr(1);
ret=MTX();
que(t1,t2,1,n,1);
printf("%lld\n",(ret[0][0]+ret[0][1])%mod);
}
}
return 0;
}

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

  1. CF718C Sasha and Array(线段树维护矩阵)

    题解 (不会矩阵加速的先去学矩阵加速) 反正我想不到线段树维护矩阵.我太菜了. 我们在线段树上维护一个区间的斐波那契的列矩阵的和. 然后询问时提取每个符合题意列矩阵的答案项(不是列矩阵存了两项吗,一个 ...

  2. [CF718C] Sasha and Array

    Description 给定一个数列,维护两种操作 操作 \(1\),将区间 \([l,r]\) 的数字统一加 \(x\). 操作 \(2\),求 \(\sum \limits_{i=l}^r f(v ...

  3. CF718C Sasha and Array 线段树+矩阵加速

    正解:线段树 解题报告: 传送门! 首先这种斐波拉契,又到了1e9的范围,又是求和什么的,自然而然要想到矩阵加速昂 然后这里主要是考虑修改操作,ai+=x如果放到矩阵加速中是什么意思呢QAQ? 那不就 ...

  4. CF718C Sasha and Array 线段树 + 矩阵乘法

    有两个操作: 将 $[l,r]$所有数 + $x$ 求 $\sum_{i=l}^{r}fib(i)$ $n=m=10^5$   直接求不好求,改成矩阵乘法的形式:  $a_{i}=M^x\times ...

  5. CF718C Sasha and Array [线段树+矩阵]

    我们考虑线性代数上面的矩阵知识 啊呸,是基础数学 斐波那契的矩阵就不讲了 定义矩阵 \(f_x\) 是第 \(x\) 项的斐波那契矩阵 因为 \(f_i * f_j = f_{i+j}\) 然后又因为 ...

  6. codeforces 719E E. Sasha and Array(线段树)

    题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...

  7. 【codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry

    C. Sasha and Array 题目大意&题目链接: http://codeforces.com/problemset/problem/718/C 长度为n的正整数数列,有m次操作,$o ...

  8. Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵

    E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...

  9. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

随机推荐

  1. 使用OPENROWSET爆破SQL Server密码

    使用OPENROWSET爆破SQL Server密码   OPENROWSET函数是SQL Server提供的一个连接函数.它可以用于使用OLE DB方式连接一个数据库,并进行数据查询等操作.使用该函 ...

  2. SQLite中使用全文搜索FTS

    SQLite中使用全文搜索FTS   SQLite支持全文搜索.通过全文搜索功能,可以方便用户快速进行查找.在iOS中,GRDB.FMDB等SQLite框架均支持FTS技术,如FTS3.FTS4等.各 ...

  3. HDU1969

    记得用PI=acos(-1)反三角函数求,用一次排序,然后二分和贪心 #include<iostream> #include<algorithm> #include<io ...

  4. Win10蓝屏代码

    UNEXPECTED_STORE_EXCEPTION “意外存储异常”是Windows 10上的“停止错误”,表示存储组件发生意外异常. 原因 固态硬盘驱动与当前固态硬盘驱动程序不兼容或是当前固态硬盘 ...

  5. hdu1595 find the longest of the shortest(Dijkstra)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1595 find the longest of the shortest Time Limit: 100 ...

  6. 给java类加static修饰编译器会说什么?

    Illegal modifier for the class XXX;only public abstract & final are permitted.

  7. 未经处理的异常在 System.Data.dll 中发生。其它信息:在应使用条件的上下文(在 &#39;***&#39; 附近)中指定了非布尔类型的表达式。

    机房收费系统中,有些人在联合查询这个模块用的是存储过程.我先尝试着在数据库中建立了一个视图.然后在UI层做个推断并生成查询条件strCondition. 在机房收费系统的"联合查询" ...

  8. CodeForces - 344B Simple Molecules (模拟题)

    CodeForces - 344B id=46665" style="color:blue; text-decoration:none">Simple Molecu ...

  9. vue.js+koa2项目实战(五)axios 及 vue2.0 子组件和父组件之间的传值

    axios 用法: 1.安装 npm install axios --save-dev 2.导入 import axios from 'axios'; 3.使用 axios.post(url,para ...

  10. vue 父子通信过程

    1.概述 每个 Vue 实例都实现了事件接口,即: 使用 $on(eventName) 监听事件 使用 $emit(eventName, optionalPayload) 触发事件 2.示例一(未传递 ...