题目链接: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. Task CancellationTokenSource和Task.WhenAll的应用

    Task是.net4.0推出的异步编程类,与ThreadPool.QueneUserWorkItem方法类似的是,Task也是使用线程池来工作的.但Task比起这个QueneUserWorkItem的 ...

  2. JAVA基础知识(三):input.nextLine() 和input.next()

    next()方法在读取内容时,会过滤掉有效字符前面的无效字符,对输入有效字符之前遇到的空格键.Tab键或Enter键等结束符,next()方法会自动将其过滤掉:只有在读取到有效字符之后,next()方 ...

  3. Vue系列:Vue Router 路由梳理

    Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的.基于组件的路由配置 路由参数. ...

  4. mysql优化---订单查询优化(1):视图优化+索引创建

    订单的表结构采用了垂直分表的策略,将订单相关的不同模块的字段维护在不同表中 在订单处理这个页面,需要查询各种维度, 因此为了方便查询创建了v_sale_order视图(老版本) drop view v ...

  5. [原创实践]RedHat Enterprise Linux 5 安装GCC和redis

    Redis的安装需要使用GCC,Red Hat Enterprise 5默认是不安装gcc的,需要自己手动安装. 1:查看系统中是否有gcc gcc -v 查看本机linux版本 lsb_releas ...

  6. 心里想的VS嘴上说的

    心里想的VS嘴上说的 背景:昨天开会,在招行总行那边,今天检讨下自己不会说话,真是太难了我! 一.昨日重现 现在回想起当时的场景觉得自己也真是搞笑,这都没死,太难了我.昨天下午在五楼开会,这也是我入职 ...

  7. cs231n--详解卷积神经网络

    原版地址:http://cs231n.github.io/convolutional-networks/ 知乎翻译地址:https://zhuanlan.zhihu.com/p/22038289?re ...

  8. JMeter定制Sampler

    1.背景 相信大家在使用JMeter工具测试的时候,经常会遇到自带采样器无法满足测试要求的情况.面对这种情况,通常的办法是使用万能的自定义Java Request的达到测试目的.这个方法有个弊端,只要 ...

  9. 3.php基础(控制语句,函数,数组遍历)

    if条件判断语句 结构一:只判断true,不管false 结构二:既判断true,也判断false(二选一) 结构三:多条件判断 switch多分支结构 Switch语法结构说明: l Switch的 ...

  10. 微信小程序页面跳转url如何传对象参数

    两步走 首先第一步:wx.navigateTo({ url:"XXX"+"&params="+ JSON.stringify(obj); }) 第二步获 ...