您的位置:首页 > 运维架构

5-26 Population (35分) -- 待解决

2015-11-02 21:21 501 查看
5-26 Population (35分)

It is always exciting to see people settling in a new continent. As the head of the population management office, you are supposed to know, at any time, how people are distributed in this continent.

The continent is divided into square regions, each has a center with integer coordinates (x,y).
Hence all the people coming into that region are considered to be settled at the center position. Given the positions of the corners of a rectangle region, you are supposed to count the number of people living in that region.


Input Specification:

Each input file contains one test case. For each case, the character
I
in
a line signals the coming in of new groups of people. In the following lines, each line contains three integers:
X
,
Y
,
and
N
, where
X
and
Y
(1 ≤
X
,
Y
≤ 20000)
are the coordinates of the region's center, and
N
(1 ≤
N
≤ 10000)
is the number of people coming in.

The character
Q
in a line signals the query of population.
The following lines each contains four numbers: X​min​​, X​max​​, Y​min​​, Y​max​​,
where (X​min​​,Y​min​​) and (X​max​​,Y​max​​) are
the integer coordinates of the lower left corner and the upper right corner of the rectangle, respectively.

The character
E
signals the end of the test case.


Output Specification:

For each
Q
case, print in a line the population in the given
rectangle region. That is, you are supposed to count the number of people living at all the positions (x,y) such
that X​min​​≤x≤X​max​​,
and Y​min​​≤y≤Y​max​​.

Sample Input:

I
8 20 1
4 5 1
10 11 1
12 10 1
18 14 1
Q
8 10 5 15
8 20 10 14
I
7 6 1
10 3 2
7 2 1
2 3 2
10 3 1
Q
2 20 2 20
E

Sample Output:

1
3
12


时间限制:600ms
内存限制:64MB
代码长度限制:16kB
判题程序:系统默认
作者:陈越
单位:浙江大学


http://pta.patest.cn/pta/test/16/exam/4/question/688

27分运行超时代码
#include <cstdio>
#include <sstream>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;

#define Mum 20001

int n ;

int str2int(string s)
{
stringstream ss ;
int d ;
ss << s;
ss >> d ;
return d ;
}

typedef struct node
{
int x, y ; // 1 ≤ X, Y ≤ 20000
int pNum ;
struct node *left , *right ;
node(int _x , int _y , int _pNum)
{
x = _x ;
y = _y ;
pNum = _pNum ;
left = NULL ;
right = NULL ;
}
}Bnode;

int cmp(int x1 ,int y1 , int x2 , int y2)
{
if(x1 < x2)
return -1;
if(x1 == x2)
{
if(y1 < y2)
return -1 ;
if(y1 == y2)
return 0 ;
}
return 1 ;
}

Bnode* createTree(Bnode* root , int x , int y ,int pNum)
{
if(root == NULL)
{
root = new node(x , y , pNum);
}else{
if(cmp(x , y , root->x , root->y) == -1)
{
root->left = createTree(root->left , x ,y , pNum);
}else if(cmp(x , y , root->x , root->y) == 1){
root->right = createTree(root->right , x ,y , pNum);
}else{
root->pNum = root->pNum + pNum ;
}
}
return root ;
}

Bnode* root = NULL ;
int cnt = 0 ;
int xmin , xmax ;
int ymin , ymax ;

bool isOk(int x , int y)
{
if(x >= xmin && x <= xmax && y >= ymin && y <= ymax)
return true ;
return false ;
}

void inOrder(Bnode* root)
{
if(root != NULL)
{
inOrder(root->left);
if( isOk(root->x , root->y) )
{
cnt += root->pNum ;
}
if(root->x > xmax)
return ;
inOrder(root->right);
}
}

void inOrder2(Bnode *root)      //非递归中序遍历
{
stack<Bnode*> s;
Bnode *p = root;
while(p!=NULL||!s.empty())
{
while(p!=NULL)
{
s.push(p);
p=p->left;
}
if(!s.empty())
{
p=s.top();
if( isOk(p->x , p->y) )
{
cnt += p->pNum ;
}
if(p->x > xmax)
return ;
s.pop();
p=p->right;
}
}
}

int main()
{
//freopen("in.txt", "r", stdin);
string c;
bool flag = true ;
while( cin >> c)
{
if(c == "E")
break ;
if(c == "I")
{
flag = true ;
}
else if(c == "Q")
{
flag = false ;
}else{
if(flag == true)
{
int x , y , pNum ;
x = str2int(c);
scanf("%d%d\n" , &y , &pNum) ;
root = createTree(root , x , y , pNum) ;
}else{
xmin = str2int(c);
scanf("%d%d%d\n" , &xmax , &ymin ,&ymax) ;
cnt = 0 ;
//inOrder(root) ;
inOrder2(root);
printf("%d\n" , cnt) ;
}
}
}
return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: