Revenge of kNN II
Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 179 Accepted Submission(s): 51
Problem Description
In pattern recognition, the k-Nearest Neighbors algorithm (or k-NN for short) is a non-parametric method used for classification and regression. In both cases, the input consists of the k closest training examples in the feature space.
In k-NN regression, the output is the property value for the object. This value is the average of the values of its k nearest neighbors.
---Wikipedia
Today, kNN takes revenge on you, again. You have to handle a kNN case in one-dimensional coordinate system. There are N points with a position Xi and value Vi. Then there are M kNN queries for point with index i, recalculate its value by averaging the values its k-Nearest Neighbors. Note you have to replace the value of i-th point with the new calculated value. And if there is a tie while choosing k-Nearest Neighbor, choose the one with the minimal index first.
(Have you ever tried the problem “Revenge of kNN”? They are twin problems!)
In k-NN regression, the output is the property value for the object. This value is the average of the values of its k nearest neighbors.
---Wikipedia
Today, kNN takes revenge on you, again. You have to handle a kNN case in one-dimensional coordinate system. There are N points with a position Xi and value Vi. Then there are M kNN queries for point with index i, recalculate its value by averaging the values its k-Nearest Neighbors. Note you have to replace the value of i-th point with the new calculated value. And if there is a tie while choosing k-Nearest Neighbor, choose the one with the minimal index first.
(Have you ever tried the problem “Revenge of kNN”? They are twin problems!)
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case begins with two integers N and M. Then N lines follows, each line contains two integers Xi and Vi. Then M lines with the queried index Qi and Ki follows, in which Ki indicating the number of k-Nearest Neighbors
[Technical Specification]
1. 1 <= T <= 5
2. 2 <= N <= 100 000
3. 1 <= M <= 100 000
4. 1 <= Vi <= 1 000
5. 1 <= Xi <= 1 000 000 000, and no two Xi are identical.
6. 1 <= Qi <= N
7. 1 <= Ki <= N - 1
Each test case begins with two integers N and M. Then N lines follows, each line contains two integers Xi and Vi. Then M lines with the queried index Qi and Ki follows, in which Ki indicating the number of k-Nearest Neighbors
[Technical Specification]
1. 1 <= T <= 5
2. 2 <= N <= 100 000
3. 1 <= M <= 100 000
4. 1 <= Vi <= 1 000
5. 1 <= Xi <= 1 000 000 000, and no two Xi are identical.
6. 1 <= Qi <= N
7. 1 <= Ki <= N - 1
Output
For each test case, output sum of all queries rounded to three fractional digits.
Sample Input
1 5 3 1 2 2 3 3 6 4 8 5 8 2 2 3 2 4 2
Sample Output
17.000HintFor the first query, the 2-NN for point 2 is point 1 and 3, so the new value is (2 + 6) / 2 = 4. For the second query, the 2-NN for point 3 is point 2 and 4, and the value of point 2 is changed to 4 by the last query, so the new value is (4 + 8) / 2 = 6. Huge input, faster I/O method is recommended./*首先我们将点按照横坐标从小到大排序, 然后对于每次查询,我们先二分距离mid, 然后再二分查找在X-mid,X+mid里面有多少数, 如果小于K则抬升下界,如果大于K+1则降低上界, 如果等于K则直接更新,还有就是正好等于K+1的时候, 看最两端到底哪个应该被排除。 更新值以及区间求和用树状数组维护就好了。*/ #include<stdio.h> #include<string.h> #include<algorithm> #define N 100005 using namespace std; struct node { int x,id; double v; }a[N]; double c[N]; int n,mark[N]; bool cmp(node a,node b) { return a.x<b.x; } int lowbit(int x) { return x&(-x); } void update(int x,double num) { while(x<=n) { c[x]+=num; x+=lowbit(x); } } int x_find(int x) { int l=1,r=n,ans; while(l<=r) { int mid=(l+r)>>1; if(a[mid].x>=x) { ans=mid; r=mid-1; } else l=mid+1; } return ans; } int y_find(int x) { int l=1,r=n,ans; while(l<=r) { int mid=(l+r)>>1; if(a[mid].x<=x) { ans=mid; l=mid+1; } else r=mid-1; } return ans; } double getsum(int x) { double sum=0; while(x) { sum+=c[x]; x-=lowbit(x); } return sum; } int main() { int t,i,u,x,y,l,r,m,q,k; double s,aa; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { scanf("%d%lf",&a[i].x,&a[i].v); a[i].id=i; } memset(c,0,sizeof(c)); sort(a+1,a+1+n,cmp); for(i=1;i<=n;i++) { mark[a[i].id]=i; update(i,a[i].v); } s=0; for(i=1;i<=m;i++) { scanf("%d%d",&q,&k); u=mark[q]; l=1; r=a[n].x; while(l<=r) { int mid=(l+r)>>1; x=x_find(a[u].x-mid); y=y_find(a[u].x+mid); if(y-x<k) l=mid+1; else if(y-x>k+1) r=mid-1; else if(y-x==k) { aa=(getsum(y)-getsum(x-1)-a[u].v)/k; s+=aa; update(u,-a[u].v); update(u,aa); a[u].v=aa; break; } else if(y-x==k+1) { if(a[u].x-a[x].x==a[y].x-a[u].x) { if(a[x].id<a[y].id) y--; else x++; } else if(a[u].x-a[x].x<a[y].x-a[u].x) y--; else x++; aa=(getsum(y)-getsum(x-1)-a[u].v)/k; s+=aa; update(u,-a[u].v); update(u,aa); a[u].v=aa; break; } } } printf("%.3lf\n",s); } return 0; }