hdu 4027 Can you answer these queries? The 36th ACM/ICPC Asia Regional Shanghai Site

3570 단어 structtreequeryBuild
제목 연결:http://acm.hdu.edu.cn/showproblem.php?pid=4027
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
경 기 를 할 때 반드시 업데이트 해 야 한 다 는 것 을 알 았 지만 어떻게 업데이트 해 야 할 지 생각 하지 못 해 한 번 에 업 데 이 트 했 습 니 다. 과감 한 TLE, 사실은 만 알 면 됩 니 다.int 64 범위 내의 수 는 최대 8 번 을 취하 면 1 또는 0 에 이 를 수 있 습 니 다. 그러면 생각 이 있 습 니 다. 즉, 모든 수의 i 번 뿌리 를 미리 구 하 는 것 입 니 다. 나 무 를 만 드 는 과정 에서 직접 업데이트 하고 업데이트 하 는 것 입 니 다. 그러면 매번 업데이트 할 때마다 뿌리 를 얻 는 횟수 만 업데이트 하면 됩 니 다.
코드 는 다음 과 같 습 니 다:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

const int maxx=100002;

struct Tree
{
    __int64 sum[9];
    int cnt;
    int col;
};

__int64 da[maxx],mx;
int lim;

Tree tree[maxx<<2];

void pushUp(int rt)
{
    for (int i=0;i<=lim;i++)
    {
        tree[rt].sum[i]=tree[rt<<1].sum[i]+tree[rt<<1|1].sum[i];
    }
}

void pushDown(int rt)
{
    if (tree[rt].col!=0)
    {
        tree[rt<<1].cnt+=tree[rt].col;
        tree[rt<<1].col+=tree[rt].col;
        tree[rt<<1|1].cnt+=tree[rt].col;
        tree[rt<<1|1].col+=tree[rt].col;
        tree[rt].col=0;
    }
}

void update(int L,int R,int l,int r,int rt)
{
    if (L<=l && r<=R)
    {
        tree[rt].col+=1;
        tree[rt].cnt+=1;
        return;
    }
    pushDown(rt);
    int m=(l+r)>>1;
    if (L<=m)update(L,R,lson);
    if (R>m)update(L,R,rson);

}

void build(int l,int r,int rt)
{
    tree[rt].cnt=tree[rt].col=0;
    if (l==r)
    {
        tree[rt].sum[0]=da[l];
        for (int i=1;i<=lim;i++)
        {
            tree[rt].sum[i]=(__int64)sqrt((double)tree[rt].sum[i-1]);
        }
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushUp(rt);
}

__int64 query(int L,int R,int l,int r,int rt)
{
    if (L<=l && r<=R)
    {
        if (tree[rt].cnt>lim)tree[rt].cnt=lim;
        if (tree[rt].cnt==lim || l==r)
        {
            return tree[rt].sum[tree[rt].cnt];
        }
        pushDown(rt);
        int m=(l+r)>>1;
        return query(L,R,lson)+query(L,R,rson);
    }
    pushDown(rt);
    int m=(l+r)>>1;
    __int64 ret=0;
    if (L<=m) ret+=query(L,R,lson);
    if (R>m) ret+=query(L,R,rson);
    return ret;
}

int main()
{
    int n,o,cas=1;
    while (scanf("%d",&n)!=EOF)
    {
        mx=0;
        int i;
        printf("Case #%d:
",cas++); for (i=1;i<=n;i++) { scanf("%I64d",&da[i]); if (mx<da[i])mx=da[i]; } for (lim=1;mx>1;mx=(__int64)sqrt((double)mx),lim++); build(1,n,1); scanf("%d",&o); for (i=1;i<=o;i++) { int x,y,mark; scanf("%d%d%d",&mark,&x,&y); if (x>y)swap(x,y); if (mark==0) { update(x,y,1,n,1); } else { printf("%I64d
",query(x,y,1,n,1)); } } printf("
"); } return 0; }

The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest

좋은 웹페이지 즐겨찾기