Daily Division
时间限制: 1 Sec 内存限制: 128 MB
题目描述
Oostende Beach is a very long beach located in the north of Belgium. On this beach, there are n huts located along a straight line. People can rent a room in one of those huts to spend their beach vacations together with the other
tenants.
Every day at lunch time, a food truck rides by to serve fries to the guests. The truck will park in front of one of the huts and people will form two queues. The people staying in huts to the left of the food truck will queue on the left,and the people to the right of the food truck will queue together on the right. The people staying in the hut in front of the food truck will split their group in half, one half going to the left queue and the other half going to the right queue. If this is an odd number of people,the remaining person will go to the queue with fewer people, or choose one randomly if the queues have the same length. The food truck will always position itself so that the difference between the number of people in the left queue and the number of people in the right queue is as small as possible.
Each night the number of guests in exactly one of the huts changes. Can you help the food truck find the best position for each day?
输入
• The first line of the input consists of two integers 1 ≤ n ≤ 105, the number of huts, and 1 ≤ q ≤ 105, the number of days.
• The second line has n integers a0, . . . , an−1 satisfying 1 ≤ ai ≤ 106 for 0 ≤ i < n, where ai is the current number of people in hut i.
• Then follow q lines with two integers 0 ≤ i < n and 1 ≤ x ≤ 106. The jth of these lines indicates that at day j the number of people in hut i changes to x .
输出
Print q lines: the optimal position of the foodtruck after each of the q nights. If there are multiple optimal positions, print the smallest one.
样例输入
5 4
3 1 3 4 2
0 5
0 9
4 5
2 1
样例输出
2
1
2
1
我们使用树状数组维护区间和,每次询问二分序列,如果左边和>右边就往左找,否则往右,最后解是l
l-1,l+1三者最小值。(这里和普通二分不一样,因为,没有必要保证和左边大于右边或者小于,直接让差值最小即可)
下面是ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#define ll long long
using namespace std;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
const int N=1e6+10;
ll n,q,a[N],c[N],mx;
int lowbit(int x){
return x&-x;
}
void add(int x, ll y)
{
for (;x <= N-2; x += x & -x) c[x] += y;
}
ll sum(int x)
{
ll ans = 0;
for (; x; x -= x &-x) ans += c[x];
return ans;
}
ll gg(int x)
{
ll l=(x==1?0:sum(x-1));
ll r=(x==n?0:sum(n)-sum(x));
if((sum(x) - sum(x-1))&1)
{
if(l<=r) l++;
else r++;
}
return abs(l-r);
}
int check(int x){
ll l=(x==1?0:sum(x-1));
ll r=(x==n?0:sum(n)-sum(x));
if((sum(x) - sum(x-1))&1)
{
if(l<=r) l++;
else r++;
}
return l<=r;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
mx=max(mx,a[i]);
}
for(int i=1;i<=n;i++) add(i,a[i]);
while(q--){
int i,x;
scanf("%d%d",&i,&x);
add(i+1,-sum(i+1)+sum(i));
add(i+1,x);
int l=1,r=n;
while(r - l > 1){
int mid=(l+r)>>1;
if(check(mid)) l=mid;
else r=mid;
}
ll val = lnf;
int gh = l;
ll t = l-1<1?lnf:gg(l-1), h = gg(l), f =l+1>n?lnf:gg(l+1);
if (val > t)
{
val = t;
l = gh-1;
}
if (val > h)
{
val = h;
l = gh;
}
if (val > f)
{
val = f;
l = gh+1;
}
printf("%d\n",l-1);
}
return 0;
}