Codeforces 755D:PolandBall and Polygon(思维+线段树)
http://codeforces.com/problemset/problem/755/D
题意:给出一个n正多边形,还有k,一开始从1出发,向第 1 + k 个点连一条边,然后以此类推,直到走完 n 次。对于每一步都要输出当前多边形中有多少个多边形。
思路:画了几幅图后发现规律:两个点相连,这条边多产生出来的多边形的数目等于跨越的边的数目+1,跨越边的数目是 x 和 x + k 中已经走过的点的数目。那么每次走一步,就求这段区间里面已经走过的点的数目,点的数目+1就是答案,区间查询单点更新,我用了线段树来维护。但是要注意 k > n / 2 的情况。因为 k > n / 2 的话,其实这时的 k 和 n - k 是一样的,就好比 n = 5, k = 2or3 的情况是一样的。因为这个情况WA了两次。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
#define N 1000010
#define INF 0x3f3f3f3f
#define lson rt<<1, l, m
#define rson rt<<1|1, m+1, r
LL tree[N<<];
vector<LL> ans; void pushup(int rt) { tree[rt] = tree[rt<<] + tree[rt<<|]; } void update(int rt, int l, int r, int id) {
if(l == r && l == id) {
tree[rt]++;
return ;
}
int m = (l + r) >> ;
if(id <= m) update(lson, id);
else update(rson, id);
pushup(rt);
} LL query(int rt, int l, int r, int L, int R) {
LL ans = ;
if(L <= l && r <= R) return tree[rt];
int m = (l + r) >> ;
if(L <= m) ans += query(lson, L, R);
if(m < R) ans += query(rson, L, R);
return ans;
} LL getsum(int l, int r, int n) {
LL ans = ;
if(l > r) {
ans += query(, , n, l, n);
ans += query(, , n, , r);
} else {
ans += query(, , n, l, r);
}
return ans;
} int main()
{
int n, k;
cin >> n >> k;
int now = + k, pre;
if(k > n / ) k = n - k;
LL res = ;
for(int i = ; i <= n; i++) {
pre = now; update(, , n, pre);
now += k; if(now > n) now -= n;
res += getsum(pre, now, n) - ;
ans.push_back(res);
update(, , n, now);
}
for(int i = ; i < n - ; i++) printf("%I64d ", ans[i] + );
printf("%I64d\n", ans[n-]);
return ;
}
Codeforces 755D:PolandBall and Polygon(思维+线段树)的更多相关文章
- codeforces 755D. PolandBall and Polygon(线段树+思维)
题目链接:http://codeforces.com/contest/755/problem/D 题意:一个n边形,从1号点开始,每次走到x+k的位置如果x+k>n则到x+k-n的位置,问每次留 ...
- codeforces 755D. PolandBall and Polygon
D. PolandBall and Polygon time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- CodeForces 755D PolandBall and Polygon ——(xjbg)
每次连线,起点和终点之间,每一个被点亮的点,这些点都能连出去两条线,因此可以增加的块数+2(1这个点除外,因为只有连出的点没有连进的点),计算起点和终点之间有几个点被点亮即可,然后1这个点特判一下.感 ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- Buy Tickets POJ - 2828 思维+线段树
Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- codeforces 459 D. Pashmak and Parmida's problem(思维+线段树)
题目链接:http://codeforces.com/contest/459/problem/D 题意:给出数组a,定义f(l,r,x)为a[]的下标l到r之间,等于x的元素数.i和j符合f(1,i, ...
- codeforces 1017C - Cloud Computing 权值线段树 差分 贪心
https://codeforces.com/problemset/problem/1070/C 题意: 有很多活动,每个活动可以在天数为$[l,r]$时,提供$C$个价格为$P$的商品 现在从第一天 ...
随机推荐
- MSRA专访摘要
前段时间有幸参加微软亚洲研究院之旅,顺便投简历,没想到在两次访谈迎来,并且是连续 的两次被拒绝.严重的刺激到了我.导致我疯狂的复习刷Offer.如今最终算是告于段落.如今也最终有空沉下心来总结 总结近 ...
- 专访Jeffrey Richter:Windows 8是微软的重中之重
Jeffrey Richter 以其多本 Windows 核心技术的经典著作而闻名,同时,他深入掌握微软的 .NET 等一系列核心技术,他所创办的 Wintellect 公司与微软有密切的合作关系,他 ...
- layerui
引用layer.js,官网:http://layer.layui.com/常用属性:btn/icon/skin/time/content/yes(点击确认.提交) 常用窗体.alert layer.a ...
- c# 编写REST的WCF
REST(Representational State Transfer)即 表述性状态传递 ,简称REST,通俗来讲就是:资源在网络中以某种表现形式进行状态转移. RESTful是一种软件架构风格. ...
- 一个字体,大小,颜色可定义的自绘静态框控件-XColorStatic 类(比较好看,一共19篇自绘文章)
翻译来源:https://www.codeproject.com/Articles/5242/XColorStatic-a-colorizing-static-control XColor Stati ...
- FTPHelper
转载自 :https://blog.csdn.net/jiankunking/article/details/50016043 using System; using System.Collectio ...
- Application.StartupPath和System.Environment.CurrentDirectory的区别
System.Environment.CurrentDirectory的含义是获取或设置当前工作路径,而Application.StartupPath是获取程序启动路径,表面上看二者没什么区别,但实际 ...
- sklearn中LinearRegression使用及源码解读
sklearn中的LinearRegression 函数原型:class sklearn.linear_model.LinearRegression(fit_intercept=True,normal ...
- QImage的浅拷贝与深拷贝
首先简单说说什么是浅拷贝和深拷贝:浅拷贝就比如像引用类型,而深拷贝就比如值类型,即浅拷贝是共用一块内存的,而深拷贝是复制一份内容. 我们再来看看QImage类的几个构造函数: // 浅拷贝 QI ...
- qt+opencv对两幅图片进行融合
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 源代码: #include "widget.h" #include "ui ...