4299: Codechef FRBSUM

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\n",ans);
57     }
58     return 0;
59 }

 

转载于:https://www.cnblogs.com/mjtcn/p/9361100.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值