ZOJ 3279

时间:2021-02-28 19:55:01

线段树单点更新

//============================================================================
// Name : E.cpp
// Author : L_Ecry
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 100050
using namespace std;
int sum[N*];
int a[N];
void build(int l,int r,int i)
{
if(l==r)
{
sum[i]=a[l];
return ;
}
int mid=(l+r)>>;
build(l,mid,i<<);
build(mid+,r,i<<|);
sum[i]=sum[i<<]+sum[i<<|];
}
void update(int l,int r,int p,int va,int i)
{
if(l==r)
{
sum[i]=va;
return ;
}
int mid=(l+r)>>;
if(p<=mid)update(l,mid,p,va,i<<);
else update(mid+,r,p,va,i<<|);
sum[i]=sum[i<<]+sum[i<<|];
}
int query(int l,int r,int va,int i)
{
if(l==r)
{
return l;
}
int mid=(l+r)>>;
if(va<=sum[i<<])return query(l,mid,va,i<<);
else return query(mid+,r,va-sum[i<<],i<<|);
}
int n,m;
void init()
{
for(int i=;i<=n;++i)
scanf("%d",&a[i]);
build(,n,);
}
void solve()
{
scanf("%d",&m);
while(m--)
{
char c;
int x,y;
scanf(" %c",&c);
if(c=='q')
{
scanf("%d",&x);
printf("%d\n",query(,n,x,));
}
else
{
scanf("%d%d",&x,&y);
update(,n,x,y,);
}
}
}
int main() {
while(scanf("%d",&n)!=EOF)
{
init();
solve();
}
return ;
}