Who Gets the Most Candies? Emirp establish and update the tree line

Problem 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. IfA 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
 **************************************************************************************************************************************************************************************************************
Segment tree:
first overall built a tree, then the tree has been selected to update, by deleting from the tree
***************************************************************************************************************************************************************************************************************
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 #define L(rt) (rt<<1)
 8 #define R(rt) (rt<<1|1)
 9 
10 const int maxn=500010;
11 
12 int n,id;
13 
14 struct node{
15     int l,r,sum;    //sum 表 该区间剩余人数
16 }tree[maxn*3];
17 
18 struct{DATE
 . 19      int Val;
 20 is      char name [ 15 ];
 21 is  } Boy [MAXN];
 22 is  
23 is  int ANS [MAXN]; // ANS [i] to save the number of person i candy can get out of 
24  
25  void Build ( int L, int R & lt, int RT) {
 26 is      Tree [RT] .L = L;
 27      Tree [RT] .r = R & lt;
 28      Tree [RT] = R & lt .sum-L + . 1 ;
 29      IF (L == R & lt)
 30          return ;
 31 is      int MID = (L + R & lt) >>1;
32     build(l,mid,L(rt));
33     build(mid+1,r,R(rt));
34 }
35 
36 int update(int key,int rt){
37     tree[rt].sum--;
38     if(tree[rt].l==tree[rt].r)
39         return tree[rt].l;
40     if(tree[L(rt)].sum>=key)
41         update(key,L(rt));
42     else
43         update(key-tree[L(rt)].sum,R(rt));
44 }
45 
46 void Solve(){   //计算ans
47     memset(ans,0,sizeof(ans));
48     for(int i=1;i<=n;i++){
49         ans[i]++;
50         for(int j=2*i;j<=n;j+=i)
51             ans[j]++;
52     }
53     int max1=ans[1];
54     id=1;
55     for(int i=2;i<=n;i++)   // find the sugar of a few people get up out of 
56 is          IF (ANS [I]> MAX1) {
 57 is              MAX1 = ANS [I];
 58              ID = I;
 59          }
 60  }
 61 is  
62 is  int main () {
 63 is  
64      / / The freopen ( "input.txt", "R & lt", stdin); 
65  
66      int I, K, MOD;
 67      the while (~ Scanf ( " % D% D " , & n-, & K)) {
 68          the Solve ();
 69          for (I = . 1 ; I <= n-; I ++ )
 70              Scanf (" % S% D " , Boy [I] .name, & Boy [I] .val);
 71 is          Build ( . 1 , n-, . 1 );
 72          MOD = Tree [ . 1 ] .sum;
 73 is          int POS = 0 ;
 74          Boy [ 0 ] .val = 0 ;
 75          n-= ID;
 76          the while (N-- ) {
 77              IF (Boy [POS] .val> 0 )   // humans remaining table k k-th from the left in the dequeue 
78                  K = ((- K- . 1 + Boy [POS] .val- . 1 )% + MOD MOD) MOD +%1;
79             else
80                 k=((k-1+boy[pos].val)%mod+mod)%mod+1;
81             pos=update(k,1);
82             mod=tree[1].sum;
83         }
84         printf("%s %d\n",boy[pos].name,ans[id]);
85     }
86     return 0;
87 }
View Code

 

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3485847.html

Guess you like

Origin blog.csdn.net/weixin_34080903/article/details/93432862