[Luo gu p1198/bzoj1012] [JSOI2008] Maximum number-tree array/segment tree?

Source: Internet
Author: User

Actually has learned the tree array and the line segment tree, but does not bother to do the question, so so far did not write many blogs

Description

Now you are asked to maintain a sequence that requires the following two actions:

1, query operation.

Syntax: Q L

Function: Query the maximum number of L in the number of the end of the current sequence and output the value of this number.

Limit: l does not exceed the length of the current series. (l>=0)

2, insert operation.

Syntax: A n

function: N plus t, where T is the answer to the most recent query operation (t=0 if the query operation has not been performed), and the resulting result is modeled on a fixed constant d and the resulting answer is inserted at the end of the sequence.

Limit: N is an integer (possibly negative) and within a long range.

Note: The initial sequence is empty and does not have a number.

Input&outputinput

The first line is two integers, M and D, where m represents the number of operations (M <= 200,000), and D is satisfied (0<d<2,000,000,000) as described above

The next M-line, one string per line, describes a specific operation. The syntax is as described above.

Output

For each query operation, you should output the results sequentially, with each result in one row.

Sampleinput
5 100A 96Q 1A 97Q 1Q 2
Output
969396
Solution
  • Originally started to hit the line of the tree, but rokua the strengthening of the data t the last point, so instead of a tree-like array. Because the data is inserted only at the end of the queue, the line tree is a bit overqualified. The tree array can maintain the maximum interval, and the query is updated by continuously updating the RET.
  • 90 Segment Tree ( may also be Ben Konjac Konjac too water ):

    #include <iostream> #include <cstdio> #include <algorithm> #define MAXM 200001using namespace std;   typedef long LONG Ll;struct node{ll mx;int L,r,lc,rc;node () {lc=rc=-1; }}tree[maxm<<1];ll M,d,l,n,sum,t;char c;inline LL Rd () {ll x=0;bool F=0;char C=getchar (); while (c< ' 0 ' | |    C> ' 9 ') {if (c== '-') f=1; C=getchar ();}    while (c>= ' 0 ' &&c<= ' 9 ') {x= (x<<1) + (x<<3) + (c^48); C=getchar ();} return f?-x:x; }int cnt;int rt=cnt++;void pushup (int cur) {int Lc=tree[cur].lc,rc=tree[cur].rc;tree[cur].mx=max (TREE[LC].MX,TREE[RC ].mx); TREE[CUR].L=TREE[LC].L;TREE[CUR].R=TREE[RC].R;}    void build (int l,int r,int cur) {if (l==r) {tree[cur].mx=0;    Tree[cur].l=tree[cur].r=l; return;} int mid= (L+R) >>1;tree[cur].lc=cnt++;tree[cur].rc=cnt++;build (L,MID,TREE[CUR].LC); build (Mid+1,r,tree[cur]. RC);p ushup (cur);}    void upd (int pos,ll c,int cur) {if (TREE[CUR].L==TREE[CUR].R) {tree[cur].mx=c; return;} int mid= (TREE[CUR].L+TREE[CUR].R) >>1;if (pos<=mid) upd (pOS,C,TREE[CUR].LC), if (Pos>mid) upd (pos,c,tree[cur].rc);p ushup (cur);} ll query (int l,int r,int cur) {if (tree[cur].l>=l&&tree[cur].r<=r) {return tree[cur].mx;} int mid= (TREE[CUR].L+TREE[CUR].R) >>1;ll mx=0;if (l<=mid) Mx=max (Mx,query (L,R,TREE[CUR].LC)); if (R>mid) Mx=max (Mx,query (l,r,tree[cur].rc)); return MX;}    int main () {m=rd ();d =rd (); build (1,200000,rt); for (int i=1;i<=m;++i) {cin>>c;        if (c== ' Q ') {l=rd ();        T=query (SUM-L+1,SUM,RT);    printf ("%d\n", t);        } else if (c== ' A ') {n=rd ();        ll tmp= (n%d+t%d)%d;        UPD (SUM+1,TMP,RT);    sum++; }}return 0;}
  • Tree-like array:

      #include <iostream> #include <cstdio> #include <algorithm> #define MAXN 200005using namespace Std;typedef long long Ll;ll mx (ll a,ll b) {return (a>b)? A:b;} inline LL Rd () {ll x=0;char C=getchar (); bool F=false;while (c< ' 0 ' | |    C> ' 9 ') {if (c== '-') f=true; C=getchar ();}    while (c>= ' 0 ' &&c<= ' 9 ') {x= (x<<1) + (x<<3) + (c^48); C=getchar (); }return f?-x:x; }ll b[maxn],d;int sum;int lowbit (int x) {return x&-x;} int Add (ll v) {for (int x=sum;x;x-=lowbit (x)) b[x]=mx (b[x],v);} int query (int pos) {ll ans=0;for (int x=sum-pos+1;x<=sum;x+=lowbit (x)) ans=mx (ans,b[x]); return ans;    int main () {int M,p;char q;ll t=0;scanf ("%d%lld", &m,&d); for (int i=1;i<=m;++i) {cin>>q;        if (q== ' A ') {sum++;        ll N;        scanf ("%lld", &n);    Add ((n+t)%d);        } else{scanf ("%d", &p);        T=query (P);    printf ("%lld\n", t); }}return 0;}  

    wrote a quick read, but it didn't work.

[valley p1198/bzoj1012][jsoi2008] Maximum number-tree array/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.