【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次操作,$opt==1$时,对$[L,R]$全部加x,$opt==2$时,对$[L,R]$求$\sum_{i=L}^{R}Fibonacc(a_{i})$。
题解:
线段树+矩阵快速幂。
在每个线段树存一个转移矩阵,然后YY即可。
代码:
#include<cstdio>
#include<cstring>
#define lc t<<1
#define rc (t<<1)|1
typedef long long ll;
const int N=;
const ll mod=(ll)1e9+;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct Martrix{
ll a[][];
Martrix(){memset(a,,sizeof(a));}
inline void e(){
memset(a,,sizeof(a));
for(int i=;i<;i++)
a[i][i]=;
}
inline void fibe(){
for(int i=;i<;i++)
for(int j=;j<;j++)
a[i][j]=;
a[][]=;
}
friend Martrix operator *(Martrix x,Martrix y){
Martrix z;
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
z.a[i][j]=(z.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
return z;
}
friend Martrix operator ^(Martrix a,int b){
Martrix ans;ans.e();
while(b){
if(b&) ans=ans*a;
b>>=;
a=a*a;
}return ans;
}
}fibe_1;
struct Tree{
Martrix x,lazy;
}tree[N<<];
const int root=;
inline void update(int t){
tree[t].x.a[][]=(tree[lc].x.a[][]+tree[rc].x.a[][])%mod;
tree[t].x.a[][]=(tree[lc].x.a[][]+tree[rc].x.a[][])%mod;
}
inline void downdate(int t){
tree[lc].x=tree[t].lazy*tree[lc].x;
tree[rc].x=tree[t].lazy*tree[rc].x;
tree[lc].lazy=tree[lc].lazy*tree[t].lazy;
tree[rc].lazy=tree[rc].lazy*tree[t].lazy;
tree[t].lazy.e();
}
inline void build(int l,int r,int t){
tree[t].lazy.e();
if(l==r){
int num=read()-;
tree[t].x.fibe();
tree[t].x=(tree[t].x^num)*fibe_1;
return ;
}int mid=l+r>>;
build(l,mid,t<<);build(mid+,r,(t<<)|);
update(t);
}
inline void add(int l,int r,int L,int R,int t,Martrix x){
if(l!=r)
downdate(t);
if(L<=l&&r<=R){
tree[t].x=x*tree[t].x;
tree[t].lazy=x;
return ;
}
int mid=l+r>>;
if(R>mid) add(mid+,r,L,R,rc,x);
if(L<=mid) add(l,mid,L,R,lc,x);
update(t);
}
inline ll sum(int l,int r,int L,int R,int t){
if(l!=r)
downdate(t);
if(L<=l&&r<=R){
return tree[t].x.a[][];
}
int mid=l+r>>;
ll ans=;
if(R>mid) ans+=sum(mid+,r,L,R,rc);
if(L<=mid) ans+=sum(l,mid,L,R,lc);
return ans%mod;
}
int n,m;
int main(){
n=read(),m=read();
fibe_1.a[][]=;
build(,n,);
for(int i=,opt,l,r,x;i<=m;i++){
opt=read();
l=read(),r=read();
if(opt==){
x=read();
Martrix now;now.fibe();
now=now^x;
add(,n,l,r,root,now);
}
else{
printf("%lld\n",sum(,n,l,r,root));
}
}
}
D. Andrew and Chemistry
题目大意&链接:
http://codeforces.com/problemset/problem/718/D
(辣鸡有机化学)给一个无向图,每个节点只允许有至多4条边,且至少一条边,而且这个图是个树,现在新建一个节点,并与这个节点连条边,求可以构成多少个不一样的图(恩……就是是不是同构)。n<=100000
题解:
写个靠谱的哈希……然后因为节点多,但是只会有最多4条边,设F[x][i]表示从第x个节点,走第i条边得到的hash值,如果已经得到F[x][i]就不要再dfs下去了。
(什么你问我时间复杂度)
代码:
#include<cstdio>
#include<algorithm>
#include<set>
inline int read(){
int s=;char ch=getchar();
while(ch<''||ch>'') ch=getchar();
while(ch>=''&&ch<='') s=s*+(ch^),ch=getchar();
return s;
}
typedef unsigned long long ull;
ull P[]={0u,76543u,233u,123457u,56741857u,1537427u};
const int N=;
ull f[N];
ull g[N][];
std::set<ull> st;
int n;
struct edges {
int v;edges *last;
}edge[N<<],*head[N];int cnt;
inline void push(int u,int v){
edge[++cnt]=(edges){v,head[u]};head[u]=edge+cnt;
}
int a[];
inline bool cmp(int x,int y){
return f[x]<f[y];
}
inline void dfs(int x,int fa){
int num=;
f[x]=1u;
int k=;
for(edges *i=head[x];i;i=i->last){
k++;
if(fa==i->v) continue;
if(!g[x][k])
dfs(i->v,x),g[x][k]=f[i->v];
else
f[i->v]=g[x][k];
}
for(edges *i=head[x];i;i=i->last)
if(fa==i->v) continue;
else a[++num]=i->v;
std::sort(a+,a++num,cmp);
for(int i=;i<=num;i++)
f[x]=f[x]*P[]+f[a[i]]*P[i];
}
int red[N];
int main(){
n=read();
for(int i=;i<n;i++){
int u=read(),v=read();
push(u,v);push(v,u);
red[u]++,red[v]++;
}
//printf("s");
for(int i=;i<=n;i++){//printf("i=%d\n",i);
if(red[i]<){
dfs(i,i);
// printf("i=%d\n",i);
st.insert(f[i]);
}
}
printf("%d\n",st.size());
}
【codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry的更多相关文章
- 【题解】[CF718C Sasha and Array]
[题解]CF718C Sasha and Array 对于我这种喜欢写结构体封装起来的选手这道题真是太对胃了\(hhh\) 一句话题解:直接开一颗线段树的矩阵然后暴力维护还要卡卡常数 我们来把\(2 ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- 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 ...
- 【一天一道LeetCode】#88. Merge Sorted Array
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- Codeforces Round #373 (Div. 2) E. Sasha and Array 矩阵快速幂+线段树
E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input standard ...
- 【Codeforces Round #438 C】 Qualification Rounds
[链接]h在这里写链接 [题意] 给你n个问题,每个人都知道一些问题. 然后让你选择一些问题,使得每个人知道的问题的数量,不超过这些问题的数量的一半. [题解] 想法题. 只要有两个问题. 这两个问题 ...
- 【Codeforces Round #438 B】Race Against Time
[链接]h在这里写链接 [题意] 时针.分钟.秒针走不过去. 问你从t1时刻能不能走到t2时刻 [题解] 看看时针.分钟.秒针的影响就好. 看看是不是在整时的位置就好. 然后看看影响到x不能到y; 然 ...
- 【Codeforces Round #438 A】Bark to Unlock
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 枚举它是在连接处,还是就是整个字符串就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc+ ...
- 【Codeforces 321E / BZOJ 5311】【DP凸优化】【单调队列】贞鱼
目录 题意: 输入格式 输出格式 思路: DP凸优化的部分 单调队列转移的部分 坑点 代码 题意: 有n条超级大佬贞鱼站成一行,现在你需要使用恰好k辆车把它们全都运走.要求每辆车上的贞鱼在序列中都是连 ...
随机推荐
- WPF如何得到一个在用户控件内部的元素的坐标位置
例如有这样一个用户控件: <UserControl d:DesignHeight="100" d:DesignWidth="200" ...> &l ...
- IoC和DI的基本概念的思维导图
最近在学习Spring开发,IoC这个概念让我有点儿迷糊,控制反转这四个字是在是无法做到望文生义,于是乎就找了一些材料来学习,研究了半天,绘制了下面这幅思维导图.仅供参考!
- SAP BADI的“多次使用”(multiple use)
SAP中的某些BADI是不允许多用(multiple use)的,即不能同时存在多个活动的增强实施类.如下图中的这种,无论为其创建多少个实施类,都只有活动的那一个会被触发: tips : 业务加载项定 ...
- Delphi 项目总结
Delphi 项目总结 随着项目的失败,这些天一直在总结失败的原因,到底是为什么? 一.技术层面 1.少用指针类型,多用类. 虽然指针类型能有效的节约内 ...
- Day5_递归_二分法
递归调用: 在调用一个函数的过程中,直接或间接的调用函数本身. def func(): print('from func') 间接调用: def foo(): print('form foo') ba ...
- AI 学习之路
前言:本文章纯属自己学习路线纪录,不喜勿喷. 最近AI很火,几乎是个程序员 都要去学习AI,作为一个菜鸡小前端,我也踏上了学习AI的方向. 在学习之中,最开始遇到了很多的困难,比如你不知道如何切入进来 ...
- 学习了解 Exchanger - 实现生产者消费者模型
例子很简单 Exchanger可以理解为消息队列或者说是一个通信管道,从一边拿到消息,另外一边进行消费. 不过这个是同步实现的,消费者在exchange之前,生产者一直处于等待状态,而不是一直生产. ...
- 从GitHub中整理出来的15个最受欢迎的Python开源框架,你喜欢哪个
从GitHub中整理出的15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python Web应用开发框架 Djang ...
- MySQL数据库主从分离的配置方法
1.介绍 MySQL数据库设置读写分离,可以使对数据库的写操作和读操作在不同服务器上执行,提高并发量和响应速度.现在的网站一般大点的,都采用有数据库主从分离.读写分离,既起到备份作用也可以减轻数据库的 ...
- python中@classmethod @staticmethod区别
Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...