hdu4288 Coder (segment tree + detach)

Source: Internet
Author: User

Topic Links:huangjing
Test Instructions:three operations were given in the topic1:add x is inserting x into the2:delete x is to remove x3:sum is to seek the%5=3 of the elements of the subscript. Another condition is that both insertions and deletions are guaranteed to be orderly in sequence.

。。 first of all, tell a kind of violent wording.

。 Due to the time is sufficient, you need to understand the functions inside the STL.

。 is to declare the container of a vector directly. Then directly with the vector inside the operation analogy insert,erase and so on.

。 It's just that it's very inefficient. finally ran out more than 6,000 Ms.

。 (Strong brother's code) code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map > #include <vector> #include <cmath> #include <string> #include <queue> #define EPS 1e-9# Define ll long Long#define INF 0x3f3f3f3fusing namespace Std;char s[5];int n;vector<int>a;int main () {int Len,val    ;    Vector<int>::iterator ITER;        while (cin>>n) {len=0;        A.clear ();            while (n--) {scanf ("%s", s);                if (s[0]== ' s ') {long long ans = 0;                for (int i=2; i < Len; i+=5) ans + = a[i];            cout<<ans<<endl;                } else if (s[0]== ' a ') {len++;                scanf ("%d", &val);                Iter=lower_bound (A.begin (), A.end (), Val);            A.insert (Iter,val);                 } else {len--;       scanf ("%d", &val);          Iter= Lower_bound (A.begin (), A.end (), Val); A.erase (ITER); Basic Coding}}} return 0;}


Another approach is the line tree approach, which maintains 5 segment trees. The number of each node that is stored in the structure. First, because the segment tree does not support the insertion, deletion, to maintain a number of CNT, when inserting a number, you see the original%3 number, now take surplus is certainly equal to 2, then how to do?? Then this CNT has a wonderful role to play. Each time you insert a delete, the corresponding number of nodes is changed to maintain the 5 tree segment.

。

Finally, because there is no data to tell the scope, so to take discretization, and then off-line processing, and finally get all the total number of operations, and then build on this. The first time with discretization, think good tall on ... code: (from Cxlove)

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map > #include <vector> #include <cmath> #include <string> #include <queue> #define EPS 1e-9# Define ll long Long#define INF 0x3f3f3f3fusing namespace Std;const int maxn=100000+10;int N,a[maxn],b[maxn];char OP[MAXN]    [5];struct tree{int cnt; ll sum[5];}    Tree[maxn<<2];void buildtree (int l,int r,int dex) {tree[dex].cnt=0;    memset (tree[dex].sum,0,sizeof (tree[dex].sum));    if (l==r) return;    int mid= (L+R) >>1;    Buildtree (l,mid,dex<<1); Buildtree (mid+1,r,dex<<1|1);} void push_up (int dex) {for (int i=0;i<5;i++) TREE[DEX].SUM[I]=TREE[DEX&LT;&LT;1].SUM[I]+TREE[DEX&LT;&LT;1|1].S um[((i-tree[dex<<1].cnt)%5+5)%5];}    void update (int l,int r,int dex,int pos,int flag,int val) {tree[dex].cnt+=flag;        if (l==r) {if (flag==1) Tree[dex].sum[1]=val;        else tree[dex].sum[1]=0;    Return} int mid= (L+R) >>1;    if (pos<=mid) update (l,mid,dex<<1,pos,flag,val);    else update (mid+1,r,dex<<1|1,pos,flag,val); PUSH_UP (DEX);}    int main () {int tot,pos,flag;        while (~SCANF ("%d", &n)) {tot=0;            for (int i=1;i<=n;i++) {scanf ("%s", Op[i]);                if (op[i][0]!= ' s ') {scanf ("%d", &b[i]);            A[tot++]=b[i];        }} sort (A,a+tot);        Tot=unique (A,a+tot)-A;        if (tot==0) memset (tree[1].sum,0,sizeof (tree[1].sum));        else Buildtree (1,tot,1);            for (int i=1;i<=n;i++) {pos=lower_bound (A,a+tot,b[i])-A;            pos++;                if (op[i][0]== ' a ') {flag=1;            Update (1,tot,1,pos,flag,b[i]);                } else if (op[i][0]== ' d ') {flag=-1;            Update (1,tot,1,pos,flag,b[i]); } else printf ("%i64d\n", tree[1].sum[3]); }} return 0;}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

hdu4288 Coder (segment tree + detach)

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.