poj2886线段树(单点修改,区间查询)
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
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线段树(单点修改,区间查询)的更多相关文章
- HDU 1166 敌兵布阵 <线段树 单点修改 区间查询>
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- I Hate It(线段树点修改区间查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others) ...
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...
- Ocean的礼物(线段树单点修改)
题目链接:http://oj.ismdeep.com/contest/Problem?id=1284&pid=0 A: Ocean的礼物 Time Limit: 5 s Memory ...
- NYOJ-568/1012//UVA-12299RMQ with Shifts,线段树单点更新+区间查询
RMQ with Shifts 时间限制:1000 ms | 内存限制:65535 KB 难度:3 -> Link1 <- -> Link2 <- 以上两题题意是一样 ...
- 校内模拟赛T5:连续的“包含”子串长度( nekameleoni?) —— 线段树单点修改,区间查询 + 尺取法合并
nekameleoni 区间查询和修改 给定N,K,M(N个整数序列,范围1~K,M次查询或修改) 如果是修改,则输入三个数,第一个数为1代表修改,第二个数为将N个数中第i个数做修改,第三个数为修改成 ...
- HDU - 1166 敌兵布阵 方法一:(线段树+单点修改,区间查询和) 方法二:利用树状数组
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...
- [线段树]区间修改&区间查询问题
区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...
随机推荐
- CodeForces 696A Lorenzo Von Matterhorn (LCA + map)
方法:求出最近公共祖先,使用map给他们计数,注意深度的求法. 代码如下: #include<iostream> #include<cstdio> #include<ma ...
- POJ - 3062 Borg Maze
题目链接:http://poj.org/problem?id=3026 Svenskt Masterskap我程序员/ Norgesmesterskapet 2001 Description The ...
- OpenCv的Java,C++开发环境配置
1.OpenCV 下载及安装配置 opencv的下载地址:http://opencv.org/downloads.html 最新版本:opencv3.0.0 注意:支持的visual studio20 ...
- CodeForces 190D Non-Secret Cypher
双指针. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- Android Studio相关的坑
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- Chapter 1 First Sight——20
After two classes, I started to recognize several of the faces in each class. 两节课之后,我开始记住了每节课的那几张脸. ...
- json optString getString
optString 和 getString 区别. optString 当接收到的为空时候 不会报错
- 2016大连网络赛 Function
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Probl ...
- WeakSelf和StrongSelf
转载自:http://sherlockyao.com/blog/2015/08/08/weakself-and-strongself-in-blocks/ 现在我们用 Objective-C 写代码时 ...
- apk文件分析原则
如果在dex生成的jar文件里没有发现关键内容的话,就要注意jar里面的native函数以及loadlibrary操作,从而可以判断出加载了哪些so,调用了什么函数.就不会出现判断不出是不是加载了某s ...