并不对劲的hdu4777
n rabbits were numbered form 1 to n. All rabbits' weight is an integer. For some unknown reason, two rabbits would fight each other if and only if their weight is NOT co-prime.
Now the king had arranged the n rabbits in a line ordered by their numbers. The king planned to send some rabbits into prison. He wanted to know that, if he sent all rabbits between the i-th one and the j-th one(including the i-th one and the j-th one) into prison, how many rabbits in the prison would not fight with others.
Please note that a rabbit would not fight with himself.
Input
The input consists of several test cases.
The first line of each test case contains two integer n, m, indicating the number of rabbits and the queries.
The following line contains n integers, and the i-th integer W i indicates the weight of the i-th rabbit.
Then m lines follow. Each line represents a query. It contains two integers L and R, meaning the king wanted to ask about the situation that if he sent all rabbits from the L-th one to the R-th one into prison.
(1 <= n, m, W i <= 200000, 1 <= L <= R <= n)
The input ends with n = 0 and m = 0.
Output
For every query, output one line indicating the answer.Sample Input
3 2
2 1 4
1 2
1 3
6 4
3 6 1 2 5 3
1 3
4 6
4 4
2 6
0 0
Sample Output
2
1
1
3
1
2
Hint
In the second case, the answer of the 4-th query is 2, because only 1 and 5 is co-prime with other numbers in the interval [2,6] .
——————————————————并不对劲的分界线——————————
听说很对劲的太刀流做出了本题,并不对劲的片手流为了反驳他,决定与他针锋相对。于是就也打算做这道题。
这题就是求区间内与区间内不包括自己所有数都互质的数有多少个。
先说个简单的小技巧:差分。想必大家都知道前缀和可以O(n)通过预处理,进行O(1)的查询。而差分刚好与前缀和相反,是O(1)修改,O(n)查询。实现方式也与前缀和相反。一开始有一个全是0的数列,每次对于[l,r]进行区间加k时,再
l处+k,r+1处-k,这样每个位置的前缀和表示这个位置加了多少数。查询次数只有一次时可以用这个,而且显然比线段树好写多了。
再看这一题,对于第x个w[x],先通过分解质因数算出离它最近的两个与它不互质的数,分别记作pre[x],suf[x]。那么对于[l[i],r[i]]这一段区间,x会对解有贡献当且仅当pre[x]<l且r<suf[x]。
这时发现这有点像个二维偏序,那么一切都好办了。
好办个潜口龙啊!这个问题是找l>pre[x]且r<suf[x]且l<x<r的x有多少个!不过其实可以将询问按l排序,这样通过插入/删除,使得被统计的那些满足与l有关的条件。每次插入时,要将x到suf[x]-1区间+1,删除则是刚好反过来。那么答案就是r处对应的值了。会发现只有区间加和单点查,所以就可以用一开始说的树状数组差分了。
不得不说,细节还真麻烦…
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);i++)
#define dwn(i,x,y) for(register int i=(x);i>=(y);i--)
#define maxn 200002
using namespace std;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(isdigit(ch)==0 && ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return x*f;
}
inline void write(int x)
{
int f=0;char ch[20];
if(!x){puts("0");return;}
if(x<0){putchar('-');x=-x;}
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar('\n');
}
typedef struct que{int l,r,ord,ans;} Q;
vector<int >v[maxn];
Q q[maxn];
bool isp[maxn];
int n,m,w[maxn],fir[maxn],l[maxn],r[maxn],ord[maxn],hd,tr[maxn];
inline int lt(int x){return x&-x;}
inline int ask(int i){int ans=0;for(;i;i-=lt(i))ans+=tr[i];return ans;}
inline void add(int i,int k){for(;i<=n;i+=lt(i))tr[i]+=k;}
bool cmpl(Q x,Q y){return x.l==y.l?x.r<y.r:x.l<y.l;}
bool cmp2(int x,int y){return l[x]==l[y]?r[x]<r[y]:l[x]<l[y];}
bool cmpod(Q x,Q y){return x.ord<y.ord;}
inline void getp(int lim)
{
lim--;
isp[0]=isp[1]=1;
rep(i,2,lim)if(!isp[i]){rep(j,2,lim/i)isp[i*j]=1;}
rep(i,2,lim)if(!isp[i]){rep(j,1,lim/i)v[i*j].push_back(i);}
}
inline void reset()
{
memset(fir,0,sizeof(fir));
memset(tr,0,sizeof(tr));
rep(i,1,n)l[i]=0,r[i]=n+1;
}
int main()
{
memset(isp,0,sizeof(isp));
getp(maxn);
while(1)
{
n=read(),m=read();
if(n==0 && m==0)break;
reset();
rep(i,1,n)
{
w[i]=read();int tmp=w[i],lim=v[w[i]].size()-1;ord[i]=i;
rep(j,0,lim)
{
l[i]=max(l[i],fir[v[w[i]][j]]);
fir[v[w[i]][j]]=i;
while(tmp%v[w[i]][j]==0)tmp/=v[w[i]][j];
}
//cout<<"+"<<endl;
}
memset(fir,31,sizeof(fir));
dwn(i,n,1)
{
int tmp=w[i],lim=v[w[i]].size()-1;
rep(j,0,lim)
{
r[i]=min(r[i],fir[v[w[i]][j]]);
fir[v[w[i]][j]]=i;
while(tmp%v[w[i]][j]==0)tmp/=v[w[i]][j];
}
}
rep(i,1,m)q[i].l=read(),q[i].r=read(),q[i].ord=i;
sort(q+1,q+m+1,cmpl);
sort(ord+1,ord+n+1,cmp2);
int j=1,k=1;
rep(i,1,m)
{
while(l[ord[j]]<q[i].l&&j<=n)add(ord[j],1),add(r[ord[j]],-1),j++;
while(k<q[i].l&&k<=n)add(k,-1),add(r[k],1),k++;
q[i].ans=ask(q[i].r);
}
sort(q+1,q+m+1,cmpod);
rep(i,1,m)
write(q[i].ans);
}
return 0;
}
/*
3 2
2 1 4
1 2
1 3
6 4
3 6 1 2 5 3
1 3
4 6
4 4
2 6
0 0
*/
并不对劲的hdu4777的更多相关文章
- 并不对劲的BJOI2019
一些感想 现实并非游戏,并不支持反复刷关 猎人和防御工事一起被老山龙摧毁了: 猎人惨死雨中,结云村永无放晴之日: 猎人被狂龙病毒侵蚀,天空山上黑蚀龙泛滥. 好像这才是怪物猎人系列的真实结局呢 day ...
- 并不对劲的uoj276. [清华集训2016]汽水
想要很对劲的讲解,请点击这里 题目大意 有一棵\(n\)(\(n\leq 50000\))个节点的树,有边权 求一条路径使该路径的边权平均值最接近给出的一个数\(k\) 输出边权平均值下取整的整数部分 ...
- 并不对劲的DFT
FFT是一个很多人选择背诵全文的算法. #include<algorithm> #include<cmath> #include<complex> #include ...
- 并不对劲的字符串专题(三):Trie树
据说这些并不对劲的内容是<信息学奥赛一本通提高篇>的配套练习. 并不会讲Trie树. 1.poj1056-> 模板题. 2.bzoj1212-> 设dp[i]表示T长度为i的前 ...
- 并不对劲的字符串专题(二):kmp
据说这些并不对劲的内容是<信息学奥赛一本通提高篇>的配套练习. 先感叹一句<信息学奥赛一本通提高篇>上对kmp的解释和matrix67的博客相似度99%(还抄错了),莫非mat ...
- 并不对劲的bzoj1861: [Zjoi2006]Book 书架
传送门-> 这题的正确做法是splay维护这摞书. 但是并不对劲的人选择了暴力(皮这一下很开心). #include<algorithm> #include<cmath> ...
- 并不对劲的bzoj3932: [CQOI2015]任务查询系统
传送门-> 离线操作听上去很简单,遗憾的是它强制在线. 每个时刻可以看成可持久化线段树中的一个版本,而每一个版本的线段树维护的是值某一段区间且在这个版本对应的时刻出现的数之和. 会发现同一时刻可 ...
- 并不对劲的bzoj1853:[SCOI2010]幸运数字
传送门-> 据说本题的正确读法是[shìng运数字]. 听上去本题很适合暴力,于是并不对劲的人就去写了.其实这题就是一个很普(有)通(趣)暴力+神奇的优化. 首先,会发现幸运数字很少,那么就先搜 ...
- 并不对劲的bzoj4199: [Noi2015]品酒大会
传送门-> 又称普及大会. 这题没什么好说的……后缀自动机裸题……并不对劲的人太菜了,之前照着标程逐行比对才过了这道题,前几天刚刚把这题一遍写对…… 这题的输出和某两点相同后缀的长度有关,那么把 ...
随机推荐
- 【贪心+二分】codeforces D. Magazine Ad
codeforces.com/contest/803/problem/D [题意] 给定一个字符串,字符串里可能有空格和连字符‘-’,空格和连字符的意义是一样的,都表示:能在那个位置把字符串分成两部分 ...
- OpenJudge 6042 雇佣兵
37:雇佣兵 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 雇佣兵的体力最大值为M,初始体力值为0.战斗力为N.拥有X个能量元素. 当雇佣兵的体力值恰好为M时,才可以参加一个 ...
- CF830B:Cards Sorting
对叠放着的n张牌,第i张牌写有数字Ai,进行操作:将牌堆顶的牌取出,若是当前牌堆最小值就扔掉,否则放到牌堆底,求牌堆空时操作次数. 怎么看怎么像约瑟夫..不过约瑟夫DP我不太熟,于是就yy了一下 “当 ...
- 如何改变linux系统的只读文件的权限
vim 编辑可以在命令模式输入 :wq! 保存退出可以用chmod 命令修改文件权限. chmod命令是非常重要的,用于改变文件或目录的访问权限.用户用它控制文件或目录的访问权限.该命令有两种用法.一 ...
- msp430入门编程35
msp430中C语言的可移植--规划软件层次
- php 翻转字符串
//方法一 function strrev_charset($string,$charset='utf-8'){ if(!is_string($string) || !mb_check_encodin ...
- hashlib-sha摘要算法模块
摘要:hashlib: 摘要算法的模块 用处: 1.查看某两个文件是否完全一致 "abcdefggg" "abcdefhhg" 2.加密认证 把密码加密后写入文 ...
- Linux中的进程与线程
介绍了Linux下fork()创建进程以及使用pthread_create()创建线程的方法 1. 基于进程的斐波那契数列 在下面的代码中,由子进程进行斐波那契数列的输出,父进程要等待子进程输出完毕, ...
- 创建SSH keys
1.检查是否已经有SSH Key存在 windows: type "%userprofile%\.ssh\id_rsa.pub" Linux: cat ~/.ssh/id_rsa. ...
- 【webstorm 系列之一】快捷键很好用啊
书签 bookmarks , 在多文件中调试很方便 断点只能在js文件中用,而bookmark可以在所有文件中使用 书签开关 F11 (给光标所在行加书签) 显示书签 Shift + F11 书签号 ...