HDU 4288 line segment tree sum of medians like lazysales girl coder

Source: Internet
Author: User

Question:

That is, the number is inserted continuously, and the number is pushed from small to large. Calculate the sum of the numbers 3, 8, 13.

Solution:

Line Segment tree. The coordinates of the Line Segment tree indicate the largest number of all given numbers (0 is not yet,> 0 is the largest number of I already inserted ). Then ans [] [I] indicates the sum of the Number % 5 of the current interval (subscripts I (I) (which may be a bit unclear ..) Yes, yes. The last query is O (1.

Question preparation process:

Finally, there was a question about the idea of an online competition... At that time, the idea was somewhat biased. During the competition, the node meaning of my line segment tree is correct, that is, I actually represent all the entire intervals... As a result, I don't know how to update the update after me .... In addition, this is my upcoming question. I have to speed up my question preparation. If the competition is like this, I really regret it.

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # define maxn 111111 # define lson l, m, RT <1 # define rson m + 1, R, RT <1 | 1 # define havem int M = (L + r)> 1 using namespace STD; int sum [maxn <2], n, x [maxn], flag, add [maxn], xsub ;__ int64 ans [maxn <2] [5]; char op [maxn] [5]; void build (int l, int R, int RT) {for (INT I = 0; I <5; I ++) ans [RT] [I] = 0; sum [RT] = 0; If (L = r) Return; havem; build (lson); Build (rson);} void push_up (int rt) {for (INT I = 0; I <5; I ++) ans [RT] [I] = ans [RT <1] [I] + ans [RT <1 | 1] [(I-sum [RT <1]) % 5 + 5) % 5]; // it must be the remainder plus 5 and then the remainder} void Update (INT POs, int L, int R, int RT) {sum [RT] + = 2 * flag-1; if (L = r) {ans [RT] [0] = x [POS] * flag; return ;} int M = (L + r)> 1; if (Pos <= m) Update (Pos, lson); else Update (Pos, rson); push_up (RT );} int Main () {While (~ Scanf ("% d", & N) {xsub = 0; For (INT I = 0; I <n; I ++) {scanf ("% s ", OP [I]); If (OP [I] [0] = 'A' | op [I] [0] = 'D ') scanf ("% d", & add [I]), X [xsub ++] = add [I];} Sort (x, x + xsub ); xsub = unique (x, x + xsub)-X; build (0, xsub, 1); // start from 1 for (INT I = 0; I <N; I ++) {If (OP [I] [0]! ='S ') {int low = lower_bound (x, x + xsub, add [I])-X; If (OP [I] [0] = 'A ') flag = 1; else flag = 0; Update (low, 0, xsub, 1);} else printf ("% i64d \ n ", ans [1] [2]) ;}} return 0 ;}

The code of the competition...

/*Pro: 0Sol:date:*/#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <queue>#include <set>#include <vector>#define lson l , m , rt << 1#define rson m + 1, r , rt << 1 | 1#define maxn 111111 //using namespace std;int X[maxn],n,add[maxn],xsub;__int64 p[maxn << 2][6];char op[maxn][10];short pp[maxn];void build(int l, int r, int rt){    for(int i = 0; i < 5; i ++)        p[rt][i] = 0;    if(l == r) return ;    int m = (l + r) >> 1;    build(lson); build(rson);}void push_up(int rt){    for(int i = 0; i < 5; i ++)        p[rt][i] = p[rt << 1][i] + p[rt << 1 | 1][i];}void update(int pos, short ii, int c, int l,int r, int rt){//    if(l == r) {        p[rt][ii] += c * X[pos];        return ;    }    int m = (l + r) >> 1;    if(pos <= m) update(pos,ii,c,lson);    else update(pos,ii,c,rson);    push_up(rt);}int main(){    while(~scanf("%d",&n)){        xsub = 0;        for(int i = 0; i < n; i ++){            scanf("%s",&op[i]);            if(op[i][0] != 's')                scanf("%d",&add[i]),                X[xsub ++] = add[i];        }        sort(X, X + xsub);        int m = 1;        for(int i = 1; i <xsub; i ++){            if(X[i] != X[i - 1]) X[m ++] = X[i];        }        build(0, m, 1);        int x = 0;        for(int i = 0; i < n; i ++){            if(op[i][0] == 'a'){                int ll = lower_bound(X,X + m,add[i]) - X;                x ++, update(ll, x % 5, 1, 0 , m , 1);//???                pp[ll] = x % 5;            }            else if(op[i][0] == 'd'){                int ll = lower_bound(X,X + m,add[i]) - X;                x -- , update(ll, pp[ll], -1, 0,m, 1);            }            else{                if(3 > x)                    printf("0\n");                else{                    printf("%I64d  !!!  \n",p[1][3]);                }            }        }    }return 0;}

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.