Zoj 3279 [tree array + binary]

Source: Internet
Author: User

Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 3618

It hurts. Use CIN to enter the letter "tle !!!

#include<iostream>#include<vector>#include<map>#include<stack>#include<algorithm>#include<queue>#include<list>#include<set>#include<string.h>#include<stdlib.h>#include<math.h>#include<stdio.h>#include<ctype.h>#include<iomanip>using namespace std;#define LL long long#define pi acos(-1)#define N 100010#define INF 999999999#define eps 1e-8//****************************************//zoj 3279//Copyright@leolin All rights reserved.//****************************************int level[N],tree[N];int n;int lowbit(int t){    return t&(-t);}void update(int pos,int num){    while(pos<=n)    {        tree[pos]+=num;        pos+=lowbit(pos);    }}int sum(int i){    int tot=0;    while(i>0)    {        tot+=tree[i];        i-=lowbit(i);    }    return tot;}int find(int a){    int i,j,k;    int low=1,up=n,mid;    while(low<=up)    {        mid=(low+up)>>1;        if(sum(mid)>=a)        {            up=mid-1;        }        else        low=mid+1;    }    return low;}char str[10];char c[100];int main(){    //freopen("a.txt","r",stdin);    while(scanf("%d",&n)!=EOF)    {        int i;        for(i=0;i<=n;i++)tree[i]=0;        for(i=1;i<=n;i++)        {            scanf("%d",&level[i]);            update(i,level[i]);        }        int m;        scanf("%d",&m);        while(m--)        {            scanf("%s",str);            if(str[0]=='p')            {                int a,b;                scanf("%d%d",&a,&b);                update(a,-level[a]);                level[a]=b;                update(a,level[a]);            }            if(str[0]=='q')            {                int a;                scanf("%d",&a);                printf("%d\n",find(a));            }        }    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.