CF755D PolandBall and Polygon(树状数组)

题目描述

PolandBall has such a convex polygon with nn veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments.

He chose a number kk such that g c d ( n , k ) = 1 gcd(n,k)=1 gcd(n,k)=1 . Vertices of the polygon are numbered from 1 1 1 to n n n in a clockwise way. PolandBall repeats the following process n n n times, starting from the vertex 1 1 1 :

Assume you’ve ended last operation in vertex x x x (consider x = 1 x=1 x=1 if it is the first operation). Draw a new segment from vertex x$ to k$ -th next vertex in clockwise direction. This is a vertex x + k x+k x+k or x + k − n x+k-n x+kn depending on which of these is a valid index of polygon’s vertex.

Your task is to calculate number of polygon’s sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon’s sides.

输入格式

There are only two numbers in the input: n , k n,k n,k( 5 < = n < = 1 0 6 5<=n<=10^{6} 5<=n<=106, 2 < = k < = n − 2 2<=k<=n-2 2<=k<=n2 , g c d ( n , k ) = 1 gcd(n,k)=1 gcd(n,k)=1 ).

输出格式

You should print n n n values separated by spaces. The i − t h i -th ith value should represent number of polygon’s sections after drawing first i i i lines.

题意翻译

给出一个 n n n边形,和距离 k k k。 第一次连接 1 1 1 k + 1 k+1 k+1,第二次连接 k + 1 k+1 k+1 ( k + 1 + k ) (k+1+k)%n (k+1+k),依次进行 n n n次,每次结束后输出 n n n边形被分割成了几个区域。

输入输出样例

输入 #1

5 2

输出 #1

2 3 5 8 11 

输入 #2

10 3

输出 #2

2 3 4 6 9 12 16 21 26 31 

说明/提示

The greatest common divisor (gcd) of two integers aa and bb is the largest positive integer that divides both aa and bb without a remainder.

For the first sample testcase, you should output “ 2   3   5   8   11 2 \ 3 \ 5 \ 8 \ 11 2 3 5 8 11”. Pictures below correspond to situations after drawing lines.

imgimg

imgimg

img

题解

  • 对于本题,我们可以发现每多一条线它对答案的贡献是它与其他的线的交点 + 1 +1 +1
  • 所以我们只需统计交点即可。
    线段可以被看作有两个点的集合,当我们新加入一条线段时,我们只需将对应两个端点的权值++,即可代表我们加入了这条线段。
  • 所以统计交线的个数也就很 e a s y easy easy了。假设我们要从 l l l连向 r r r,我们只需计算 s u m ( r − 1 ) − s u m ( l ) sum(r-1)-sum(l) sum(r1)sum(l),即我们只需计算 l , r l,r l,r中间点的权值和即可。 但是如果 r r r的编号超过了 n n n,我们就需要对 r r r进行取模,这时r有可能会小于 l l l,那么我们只需计算 s u m ( n ) + s u m ( r − 1 ) − s u m ( l ) sum(n)+sum(r-1)-sum(l) sum(n)+sum(r1)sum(l)。(对照样例的图理解一下吧)
  • 注意 k k k应该取 m i n ( k , n − k ) min(k,n-k) min(k,nk)以及要开 l o n g   l o n g long \ long long long

code

#include <bits/stdc++.h> 
using namespace std; 
const int maxn = 1e6 + 1000; 
typedef long long LL; 

int n, k; 
LL c[maxn]; 

inline int lowbit(int x) { return x & (-x); }

inline void add(int x, int val ) {
	for (; x <= n; x += lowbit(x)) c[x] += val; 
}

inline LL query(int x) {
	LL ret = 0; 
	for (; x; x -= lowbit(x)) {
		ret += c[x]; 
	}
	return ret; 
}

int main() {
	scanf("%d%d", &n, &k); 
	k = min(k, n - k); 
	LL ans = 1; int now = 1, to; 
	for (int i = 1; i <= n; ++i) {
		to = now + k; ++ans; 
		if (to > n) {
			to -= n; 
			ans += query(n) + query(to - 1) - query(now); 
		}
		else {
			ans += query(to - 1) - query(now); 
		}
		add(now, 1); add(to, 1); 
		now = to; 
		printf("%lld ", ans); 
	}
	return 0; 
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值