Portal
Title description
Given an n-sided polygon, and the distance k. Connect 1 and k+1 for the first time, connect k+1 and (k+1+k)%n for the second time, and proceed n times in sequence. After each end, the output n-sided polygon is divided into several regions.
analysis
Drawing a picture, you can observe and draw conclusions. Each time the answer is added 1 to the previous answer plus the number of
intersections. So how do
we maintain the number of intersections? We can calculate how many points have a straight line between two points. , Use a tree array for maintenance. It
should be noted that we need to maintain the smaller side, otherwise some edges will be recalculated
Code
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
ll tr[N];
int n,m;
int lowbit(int x){
return x & -x;
}
void add(int x,int c){
for(int i = x;i <= n;i += lowbit(i)) tr[i] += c;
}
ll sum(int x){
ll res = 0;
for(int i = x;i;i -= lowbit(i)) res += tr[i];
return res;
}
int main(){
scanf("%d%d",&n,&m);
int s = 1,e;
m = min(m,n - m);
ll ans = 1;
for(int i = 1;i <= n;i++){
e = (s + m - 1) % n + 1;
if(s > e){
ans += (1ll + sum(n) - sum(s) + sum(e - 1) - sum(0));
}
else{
ans += (1ll + sum(e - 1) - sum(s));
}
add(s,1);
add(e,1);
s = e;
printf("%lld ",ans);
}
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/