hdu 3074 线段树模板题

线段树经典题目……

/*
线段树单点更新 节点记录元素个数
查找第K元素所在
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#define lson l , mid , dex<<1
#define rson mid + 1 , r , dex<<1|1
#define havemid int mid = (l + r) >> 1
#define LL long long
const double eps = 1e-8;
const double PI = 2.0 * asin(1.0);
using namespace std;
const int Max = 100010;
LL sum[Max<<2];
void Push_up(int dex)
{
    sum[dex] = sum[dex<<1] + sum[dex<<1|1];
}
void update(LL pos , LL change , int l , int r , int dex)
{
    if(l == r)
    {
        sum[dex] = change;
        return ;
    }
    havemid;
    if(pos <= mid)  update(pos , change , lson);
    else            update(pos , change , rson);
    Push_up(dex);
}
int query(int pos , int l , int r , int dex)
{
//    cout<<"l: "<<l<<" r:"<<r<<" pos:"<<pos<<endl;
    if(l == r)  return l;
    havemid;
    if(sum[dex<<1] >= pos)  return query(pos , lson);
    else
    {
//        pos -= sum[dex<<1];
        return query(pos - sum[dex<<1], rson);
    }
}
int main()
{
//    freopen("3279.txt","r",stdin);
    int n;
    while(~scanf("%d",&n))
    {
        memset(sum , 0 , sizeof(sum));
        for(int i=1; i<=n ;i++)
        {
            LL a;
            scanf("%lld",&a);
            update(i , a , 1 , n , 1);
        }
        int q;
        scanf("%d",&q);
        while(q--)
        {
            char ch[5];
            int a;
            LL b;
            scanf("%s",ch);
            if(ch[0] == 'q')
            {
                scanf("%d",&a);
                printf("%d\n",query(a , 1 , n , 1));
            }
            else
            {
                scanf("%d %lld",&a, &b);
                update(a , b , 1 , n , 1);
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值