codeforces 474F Ant colony(线段树)
来源:互联网 发布:没货源怎么开淘宝店 编辑:程序博客网 时间:2024/05/18 13:48
对于每个查询的区间,能活下来的蚂蚁的个数就是这段区间中等于这段区间gcd的s的个数,而gcd一定小于等于这段区间的最小值,所以可用线段树来维护每个区间的gcd,最小值和最小值出现的次数,如果gcd等于最小值,那么这段区间中能活下来的蚂蚁的个数就是最小值出现的次数。
#include <iostream>#include<cstdio>#include<cstring>using namespace std;int s[100005];int gcd(int a,int b){ if(!b) return a; return gcd(b,a%b);}struct A{ int l,r,d,mn,times;}a[4*100005];void build(int k,int l,int r){ a[k].l=l; a[k].r=r; if(l==r) { a[k].mn=s[l]; a[k].times=1; a[k].d=s[l]; return ; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); a[k].d=gcd(a[k<<1].d,a[k<<1|1].d); if(a[k<<1].mn<a[k<<1|1].mn){ a[k].mn=a[k<<1].mn; a[k].times=a[k<<1].times; } else if(a[k<<1].mn>a[k<<1|1].mn){ a[k].mn=a[k<<1|1].mn; a[k].times=a[k<<1|1].times; } else{ a[k].mn=a[k<<1].mn; a[k].times=a[k<<1].times+a[k<<1|1].times; }}int ans;struct B{ int d,mn,times;};B query(int k,int ql,int qr){ B tmp; if(ql<=a[k].l && a[k].r<=qr){ tmp.d=a[k].d; tmp.mn=a[k].mn; tmp.times=a[k].times; return tmp; } int mid=(a[k].l+a[k].r)>>1; B left,right; if(qr<=mid) return query(k<<1,ql,qr); else if(mid<ql) return query(k<<1|1,ql,qr); else{ left=query(k<<1,ql,qr); right=query(k<<1|1,ql,qr); tmp.d=gcd(left.d,right.d); if(left.mn<right.mn){ tmp.mn=left.mn; tmp.times=left.times; } else if(left.mn>right.mn){ tmp.mn=right.mn; tmp.times=right.times; } else{ tmp.mn=left.mn; tmp.times=left.times+right.times; } return tmp; }}int check(B x){ if(x.d==x.mn) return x.times; else return 0;}int main(){ int n,t,i; scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d",s+i); } build(1,1,n); scanf("%d",&t); for(i=1;i<=t;i++){ int l,r; scanf("%d%d",&l,&r); printf("%d\n",(r-l+1)-check( query(1,l,r) )); } return 0;}
0 0
- codeforces 474F Ant colony(线段树)
- codeforces 474F F. Ant colony(线段树+数论)
- codeforces 474F F. Ant colony(线段树+数论)
- CodeForces 474F Ant colony(线段树)
- CodeForces 474F Ant colony 【线段树】【gcd】【二分】
- Codeforces 474 F. Ant colony
- Codeforces Round #271 (Div. 2)F. Ant colony(线段树)
- Codeforces Round #271 (Div. 2) F. Ant colony(线段树)
- 【CODEFORCES】 F. Ant colony
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树求区间gcd)
- Codeforces Round #271 (Div. 2) F - Ant colony 线段树 + GCD
- F. Ant colony(Codeforces Round #271)
- 【Codeforces Round 271 (Div 2)F】【贪心 线段树】Ant colony 区间段内是其他所有数因子的数的个数
- Codeforces Round #271 (Div. 2) F Ant colony
- 【CodeForces】240F - TorCoder 线段树
- Codeforces 240F. TorCoder 线段树
- Codeforces Round #397 F Souvenirs(线段树)
- HOJ13030 数位dp
- Bayan programming Contest 2014-2015 Qualification A-Bertaco's Police
- 【重读设计模式】模版方法模式
- 创建和销毁对象
- Hibernate 专题
- codeforces 474F Ant colony(线段树)
- Java的native方法
- Bayan programming Contest 2014-2015 Qualification C-Extraterrestrial Rotations
- 增加短信
- [置顶] View工作原理(四)view的layout过程
- Bayan programming Contest 2014-2015 Qualification B-Weird Officer
- Cocos2d-x_CCLabelProtocol(标签类)
- tcpdump详细用法
- HTML的电子邮件链接标签mailto用法详解