"bzoj2809" apio2012-dispatching

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=2809 (Topic link)

Test instructions: Give a tree, each node has two weights c,l, representing the charge and leadership respectively, find a point I in the tree, and find some points in the tree to form a set, so that the sum of all points in the set of C and not more than M, and l[i]* the number of elements in the set and maximum.

Solution
I heard that the solution of this problem can be stacked, but it will not do, we consider a different method. Just recently learned the MO team algorithm, so the brain hole open, seems to find the method-Mo Team + sub-block, is not very god.
First, we sorted the nodes in the tree by DFS order, get a series of S, the subtree inquiry into the interval in s to inquire, a total of n subtrees tree, so there is only n query, can bear.
Then we found that for a single node, "so that all points in the collection of C and not more than M, and the number of elements in the li* set and the maximum" can be greedy, that is, as far as possible to choose a small C node to join the collection. So we think of the weight of C discretization after the block (note is the weight C instead of the sequence s), for the non-modified interval query with the team to transfer, the weight of the block is O (1), the query is O (sqrt (n)), the complexity is still O (n*sqrt (n)).
The next step is the detail question. We first DFS to find the DFS sequence and after the interval sort (with the left end of the block as the first keyword, the right end as the second keyword), and then the tree nodes according to the size of C, the value of C is recorded in the ma[] array, and use the p[] array to record the number of the current node discretization. Building blocks for the value of the C domain.
How do i insert, delete, and inquire? For insert and delete, we use an array of b[] to record the node in C for X has several in the current query, with cnts[] to record X in the block where there are several nodes in the current query, with sumv[] to record X in the block in which all the current C's and.
With these three array queries it's good to do it. Notice the difference between the block of the MO team and the weight, don't confuse it.

Code:

bzoj2120#include<algorithm> #include <iostream> #include <cstring> #include <cstdlib># include<cstdio> #include <cmath> #include <vector> #define MOD 1000000007#define inf 2147483640# Define LL long Long#define free (a) freopen (a ".", "R", stdin); Freopen (a ". Out", "w", stdout); using namespace Std;inline LL    Getint () {LL X=0,f=1;char Ch=getchar ();    while (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' && ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} const int maxn=100010;struct Edge {int next,to;} E[maxn<<2];struct interval {int l,r,id,l;} t[maxn];struct data {int dfn,c,l;} A[maxn];int N,m,m,cnt,mo_block,chunk_block;int l[maxn],head[maxn],cnts[maxn],pos[maxn],sumv[maxn],b[maxn],ma[ maxn],p[maxn];void Insert (int u,int v) {e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;}    void Dfs (int u) {t[u].l=a[u].dfn=++cnt;    for (int i=head[u];i;i=e[i].next) DFS (e[i].to); T[u].r=cnt;t[u].id=u;t[u]. L=A[U].L;}BOOL CCMP (data A,data b) {return A.C&LT;B.C;} BOOL poscmp (interval A,interval b) {return POS[A.L]==POS[B.L]?A.R&LT;B.R:POS[A.L]&LT;POS[B.L];}    void Mo_build () {mo_block= (int) sqrt (n); for (int i=1;i<=n;i++) pos[i]= (i-1)/mo_block+1;}    void Chunk_build () {chunk_block= (int) sqrt (CNT); M= (cnt%chunk_block>0)?    Cnt/chunk_block+1:cnt/chunk_block;    for (int i=1;i<=cnt;i++) pos[i]= (i-1)/chunk_block+1; for (int i=1;i<=m;i++) l[i]= (i-1) *chunk_block+1;}    void Update (int x,int val) {b[x]+=val;    Cnts[pos[x]]+=val; sumv[pos[x]]+= (LL) val*ma[x];}    int query () {LL tot=0;int ans=0;        for (int i=1;i<=m;i++) {tot+=sumv[i];ans+=cnts[i];            if (tot>m) {tot-=sumv[i];ans-=cnts[i]; for (int j=l[i];;                J + +) {tot+= (LL) ma[j]*b[j];                ANS+=B[J];            if (tot>m) return ans-(int) (TOT-M)/ma[j]-((tot-m)%ma[j]!=0); }}} return ans;} int main () {scanf ("%d%d", &n,&m);        for (int i=1;i<=n;i++) {int x;        scanf ("%d%d%d", &AMP;X,&AMP;A[I].C,&AMP;A[I].L);    Insert (x,i);    } cnt=0;    DFS (E[HEAD[0]].TO);    Mo_build ();    Sort (t+1,t+1+n,poscmp);    for (int i=1;i<=n;i++) pos[i]=0;    Sort (a+1,a+1+n,ccmp);    CNT=0;MA[P[A[1].DFN]=++CNT]=A[1].C;        for (int i=2;i<=n;i++) {if (a[i].c!=a[i-1].c) cnt++;    MA[P[A[I].DFN]=CNT]=A[I].C;    } LL ans=0;    Chunk_build ();        for (int. l=1,r=0,i=1;i<=n;i++) {for (; r<t[i].r;r++) update (p[r+1],1);        for (; r>t[i].r;r--) update (P[R],-1);        for (; l<t[i].l;l++) update (P[L],-1);        for (; l>t[i].l;l--) update (p[l-1],1); Ans=max (ans, (LL) query () *t[i].    L);    } printf ("%lld\n", ans); return 0;}

  

"bzoj2809" apio2012-dispatching

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.