题意:给了n个数,然后又m次查询,询问[L,R] 内有多少个数与其他的数不互质。

解:

我们首先可以通过处理得出每个数的有效区间,LR 就是 左边L位置上的数 和他不互质, 右边R位置上的数和不互质,

我们对于询问排序,R小的排前面,枚举每个R,在loc位置就将第loc个点在loc的位置加上一个1在loc的L(左不互质点)减一个1,再将枚举到该位的时候对于有在这个位置上R的点 在loc位置减1

在loc的L位置加1

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <vector>
#include <cmath>
using namespace std;
const int maxn=;
typedef long long LL;
bool vis[maxn];
int yinzi[maxn][],numofYZ[maxn];
void sieve()
{ memset(vis,false,sizeof(vis));
memset(numofYZ,false,sizeof(numofYZ));
for(LL i=; i<=; i++)
{
if(vis[i])continue;
yinzi[i][numofYZ[i]++]=i;
for(LL j=i+i; j<=; j+=i)
{
vis[j]=true;
yinzi[j][numofYZ[j]++]=i;
}
}
}
struct point{
int L,R,id;
bool operator <(const point &rhs)const {
return R<rhs.R||( R == rhs.R && L<rhs.L);
}
}wLR[maxn],Q[maxn];
int Loc[maxn];
int w[maxn];
vector<int>G[maxn];
void init(int n)
{
for(int i=; i<=n+; i++)
G[i].clear();
}
int n,m;
int C[maxn];
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int v)
{
if(x<=)return ;
while(x<=n)
{
C[x]+=v;
x+=lowbit(x);
}
}
int sum(int x)
{
int ans=;
while(x>)
{
ans+=C[x];
x-=lowbit(x);
}
return ans;
}
int ans[maxn];
int main()
{
sieve();
while(scanf("%d%d",&n,&m)==&&n)
{
int maW=;
for(int i=; i<=n; i++)
{
scanf("%d",&w[i]);
maW=max(maW,w[i]);
}
memset(Loc,,sizeof(Loc));
for(int i=; i<=n; i++)
{
int ww=w[i];
int L=;
for(int j=; j<numofYZ[ ww ]; j++)
L=max(L,Loc[ yinzi[ ww ][ j ] ]);
wLR[i].L=L;
for(int j=; j<numofYZ[ ww ]; j++)
Loc[ yinzi[ ww ][ j ] ] = i;
}
for(int i=; i<=maW; i++)Loc[i]=n+;
for(int i=n; i>; i--)
{
int ww=w[i];
int R=n+;
for(int j=; j<numofYZ[ ww ]; j++)
R=min(R,Loc[ yinzi[ ww ][ j ] ]);
wLR[i].R=R;
G[R].push_back(i);
for(int j=; j < numofYZ[ ww ]; j++)
Loc[ yinzi[ww][ j ] ]=i;
}
for(int i=; i<m; i++)
{
Q[i].id=i;
scanf("%d%d",&Q[i].L,&Q[i].R);
}
sort(Q,Q+m);
int now=;
memset(C,,sizeof(C));
for(int i=; i<m; i++)
{
while(now<=Q[i].R)
{
for(int j=; j<G[now].size(); j++)
{
int to=G[now][j];
add(to,-);
add(wLR[to].L,);
}
add(now,);
add(wLR[now].L,-); now++;
}
ans[Q[i].id]=sum(Q[i].R)-sum(Q[i].L-);
}
for(int i=; i<m; i++)
printf("%d\n",ans[i]);
init(n);
}
return ;
}

hdu4777 树状数组的更多相关文章

  1. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  2. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  3. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  5. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

  6. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  7. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  8. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

  9. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

随机推荐

  1. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project demo: Fatal error com piling: 无效的标记: -parameters

    背景:本项目使用JDK1.8 编译maven工程的时候出现如下错误: Failed to execute goal org.apache.maven.plugins:maven-compiler-pl ...

  2. Datagrip连接SQLServer Connecting DataGrip to MS SQL Server

    Connecting DataGrip to MS SQL Server Posted on June 21, 2016 by Maksim Sobolevskiy Some specific nee ...

  3. C# 移除Response Header,403调整返回为404Make IIS return a 404 status code instead of 403

    Server Information Revealed For the benefit of those who land here through a google/bing search:: He ...

  4. oracle 子查询中null的问题(in 和 not in)

    这里的in后面的句子可以理解为or拼接,简单举例即 in (9566,9839,null)可以等价于mgr=9566 or mgr=9839 or mgr=null, not in (9566,983 ...

  5. 查询表DML和DDL操作的最后时间

    查询test表DML操作的最后时间的语句: select max(ora_rowscn),to_char(scn_to_timestamp(max(ora_rowscn)),'yyyy-mm-dd h ...

  6. 数据交互axios的用法

    参考: https://www.cnblogs.com/zhouyangla/p/6753673.html

  7. oracle sqlplus命令详解

    涉及到的知识要点 a.带有一个&的替换变量的用法b.带有两个&的替换变量用法c.define命令用法d.accept命令用法e.定制SQL*Plus环境f.在glogin.sql文件中 ...

  8. Integer 的 valueOf 方法 与 常量池(对 String Pool 的部分理解)

    举例: public class Test { @org.junit.Test public void intTest() { Integer t1 = 128; Integer t2 = 127; ...

  9. python 第三方扩展库的安装

    主要就是采用 easy_install 和pip安装,一定要把这两个东西安装好.http://peak.telecommunity.com/DevCenter/EasyInstall下载ez_setu ...

  10. mui 轮播

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...