hdu 1556 Color the ball
If you want to change the interval, then the node must be updated, the query is added up. (interval changes.) Single-point query)
#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;const int maxn=100000+5 ; int c[maxn];int n;int lowbit (int x) { return (-X) &x;} void update (int x,int y) {for (int i=x; i>0;i-=lowbit (i)) c[i]+=y;} int query (int x) { int s=0; for (int k=x; k<=n; k+=lowbit (k)) s+=c[k]; return s;} int main () { int a A, B; while (Cin>>n) {for (int i=1; i<=n; i++) c[i]=0; for (int h=0;h<n;h++) { scanf ("%d%d", &a,&b); Update (b,1); Update (A-1,-1); } for (int j=1;j<=n;j++) if (j==n) printf ("%d\n", Query (j)); else printf ("%d", query (j)); printf ("%d%c", Query (i), i==n? ') \ n ': '); } return 0;}
CSU 1335 Takahashi and Low bridge (tree-like array + dichotomy)
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int N = 1e5+10;int c[n],m,n,k,a[n];int x[n],y[n];int lowbit (int k) {return k& (-K);} void Add (int k,int He) {while (k>0) {c[k]+=he; K-=lowbit (k); }}int Q (int k) {int query=0; while (k<=n) {query+=c[k]; K+=lowbit (k); } return query; int main () {#ifndef Online_judge freopen ("In.cpp", "R", stdin); #endif//Online_judge int t,from,to,he,kkk=1; while (~SCANF ("%d%d%d", &n,&m,&k)) {memset (c,0,sizeof (c)); for (int i=0;i<n;i++) scanf ("%d", &a[i]); Sort (a,a+n); int cur=1; int s,t; for (int i=1;i<=m;i++) {scanf ("%d%d", &s,&t); int From=upper_bound (a,a+n,cur)-A; int To=upper_bound (a,a+n,s)-A; Add (from,-1); Add (to,+1); cur=t; } int ans = 0; for (int i=1;i<=n;i++) {int KK =q (i); if (kk>=k) ans = ans+1; } printf ("Case%d:%d\n", Kkk++,ans); } return 0;}
Related Topics: Hdu A simple problem with integers
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Variable interval tree array, single point query