Who Gets the Most Candies?
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 11955   Accepted: 3734
Case Time Limit: 2000MS

Description

N children are sitting in a circle to play a game.

The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from the K-th child, who tells all the others the integer on his card and jumps out of the circle. The integer on his card tells the next child to jump out. Let A denote the integer. If A is positive, the next child will be the A-th child to the left. If A is negative, the next child will be the (−A)-th child to the right.

The game lasts until all children have jumped out of the circle. During the game, the p-th child jumping out will get F(p) candies where F(p) is the number of positive integers that perfectly divide p. Who gets the most candies?

Input

There are several test cases in the input. Each test case starts with two integers N (0 < N ≤ 500,000) and K (1 ≤ K ≤ N) on the first line. The next N lines contains the names of the children (consisting of at most 10 letters) and the integers (non-zero with magnitudes within 108) on their cards in increasing order of the children’s numbers, a name and an integer separated by a single space in a line with no leading or trailing spaces.

Output

Output one line for each test case containing the name of the luckiest child and the number of candies he/she gets. If ties occur, always choose the child who jumps out of the circle first.

Sample Input

4 2
Tom 2
Jack 4
Mary -1
Sam 1

Sample Output

Sam 3

像是约瑟夫问题,用线段树表示区间中有多少可用
 #include <stdio.h>
char name[][];
int ex[], sum[];
int prim[] = {, , , , , , , , , , , , , , , , , , , , , , , , };
void build(int o, int l, int r) {
if (l == r) {
sum[o] = ;
return;
}
int mid = l + r >> ;
build(o << , l, mid);
build(o << | , mid + , r);
sum[o] = sum[o << ] + sum[o << | ];
}
int update(int o, int l, int r, int pos) {
int ans();
if (l == r) {
sum[o] -= ;
ans = l;
return ans;
}
int mid = l + r >> ;
if (sum[o << ] >= pos) ans = update(o << , l, mid, pos);
else ans = update(o << | , mid + , r, pos - sum[o << ]);
sum[o] = sum[o << ] + sum[o << | ];
return ans;
}
int query(int o, int l, int r, int ql, int qr) {
int ans();
if (ql <= l && r <= qr) {
return sum[o];
}
int mid = l + r >> ;
if (ql <= mid) ans += query(o << , l, mid, ql, qr);
if (qr > mid) ans += query(o << | , mid + , r, ql, qr);
return ans;
}
int solve(int x) {
int sum = ;
for (int i = ; i < ; i++) {
int ans = ;
while (x % prim[i] == ) {
ans++;
x /= prim[i];
}
sum *= ans + ;
}
return sum;
}
int main() {
int n, k, ans, key;
while (~scanf("%d%d", &n, &k)) {
for (int i = ; i <= n; i++) scanf("%s %d", name[i], &ex[i]);
int m = n;
build(, , n);
int pos = update(, , n, k);
ans = solve(); key = ;
m--;
for (int i = ; i <= n; i++) {
int id = ((query(, , n, , (pos - == ? : pos - )) + (ex[pos] < ? ex[pos] + : ex[pos])) % m + m) % m;
if (!id) id = m;
m--;
pos = update(, , n, id);
int p = solve(i);
if (p > ans) {
key = pos;
ans = p;
}
}
printf("%s %d\n", name[key], ans);
}
return ;
}
												

poj2886线段树(单点修改,区间查询)的更多相关文章

  1. HDU 1166 敌兵布阵 <线段树 单点修改 区间查询>

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  2. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  3. I Hate It(线段树点修改区间查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others) ...

  4. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

  5. Ocean的礼物(线段树单点修改)

    题目链接:http://oj.ismdeep.com/contest/Problem?id=1284&pid=0 A: Ocean的礼物 Time Limit: 5 s      Memory ...

  6. NYOJ-568/1012//UVA-12299RMQ with Shifts,线段树单点更新+区间查询

    RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 ->  Link1  <- -> Link2  <- 以上两题题意是一样 ...

  7. 校内模拟赛T5:连续的“包含”子串长度( nekameleoni?) —— 线段树单点修改,区间查询 + 尺取法合并

    nekameleoni 区间查询和修改 给定N,K,M(N个整数序列,范围1~K,M次查询或修改) 如果是修改,则输入三个数,第一个数为1代表修改,第二个数为将N个数中第i个数做修改,第三个数为修改成 ...

  8. HDU - 1166 敌兵布阵 方法一:(线段树+单点修改,区间查询和) 方法二:利用树状数组

    C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...

  9. [线段树]区间修改&区间查询问题

    区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...

随机推荐

  1. Lucene入门教程

    Lucene教程 1 lucene简介 1.1 什么是lucene     Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么 ...

  2. ssh能够连接而sftp不能连接的解决方法

    ssh能够连接而sftp不能连接的解决方法   昨天开始用FileZilla一直不能登录远程的服务器,ssh的登录就OK,因为是服务器,也不敢乱动.查了好多资料终于解决了. 首先,查看一下系统的安全日 ...

  3. Chapter 1 First Sight——19

    "I'm headed toward building four, I could show you the way…" Definitely over-helpful. &quo ...

  4. mysql date range

    http://stackoverflow.com/questions/9935690/mysql-datetime-range-query-issue " ";

  5. margin:0 auto在ie7浏览器里面无效

    把文件头改成 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w ...

  6. 任意2个io直接驱动LCD1602,并且不需外加芯片(转)

    http://www.amobbs.com/thread-4301955-1-1.html *此处只摘录部分内容,详细内容请关注原贴. 这就是电路,细心的朋友会发现实物图中有几个贴片的阻容件,秘密就在 ...

  7. css居中

    <html><head lang="en"> <meta charset="UTF-8"> <title>< ...

  8. QWidget QMainWindow QDialog 三个基类的区别

    Qt类是一个提供所需的像全局变量一样的大量不同的标识符的命名空间.通常情况下,你可以忽略这个类.QObject和一些其它类继承了它,所以在这个Qt命名空间中定义的所有标识符通常情况下都可以无限制的使用 ...

  9. 2304: Lights Out(枚举)

    枚举第一行所有可能的的情况 #include<iostream> #include<cstdio> #include<cstring> #include<al ...

  10. 多元线性回归----Java简单实现

    http://www.cnblogs.com/wzm-xu/p/4062266.html 多元线性回归----Java简单实现   学习Andrew N.g的机器学习课程之后的简单实现. 课程地址:h ...