标签:des style blog color os io for div
线段树单点更新
//============================================================================ // 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*4]; int a[N]; void build(int l,int r,int i) { if(l==r) { sum[i]=a[l]; return ; } int mid=(l+r)>>1; build(l,mid,i<<1); build(mid+1,r,i<<1|1); sum[i]=sum[i<<1]+sum[i<<1|1]; } void update(int l,int r,int p,int va,int i) { if(l==r) { sum[i]=va; return ; } int mid=(l+r)>>1; if(p<=mid)update(l,mid,p,va,i<<1); else update(mid+1,r,p,va,i<<1|1); sum[i]=sum[i<<1]+sum[i<<1|1]; } int query(int l,int r,int va,int i) { if(l==r) { return l; } int mid=(l+r)>>1; if(va<=sum[i<<1])return query(l,mid,va,i<<1); else return query(mid+1,r,va-sum[i<<1],i<<1|1); } int n,m; void init() { for(int i=1;i<=n;++i) scanf("%d",&a[i]); build(1,n,1); } 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(1,n,x,1)); } else { scanf("%d%d",&x,&y); update(1,n,x,y,1); } } } int main() { while(scanf("%d",&n)!=EOF) { init(); solve(); } return 0; }
标签:des style blog color os io for div
原文地址:http://www.cnblogs.com/L-Ecry/p/3872358.html