[莫队算法 线段树 斐波那契 暴力] Codeforces 633H Fibonacci-ish II
题目大意:给出一个长度为n的数列a。
对于一个询问lj和rj。将a[lj]到a[rj]从小到大排序后并去重。设得到的新数列为b,长度为k,求F1*b1+F2*b2+F3*b3+...+Fk*bk。当中F为斐波那契数列。F1=F2=1。对每一个询问输出答案模m。
区间查询离线 用莫队算法
开棵权值线段树,然后用斐波那契的性质update
F(n+m)=F(n+1)*F(m)+F(n)*F(m-1);
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std; inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
} inline void read(int &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
} int P;
int sx[30005];
int icnt; inline int Bin(int x){
return lower_bound(sx+1,sx+icnt+1,x)-sx;
} struct SEGTREE{
struct node{
int k;
int fk,fk_1;
int a1,a2;
friend node operator + (node &A,node &B){
if (!A.k) return B;
if (!B.k) return A;
node ret;
ret.k=A.k+B.k;
(ret.fk=(A.fk+A.fk_1)*B.fk+A.fk*B.fk_1)%=P;
(ret.fk_1=A.fk*B.fk+A.fk_1*B.fk_1)%=P;
ret.a1=A.a1;
(ret.a1+=A.fk*B.a2+A.fk_1*B.a1)%=P;
ret.a2=A.a2;
(ret.a2+=(A.fk+A.fk_1)*B.a2+A.fk*B.a1)%=P;
return ret;
}
};
node T[120005];
int cnt[120005];
int M,TH;
inline void Build(int n){
for (M=1,TH=0;M<n+2;M<<=1,TH++);
}
inline int Query(){
return T[1].a1;
}
inline void Change(int s,int r){
s+=M;
if (r==1)
{
cnt[s]++;
if (cnt[s]==1)
{
T[s].k=1;
T[s].fk=1;
T[s].fk_1=0;
(T[s].a1=sx[s-M])%=P;
(T[s].a2=sx[s-M])%=P;
while (s>>=1)
T[s]=T[s<<1]+T[s<<1|1];
}
}
else if (r==-1)
{
cnt[s]--;
if (cnt[s]==0)
{
T[s].k=0;
T[s].fk=0;
T[s].fk_1=0;
T[s].a1=0;
T[s].a2=0;
while (s>>=1)
T[s]=T[s<<1]+T[s<<1|1];
}
}
}
}SEG; int n,Q,B;
int a[30005],ans[30005]; struct event{
int x,y,lpos;
int idx;
bool operator < (const event &B) const{
return lpos==B.lpos?y<B.y:lpos<B.lpos;
}
}eve[30005]; inline void Mos()
{
int l=1,r=0;
for (int i=1;i<=Q;i++)
{
while (r<eve[i].y) SEG.Change(Bin(a[++r]),1);
while (r>eve[i].y) SEG.Change(Bin(a[r--]),-1);
while (l<eve[i].x) SEG.Change(Bin(a[l++]),-1);
while (l>eve[i].x) SEG.Change(Bin(a[--l]),1);
ans[eve[i].idx]=SEG.Query();
}
} int main()
{
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n); read(P); B=sqrt(n);
for (int i=1;i<=n;i++)
read(a[i]),sx[++icnt]=a[i];
sort(sx+1,sx+icnt+1);
icnt=unique(sx+1,sx+icnt+1)-sx-1;
SEG.Build(icnt);
read(Q);
for (int i=1;i<=Q;i++)
{
read(eve[i].x); read(eve[i].y);
eve[i].lpos=(eve[i].x-1)/B+1; eve[i].idx=i;
}
sort(eve+1,eve+Q+1);
Mos();
for (int i=1;i<=Q;i++)
printf("%d\n",ans[i]);
return 0;
}
然而出题人太奇妙,这样的做法常数极大,还是暴力短小精悍
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e4+5;
pair<int,int> a[maxn];
int ans[maxn],step[maxn],f[maxn],l[maxn],r[maxn],last[maxn]; int main()
{
freopen("t.in","r",stdin);
freopen("t1.out","w",stdout);
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i].first),a[i].second=i;
sort(a+1,a+1+n);
f[0]=1,f[1]=1;
for(int i=2;i<=n;i++)
f[i]=(f[i-1]+f[i-2])%m;
int q;scanf("%d",&q);
for(int i=1;i<=q;i++)
{
scanf("%d%d",&l[i],&r[i]);
last[i]=-1;
}
for(int i=1;i<=n;i++)
{
int d = a[i].first % m;
for(int j=1;j<=q;j++)
{
if(a[i].second<l[j]||a[i].second>r[j])continue;
if(a[i].first==last[j])continue;
ans[j]=(ans[j]+f[step[j]++]*d)%m;
last[j]=a[i].first;
}
}
for(int i=1;i<=q;i++)
printf("%d\n",ans[i]);
}
[莫队算法 线段树 斐波那契 暴力] Codeforces 633H Fibonacci-ish II的更多相关文章
- [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
- Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- 【CF446C】DZY Loves Fibonacci Numbers (线段树 + 斐波那契数列)
Description 看题戳我 给你一个序列,要求支持区间加斐波那契数列和区间求和.\(~n \leq 3 \times 10 ^ 5, ~fib_1 = fib_2 = 1~\). Solut ...
- hdu 4983 线段树+斐波那契数
http://acm.hdu.edu.cn/showproblem.php?pid=4893 三种操作: 1 k d, 修改k的为值增加d 2 l r, 查询l到r的区间和 3 l r, 从l到r区间 ...
- CF633H Fibonacci-ish II 莫队、线段树、矩阵乘法
传送门 这题除了暴力踩标程和正解卡常数以外是道很好的题目 首先看到我们要求的东西与\(Fibonacci\)有关,考虑矩阵乘法进行维护.又看到\(n \leq 30000\),这告诉我们正解算法其实比 ...
- SPOJ DQUERY - D-query (莫队算法|主席树|离线树状数组)
DQUERY - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query ...
- Python开发【算法】:斐波那契数列两种时间复杂度
斐波那契数列 概述: 斐波那契数列,又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1, ...
- 算法笔记_001:斐波那契数的多种解法(Java)
本篇文章解决的问题来源于算法设计与分析课程的课堂作业,主要是运用多种方法来计算斐波那契数.具体问题及解法如下: 一.问题1: 问题描述:利用迭代算法寻找不超过编程环境能够支持的最大整数的斐波那契数是第 ...
- hdu 4099 字典树 + 斐波那契
题意: 给你一个串(最长40位)问你这个串是斐波那契F(n) n <= 99999中的那个数的前缀,如果存在多个输出最小的n否则输出-1. 思路: 给的串最长40位,那 ...
随机推荐
- python 重新执行循环中出错的那一次
# coding: utf-8 li = [1,2,3,4,5] for num in li: while True: try: # do something except some error: c ...
- web服务器解析漏洞总结(转)
转:http://www.secpulse.com/archives/3750.html 解析漏洞总结 2015 /1/27 22:09 一.IIS 5.x/6.0解析漏洞 IIS 6.0解析利用方法 ...
- thinkphp 检测上传的图片中是否含有木马脚本
1.检测原理 要想检测图片中是否含有木马脚本,首先从制作原理来分析这种木马程序.这种木马程序是十六进制编码写的,图片的十六进制代码中主要包含<% ( ) %>.<? ( ) ?> ...
- 【HDU 5283】Senior's Fish
http://acm.hdu.edu.cn/showproblem.php?pid=5283 今天的互测题,又爆零了qwq 考虑每个点对答案的贡献. 对每个点能产生贡献的时间线上的左右端点整体二分. ...
- [Codeforces #188] Tutorial
Link: Codeoforces #188 传送门 A: 先全转为正数,后面就全是指数级增长了 #include <bits/stdc++.h> using namespace std; ...
- AC自动机及KMP练习
好久都没敲过KMP和AC自动机了.以前只会敲个kuangbin牌板子套题.现在重新写了自己的板子加深了印象.并且刷了一些题来增加自己的理解. KMP网上教程很多,但我的建议还是先看AC自动机(Trie ...
- BZOJ 4327 JSOI2012 玄武密码(后缀自动机)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4327 [题目大意] 求每个子串在母串中的最长匹配 [题解] 对母串建立后缀自动机,用每 ...
- Codeforces 804E The same permutation(构造)
[题目链接] http://codeforces.com/contest/804/problem/E [题目大意] 给出一个1到n的排列,问每两个位置都进行一次交换最终排列不变是否可能, 如果可能输出 ...
- 猫、路由器、交换机和PC
转载:http://duanzw102.blog.163.com/blog/static/161838173201392431722650/ 猫是 modem,是有网络供应商,比如电信公司提供的拨号工 ...
- AS3.0 Vector的运用
使用Vector类编程 一个array(数组)就像是把一套变量组织在一起的容器.单个数组可以含有许多不同的值.你可以储存和取得数组中的单个值(也就是数组elements(元素)).你也可以通过直接操作 ...