http://www.lightoj.com/volume_showproblem.php?problem=1097

题意:一个自然数序列,先去掉所有偶数项,在此基础上的序列的第二项为3,则删去所有3的倍数的元素,再是7……重复操作,最后问第n项的值

思路:使用线段树构造序列,对一个数进行标记是否已被删去,和为元素个数。由于样例给出了大小,所以很容易控制空间。

/** @Date    : 2016-12-05-19.34
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/ #include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1429431;
const double eps = 1e-8; struct yuu
{
int l, r;
int sum;
}tt[N * 3]; int ans[100010]; void pushup(int p)
{
tt[p].sum = tt[p << 1].sum + tt[p << 1 | 1].sum;
} void build(int l, int r, int p)
{
tt[p].l = l;
tt[p].r = r;
if(l == r)
{
tt[p].sum = l & 1;
return ;
}
int mid = (l + r) >> 1;
build(l , mid, p << 1);
build(mid + 1, r, p << 1 | 1);
pushup(p); } void updata(int x, int p)
{
if(tt[p].l == tt[p].r)
{
tt[p].sum = 0;
return ;
}
if(x <= tt[p << 1].sum)
updata(x, p << 1);
else
updata(x - tt[p << 1].sum, p << 1 | 1);
pushup(p);
} int query(int len, int p)
{
if(tt[p].l == tt[p].r)
return tt[p].l; if(len <= tt[p << 1].sum)
query(len, p << 1);
else query(len - tt[p << 1].sum, p << 1 | 1); }
int main()
{
int c = 1;
ans[1] = 1;
build(1, 1429431, 1);
for(int i = 2; i <= 1429431; i++)
{
int p = query(i, 1);
for(int j = 0; p + j <= 1429431; j+=p-1)
updata(p+j, 1);
if(c >= 100000)
break;
ans[++c] = query(i, 1);
}
int T;
int cnt = 0;
cin >> T;
while(T--)
{
int n;
cin >> n;
printf("Case %d: %d\n", ++cnt, query(n, 1));
}
return 0;
}

LightOJ 1097 - Lucky Number 线段树的更多相关文章

  1. lightoj 1097 - Lucky Number(线段树)

    Lucky numbers are defined by a variation of the well-known sieve of Eratosthenes. Beginning with the ...

  2. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  3. HDU-1394 Minimum Inversion Number 线段树+逆序对

    仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...

  4. K-th Number 线段树(归并树)+二分查找

    K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第 ...

  5. hdu1394Minimum Inversion Number(线段树,求最小逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  6. hdu1394(Minimum Inversion Number)线段树

    明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...

  7. HDU 1394 Minimum Inversion Number(线段树 或 树状数组)

    题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...

  8. K-th Number 线段树的区间第K大

    http://poj.org/problem?id=2104 由于这题的时间限制不紧,所以用线段树水一水. 每个节点保存的是一个数组. 就是对应区间排好序的数组. 建树的时间复杂度需要nlogn 然后 ...

  9. hdu - 1394 Minimum Inversion Number(线段树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...

随机推荐

  1. Mybatis中resultMap与resultType区别

    MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultM ...

  2. POJ 1639 Picnic Planning(最小度限制生成树)

    Description The Contortion Brothers are a famous set of circus clowns, known worldwide for their inc ...

  3. 硬盘引导扇区、多分区图、不通硬盘的LINUX逻辑分区数量

    主要启动记录区(Master Boot Record,MBR):可以安装开机管理程序的地方,有446byte 分割表(Paritition table):记录整块硬盘分割的状态,有64bytes 下面 ...

  4. C#的垃圾回收

    C#中垃圾回收 GC.Collect();强制进行内存回收.

  5. 使用 Python 操作 Git 版本库 - GitPython

    GitPython 是一个用于操作 Git 版本库的 python 包, 它提供了一系列的对象模型(库 - Repo.树 - Tree.提交 - Commit等) 用于操作版本库中的相应对象. 版本库 ...

  6. c++内存分类

    1. 代码段:放置代码 2. 静态数据段:放置全局变量和static的局部变量,字符串常量 3. 动态数据段:栈,放置局部作用域的变量,离开函数返回后就会被释放:堆,必须手动的分配和释放. 关于字符串 ...

  7. Visual C++中对运行时库的支持

    原文地址:http://blog.csdn.net/wqvbjhc/article/details/6612099 一.什么是C运行时库 1)C运行时库就是 C run-time library,是 ...

  8. Oracle查询字段中有空格的数据

    一.问题说明 最近在给某个用户下的表批量添加注释时,在程序中将注释名用trim()过滤一遍就可以了,但是在程序执行成功后怎么检测添加的注释名是否有空格存在呢? 二.解决方法 1.SELECT * FR ...

  9. [剑指Offer] 51.构建乘积数组

    题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不 ...

  10. UIKit中的几个核心对象的介绍:UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍

    UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...