8VC Venture Cup 2017 - Elimination Round D. PolandBall and Polygon 树状数组
D. PolandBall and Polygon
链接:
http://codeforces.com/contest/755/problem/D
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <string> 8 #include <map> 9 #include <set> 10 #include <stack> 11 #include <queue> 12 using namespace std; 13 #define rep(i,a,n) for (int i=a;i<=n;i++) 14 #define per(i,a,n) for (int i=n;i>=a;i--) 15 #define pb push_back 16 #define mp make_pair 17 #define all(x) (x).begin(),(x).end() 18 #define fi first 19 #define se second 20 #define SZ(x) ((int)(x).size()) 21 typedef vector<int> VI; 22 typedef long long ll; 23 typedef pair<int, int> PII; 24 const ll mod = 1000000007; 25 // head 26 27 ll n, k; 28 const ll maxn = 1e6 + 7; 29 ll Tree[maxn]; 30 ll lowbit(ll x) { 31 return (x&-x); 32 } 33 void add(ll x, ll value) { 34 for (ll i = x; i <= n; i += lowbit(i)) 35 Tree[i] += value; 36 } 37 ll get(ll x) { 38 ll sum = 0; 39 for (ll i = x; i; i -= lowbit(i)) 40 sum += Tree[i]; 41 return sum; 42 } 43 44 int main() { 45 cin >> n >> k; 46 if (2 * k > n) k = n - k; 47 ll s = 1, e; 48 ll ans = 1; 49 do { 50 e = s + k; 51 if (e <= n) ans += get(e - 1) - get(s) + 1; 52 else { 53 e -= n; 54 ans += get(e - 1) + get(n) - get(s) + 1; 55 } 56 add(s, 1); 57 add(e, 1); 58 cout << ans << " "; 59 s = e; 60 } while (s != 1); 61 return 0; 62 }