题解 CF755D 【PolandBall and Polygon】

版权声明:版权所有,转载请标明博文出处。 https://blog.csdn.net/HQG_AC/article/details/84526757

Codeforces 755D PolandBall and Polygon 题解

题意:

给你一个n边行,每次让你将x与(x+k)%n的点连一条边,每次询问该多边形内面积被划分成了几块

分析:

我们发现这个输出很有规律,但是规律好像不是很好找。

于是我们考虑其他方法。

根据样例分析的图画,我们发现:

1.前两幅图只对答案做出了1的贡献

2.第3幅图对答案做出了2的贡献,且他连出的边与边(1,3)有交点;

3.图4,5都对答案做出了3的贡献,且有两个交点;

根据这个发现,我们大胆的推断出了结论:每条边对答案的贡献是与他的交点的个数+1

那么怎么算交点个数呢?

题目给了我们一个台阶,他告诉我们n与k互质且k不变,

于是不会出现重边或边包含边的问题,于是我们的问题就转化成了维护x到(x+k)%n中的点延伸出的边的数量(这些边必与本边相交),这个可以通过任何一种数据结构维护,like树状数组

注意两个细节:

1.k = min(k, n - k)

2.long long

分类讨论搞一下就AC了吧。

代码: //一大堆头文件不要管

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;

#define rep(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define Rep(i, a, b) for (int (i) = (a) - 1; (i) < (b); (i)++)
#define REP(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define clr(a) memset(a, 0, sizeof(a))
#define Sort(a, len, cmp) sort(a + 1, a + len + 1, cmp)
#define ass(a, sum) memset(a, sum, sizeof(a))

#define ls ((rt) << 1)
#define rs ((rt) << 1 | 1)
#define lowbit(x) (x & -x)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
#define ENDL cout << endl
#define SZ(x) ((int)x.size())

typedef long long ll ;
typedef unsigned long long ull ;
typedef vector <int> Vi ;
typedef pair <int, int> Pii ;
typedef pair <ll, ll> Pll ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
typedef map <ll, ll> mll ;

const int N = 1000100 ;
const double eps = 1e-8 ;
const int iinf = INT_MAX ;
const ll linf = 2e18 ;
const double dinf = 1e30 ;
const int MOD = 1000000007 ;

inline int read(){
    int X = 0, w = 0 ;
	char ch = 0 ;
    while (!isdigit(ch)) { w |= ch == '-' ; ch = getchar() ; }
    while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar() ;
    return w ? - X : X ;
}

void write(int x){
     if (x < 0) putchar('-'), x = - x ;
     if (x > 9) write(x / 10) ;
     putchar(x % 10 + '0') ;
}

void print(int x) {
	cout << x << endl ;
	exit(0) ;
}

void PRINT(string x) {
	cout << x << endl ;
	exit(0) ;
}

void douout(double x){
	 printf("%lf\n", x + 0.0000000001) ;
}

int n, k, now, to ;
ll ans ;
int bit[N] ;

void add(int a, int x) {
	for (; a <= n; a += lowbit(a)) bit[a] += x ;
}

int sum(int a) {
	int res = 0 ;
	for (; a; a -= lowbit(a)) res += bit[a] ;
	return res ;
}

signed main(){
	scanf("%d%d", &n, &k) ;
	k = min(k, n - k) ;
	now = 1 ;
	ans = 1 ;
	for (int i = 1; i <= n; i++) {
		to = now + k ;
		ans++ ;
		if (to > n) {
			to -= n ;
			ans += sum(n) - sum(now) + sum(to - 1) ;
		}
		else {
			ans += sum(to - 1) - sum(now) ;
		}
		add(now, 1) ;
		add(to, 1) ;
		now = to ;
		printf("%lld ", ans) ;
	}
}

/*
写代码时请注意:
	1.是否要开Long Long?数组边界处理好了么?
	2.实数精度有没有处理?
	3.特殊情况处理好了么?
	4.做一些总比不做好。
思考提醒:
	1.最大值和最小值问题可不可以用二分答案?
	2.有没有贪心策略?否则能不能dp?
*/

猜你喜欢

转载自blog.csdn.net/HQG_AC/article/details/84526757