hdu4777 树状数组
题意:给了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 树状数组的更多相关文章
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- bzoj1878--离线+树状数组
这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]
3529: [Sdoi2014]数表 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1399 Solved: 694[Submit][Status] ...
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
- 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组
E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
- 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序
3881: [Coci2015]Divljak Time Limit: 20 Sec Memory Limit: 768 MBSubmit: 508 Solved: 158[Submit][Sta ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
随机推荐
- Direct3D 11 Tutorial 6:Lighting_Direct3D 11 教程6:灯光
概述 在之前的教程中,世界看起来很无聊,因为所有对象都以相同的方式点亮. 本教程将介绍简单照明的概念及其应用方法. 使用的技术将是朗伯照明. 本教程的结果将修改前面的示例以包含光源. 该光源将附在轨道 ...
- js 对象转&拼接
function pars(param, key, encode) { if (param == null) return ''; var arr = []; var t = typeof (para ...
- 阻止a链接跳转方法总结
总结下a标签阻止默认行为的几种简单方法(1) <a href="javascript:void(0);" > 点我 </a> onclick方法负责执行js ...
- TIJ -- CountDownLatch
1. 2. Class : CountDownLatchDemo package lime.thinkingInJava._021._007._001; import java.sql.Time; i ...
- 转:spring配置文件中xsd引用问题
来自:http://blog.csdn.net/dingqinghu/article/details/46758671
- Day8 函数指针做函数参数
课堂笔记 课程回顾 多态 virtual关键字 纯虚函数 virtual func() = 0; 提前布局vptr指针 面向接口编程 延迟绑定 多态的析构函数的虚函数. ...
- Elasticsearch 学习之子聚集过滤
. 获取子聚合结果总数 { , "aggs": { "msisdnAgg": { "terms": { "field": ...
- MySQL 在高并发下的 订单撮合 系统使用 共享锁 与 排他锁 保证数据一致性
作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...
- E - Heavy Transportation
来源poj1797 Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now ...
- [LeetCode] Clone Graph 克隆无向图
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...