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$的商品 现在从第一天 ...
随机推荐
- VC绘制控件如何防止闪烁
理论上不管什么控件都适用,方法如下: 新建一个MFC类,继承原来的控件类型,对于VC自动生成的类进行如下改动: 首先覆盖 OnEraseBkgnd() 防止擦除时填涂背景: BOOL CXXXCtrl ...
- WCF服务的IIS托管(应用程序)
基本思路 建立与发布参考网站托管 在IIS中某一网站,选择添加应用程序 访问服务uri:http://localhost/wcfAppTest/Service1.svcwcfAppTest/Ser ...
- sql 从未连续的Id中选择10~30条数据
select * from(select *,row_number()over(order by ProductID) as num from Products) as t where t.num&g ...
- WPF中INotifyPropertyChanged用法与数据绑定
在WPF中进行数据绑定的时候常常会用到INotifyPropertyChanged接口来进行实现,下面来看一个INotifyPropertyChanged的案例. 下面定义一个Person类: usi ...
- TinyMCE插件CodeSample前后端应用
后端使用插件CodeSample方法: <html> <textarea id="editor_id" name="content" plac ...
- Mysql数据库表的自增主键ID号乱了,需要重新排列。
Mysql数据库表的自增主键ID号乱了,需要重新排列. 原理:删除原有的自增ID,重新建立新的自增ID. 1,删除原有主键:ALTER TABLE `table_name` DROP `id`; 2, ...
- Codejock.Xtreme.Toolkit.Pro.v15.3.1 下载 与 VS2015补丁使用方法
Codejock.Xtreme.Toolkit.Pro.v15.3.1 下载 与 VS2015补丁使用方法 打算放在CSDN进行下载的,上传完成后发现资源分设置的1分,本打算赚点下载分的.在页面上没有 ...
- 用JavaScriptSerializer解析JSON
引用System.Web.Extensions using System.Web.Script.Serialization; var serializer = new JavaScriptSerial ...
- [Erlang-0016][aque_tcp] 一个 Erlang TCP 组件
项目地址:https://github.com/liangjingyang/aque_tcp 欢迎任何形式的转载,但请务必注明出处:http://www.cnblogs.com/liangjingya ...
- coci2018 题解
plahte 给定一些矩形和一些有颜色的点,求每个矩形上有多少种颜色的点,保证矩形只有包含和不相交两种关系,规模 \(10^5\). 把每个矩形看成一个点,用扫描线建出森林,同时也顺便处理点. 然后做 ...