题目链接:http://codeforces.com/contest/755/problem/D

题意:一个n边形,从1号点开始,每次走到x+k的位置如果x+k>n则到x+k-n的位置,问每次留下来的路径把这个多边形划分成了几个部分。

很明显只要求x到x+k位置之间的点有几个入度就行了,而且只要求小区间内除去两点剩下的点的入度即可。

不需要考虑x或x+k点到该点的入度因为更本不可能从x或x+k到该点。

所以可以将k值稍微优化一下全都统一到k<n/2,这不影响结果因为假设5个点1到3,k=2。5个点1到4,k=3。但是点的位置可以任意调的,

只要是四边形就行所以都是一样的。

这样就方便用线段树或者用树状数组单点更改与区间求和。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;
const int M = 1e6 + 10;
struct TnT {
int l , r , sum;
}T[M << 2];
void build(int l , int r , int p) {
int mid = (l + r) >> 1;
T[p].l = l , T[p].r = r , T[p].sum = 0;
if(l == r) {
return ;
}
build(l , mid , p << 1);
build(mid + 1 , r , (p << 1) | 1);
}
void updata(int p , int pos) {
int mid = (T[p].l + T[p].r) >> 1;
if(T[p].l == T[p].r && T[p].l == pos) {
T[p].sum++;
return ;
}
if(mid < pos) {
updata((p << 1) | 1 , pos);
}
else {
updata(p << 1 , pos);
}
T[p].sum = T[p << 1].sum + T[(p << 1) | 1].sum;
}
int query(int l , int r , int p) {
int mid = (T[p].l + T[p].r) >> 1;
if(T[p].l == l && T[p].r == r) {
return T[p].sum;
}
if(mid < l) {
return query(l , r , (p << 1) | 1);
}
else if(mid >= r) {
return query(l , r , p << 1);
}
else {
return query(l , mid , p << 1) + query(mid + 1 , r , (p << 1) | 1);
}
}
int main() {
int n , k , sta = 1 , end;
cin >> n >> k;
build(1 , n , 1);
ll ans = 1;
if(k > (n/2))
k = n - k;
for(int i = 1 ; i <= n ; i++) {
end = sta + k;
if(end > n) {
end -= n;
}
if(sta < end) {
ans = ans + 1 + query(min(sta , end) , max(sta , end) , 1) - query(sta , sta , 1) - query(end , end , 1);
}
else {
ans = ans + 1 + query(1 , n , 1) - query(end , sta , 1);
}
updata(1 , sta);
updata(1 , end);
cout << ans << ' ';
sta = end;
}
return 0;
}

codeforces 755D. PolandBall and Polygon(线段树+思维)的更多相关文章

  1. codeforces 755D. PolandBall and Polygon

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

  2. codeforces 626 G. Raffles(线段树+思维+贪心)

    题目链接:http://codeforces.com/contest/626/problem/G 题解:这题很明显买彩票肯定要买贡献最大的也就是说买p[i]*(num[i]+1)/(num[i]+a[ ...

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

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

  4. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  5. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  6. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

  7. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  8. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  9. Codeforces 444C DZY Loves Colors(线段树)

    题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...

随机推荐

  1. coffeescript 函数 箭头表达式

    函数 do可以形成闭包,使方法作用域不受外部变化的影响. 隐式返回最后一个表达式的值 函数调用省略括号 用arguments数组访问传递给函数的所有对象(低可读性) @name为this.name的简 ...

  2. Another option to bootup evidence files

    When it comes to booting up evidence files acquired from target disk, you got two options. One is VF ...

  3. Altium Designer16绘制51单片机的一些经验总结

    制作这块51单片机的还是蛮艰辛的,应该是我水平太差,现在这块51板已经稳定了,也把这块板子制作过程中的一些问题及经验总结记录下来.这块板子制作出了很大问题很大原因是因为我对Altium Designe ...

  4. MyBatis 二级缓存全详解

    目录 MyBatis 二级缓存介绍 二级缓存开启条件 探究二级缓存 二级缓存失效的条件 第一次SqlSession 未提交 更新对二级缓存影响 探究多表操作对二级缓存的影响 二级缓存源码解析 二级缓存 ...

  5. 天气预报APP(2)

    之前实现了能够罗列可以罗列出全国所有的省.市.县,然后就是查询全国任意城市的天气信息.查询天气信息使用的是和风天气的api,这个api获得的天气信息是JSON格式的. 使用GSON库解析JSON数据的 ...

  6. python环境的安装 环境变量和系统变量

    一.python 的安装 python 2.7 和 python 3.6的安装(一路点点点就行) 在安装的时候注意一下红框的内容,意思代表将其添加到环境变量中 环境变量是在操作系统中一个具有特定名字的 ...

  7. 记一次mysql主从同步因断电产生的不能同步问题 1236 1032

    背景: 项目新上线一个月,qa需要测试断电服务拉起,服务拉起成功后,发现mysql主从异常,以下是发现的问题以及解决方案 问题1: Slave_IO_Running:  No 一方面原因是因为网络通信 ...

  8. HTML文件上传与下载

    文件下载 传统的文件下载有两种方法: 使用<a/>标签,href属性直接连接到服务器的文件路径 window.location.href="url" 这两种方法效果一样 ...

  9. pycharm的补充

    pycharm 快捷键 tab自动补全 首行缩进 ctrl+?是全行加#进行注释 ctrl+d 复制上一行 ctrl +z 撤销 ctrl+shift+z 撤销的撤销 更改字体大小

  10. 使用sc 命令写脚本 添加和删除服务 简单应用

    添加服务 @echo.服务启动...... @echo off @sc create 服务名 binPath= "%~dp0\服务路径" @sc config 服务名 start= ...