Data structures that can be persisted

Source: Internet
Author: User

data structures that can be persisted

Title Link: http://acm.xidian.edu.cn/problem.php?id=1181

Using vectors to achieve persistence

This requires a persistent data structure that supports interval queries. Here, using the vector cleverly implements the:pair<time,value> with the pair to store the timestamp and the current time value, the query when using two points to find.

The code is as follows:

1#include <cstdio>2#include <vector>3#include <iostream>4#include <algorithm>5 #defineX First6 #defineY Second7 #defineN 10000058 using namespacestd;9typedef pair<int,int>P;Ten intN,q,times,type,c,arr[n]; OneVector<p>A[n]; AVector<p>:: iterator it; - intLowbit (intx) { -     returnx&-x; the } - intQueryintLintRintt) { -     intans=0; -      for(intI=r;i>0; i-=lowbit (i)) { +It=upper_bound (A[i].begin (), A[i].end (), Make_pair (T,0)); -         if(It!=a[i].end () &&it->x==t) ans+=it->Y; +         Else if(it!=A[i].begin ()) { Ait--; atAns+=it->Y; -         } -     } -      for(inti=l-1;i>0; i-=lowbit (i)) { -It=upper_bound (A[i].begin (), A[i].end (), Make_pair (T,0)); -         if(It!=a[i].end () &&it->x==t) ans-=it->Y; in         Else if(it!=A[i].begin ()) { -it--; toAns-=it->Y; +         } -     } the     returnans; * } $ voidAddintIndexintaddition) {Panax Notoginseng     intPre; -      for(inti=index;i<=n;i+=lowbit (i)) { the         if(A[i].empty ()) pre=0; +         ElsePre=A[i].back (). Y; AA[i].push_back (Make_pair (times,pre+addition)); the     } + } - intMainvoid){ $scanf"%d%d",&n,&q); $      while(q--){ -scanf"%d",&type); -         if(type==1){ thetimes++; -             inti,x;Wuyiscanf"%d%d",&i,&x); theAdd (i^c,x); -arr[i^c]+=x; Wuc=arr[i^c]; -}Else if(type==2){ About             intl,r,t; $scanf"%d%d%d",&l,&r,&t); -C=query (l^c,r^c,t); -printf"%d\n", c); -         } A     } +}

Data structures that can be persisted

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.