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