A.敌兵布阵
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 149073 Accepted Submission(s): 61803
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
每组数据第一行一个正整数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条命令
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
#include
#include
#include
using namespace std;
typedef long long LL;
const int maxn=1e5+7;
int a[maxn],tree[maxn*4];
int lz[maxn*4];
void build(int node ,int start, int endd)
{
if(start == endd)//叶子节点
{
tree[node] = a[start];
return ;
}
else{
int mid = start + ((endd - start) >> 1);
int lson = node << 1 ;//左儿子:2 * node
int rson = (node << 1) | 1;//右儿子:2 * node + 1
build(lson, start, mid);
build(rson, mid+1, endd);
tree[node] = tree[lson] + tree[rson];
}
}
void pushup(int node)
{
tree[node] = tree[node<<1] + tree[node<<1|1];
}
//单点更新,idx为需要更新的叶子节点编号,add为更改值
void update(int node, int start, int endd, int idx, int instead)
{
if(start == endd)
{
tree[node] += instead;
return ;
}
else
{
int mid = start + ((endd - start) >> 1);
int lson = (node << 1);
int rson = (node << 1) | 1;
if( idx <= mid)
{
update(lson, start, mid, idx, instead);
}
else
{
update(rson, mid+1, endd, idx, instead);
}
pushup(node);
}
}
LL query(int node, int start, int endd, int l, int r)
{
if(l <= start && r >= endd)
{
return tree[node];
}
// pushdown(node, start, endd);
int mid = start + ((endd-start)>>1);
LL ans=0;
if(l <= mid) ans+=query(node<<1, start, mid, l, r);
if(r >= mid+1) ans+=query(node<<1|1, mid+1, endd, l, r);
return ans;
}
int main()
{
int t;
scanf("%d",&t);
int cas=1;
while(t--){
printf("Case %d:\n",cas++);
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
build(1,1,n);
char str[10];
while(scanf("%s",str)!=EOF){
if(!strcmp(str,"End")) break;
if(!strcmp(str,"Add"))
{
int idx,ad;
scanf("%d%d",&idx,&ad);
update(1,1,n,idx,ad);
}
if(!strcmp(str,"Sub"))
{
int idx,ad;
scanf("%d%d",&idx,&ad);
update(1,1,n,idx,-ad);
}
if(!strcmp(str,"Query"))
{
int l,r;
scanf("%d%d",&l,&r);
//for(int i=1;i<=25;i++) printf("tree[%d]:%d\n",i,tree[i]);
printf("%lld\n",query(1,1,n,l,r));
}
}
}
return 0;
}
B.I Hate It
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 123585 Accepted Submission(s): 45493
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
在每个测试的第一行,有两个正整数 N 和 M ( 0
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
#include
#include
#include
#include
#define max2(a,b) a>b?a:b
using namespace std;
typedef long long LL;
const int maxn=2e5+9;
LL mac(LL a,LL b)
{
return a>=b?a:b;
}
LL a[maxn];
LL tree[maxn*4];
LL lz[maxn*4];
void pushup(LL node)
{
tree[node] = mac(tree[node<<1],tree[node<<1|1]);
}
void build(LL node ,LL start, LL endd)
{
if(start == endd)//叶子节点
{
tree[node] = a[start];
return ;
}
else{
LL mid = (start + endd) >> 1;
//LL lson = node << 1 ;
//LL rson = (node << 1) | 1;
build(node<<1, start, mid);
build((node<<1)|1, mid+1, endd);
pushup(node);
}
}
void update(LL node, LL start, LL endd, LL idx, LL instead)
{
//prLLf("node:%d\n",node);
if(start == endd)
{
tree[node] = instead;
return ;
}
LL mid = (start + endd) >> 1;
LL lson = (node << 1);
LL rson = (node << 1) | 1;
if( idx <= mid)
{
update(lson, start, mid, idx, instead);
}
else
{
update(rson, mid+1, endd, idx, instead);
}
pushup(node);
//pushup(node);
}
LL query(LL node, LL start, LL endd, LL l, LL r)
{
//printf("node2:%d\n",node);
if(l <= start && r>=endd)
{
return tree[node];
}
//push_down(node, start, endd);
LL mid = (start + endd)>>1;
LL ansl=0;
LL ansr=0;
if(l <= mid) ansl = query( node<<1, start, mid, l, r);
if(r >= mid+1) ansr = query( node<<1|1, mid+1, endd, l, r);
return mac(ansl,ansr);
}
char ch[10];
int main()
{
LL n,m;
while(scanf("%lld%lld",&n,&m)!=EOF){
for(LL i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
getchar();
build(1,1,n);
while(m--)
{
scanf("%s",ch);
if(ch[0]=='Q')
{
LL l,r;
scanf("%lld%lld",&l,&r);
//for(int i=1;i<=50;i++) printf("tree[%d]:%d\n",i,tree[i]);
printf("%lld\n",query(1,1,n,l,r));
}
if(ch[0]=='U')
{
LL idx,ad;
scanf("%lld%lld",&idx,&ad);
update(1,1,n,idx,ad);
}
}
}
return 0;
}
hdu为什么用库里的max会tle???
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 2144 | Accepted: 911 |
Description
Everybody is fond of computers, but buying a new one is always a money challenge. Fortunately, there is always a convenient way to deal with. You can replace your computer and get a brand new one, thus saving some maintenance cost. Of course, you must pay a fixed cost for each new computer you get.
Suppose you are considering an n year period over which you want to have a computer. Suppose you buy a new computer in year y, 1<=y<=n Then you have to pay a fixed cost c, in the year y, and a maintenance cost m(y,z) each year you own that computer, starting from year y through the year z, z<=n, when you plan to buy - eventually - another computer.
Write a program that computes the minimum cost of having a computer over the n year period.
Input
The program input is from a text file. Each data set in the file stands for a particular set of costs. A data set starts with the cost c for getting a new computer. Follows the number n of years, and the maintenance costs m(y,z), y=1..n, z=y..n. The program prints the minimum cost of having a computer throughout the n year period.
White spaces can occur freely in the input. The input data are correct and terminate with an end of file.
Output
For each set of data the program prints the result to the standard output from the beginning of a line.
Sample Input
3 3 5 7 50 6 8 10
Sample Output
19
Hint
An input/output sample is shown above. There is a single data set. The cost for getting a new computer is c=3. The time period n is n=3 years, and the maintenance costs are:
- For the first computer, which is certainly bought: m(1,1)=5, m(1,2)=7, m(1,3)=50,
- For the second computer, in the event the current computer is replaced: m(2,2)=6, m(2,3)=8,
- For the third computer, in the event the current computer is replaced: m(3,3)=10.
Source
#include
#include
#include
#include
using namespace std;
int dp[1007][1007];
int main()
{
int c;
while(scanf("%d",&c)!=EOF)
{
memset(dp,0,sizeof(dp));
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
scanf("%d",&dp[i][j]);
}
}
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
if(i==1)
{
dp[i][j]+=c;
}
else
dp[i][j]=(dp[i-1][i-1]+dp[i][j]+c)<(dp[i-1][j])?(dp[i-1][i-1]+dp[i][j]+c):(dp[i-1][j]);
//printf("dp[%d][%d]:%d\n",i,j,dp[i][j]);
}
}
printf("%d\n",dp[n][n]);
}
return 0;
}
D不会做啊淦
E.Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 52820 Accepted Submission(s): 24709
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
#include
#include
#include
using namespace std;
const int maxn=1e5+7;
typedef long long LL;
int tree[maxn*4];
int lz[maxn*4];
int a[maxn];
//区间改值,区间查询
void pushup(int node)
{
tree[node] = tree[node<<1] + tree[node<<1|1];
}
void pushdown(int node, int start, int endd)
{
if(lz[node])
{
int c = lz[node];
lz[node<<1] = c;
lz[node<<1|1] = c;
int mid = start + ((endd - start)>>1);
tree[node<<1] = (mid-start+1) * c;
tree[node<<1|1] = (endd-mid) * c;
lz[node] = 0;
}
}
void build(int node ,int start, int endd)
{
lz[node] = 0;
if(start == endd)//叶子节点
{
tree[node] = a[start];
return ;
}
else{
int mid = start + ((endd - start) >> 1);
int lson = node << 1 ;//左儿子:2 * node
int rson = (node << 1) | 1;//右儿子:2 * node + 1
build(lson, start, mid);
build(rson, mid+1, endd);
//tree[node] = tree[lson] + tree[rson];
}
pushup(node);
}
void update(int node, int start, int endd, int l, int r,int c)
{
if(l <= start && r >= endd)
{
//tree[node] = (endd-start+1) * lz[node];
lz[node] = c;
tree[node] = (endd-start+1) * c;
// printf("latree[%d]:%d\n",node,tree[node]);
return ;
}
pushdown(node, start, endd);
int mid = start + ((endd - start)>>1);
if(l <= mid) update(node<<1, start, mid, l, r, c);
if(r >= mid+1) update(node<<1|1, mid+1, endd, l, r,c);
pushup(node);
// printf("tree[2]:%d\n",tree[2]);
}
LL query(int node, int start, int endd, int l, int r)
{
if(l <= start && r >= endd)
{
return tree[node];
}
pushdown(node, start, endd);
int mid = start + ((endd-start)>>1);
LL ans=0;
if(l <= mid) ans+=query(node<<1, start, mid, l, r);
if(r >= mid+1) ans+=query(node<<1|1, mid+1, endd, l, r);
return ans;
}
int main()
{
int t;
scanf("%d",&t);
int x=1;
while(t--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) a[i]=1;
build(1,1,n);
int q;
scanf("%d",&q);
while(q--)
{
int l,r,z;
scanf("%d%d%d",&l,&r,&z);
update(1,1,n,l,r,z);
//for(int i=1;i<26;i++) printf("tree[%d]:%d\n",i,tree[i]);
}
printf("Case %d: The total value of the hook is %lld.\n",x++,query(1,1,n,1,n));
}
return 0;
}
Hihocoder#1078 : 线段树的区间修改
描述
对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题改了改,又出给了小Ho:
假设货架上从左到右摆放了N种商品,并且依次标号为1到N,其中标号为i的商品的价格为Pi。小Hi的每次操作分为两种可能,第一种是修改价格——小Hi给出一段区间[L, R]和一个新的价格NewP,所有标号在这段区间中的商品的价格都变成NewP。第二种操作是询问——小Hi给出一段区间[L, R],而小Ho要做的便是计算出所有标号在这段区间中的商品的总价格,然后告诉小Hi。
那么这样的一个问题,小Ho该如何解决呢?
提示:推动科学发展的除了人的好奇心之外还有人的懒惰心!
输入
每个测试点(输入文件)有且仅有一组测试数据。
每组测试数据的第1行为一个整数N,意义如前文所述。
每组测试数据的第2行为N个整数,分别描述每种商品的重量,其中第i个整数表示标号为i的商品的重量Pi。
每组测试数据的第3行为一个整数Q,表示小Hi进行的操作数。
每组测试数据的第N+4~N+Q+3行,每行分别描述一次操作,每行的开头均为一个属于0或1的数字,分别表示该行描述一个询问和一次商品的价格的更改两种情况。对于第N+i+3行,如果该行描述一个询问,则接下来为两个整数Li, Ri,表示小Hi询问的一个区间[Li, Ri];如果该行描述一次商品的价格的更改,则接下来为三个整数Li,Ri,NewP,表示标号在区间[Li, Ri]的商品的价格全部修改为NewP。
对于100%的数据,满足N<=10^5,Q<=10^5, 1<=Li<=Ri<=N,1<=Pi<=N, 0 对于每组测试数据,对于每个小Hi的询问,按照在输入中出现的顺序,各输出一行,表示查询的结果:标号在区间[Li, Ri]中的所有商品的价格之和。输出
- 样例输入
-
10 4733 6570 8363 7391 4511 1433 2281 187 5166 378 6 1 5 10 1577 1 1 7 3649 0 8 10 0 1 4 1 6 8 157 1 3 4 1557
- 样例输出
-
4731 14596
思路:区间修改,区间查询#include
#include #include #include #define LL long long using namespace std; const int maxn=1e5+7000; int tree[maxn*4]; int a[maxn]; int change[maxn*4]; int lz[maxn*4]; void build(int node ,int start, int endd) { if(start == endd)//叶子节点 { tree[node] = a[start]; //printf("node:%d\n",node); return ; } else{ int mid = start + ((endd - start) >> 1); int lson = node << 1 ;//左儿子:2 * node int rson = (node << 1) | 1;//右儿子:2 * node + 1 build(lson, start, mid); build(rson, mid+1, endd); tree[node] = tree[lson] + tree[rson]; } } void pushup(int node) { tree[node] = tree[node<<1] + tree[node<<1|1]; } void pushdown(int node, int start, int endd) { //printf("node2:%d\n",node); if(change[node]) { int c = change[node]; change[node<<1] = c; change[node<<1|1] = c; int mid = start + ((endd-start)>>1); tree[node<<1] = (mid-start+1) * c; tree[node<<1|1] = (endd-mid) * c; change[node] = 0; } } void update(int node, int start, int endd, int l, int r,int c) { if(l <= start && r >= endd) { change[node] = c; tree[node] = (endd-start+1) * c; return ; } pushdown(node, start, endd); int mid = start + ((endd - start)>>1); if(l <= mid) update(node<<1, start, mid, l, r, c); if(r >= mid+1) update(node<<1|1, mid+1, endd, l, r,c); pushup(node); } int query(int node, int start, int endd, int l, int r) { //printf("!\n"); if(l <= start && r >= endd) { //printf("node3:%d\n",node); return tree[node]; } pushdown(node, start, endd); int mid = start + ((endd-start)>>1); int ans=0; if(l <= mid) ans+=query(node<<1, start, mid, l, r); if(r >= mid+1) ans+=query(node<<1|1, mid+1, endd, l, r); return ans; } int main() { int t; scanf("%d",&t); for(int i=1;i<=t;i++) { scanf("%d",&a[i]); } build(1,1,t); int n; scanf("%d",&n); while(n--) { int x,l,r,c; scanf("%d%d%d",&x,&l,&r); if(x==1) { scanf("%d",&c); update(1,1,t,l,r,c); } else { /*for(int i=1;i<50;i++) { printf("tree[%d]:%d\n",i,tree[i]); }*/ printf("%d\n",query(1,1,t,l,r)); } } return 0; }