【数据结构】【CF1073D】 Berland Fair(2)
发布时间:2021-03-31 20:19 所属栏目:53 来源:网络整理
导读:\(O(n \log n)\) #include ctime#include cstdio#ifdef ONLINE_JUDGE#define freopen(a,c)#endif#define rg register#define ci const int#define cl const long longtypedef long long ll;namespace IPT { const i
\(O(n \log n)\) #include <ctime> #include <cstdio> #ifdef ONLINE_JUDGE #define freopen(a,c) #endif #define rg register #define ci const int #define cl const long long typedef long long ll; namespace IPT { const int L = 1000000; char buf[L],ch = IPT::GetChar(); } if (lst == '-') x = -x; } namespace OPT { int buf[120]; } template <typename T> inline void qw(T x,putchar('-');} rg int top=0; do {OPT::buf[++top] = int(x % 10 + '0');} while (x /= 10); while (top) putchar(OPT::buf[top--]); if (pt) putchar(aft); } const int maxn = 200010; const int maxt = 400010; int n; ll t,ans; int tree[maxn],MU[maxn]; struct Tree { Tree *ls,*rs; int l,r; ll v; inline void pushup() { this->v = this->ls ? (this->rs ? this->ls->v + this->rs->v : this->ls->v) : this->rs->v; } }; Tree *pool[maxt],qwq[maxt],*rot; int top; inline int lowbit(ci x) {return x & -x;} int check(int,ll); void buildpool(); void build(Tree*,ci,ci); void update(int,ci); void update(Tree*,ci); ll ask(Tree*,ci); int ask(int); int ask(Tree*,cl); signed main() { freopen("1.in",stdin); qr(n); qr(t); ll s = 0; for (rg int i = 1; i <= n; ++i) {qr(MU[i]); s+= MU[i]; update(i,1);} int cnt = n; buildpool(); build(rot,n); while (cnt) { if (!t) break; ans += t / s * cnt; t %= s; int k = 0; do { int pre = k; k = check(k,t); if (k > n) { t -= ask(rot,pre + 1,n); ans += ask(n) - ask(pre); break; }; update(rot,k); update(k,-1); --cnt; t -= ask(rot,pre,k - 1); s -= MU[k]; ans += ask(k) - ask(pre); } while (cnt && t); } qw(ans,true); } int check(int pre,ll x) { x += ask(rot,pre); ll s = ask(rot,n); if (s <= x) return n + 1; return ask(rot,x); } void buildpool() { for (rg int i = 0; i < maxt; ++i) pool[i] = qwq + i; rot = pool[maxt - 1]; top = maxt - 2; } void build(Tree *u,ci l,ci r) { u->l = l; u->r = r; if (l == r) {u->v = MU[l]; return;} int mid = (l + r) >> 1; if (l <= mid) build(u->ls = pool[top--],l,mid); if (mid < r) build(u->rs = pool[top--],mid + 1,r); u->pushup(); } void update(int x,ci v) { while (x <= n) { tree[x] += v; x += lowbit(x); } } void update(Tree* u,ci x) { if ((u->l > x) || (u->r < x)) return; if (u->l == u->r) {u->v = 0; return;} if (u->ls) update(u->ls,x); if (u->rs) update(u->rs,x); u->pushup(); } int ask(int x) { int _ret = 0; while (x) { _ret += tree[x]; x -= lowbit(x); } return _ret; } ll ask(Tree *u,ci r) { if ((u->l > r) || (u->r < l)) return 0; if ((u->l >= l) && (u->r <= r)) return u->v; return u->ls ? (u->rs ? ask(u->ls,r) + ask(u->rs,r) : ask(u->ls,r)) : ask(u->rs,r); } int ask(Tree *u,cl v) { if (u->l == u->r) return u->l; if (u->ls->v <= v) return ask(u->rs,v - u->ls->v); else return ask(u->ls,v); } Summary该define int ll就要define啊= =要不然可能会fst的很惨= = 一个数对比自己小的数取模一次至少减少一半。 (编辑:ASP站长网) |
相关内容
网友评论
推荐文章
热点阅读