线段树

View Code
敌兵布阵
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22643 Accepted Submission(s): 9874


Problem Description
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.

 
Input
第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令

 
Output
对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。

 
Sample Input
1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End

 
Sample Output
Case 1: 6 33 59
#include
#include
struct node
{
 int ld,rd;
 int val;
 node *lch, *rch;
};
node *root;

void build(int l, int r,node *&p)
{
// if( p == NULL )  child has a address not equal null, but not assign memory
 p = new node;
 p->ld = l, p->rd = r;
 p->val = 0;
 if( p->ld == p->rd )
  return;
 int mid = (p->ld+p->rd)/2 ;
 build(p->ld, mid, p->lch);
 build(mid+1, p->rd, p->rch);
}
int cnt;
void search(int l, int r, node *&p)
{
 int mid = (p->ld + p->rd)/2 ;
 if( p->ld == l && p->rd == r) // 完全匹配才 +
  cnt += p->val;
 else
 {
  if(mid >= r)
   search(l, r, p->lch);
  else if( mid+1 <= l )
   search(l, r, p->rch);
  else  //因为是连续多个  可能l r 不匹配
  {
   search(l, mid, p->lch);
   search(mid+1, r, p->rch);
  }
 }
}
void update(int pos, int data, node *&p)
{
 // update only if l or r( actually l always equals r) lies between p->ld && p->rd
 int mid = (p->ld + p->rd)/2 ;
 if(p->ld <= pos && p->rd >= pos)
  p->val += data;
 if(p->ld == p->rd)
  return ;
 if(mid >= pos)
  update(pos, data, p->lch);
 else if(mid+1 <= pos)
  update(pos, data, p->rch);
 
}
void deletNode(node *&p)
{
 if(p ->ld == p->rd)
  return ;
 deletNode(p->lch);
 deletNode(p->rch);
 delete p ;
}
int main()
{
 int cas, z, i, n, data, a, b;
 char str[10];
 scanf("%d", &cas);
 for(z=1; z<=cas; z++)
 {
  
  scanf("%d",&n);
  build(1, n, root);
  for(i=1; i<=n; i++)
  {
   scanf("%d",&data);
   update(i, data, root);
  }
  printf("Case %d:\n", z);
  while(scanf("%s", str),strcmp(str, "End") != 0)
  {
   scanf("%d %d", &a, &b);
   if( strcmp(str, "Query") == 0 )
   {
    cnt = 0;
    search(a, b, root);
    printf("%d\n",cnt);
   }
   else if( strcmp(str, "Add") == 0 )
    update(a, b, root);
   else
    update(a, -b, root);
  }
  deletNode(root);
 }
 return 0;
}

 

View Code
Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5631 Accepted Submission(s): 2578


Problem Description
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.

On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.

Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.

When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.

If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).

Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.

 
Input
There are multiple cases (no more than 40 cases).

The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.

Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.

 
Output
For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement.

 
Sample Input
3 5 5 2 4 3 3 3

 
Sample Output
1 2 1 3 -1
#include <stdio.h>
#include <string.h>
#define MAX 200010
struct node
{
 int val;
 int ld, rd;
}tree[MAX<<2];
int width, height, place;
void build(int l, int r, int pos)
{
 tree[pos].val = width;
 tree[pos].ld = l, tree[pos].rd = r;
 if(l == r)
  return;
 int mid = (l+r)>>1;
 build(l,mid,pos*2);
 build(mid+1, r, pos*2+1);
}
int max(int a, int b)
{
 return a>=b?a:b ;
}
int min(int a, int b)
{
 return a<=b?a:b ;
}
void update(int pos, int length)
{
 if(tree[pos].val < length)
  return ;
 if(tree[pos].ld == tree[pos].rd)
 {
  tree[pos].val -= length;
  place = tree[pos].ld;
  return ;
 }
 else
 {
  if(length <= tree[pos*2].val)
   update(pos*2, length);
  else if(length <= tree[pos*2+1].val)
   update(pos*2+1, length);
  tree[pos].val = max(tree[pos*2].val, tree[pos*2+1].val);  // 巧妙利用递归的顺序!
 }
}
int main()
{
 int q, length;
 while(scanf("%d %d %d", &height, &width, &q) != EOF)
 {
   build(1, min(height,q), 1);  //注意使用min 不然会数组越界
  while(q--)
  {
   place = 0;
   scanf("%d", &length);
   update(1, length);
   printf("%d\n", place==0?-1:place);
  }
 }
 return 0;
}

 

posted @ 2013-05-07 18:15  April_Tsui  阅读(152)  评论(0编辑  收藏  举报