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的任务就是要监视这些工兵营地的活动情况.由于 ...
- [线段树]区间修改&区间查询问题
区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...
随机推荐
- vbs 截图
'VBS截屏.vbs ' Win7x64 测试通过(已安装Word2007): '参考: ' http://qtp.blogspot.com/2010/02/screenshot-vbscript ...
- linux shell: 取得某个目录下的文件名列表
取得某个目录下文件名的列表(没有子目录) ls -l dir/ | awk '{print "dir\\" $9}' #其中$9是ls -l 的第9个字段-文件名
- 去除移动端点击事件出现的背景框 tap-highlight-color
-webkit-tap-highlight-color 这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就会出现一个半透明的灰色 ...
- 使用float和display:block将内联元素转换成块元素的不同点
使用float和display:block将内联元素转换成块元素的不同点 使用float和display:block将内联元素转换成块元素的不同点:内联元素可以转换成块级元素,常用的方法比如可以为内联 ...
- angularJs中ngModel的坑
angular中指令被用的最多的除了ng-repeat之外就非他莫属了吧,由于各种业务的需求,我们经常需要去写一些继承ngModel的指令,来满足各种奇葩需求,最多的莫过于表单的验证啦,另外你在封装一 ...
- 设计模式--状态模式(分布式中间件熔断器Java实现)
最近在做分布式服务熔断,因为要实现一个熔断器状态机,所以想到状态模式.状态模式是当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类.状态模式主要解决的是当控制一个对象状态的条件表达 ...
- 快速搭建本地HTTP服务器
我们都知道,可以通过浏览器直接打开HTML文件来查看可视化的效果,但这种方式有一些局限性.比如我们在做手机端适配的时候,无法直接查看手机端的效果等.为了不受此类限制,强烈建议搭建一个本地的HTTP服务 ...
- 使用PowerDesigner画ER图详细教程
转:http://www.360doc.com/content/11/0624/15/2617151_129276457.shtml 一.概念数据模型概述数据模型是现实世界中数据特征的抽象.数据模型应 ...
- DRAM Memory Rank知识
DRAM的一些知识点,先记录下来再进行整理 1.何为Memory rank? A memory rank is a set of DRAM chips connected to the same ch ...
- SVN MERGE 和冲突
摘要:最佳做法是避免冲突.冲突时,不要把branch merge到trunk. 先由最新版本的trunk得到branch,然后再修改文件,直接merge过去就行.这样不会有冲突.先用svn merge ...