Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用二维线段树WA爆掉了!why?

Posted by acxian at 2010-01-17 15:28:25 on Problem 1195
/* 
 * File:   main.cpp
 * Author: Administrator
 *
 * Created on 2010年1月17日, 下午12:58
 */

#include <stdlib.h>
#include<stdio.h>
#include<string.h>
typedef struct Tree
{
    int left;
    int right;
    int sum;
};
typedef struct Node
{
    int left;
    int right;
    Tree tree[3*1048];
};
Node node[1048*3];
int n;

void build1(int t,int root,int l,int r)
{
    node[t].tree[root].left = l;
    node[t].tree[root].right = r;
    node[t].tree[root].sum = 0;
    if(l == r)
        return;
    int mid = (l + r) >> 1;
    build1(t,root*2,l,mid);
    build1(t,root*2+1,mid+1,r);
}

void insert1(int t,int root,int l,int v)
{
    if(node[t].tree[root].left == node[t].tree[root].right)
    {
        node[t].tree[root].sum += v;
        return;
    }
    int mid = (node[t].tree[root].left + node[t].tree[root].right) >> 1;
    if(l <= mid)
        insert1(t,root*2,l,v);
    else
        insert1(t,root*2+1,l,v);
    node[t].tree[root].sum = node[t].tree[root*2].sum + node[t].tree[root*2+1].sum;
}

int query1(int t,int root,int l,int r)
{
    if(node[t].tree[root].left == node[t].tree[root].right)
        return node[t].tree[root].sum;
    if(node[t].tree[root].left == l && node[t].tree[root].right == r)
        return node[t].tree[root].sum;
    int mid = (node[t].tree[root].left + node[t].tree[root].right) >> 1;
    if(r <= mid)
        return query1(t,root*2,l,r);
    else if(l > mid)
        return query1(t,root*2+1,l,r);
    else
        return query1(t,root*2,l,mid) + query1(t,root*2+1,mid+1,r);
}

void build(int root,int l,int r,int x,int y)
{
    node[root].left = l;
    node[root].right = r;
    build1(root,1,x,y);
    if(l == r)
        return ;
    int mid = (l + r) >> 1;
    build(root*2,l,mid,x,y);
    build(root*2+1,mid+1,r,x,y);
}

void insert(int root,int x,int y,int v)
{
    if(node[root].left == node[root].right)
    {
        insert1(root,1,y,v);
        return;
    }
    int mid = (node[root].left + node[root].right) >> 1;
    if(x <= mid)
        insert(root*2,x,y,v);
    else
        insert(root*2+1,x,y,v);
}

int query(int root,int x,int x1,int y,int y1)
{
    if(node[root].left == node[root].right)
        return query1(root,1,y,y1);
    if(node[root].left == x && node[root].right == x1)
        return query1(root,1,y,y1);
    int mid = (node[root].left + node[root].right) >> 1;
    if(x1 <= mid)
        return query(root*2,x,x1,y,y1);
    else if(x > mid)
        return query(root*2+1,x,x1,y,y1);
    else
        return query(root*2,x,mid,y,y1) + query(root*2+1,mid+1,x1,y,y1);
}

int main(int argc, char** argv)
{
    int i,j,x,y,x1,y1,d,val;
    while(scanf("%d%d",&d,&n) != EOF)
    {
        build(1,0,n+1,0,n+1);
        while(scanf("%d",&d) && d != 3)
        {
            if(d == 1)
            {
                scanf("%d%d%d",&x,&y,&val);
                x++;y++;
                insert(1,x,y,val);
            }
            else if(d == 2)
            {
                scanf("%d%d%d%d",&x,&y,&x1,&y1);
                x++;y++;x1++;y1++;
                int ans = query(1,x,x1,y,y1);
                printf("%d\n",ans);
            }
        }
    }
    return (EXIT_SUCCESS);
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator