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$的商品 现在从第一天 ...
随机推荐
- WPF GridSplitter最好设置HorizontalAlignment和VerticalAlignment,否则不可以左右移动
<Window x:Class="XamlTest.Window5" xmlns="http://schemas.microsoft.com/winf ...
- MYSQL 定时自动执行EVENT
MySQL从5.1开始支持EVENT功能,类似Oracle和MSSQL的定时任务job功能.有了这个功能之后我们就可以让MySQL自动的执行存储过程来实现数据汇总等功能了,不用像以前哪样手动操作完成了 ...
- 汇编实现获取CPU信息
这是文章最后一次更新,加入了TLB与Cache信息等资料前言:论坛上面有人不明白CPUID指令的用法,于是就萌生写这篇文章的想法,若有错误话请大侠指出,谢谢了 ^^论坛的式样貌似有问题,若式样问题导致 ...
- liunx 详细常用操作
图片来自: http://www.cnblogs.com/zhangsf/archive/2013/06/13/3134409.html 公司新员工学习有用到,Vim官网的手册又太大而全,而网上各方资 ...
- Win8 Metro(C#)数字图像处理--2.55OSTU法图像二值化
原文:Win8 Metro(C#)数字图像处理--2.55OSTU法图像二值化 [函数名称] Ostu法图像二值化 WriteableBitmap OstuThSegment(Writ ...
- C/C++网络编程时注意的问题小结
1.网络编程在自己定义结构体实现协议的时候,一定要注意字节对齐这个问题.否则sizeof和强制转换指针的时候都会出现很难发现的bug. 什么是字节对齐自行百度. #pragma pack (1)//字 ...
- iText类库再pdf中显示中文字体
using iTextSharp.text; using System; using System.Collections.Generic; using System.IO; using System ...
- Visual Studio 2017报表RDLC设计器与工具箱中Report Viewer问题
原文:VS2017入门 RDLC入门之01 本系列所有内容为网络收集转载,版权为原作者所有. VS2017初始安装后和VS2015一样,都没有ReportDesigner/ReportViewer R ...
- storm(一)
Storm 一个用来实时计算的流框架,具有高可用,低延迟,数据不丢失,分布式的特点 storm 处理数据的方式是基于消息的流水线处理,因此特别适合无状态的计算,也就是说计算单元依赖的数据全部在接受的消 ...
- 安卓环境下,通过QT调用jar包
在安卓上,许多第三方工具都提供jar包.qt可以通过jni来调用jar包.本文通过一个例子,说明安卓上QT调用jar的方式. 工具/原料 qt android jar包 jar包准备 1 ja ...