BZOJ4068: [ctsc2015]app

Source: Internet
Author: User
<span id="Label3"></p><p><p>For a selected set of tasks, these tasks can be selected for all $i$ if the number of tasks previously selected is no more than I for any given moment, $i.</p></p><p><p>Maintain a line tree $t$, the first $i$ position is $i$, each use a task, $[t,t]$ to reduce $1$.</p></p><p><p></p></p><p><p>Insert a Task:</p></p><p><p>first, we query the position of the first $0$ in the $[t,t]$ interval, which is recorded as $k$.</p></p><p><p>If there is no $0$, then you can join this task directly.</p></p><p><p>otherwise, do not use this task, or take this task to replace the $t$ in the $[1,k]$ the least valuable Task.</p></p><p><p></p></p><p><p>To delete a task:</p></p><p><p>If it is not used, delete it directly.</p></p><p><p>Otherwise $[t,t]$ to add $1$, and then find the last $0$ position $k$.</p></p><p><p>Then take the task of $t$ in the $[k+1,t]$ in the standby task set and add the Answer.</p></p><p><p></p></p><p><p>Then press $t$ with two tree segments to maintain two sets of Separate.</p></p><p><p>Time complexity $o (q\log T) $.</p></p><p><p></p></p><pre class="brush:cpp;collapse:true;;gutter:true;">#include <cstdio> #include <set> #include <map> #include <algorithm>using namespace Std;typedef pair<int,int>p;const int n=300010,m=1050000;int N,m,x,y,z,g[n],nxt[n],ed,cnt;char Ch;long long ans;map<P, Int>id;struct E{int t,p,u; E () {}e (int _t,int _p) {t=_t,p=_p,u=0;}} e[n];inline int GetID (int x,int y) {int t=id[p (x, y)],p=g[t]; g[t]=nxt[g[t]]; Return p;} inline void read (int&a) {char c;while (!) ( ((c=getchar ()) >= ' 0 ') && (c<= ' 9 ')); a=c-' 0 '; while (((c=getchar ()) >= ' 0 ') && (c<= ' 9 ')) (a*= Ten) +=c-' 0 ';} int v[m],tag[m];inline void add1 (int x,int p) {v[x]+=p,tag[x]+=p;} inline void PB (int x) {if (tag[x]) add1 (x<<1,tag[x]), add1 (x<<1|1,tag[x]), tag[x]=0;} inline void up (int x) {v[x]=min (v[x<<1],v[x<<1|1]);} void build (int x,int a,int b) {v[x]=a; If (a==b) return; int mid= (a+b) >>1; Build (x<<1,a,mid), Build (x<<1|1,mid+1,b);} void add (int x,int a,int b,int c,int d,int p) {if (c<=a&&b<=d) {add1 (x,p); return;} PB (x); int mid= (a+b) >>1; If (c<=mid) add (x<<1,a,mid,c,d,p); If (d>mid) add (x<<1|1,mid+1,b,c,d,p); Up (x);} int left (int x,int a,int b,int c,int d) {if (v[x]) return 0; If (a==b) return a; PB (x); int mid= (a+b) >>1,t=0; If (c<=mid) t=left (x<<1,a,mid,c,d); If (!t&&d>mid) t=left (x<<1|1,mid+1,b,c,d); Up (x); Return t;} int right (int x,int a,int b,int c,int d) {if (v[x]) return 0; If (a==b) return a; PB (x); int mid= (a+b) >>1,t=0; If (d>mid) t=right (x<<1|1,mid+1,b,c,d); If (!t&&c<=mid) t=right (x<<1,a,mid,c,d); Up (x); Return t;} struct segmenttree{set<p>a[n];int cnt[n],mi[m],ma[m];inline int umax (int a,int b) {if (!a) return b; If (!b) return a; Return e[a].p>e[b].p?a:b;} inline int umin (int a,int b) {if (!a) return b; If (!b) return a; Return e[a].p<e[b].p?a:b;} void add (int x,int a,int b,int c,int p) {if (a==b) {cnt[a]++; A[a].insert (P (e[p].p,p)); Mi[x]=a[a].begin ()->second; Ma[x]=a[a].rBegin ()->second; Return } int mid= (a+b) >>1; If (c<=mid) add (x<<1,a,mid,c,p); else add (x<<1|1,mid+1,b,c,p); Mi[x]=umin (mi[x<<1],mi[x<<1|1]); Ma[x]=umax (ma[x<<1],ma[x<<1|1]);} void del (int x,int a,int b,int c,int p) {if (a==b) {cnt[a]--; A[a].erase (P (e[p].p,p)); If (cnt[a]) {mi[x]=a[a].begin ()->second; Ma[x]=a[a].rbegin ()->second; }else mi[x]=ma[x]=0; Return } int mid= (a+b) >>1; If (c<=mid) del (x<<1,a,mid,c,p); else del (x<<1|1,mid+1,b,c,p); Mi[x]=umin (mi[x<<1],mi[x<<1|1]); Ma[x]=umax (ma[x<<1],ma[x<<1|1]);} int askmin (int x,int a,int b,int c,int d) {if (c<=a&&b<=d) return mi[x]; int mid= (a+b) >>1,t=0; If (c<=mid) t=askmin (x<<1,a,mid,c,d); If (d>mid) t=umin (t,askmin (x<<1|1,mid+1,b,c,d)); Return t;} int Askmax (int x,int a,int b,int c,int d) {if (c<=a&&b<=d) return ma[x]; int mid= (a+b) >>1,t=0; If (c<=mid) t=aSkmax (x<<1,a,mid,c,d); If (d>mid) T=umax (t,askmax (x<<1|1,mid+1,b,c,d)); Return t;}} t1,t2;inline void AddTask (int x) {int t=e[x].t,k=left (1,1,n,t,n); If (!k) {add (1,1,n,t,n,-1); T1.add (1,1,n,t,x); e[x].u=1; ans+=e[x].p; }else{int Y=t1.askmin (1,1,n,1,k); If (e[y].p<e[x].p) {add (1,1,n,e[y].t,n,1); T1.del (1,1,n,e[y].t,y); T2.add (1,1,n,e[y].t,y); e[y].u=0; Add (1,1,n,t,n,-1); T1.add (1,1,n,t,x); e[x].u=1; ans+=e[x].p-e[y].p; }else T2.add (1,1,n,t,x); }}inline void Deltask (int x) {if (!e[x].u) T2.del (1,1,n,e[x].t,x); else{ans-=e[x].p; Add (1,1,n,e[x].t,n,1); T1.del (1,1,n,e[x].t,x); int k=right (1,1,n,1,n), Y=t2.askmax (1,1,n,k+1,n); If (y) {add (1,1,n,e[y].t,n,-1); T2.del (1,1,n,e[y].t,y); T1.add (1,1,n,e[y].t,y); e[y].u=1; ans+=e[y].p; }}}int main () {read (n), read (m), build (1,1,n); While (m--) {while ((ch=getchar ())! = ' A ' &&ch!= ' D '); Read (x), read (y); If (ch== ' A ') { E[++ed]=e (x, y); If (!id[p (x, y)]) z=id[p (x, y)]=++cnt;else z=id[p (x, y)]; nxt[ed]=g[z],g[z]=ed; AddTask (ed); }else deltask (getid (x, y)); printf ("%lld\n", ans); } return 0;}</pre><p><p>  </p></p><p><p>BZOJ4068: [ctsc2015]app</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.