poj2886线段树(单点修改,区间查询)

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

像是约瑟夫问题,用线段树表示区间中有多少可用
 1 #include <stdio.h>
 2 char name[500005][10];
 3 int ex[500005], sum[2000005];
 4 int prim[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
 5 void build(int o, int l, int r) {
 6     if (l == r) {
 7         sum[o] = 1;
 8         return;
 9     }
10     int mid = l + r >> 1;
11     build(o << 1, l, mid);
12     build(o << 1 | 1, mid + 1, r);
13     sum[o] = sum[o << 1] + sum[o << 1 | 1];
14 }
15 int update(int o, int l, int r, int pos) {
16     int ans(0);
17     if (l == r) {
18         sum[o] -= 1;
19         ans = l;
20         return ans;
21     }
22     int mid = l + r >> 1;
23     if (sum[o << 1] >= pos) ans = update(o << 1, l, mid, pos);
24     else ans = update(o << 1 | 1, mid + 1, r, pos - sum[o << 1]);
25     sum[o] = sum[o << 1] + sum[o << 1 | 1];
26     return ans;
27 }
28 int query(int o, int l, int r, int ql, int qr) {
29     int ans(0);
30     if (ql <= l && r <= qr) {
31         return sum[o];
32     }
33     int mid = l + r >> 1;
34     if (ql <= mid) ans += query(o << 1, l, mid, ql, qr);
35     if (qr > mid) ans += query(o << 1 | 1, mid + 1, r, ql, qr);
36     return ans;
37 }
38 int solve(int x) {
39     int sum = 1;
40     for (int i = 0; i < 25; i++) {
41         int ans = 0;
42         while (x % prim[i] == 0) {
43                 ans++;
44                 x /= prim[i];
45         }
46         sum *= ans + 1;
47     }
48     return sum;
49 }
50 int main() {
51     int n, k, ans, key;
52     while (~scanf("%d%d", &n, &k)) {
53         for (int i = 1; i <= n; i++) scanf("%s %d", name[i], &ex[i]);
54         int m = n;
55         build(1, 1, n);
56         int pos = update(1, 1, n, k);
57         ans = solve(1); key = 1;
58         m--;
59         for (int i = 2; i <= n; i++) {
60             int id = ((query(1, 1, n, 1, (pos - 1 == 0 ? 1 : pos - 1)) + (ex[pos] < 0 ? ex[pos] + 1 : ex[pos])) % m + m) % m;
61             if (!id) id = m;
62             m--;
63             pos = update(1, 1, n, id);
64             int p = solve(i);
65             if (p > ans) {
66                 key = pos;
67                 ans = p;
68             }
69         }
70         printf("%s %d
", name[key], ans);
71     }
72     return 0;
73 }

原文地址:https://www.cnblogs.com/y7070/p/4750540.html