nfls d5t1.
扔到前缀和数组上考虑,就是找一个 使得 。
然后显然这个东西是成倍增长的,所以二进制下最高位相同的 只有最小的那个有可能对下面产生二倍杀从而造成贡献。
开个线段树,枚举阈值每次线段树二分找到这样的 进行 check 即可。
时间复杂度 。
赛时还胡了个不用倍增性质的。纯粹是第一时间不知道怎么用倍增性质,蠢毙了。
考虑对前缀和的单个差分思考,我们要让前面一个点变得和差分一样。
每次操作是后缀加减,然后单点修改,显然可以分块维护。
每个块内元素和目标做差,然后再和懒标记做差即可,每个块开个哈希表的话就是 。
非常难写,写一半想到了第一个做法,果断重构。
#include<bits/stdc++.h>
#define N 200005
#define int long long
struct IO {
static const int inSZ = 1 << 17;
char inBuf[inSZ], *in1, *in2;
template<class T> inline __attribute((always_inline))
T read() {
if (in1 > inBuf + inSZ - 32) [[unlikely]] {
auto len = in2 - in1;
memcpy(inBuf, in1, len);
in1 = inBuf, in2 = inBuf + len;
in2 += fread(in2, 1, inSZ - len, stdin);
if (in2 != inBuf + inSZ) *in2 = 0;
}
T res = 0;
unsigned char c;
bool neg = 0;
while ((c = *in1++) < 48) neg = c == 45;
while (res = res * 10 + (c - 48), (c = *in1++) >= 48);
return neg ? -res : res;
}
static const int outSZ = 1 << 21;
char outBuf[outSZ], *out;
template<class T> inline __attribute((always_inline))
void write(T x) {
if (out > outBuf + outSZ - 32) [[unlikely]]
{
fwrite(outBuf, 1, out - outBuf, stdout), out = outBuf;
}
if (!x) return *out++ = 48, void();
if (x < 0) *out++ = 45, x = -x;
alignas(2) const char* digits =
"0001020304050607080910111213141516171819"
"2021222324252627282930313233343536373839"
"4041424344454647484950515253545556575859"
"6061626364656667686970717273747576777879"
"8081828384858687888990919293949596979899";
alignas(64) static char buf[20];
char* p = buf + 20;
while (x >= 10) memcpy(p -= 2, digits + x % 100 * 2, 2), x /= 100;
if (x) *--p = 48 + x;
auto len = buf + 20 - p;
memcpy(out, p, len), out += len;
}
void init() {
in1 = in2 = inBuf + inSZ;
out = outBuf;
}
~IO() { fwrite(outBuf, 1, out - outBuf, stdout); }
} IO;
template<class T = int> inline T read() {
return IO.read<T>();
}
template<class T> inline void print(T x, char c) {
IO.write(x), *IO.out++ = c;
}
template<class T> inline void print(T x) {
IO.write(x);
}
inline __attribute((always_inline)) void printCh(char c) {
*IO.out++ = c;
}
using namespace std;
int a[N],qzh[N];
int n,q;
struct seg_tr{
int tr[(N<<2)+5],lz[(N<<2)+5];
void pushup(int x){
tr[x]=tr[x<<1]+tr[(x<<1)+1];
}
void build(int u,int l,int r,int a[]){
if(l==r){
tr[u]=a[l];
return;
}
int mid=(l+r)>>1;
build(u<<1,l,mid,a);
build((u<<1)+1,mid+1,r,a);
pushup(u);
}
void pushdown(int x,int l,int r){
int mid=(l+r)>>1;
tr[x<<1]+=lz[x]*(mid-l+1);
tr[(x<<1)+1]+=lz[x]*(r-mid);
lz[(x<<1)+1]+=lz[x];
lz[x<<1]+=lz[x];
lz[x]=0;
}
void upd(int u,int l,int r,int t,int x){
if(t<=l&&r<=t){
tr[u]+=x*(r-l+1);
lz[u]+=x;
return;
}
pushdown(u,l,r);
int mid=(l+r)>>1;
if(t<=mid)upd(u<<1,l,mid,t,x);
if(t>mid)upd((u<<1)+1,mid+1,r,t,x);
pushup(u);
}
int qry(int u,int l,int r,int t,int sum){
if(l==r)return (a[l]==sum?l:-1);
pushdown(u,l,r);
int mid=(l+r)>>1;
if(tr[u<<1]>=t)return qry(u<<1,l,mid,t,sum);
else return qry(u<<1|1,mid+1,r,t-tr[u<<1],sum+tr[u<<1]);
}
}tr;
signed main(){
// freopen("king.in","r",stdin);
// freopen("king.out","w",stdout);
IO.init();
n=read(),q=read();
for(int i=1;i<=n;i++)
a[i]=read(),qzh[i]=qzh[i-1]+a[i];
tr.build(1,1,n,a);
while(q--){
int id=read(),x=read();
tr.upd(1,1,n,id,x-a[id]);
a[id]=x;
if(a[1]==0){
print(1,'\n');
continue;
}
bool flg=1;
for(int i=1;i<=200000000000000;i<<=1){
int flc=tr.qry(1,1,n,i,0);
if(flc!=-1){
print(flc,'\n');
flg=0;
break;
}
}
if(flg)print(-1,'\n');
}
return 0;
}
// 魔法师小姐又把爆米花拋进嘴里,津津有味地边嚼边说:
//「所以说,这个国家的艺人才会争相参加,挑战他们。」嚼嚼嚼。
//「然后,因为能免费看表演,所以观众才全部聚集在这里吗?」
//「差不多就是这样吧。」嚼嚼嚼。
//「原来如此原来如此。」
//「你还有别的问题吗?」嚼嚼嚼。
//「你那个爆米花是在哪里买的?」nfls 2log 卡常丧心病狂????
