Portal
Tree-like array template title. Note the optimization, assuming that the value of a number is already 1. Then we will not have to operate on him, this can be used and check the set implementation.
There is also a hole in the problem, give the query interval endpoint of a, B, it is possible to a>b.
#include <cstdio> #include <cmath> #include <cctype> #include <iostream>using namespace std; inline void GET (int &t) {char C; R=v; Do{c=getchar ();} while (!isdigit (c)); while (IsDigit (c)) {t=t*10+c-' 0 '; C=getchar ();}} inline void GET (long long &t) {char C; R=v; Do{c=getchar ();} while (!isdigit (c)); while (IsDigit (c)) {t=t*10+c-' 0 '; C=getchar ();}} Long Long tree[100005],c[100005];int fa[100005];int n,m;int root (int x) {if (fa[x]==x) return x; else return Fa[x]=root (Fa[x]);} int Lowbit (long long x) {return x&-x;} void Updata (int pos,long long val) {while (pos<=n) {tree[pos]+=val; Pos+=lowbit (POS); }}long long getsum (int pos) {long long sum=0; while (pos>0) {Sum+=tree[pos]; Pos-=lowbit (POS); } return sum;} int main () {GET (n); for (int i=1;i<=n;i++) {GET (c[i]); Updata (I,c[i]); Fa[i]=i; } GET (M); int k,l,r; for (int i=1;i<=m;i++) {GET (k); GET (l); GET (R); if (R < L) Swap (l,r); if (k==1) printf ("%lld\n", Getsum (R)-getsum (L-1)); else {for (int j=root (l); J<=r;j=root (j+1)) {if (j==0) BR Eak Updata (J,-c[j]); C[J]=SQRT (C[j]); Updata (J,c[j]); if (c[j]==1) fa[j]=j+1; } } }}
CodeVS2492 Seven minutes of God's Problem 2 (tree array + and search)