#include <iostream> #include <algorithm> #include <map> #define mp make_pair using namespace std; vector <long long> dyn[300000]; long long poczd[300000]; int ost[300000]; long long wyn[300000]; long long ilper[300000]; long long szuk[300000]; long long ri[300000]; long long rek(int dl) { if(ilper[dl]>((long long)dl*(dl-1)/2)) return 0; if(ilper[dl]<0) return 0; if(dyn[dl].empty()) { poczd[dl]=ilper[dl]; } if(poczd[dl]-(long long)dyn[dl].size()<ilper[dl]) return dyn[dl][poczd[dl]-ilper[dl]]; wyn[dl]=0; for(ri[dl]=max((long long)0, ilper[dl]-(long long)(dl-1)*(dl-2)/2); ri[dl]<min((long long)dl, ilper[dl]+1) && wyn[dl]<szuk[dl]; ri[dl]++) { ilper[dl-1]=ilper[dl]-ri[dl]; szuk[dl-1]=szuk[dl]-wyn[dl]; wyn[dl]+=rek(dl-1); } dyn[dl].push_back(wyn[dl]); return dyn[dl][poczd[dl]-ilper[dl]]; } long long tmprek; void rekost(int dl) { wyn[dl]=0; ri[dl]=max((long long)0, ilper[dl]-(long long)(dl-1)*(dl-2)/2); for(; ri[dl]<dl && wyn[dl]<szuk[dl]; ri[dl]++) { ost[dl]=ri[dl]; ilper[dl-1]=ilper[dl]-ri[dl]; szuk[dl-1]=szuk[dl]-wyn[dl]; tmprek=rek(dl-1); if(wyn[dl]+tmprek>=szuk[dl]) break; wyn[dl]+=tmprek; } if(dl>1) rekost(dl-1); } long long RAND() { unsigned long long mx=(long long)RAND_MAX+1, mn=mx; long long ret=0; while(mx!=0) { ret=ret*mn+rand(); mx*=mn; } return ret; } struct node { int val; long long key; int dod; node *l, *r; node() { val=0, key=RAND(), dod=0, l=0, r=0; } }; void pchaj(node *k) { if(k!=0) { k->val+=k->dod; if(k->l!=0) k->l->dod+=k->dod; if(k->r!=0) k->r->dod+=k->dod; k->dod=0; } } node *attach(node *k, node *l, node *r) { if(k==0) return k; k->l=l; k->r=r; return k; } node *merge(node *a, node *b) { if(a==0) return b; if(b==0) return a; pchaj(a); pchaj(b); if(a->key<b->key) { return attach(a, a->l, merge(a->r, b)); } return attach(b, merge(a, b->l), b->r); } inline node *merge(pair <node *, node *> p) { return merge(p.first, p.second); } pair <node *, node *> split(node *k, int val) { if(k==0) return mp(k, k); pchaj(k); if(k->val<val) { auto tmp=split(k->r, val); return mp(attach(k, k->l, tmp.first), tmp.second); } auto tmp=split(k->l, val); return mp(tmp.first, attach(k, tmp.second, k->r)); } node *wsk[300000]; void pisz(node *k, int il) { if(k!=0) { pisz(k->r, il+1); for(int i=0; i<il; i++) cerr<<' '; cerr<<k->val<<" "<<k->dod<<" "<<k->key<<endl; pisz(k->l, il+1); } } inline void pisz(node *k) {pisz(k, 0);} void dfs(node *k) { if(k!=0) { pchaj(k); dfs(k->r); dfs(k->l); } } int main() { dyn[1].push_back(1); poczd[1]=0; long long n, k; scanf("%lld%lld", &n, &k); if((n&3)==2 or (n&3)==3) { printf("NIE\n"); return 0; } ilper[n]=n*(n-1)/4; szuk[n]=k; if(rek(n)<k) { printf("NIE\n"); return 0; } rekost(n); printf("TAK\n"); node *korz=new node(); korz->val=1000000; for(int i=1; i<=n; i++) { wsk[n-i]=new node(); wsk[n-i]->val=ost[i]; auto tmp=split(korz, ost[i]); tmp.second->dod++; tmp.second=merge(wsk[n-i], tmp.second); korz=merge(tmp); } dfs(korz); for(int i=0; i<n; i++) printf("%d ", wsk[i]->val+1); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | #include <iostream> #include <algorithm> #include <map> #define mp make_pair using namespace std; vector <long long> dyn[300000]; long long poczd[300000]; int ost[300000]; long long wyn[300000]; long long ilper[300000]; long long szuk[300000]; long long ri[300000]; long long rek(int dl) { if(ilper[dl]>((long long)dl*(dl-1)/2)) return 0; if(ilper[dl]<0) return 0; if(dyn[dl].empty()) { poczd[dl]=ilper[dl]; } if(poczd[dl]-(long long)dyn[dl].size()<ilper[dl]) return dyn[dl][poczd[dl]-ilper[dl]]; wyn[dl]=0; for(ri[dl]=max((long long)0, ilper[dl]-(long long)(dl-1)*(dl-2)/2); ri[dl]<min((long long)dl, ilper[dl]+1) && wyn[dl]<szuk[dl]; ri[dl]++) { ilper[dl-1]=ilper[dl]-ri[dl]; szuk[dl-1]=szuk[dl]-wyn[dl]; wyn[dl]+=rek(dl-1); } dyn[dl].push_back(wyn[dl]); return dyn[dl][poczd[dl]-ilper[dl]]; } long long tmprek; void rekost(int dl) { wyn[dl]=0; ri[dl]=max((long long)0, ilper[dl]-(long long)(dl-1)*(dl-2)/2); for(; ri[dl]<dl && wyn[dl]<szuk[dl]; ri[dl]++) { ost[dl]=ri[dl]; ilper[dl-1]=ilper[dl]-ri[dl]; szuk[dl-1]=szuk[dl]-wyn[dl]; tmprek=rek(dl-1); if(wyn[dl]+tmprek>=szuk[dl]) break; wyn[dl]+=tmprek; } if(dl>1) rekost(dl-1); } long long RAND() { unsigned long long mx=(long long)RAND_MAX+1, mn=mx; long long ret=0; while(mx!=0) { ret=ret*mn+rand(); mx*=mn; } return ret; } struct node { int val; long long key; int dod; node *l, *r; node() { val=0, key=RAND(), dod=0, l=0, r=0; } }; void pchaj(node *k) { if(k!=0) { k->val+=k->dod; if(k->l!=0) k->l->dod+=k->dod; if(k->r!=0) k->r->dod+=k->dod; k->dod=0; } } node *attach(node *k, node *l, node *r) { if(k==0) return k; k->l=l; k->r=r; return k; } node *merge(node *a, node *b) { if(a==0) return b; if(b==0) return a; pchaj(a); pchaj(b); if(a->key<b->key) { return attach(a, a->l, merge(a->r, b)); } return attach(b, merge(a, b->l), b->r); } inline node *merge(pair <node *, node *> p) { return merge(p.first, p.second); } pair <node *, node *> split(node *k, int val) { if(k==0) return mp(k, k); pchaj(k); if(k->val<val) { auto tmp=split(k->r, val); return mp(attach(k, k->l, tmp.first), tmp.second); } auto tmp=split(k->l, val); return mp(tmp.first, attach(k, tmp.second, k->r)); } node *wsk[300000]; void pisz(node *k, int il) { if(k!=0) { pisz(k->r, il+1); for(int i=0; i<il; i++) cerr<<' '; cerr<<k->val<<" "<<k->dod<<" "<<k->key<<endl; pisz(k->l, il+1); } } inline void pisz(node *k) {pisz(k, 0);} void dfs(node *k) { if(k!=0) { pchaj(k); dfs(k->r); dfs(k->l); } } int main() { dyn[1].push_back(1); poczd[1]=0; long long n, k; scanf("%lld%lld", &n, &k); if((n&3)==2 or (n&3)==3) { printf("NIE\n"); return 0; } ilper[n]=n*(n-1)/4; szuk[n]=k; if(rek(n)<k) { printf("NIE\n"); return 0; } rekost(n); printf("TAK\n"); node *korz=new node(); korz->val=1000000; for(int i=1; i<=n; i++) { wsk[n-i]=new node(); wsk[n-i]->val=ost[i]; auto tmp=split(korz, ost[i]); tmp.second->dod++; tmp.second=merge(wsk[n-i], tmp.second); korz=merge(tmp); } dfs(korz); for(int i=0; i<n; i++) printf("%d ", wsk[i]->val+1); } |