4299: Codechef FRBSUM
https://www.lydsy.com/JudgeOnline/problem.php?id=4299
分析:
主席树。
https://blog.sengxian.com/solutions/bzoj-4408
代码:
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 #include<iostream> 6 #include<cctype> 7 using namespace std; 8 typedef long long LL; 9 10 inline int read() { 11 int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1; 12 for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f; 13 } 14 15 const int N = 100100; 16 17 int ls[N*31], rs[N*31], sum[N*31], Root[N], CntNode; 18 19 void update(int l,int r,int &rt,int last,int p) { 20 rt = ++CntNode; 21 sum[rt] = sum[last] + p; 22 if (l == r) return; 23 int mid = (l + r) >> 1; 24 if (p <= mid) { 25 rs[rt] = rs[last]; 26 update(l, mid, ls[rt], ls[last], p); 27 } 28 else { 29 ls[rt] = ls[last]; 30 update(mid+1, r, rs[rt], rs[last], p); 31 } 32 } 33 int query(int l,int r,int Head,int Tail,int x) { 34 if (l == r) { 35 return sum[Tail] - sum[Head]; 36 } 37 int mid = (l + r) >> 1; 38 if (x <= mid) return query(l, mid, ls[Head], ls[Tail], x); 39 else return sum[ls[Tail]] - sum[ls[Head]] + query(mid+1, r, rs[Head], rs[Tail], x); 40 } 41 int main () { 42 43 int n = read(); 44 for (int i=1; i<=n; ++i) { 45 int x = read(); 46 update(1, 1e9, Root[i], Root[i-1], x); 47 } 48 int m = read(); 49 while (m -- ) { 50 int l = read(), r = read(), ans = 1, tmp; 51 while (true) { 52 tmp = query(1, 1e9, Root[l-1], Root[r], ans); 53 if (tmp < ans) break; 54 ans = tmp + 1; 55 } 56 printf("%d ",ans); 57 } 58 return 0; 59 }