题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
敌兵布阵Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 97473 Accepted Submission(s): 41240
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
Sample Output
Author
Windbreaker
Recommend
Eddy
|
解析:裸的线段树单点更新,求区间和,可以用树状数组和线段树写
代码1:
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
typedef long long LL;
using namespace std;
const int N = 200009;
int dp[N];
int n;
void add(int k, int c)
{
while(k <= n)
{
dp[k] += c;
k += k&(-k);
}
}
int sum(int k)
{
int ans = 0;
while(k)
{
ans += dp[k];
k -= k&(-k);
}
return ans;
}
int main()
{
int T, cnt = 0;
scanf("%d", &T);
char s[11];
while(T--)
{
memset(dp, 0, sizeof(dp));
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
int num;
scanf("%d", &num);
add(i, num);
}
int x, y;
printf("Case %d:\n", ++cnt);
while(true)
{
scanf(" %s", s);
if(s[0] == 'E') break;
scanf("%d%d", &x, &y);
if(s[0] == 'Q') printf("%d\n", sum(y)-sum(x-1));
else if(s[0] == 'S') add(x, -y);
else add(x, y);
}
}
return 0;
}
代码2:
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
typedef long long LL;
using namespace std;
const int N = 200009;
struct node
{
int x;
}t[N<<2];
void Creat(int i, int l, int r)
{
if(l == r)
{
scanf("%d", &t[i].x);
return ;
}
int mid = (l + r)>>1;
Creat(i*2, l, mid);
Creat(i*2+1, mid+1, r);
t[i].x = t[i*2].x + t[i*2+1].x;
}
void updata(int i, int l, int r, int x, int y)
{
if(l <= x && x <= r)
{
t[i].x += y;
if(l == r) return ;
int mid = (l + r)>>1;
updata(i*2, l, mid, x, y);
updata(i*2+1, mid+1, r, x, y);
}
}
int Find(int i, int l, int r, int x, int y)
{
if(l == x && r == y) return t[i].x;
int mid = (l + r)>>1;
if(mid >= y) return Find(i*2, l, mid, x, y);
else if(mid < x) return Find(i*2+1, mid+1, r, x, y);
return Find(i*2, l, mid, x, mid) + Find(i*2+1, mid+1, r, mid+1, y);
}
int main()
{
int T, cnt = 0, n;
scanf("%d", &T);
char s[11];
while(T--)
{
scanf("%d", &n);
Creat(1, 1, n);
int x, y;
printf("Case %d:\n", ++cnt);
while(true)
{
scanf(" %s", s);
if(s[0] == 'E') break;
scanf("%d%d", &x, &y);
if(s[0] == 'Q') printf("%d\n", Find(1, 1, n, x, y));
else if(s[0] == 'S') updata(1, 1, n, x, -y);
else updata(1, 1, n, x, y);
}
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754
|
I Hate ItTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 81816 Accepted Submission(s): 31472
Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。 学生ID编号分别从1编到N。 第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。 接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。 当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。 当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
Output
对于每一次询问操作,在一行里面输出最高成绩。
Sample Input
Sample Output
Author
linle
Source
Recommend
lcy
|
解析:线段树单点更新,求最值,可以用树状数组(比较麻烦)、线段树
代码1:
#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<stack>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=200009;
int dp[N];
int a[N];
int m,n;
void updata(int k)
{
while(k<=m)
{
int i;
dp[k]=a[k];
int lx=k&(-k);
for( i=1; i<lx; i<<=1)
dp[k]=max(dp[k],dp[k-i]);
k+=k&(-k);
}
}
int Max(int l, int r)
{
int ans=0;
while(r>=l)
{
ans=max(ans,a[r]);
r--;
for(; r-(r&(-r))>=l; r-=r&(-r))
ans=max(ans,dp[r]);
}
return ans;
}
void up(int x,int y)
{
a[x]=y;
dp[x]=0;
updata(x);
}
int main()
{
while( ~scanf("%d%d",&m,&n))
{
memset(dp,0,sizeof(dp));
for(int i=1; i<=m; i++)
{
scanf("%d",&a[i]);
updata(i);
}
char cc;
for(int i=1;i<=n;i++)
{
getchar();
scanf("%c",&cc);
int x, y;
scanf("%d%d",&x,&y);
if(cc=='U')up(x,y);
else
{
int maxx=Max(x,y);
printf("%d\n",maxx);
}
}
}
return 0;
}
代码2:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 500009;
int dp[N], n, a[N];
void add(int k)
{
while(k <= n)
{
dp[k] = a[k];
int lx = k&(-k);
for(int i = 1; i < lx; i <<= 1) dp[k] = max(dp[k], dp[k - i]);
k += k&(-k);
}
}
int sum(int x, int y)
{
int ans = 0;
while(y >= x)
{
ans = max(ans, a[y]);
y--;
for(; y - (y&(-y)) >= x; y -= y&(-y)) ans = max(ans, dp[y]);
}
return ans;
}
int main()
{
int m;
while(~scanf("%d%d", &n, &m))
{
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
add(i);
}
char s[109];
int x, y;
//scanf("%d", &m);
while(m--)
{
scanf(" %s", s);
scanf("%d%d", &x, &y);
if(s[0] == 'Q') printf("%d\n", sum(x, y));
else a[x] = y, add(x);
}
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795
Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24218 Accepted Submission(s): 9974
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.
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.
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
Author
hhanger@zju
Source
Recommend
lcy