CSU 1724 equal distance and (offline + segment tree)

Source: Internet
Author: User

Test instructions: Give you some operations, add a number to the set, delete the number in the collection, sum is a number from the X, the interval L of the sum, the number is placed in the set is automatically ranked ascending, for each sum output

The main problems: because the number is very large, you need to discretization, first off-line discretization, and then directly update, but because of the interval sum, and the number of numbers in the collection will change, so we need to maintain the value of L in each node, the L value is in this node (containing some ascending number), from the X-bit node within the If this node has the number 1 2 3 4 5, the interval is 2,sum[rt][0]=1+3+5,sum[rt][1]=2+4, meaning

So every time the update, is to directly change the value of the update in a single point sum[rt][0], then is a node sum how to update, Sum[rt][i], obviously in the left son inside, need to take sum[lrt][i], for what? Because RT this node and LRT the beginning of this node is together, so the RT node from I, equal to the LRT node starting from I, and then is the RRT node should be from which to start adding, LRT some numbers, and then take the i,i+l ... , look at the last few not taken, and then l this value is RRT inside the beginning, how to ask for this beginning, to record how many values in the LRT node NUM[LRT], and then assume that from the I-Bit and, ((num[lrt]-(i+1))%l+l)%l, This is a description of the last few LRT node is not selected, and then L ((num[lrt]-(i+1)%l+l)%l-1 is in the RRT inside the opening position (position from 0 to L-1), so OK AH;

Maybe it's a little abstract, so you need to draw a good analysis.

#include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <string> #include <vector> #include <cstdio> #include <cctype> #include <cstring># Include <sstream> #include <cstdlib> #include <iostream> #include <algorithm> #pragma comment ( linker, "/stack:102400000,102400000") using namespace std; #define MAX 100005#define MAXN 500005#defin E maxnode 105#define sigma_size 2#define lson l,m,rt<<1#define Rson m+1,r,rt<& Lt;1|1#define LRT rt<<1#define RRT rt<<1|1#define Middle int m= (r+l) >>1 #define LL Long long#define ull unsigned long long#define mem (x,v) memset (x,v,sizeof (x)) #d Efine lowbit (x) (x&-x) #define PII pair<int,int> #define BITS (a) __builtin_popcount (a) #d       Efine Mk Make_pair#define Limit  10000//const int prime = 999983;const int INF = 0x3f3f3f3f;const LL inff = 0x3f3f;const double pi = ACOs ( -1.0); const double inf = 1e18;const double eps = 1e-9;const LL mod = 1e9+7;const ull mx = 1e9+7;/********      /inline void RI (int &x) {char C;      while (C=getchar ()) < ' 0 ' | | c> ' 9 ');      x=c-' 0 '; while ((C=getchar ()) >= ' 0 ' && c<= ' 9 ') x= (x<<3) + (x<<1) +c-' 0 ';} /*****************************************************/char s[max][10];int A[max];int B[MAX];    LL sum[max<<2][5];int numb[max<<2];int n,l;void pushup (int rt) {NUMB[RT]=NUMB[LRT]+NUMB[RRT];    for (int i=0;i<l;i++) {sum[rt][i]=sum[lrt][i]+sum[rrt][l-((numb[lrt]-(i+1))%l+l)%L-1];    }}void Build (int l,int R,int RT) {numb[rt]=0;    for (int i=0;i<l;i++) sum[rt][i]=0;    if (l==r) return;    Middle;    Build (Lson); Build (Rson);}  void update (int l,int r,int rt,int pos,int D) {if (l==r) {      Sum[rt][0]+=d;        if (sum[rt][0]) numb[rt]++;        else numb[rt]--;    return;    } Middle;    if (pos<=m) update (LSON,POS,D);    else update (RSON,POS,D); Pushup (RT);}    int main () {//freopen ("test.txt", "R", stdin);    int kase=0;        while (cin>>n>>l) {int num=0;        kase++;            for (int i=0;i<n;i++) {scanf ("%s%d", S[i],&a[i]);        if (s[i][0]!= ' s ') b[num++]=a[i];        } sort (b,b+num);        int Tot=unique (b,b+num)-B;        Build (1,n,1);        printf ("Case%d:\n", Kase);            for (int i=0;i<n;i++) {if (s[i][0]== ' s ') printf ("%lld\n", sum[1][a[i]-1]);                else{int Pos=lower_bound (b,b+num,a[i])-B;                if (s[i][0]== ' d ') a[i]=-a[i];            Update (1,n,1,pos+1,a[i]); }}} return 0;}



CSU 1724 equal distance and (offline + segment tree)

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.