首页 > 代码库 > UVA 12663 第九届省赛 高桥与低桥 树状数组

UVA 12663 第九届省赛 高桥与低桥 树状数组

 1 #include <cstdio> 2 #include <math.h> 3 #include <iostream> 4 #include <cstring> 5 #include <cstdlib> 6 #include <algorithm> 7 using namespace std; 8  9 const int maxn = 1e5+10;10 int c[maxn], a[maxn];11 int lowbit(int p) {12     return p&(-p);13 }14 void update(int p, int x) {15     while(p<maxn) {16         c[p] += x;17         p += lowbit(p);18     }19 }20 int query(int p) {21     int sum = 0;22     while(p>0) {23         sum += c[p];24         p -= lowbit(p);25     }26     return sum;27 }28 int main() {29     //freopen("test.txt", "r", stdin);30     int n, m, k, x, y, t=1;31     while(scanf("%d %d %d", &n, &m, &k) != EOF) {32         memset(c, 0, sizeof(c));33         for(int i=1; i<=n; i++) scanf("%d", &a[i]);34         sort(a+1, a+n+1);35         int cur = 0;36         for(int i=1; i<=m; i++) {37             scanf("%d %d", &x, &y);38             int l = upper_bound(a+1, a+n+1, cur+1) -a;39             int r = upper_bound(a+1, a+n+1, x+1) - a;40             update(l, 1); update(r, -1);41             cur = y;42         }43         int ans = 0;44         for(int i=1; i<n; i++) {45             int f = upper_bound(a+1, a+n+1, a[i]) - a;46             if(query(f)>=k) ans++;47         }48         printf("Case %d: %d\n", t++, ans);49     }50     return 0;51 }
View Code

   这个树状数组和离散化真心优美。。。。。 感谢一下yi教!萌萌哒~~~

UVA 12663 第九届省赛 高桥与低桥 树状数组