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. A SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'Lucene40' does not exist.

    简单的建立索引和查询索引并不难,关键在于他的二次开发,让他适合你自己的需求 既然要二次开发就必须查看源码 首先看看索引过程中的核心类吧: IndexWriter 这个是核心组件, 建立和打开索引,以及 ...

  2. CodeForces 701C They Are Everywhere(map的应用)

    这个题比较好的解决办法,我觉得还是map,map的size可以很快的知道我们选了几个字母,而且可以作为计数器,知道每一个字母出现了多少次, erase函数可以清除掉一个元素. 所以,定义两个指针L和R ...

  3. AS3条件编译

    package { import flash.display.Sprite; public class Main extends Sprite { public function Main() { s ...

  4. If only it could be all the same like we first me

    为什么 你当时对我好 Why? You nice to me at that time. 又为什么 现在变得冷淡了 Why? Now you give a cold shoulder to me. 我 ...

  5. java工程开发之图形化界面之(第三课)

    上面我们讲述了通过JOptionPane进行文本I/O操作,一个是通过JOptionPane来获取的 参考链接:http://blog.sina.com.cn/s/blog_993d254201013 ...

  6. 这丫头也的还真清楚,但是跑不通呢,换3.0.3的mybatis也不行

    http://java.dzone.com/articles/ibatis-mybatis-handling-joins http://mybatis.github.io/spring/mappers ...

  7. 过滤字符串html标签方法

    过滤字符串html标签方法,如果输入的过滤标签为“*”,那么给字符串加上p标签 public static string noTagHtml(string str, string tagname) { ...

  8. FusionCharts导出图表常见问题(FAQ)汇总---FusionCharts常见问题大全

    在前面几篇文章中,我们介绍了FusionCharts生成Flash图表常见问题FAQ以及使用中的一些常见报错及调试/解决方法.本文继续介绍FusionCharts导出图表时的一些常见问题(FAQ). ...

  9. 1025:To the max(DP)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  10. FZU 2122 又见LKity(KMP+返回所有匹配位置)

    基础kmp应用,找到所有匹配位置即可 #include<stdio.h> #include<string.h> #include<algorithm> #inclu ...