zoukankan      html  css  js  c++  java
  • Codeforces755D PolandBall and Polygan

    题目戳这里

    我们只需要计算每增加一条线后穿过了几条已有的线即可。为了方便,我们令(K le N/2),并且给每条线一个方向,即(x)((x+K) ; mod ; N)。然后我们假设现在我们正在链接(a)((a+K) ; mod ; N)这条线,于是他穿过的线就是从((a-K,a+k))(模(K)意义下)$这段区间中点射出边的数目,拿个树状数组维护一下即可。

    #include<cstdio>
    #include<cstdlib>
    using namespace std;
    
    typedef long long ll;
    #define lowbit(a) (a&-a)
    const int maxn = 1000010;
    int N,K,now,tree[maxn]; ll ans = 1LL;
    
    inline void ins(int a) { for (++a;a <= N;a += lowbit(a)) ++tree[a]; }
    inline ll calc(int a) { int ret = 0; for (++a;a;a -= lowbit(a)) ret += tree[a]; return (ll)ret; }
    
    int main()
    {
    	freopen("755D.in","r",stdin);
    	freopen("755D.out","w",stdout);
    	scanf("%d %d",&N,&K); if (N - K < K) K = N-K;
    	do
    	{
    		if (now+K-1<N)
    		{
    			ans += calc(now+K-1)-calc(now);
    			if (now - K < 0) ans += calc(now-1)+calc(N-1)-calc(now-K+N);
    			else ans += calc(now-1)-calc(now-K);
    		}
    		else
    		{
    			ans += calc(N-1)-calc(now)+calc(now+K-1-N);
    			if (now - K < 0) ans += calc(now-1)+calc(N-1)-calc(now-K+N);
    			else ans += calc(now-1)-calc(now-K);
    		}
    		ins(now); now += K; if (now >= N) now -= N;
    		printf("%lld ",++ans);
    	}
    	while (now);
    	fclose(stdin); fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    codevs 1450 xth 的旅行
    Loj #6287 诗歌
    Codeforces 323C Two permutations
    Spoj MKTHNUM
    [TJOI2015]弦论
    Spoj SUBLEX
    bzoj 4338: BJOI2015 糖果
    bzoj 3462: DZY Loves Math II
    bzoj 2843: 极地旅行社
    清北学堂模拟赛d4t5 b
  • 原文地址:https://www.cnblogs.com/mmlz/p/6345594.html
Copyright © 2011-2022 走看看