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. dumpbin判断windows程序是32还是64位(包括DLL)

    http://blog.csdn.net/csfreebird/article/details/10105681 dumpbin /HEADERS gdal18.dll(or xxx.exe) 如果安 ...

  2. WPF无边框实现拖动效果

    这是在做弹幕的时候遇到的一个需求 透明背景,拖动弹幕=.= private void Window_MouseLeftButtonDown(object sender, MouseButtonEven ...

  3. symfony 数据库表生成实体、迁移数据库

    从数据库表生成实体 1. 由数据库生成模型: php bin/console doctrine:mapping:convert --from-database yml D:\db\ D:\test_b ...

  4. 【C#】VS2012+InstallShield2013制作软件更新包

    原文:[C#]VS2012+InstallShield2013制作软件更新包 上篇文章介绍了如何使用installshield制作软件的安装包,见地址:http://blog.csdn.net/cat ...

  5. Lambda表达式的参数捕获

    以常用的Action委托为例: 有如下3个无参数的方法: public void Function() { //Do something } public void Function2() { //D ...

  6. 那些证书相关的玩意儿(SSL,X.509,PEM,DER,CRT,CER,KEY,CSR,P12等)(使用OpenSSL的命令行)

    之前没接触过证书加密的话,对证书相关的这些概念真是感觉挺棘手的,因为一下子来了一大堆新名词,看起来像是另一个领域的东西,而不是我们所熟悉的编程领域的那些东西,起码我个人感觉如此,且很长时间都没怎么搞懂 ...

  7. 8086 CPU 寄存器简介(超详细,图文并茂)

    http://www.cnblogs.com/BoyXiao/archive/2010/11/20/1882716.html

  8. c#利用IronPython调用python的过程种种问题

    c#利用IronPython调用python的过程种种问题 小菜鸟一枚,最新学习了Python,感觉语言各种简短,各种第三方类库爽歪歪,毕竟之前是从c#转来的,看到Python的request类各种爽 ...

  9. C#读取数据库内容并转换成xml文件

    OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\bi ...

  10. Linux下的软件安装

    在线安装 APT:advanced packaging Tool,Debian及其派生的发行版的软件包管理工具,包含以apt-开头的多个工具,如apt-get,apt-cache,apt-cdrom ...