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(思维+线段树)的更多相关文章

  1. codeforces 755D. PolandBall and Polygon(线段树+思维)

    题目链接:http://codeforces.com/contest/755/problem/D 题意:一个n边形,从1号点开始,每次走到x+k的位置如果x+k>n则到x+k-n的位置,问每次留 ...

  2. codeforces 755D. PolandBall and Polygon

    D. PolandBall and Polygon time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  3. CodeForces 755D PolandBall and Polygon ——(xjbg)

    每次连线,起点和终点之间,每一个被点亮的点,这些点都能连出去两条线,因此可以增加的块数+2(1这个点除外,因为只有连出的点没有连进的点),计算起点和终点之间有几个点被点亮即可,然后1这个点特判一下.感 ...

  4. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  5. Buy Tickets POJ - 2828 思维+线段树

    Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ...

  6. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  7. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  8. 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, ...

  9. codeforces 1017C - Cloud Computing 权值线段树 差分 贪心

    https://codeforces.com/problemset/problem/1070/C 题意: 有很多活动,每个活动可以在天数为$[l,r]$时,提供$C$个价格为$P$的商品 现在从第一天 ...

随机推荐

  1. mac 安装使用Liteide

    1.下载go 官网下载地址(需要FQ)golang 中国镜像网站下载golangtc 下载go1.5.1.darwin-amd64.tar.gz包,解压得到一个Go目录,把go目录移动到你想要的路径下 ...

  2. 关于Hibernate中hbm2java和hbm2ddl工具

    hbm2java:根据映射文件自动生成java源文件 hbm2ddl:根据映射文件自动生成数据库的schema XDoclet:根据带有XDoclet标记的java源文件生成映射文件 Middlege ...

  3. XF 通过判断平台加载不同的图片

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. C#调用Resources.resx资源文件中的资源

    使用到了.NET中的资源文件,也就是Resources.resx,于是就学会了如何调用资源文件中的资源.首先,资源文件可以从项目属性中的资源标签添加.比如,我添加一个图片,叫做aaa.png,添加入资 ...

  5. WPF自定义窗口最大化显示任务栏

    原文:WPF自定义窗口最大化显示任务栏 当我们要自定义WPF窗口样式时,通常是采用设计窗口的属性 WindowStyle="None" ,然后为窗口自定义放大,缩小,关闭按钮的样式 ...

  6. swift 如何控制view的显示与隐藏

    swift 如何控制view的显示与隐藏 UIView有一个属性 hidden let line: UILabel = UILabel() 默认是显示的 需要显示它的时候:line.hidden = ...

  7. 浅谈Android高通(Qualcomm)和联发科(MTK)平台

    一款CPU好不好是要从多个方面考虑的,并不是说简简单单看一个主频.几个核心数就完了,更重要的是它的综合实力到底有多强,这里面当然也会牵扯到价格问题,性能相似当然是便宜的获胜,这是毋庸置疑的. 事实上, ...

  8. android Choose library dependency 搜索不到目标库

    问题:Choose library dependency 搜索不到目标库,百度了一下,发现尽是废话,无解,反正就是升级ide,我是 android studio是2.3.3(网上说升级到3.+就好了, ...

  9. Android 开发中,as或者idea对gradle的使用

    原文:Android 开发中,as或者idea对gradle的使用 本文属于转载收藏,侵删,出处:私人博客 ---------------------------------------------- ...

  10. Decision Tree

    Decision Tree builds classification or regression models in the form of a tree structure. It break d ...