3211: The flower God travels the country time Limit:5 Sec Memory limit:128 MB
submit:4057 solved:1480
[Submit] [Status] [Discuss] Description
Input
Output
Each time x=1, an integer per line indicates the pleasure of this trip
Sample Input4
1 100 5 5
5
1 1 2
2 1 2
1 1 2
2 2 3
1 1 4
Sample Output101
11
11
HINT
For 100% data, n≤100000,m≤200000, Data[i] is non-negative and less than 10^9
Source
SPOJ2713 GSS4 data has been enhanced
Because it is a root, so the number of open times not more than logn times, with a tree-like array to maintain the prefix and, for the modification operation into a single point of modification.
The closest non-1 node before the current node is maintained with and checked.
Complexity O (nlog2n)
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <cstdlib>5#include <cmath>6#include <algorithm>7 using namespacestd;8 intRead () {9 intx=0;CharCh=GetChar ();Ten while(!isdigit (CH)) ch=GetChar (); One while(IsDigit (CH)) {x=x*Ten+ch-'0'; ch=GetChar ();} A returnx; - } - intn,m; the Long Longsum[100005]; - Long Longa[100005]; - inteax100005]; - intFindintx) {returnfa[x]==x?fa[x]:fa[x]=find (Fa[x]);} + intLowbit (intx) {returnx& (-x);} - voidAddintXLong Longval) { + for(intI=x;i<=n;i+=lowbit (i)) sum[i]+=Val; A } at voidChangeintXLong Longval) { - for(intI=x;i<=n;i+=lowbit (i)) sum[i]=sum[i]-val+ (Long Long) sqrt (val); - } - Long LongQueryintx) { - Long Longans=0; - for(intI=x;i>0; I-=lowbit (i)) ans+=Sum[i]; in returnans; - } to intMain () { +n=read (); - for(intI=1; i<=n;i++) {a[i]=read (); add (I,a[i]);} the for(intI=1; i<=n;i++) fa[i]=i; *m=read (); $ while(m--) {Panax Notoginseng intX=read (), L=read (), r=read (); - if(x==2) { the intnow=find (R); + while(now>=l) { A Change (Now,a[now]); thea[now]=sqrt (A[now]); + if(a[now]<=1) Fa[now]=find (fa[now-1]); -Now=find (fa[now-1]); $ } $ } - Else { -printf"%lld\n", query (R)-query (l1)); the } - }Wuyi return 0; the}
View Code
[BZOJ3211] Flower God travels to all countries &&[bzoj3038] seven minutes of God's Problem 2 tree-like array + and check set