ACM暑期集训25

前期线段树的题刷的比较少,今天补一下。

1.单点修改,区间求和

敌兵布阵

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 123163    Accepted Submission(s): 51587


 

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<iostream>
#include<stdio.h>
#include<string>
#include<map>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn=50010;
char s[20];

struct seg
{
	int l,r,sum;
}tree[maxn<<2];

void build(int p,int l,int r)
{
	tree[p].l=l,tree[p].r=r,tree[p].sum=0;
	if(l==r) return ;
	int mid=(l+r)/2;
	build(p<<1,l,mid);
	build(p<<1|1,mid+1,r);
}

void add(int p,int x,int y)
{
	if(tree[p].l==tree[p].r)
	{
		tree[p].sum+=y;
		return ;
	}
	int mid=(tree[p].l+tree[p].r)/2;
	if(x<=mid) add(p<<1,x,y);
	else add(p<<1|1,x,y);
	tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
}

int ask(int p,int l,int r)
{
	if(l==tree[p].l && tree[p].r==r) return tree[p].sum;
	int mid=(tree[p].l+tree[p].r)/2;
	if(r<=mid) return ask(p<<1,l,r);
	else if (l>mid) return ask(p<<1|1,l,r);
	else return ask(p<<1,l,mid)+ask(p<<1|1,mid+1,r);
}
int main()
{
	int t;
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
		printf("Case %d:\n",i);
		int n;
		scanf("%d",&n);
		build(1,1,n);
		for(int j=1;j<=n;j++)
		{
			int a;
			scanf("%d",&a);
			add(1,j,a);
		}
		while(scanf("%s",s))
		{
			if(s[0]=='E') break;
			int a,b;
			scanf("%d%d",&a,&b);
			if(s[0]=='Q') printf("%d\n",ask(1,a,b));
			else if (s[0]=='A') add(1,a,b);
			else add(1,a,-b);
		}
	}
	return 0;
}

2.单点修改,区间最值

I Hate It

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 102342    Accepted Submission(s): 38486


 

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

 

5 6

1 2 3 4 5

Q 1 5

U 3 6

Q 3 4

Q 4 5

U 2 9

Q 1 5

 

Sample Output

 

5

6

5

9

#include<iostream>
#include<stdio.h>
#include<string>
#include<map>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn=200010;
char s[20];

struct seg
{
	int l,r;
	int data,ma;
}tree[maxn<<2];

void build(int p,int l,int r)
{
	tree[p].l=l,tree[p].r=r,tree[p].data=0;
	tree[p].ma=0;
	if(l==r) return ;
	int mid=(l+r)/2;
	build(p<<1,l,mid);
	build(p<<1|1,mid+1,r);
}

void add(int p,int x,int y)
{
	if(tree[p].l==tree[p].r)
	{
		tree[p].data=y;
		tree[p].ma=y;
		return ;
	}
	int mid=(tree[p].l+tree[p].r)/2;
	if(x<=mid) add(p<<1,x,y);
	else add(p<<1|1,x,y);
	if(y>tree[p].ma) tree[p].ma=y;
}

int ask(int p,int l,int r)
{
	if(l==tree[p].l && tree[p].r==r) return tree[p].ma;
	int mid=(tree[p].l+tree[p].r)/2;
	if(r<=mid) return ask(p<<1,l,r);
	else if (l>mid) return ask(p<<1|1,l,r);
	else return max(ask(p<<1,l,mid),ask(p<<1|1,mid+1,r));
}
int main()
{
	int n,m,num;
    int i;

    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,1,n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num);
            add(1,i,num);
        }
        char c;
        int x1,x2;
        while(m--)
        {
            scanf("%*c%c",&c);
            scanf("%d%d",&x1,&x2);
            if(c == 'Q')
            {
                printf("%d\n",ask(1,x1,x2));
            }
            else
            {
                add(1,x1,x2);
            }
        }
    }
	return 0;
}

3.区间修改,区间查询

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 43707    Accepted Submission(s): 20908


 

Problem Description

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.



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.

 

Input

The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
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.

 

Output

For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.

 

Sample Input

 

1

10

2

1 5 2 5 9 3

 

Sample Output

 

Case 1: The total value of the hook is 24.

自己写的好乱,在网上找了个很好的,可以当模板。

#include <cstdio>

struct node
{
    int l,r,sum,lazy;
};

node segTree[400004];

void Build(int t, int l, int r)
{
    segTree[t].l = l;
    segTree[t].r = r;
    segTree[t].lazy = 0;
    if(l == r)
    {
        segTree[t].sum = 1;
        return ;
    }
    int mid = (l+r) >> 1;
    Build(t<<1,l,mid);
    Build(t<<1|1,mid+1,r);

    segTree[t].sum = segTree[t<<1].sum + segTree[t<<1|1].sum;
}

void pushDown(int t)
{
    if(segTree[t].lazy)
    {
        segTree[t<<1].sum = (segTree[t<<1].r - segTree[t<<1].l + 1) * segTree[t].lazy;
        segTree[t<<1].lazy = segTree[t].lazy;
        segTree[t<<1|1].sum = (segTree[t<<1|1].r - segTree[t<<1|1].l + 1)*segTree[t].lazy;
        segTree[t<<1|1].lazy = segTree[t].lazy;

        segTree[t].lazy = 0;
    }
}

void Update(int t, int l, int r, int c)
{
    if(segTree[t].l >= l && segTree[t].r <= r)
    {
        segTree[t].sum = (segTree[t].r - segTree[t].l + 1) * c;
        segTree[t].lazy = c;
        return ;
    }

    pushDown(t);

    if(r >= segTree[t<<1|1].l)
        Update(t<<1|1,l,r,c);
    if(l <= segTree[t<<1].r)
        Update(t<<1,l,r,c);

    segTree[t].sum = segTree[t<<1].sum + segTree[t<<1|1].sum;
}

int main()
{
    int t,n,o;
    int time = 0;
    scanf("%d",&t);
    while(t--)
    {
        ++time;
        scanf("%d",&n);
        Build(1,1,n);
        scanf("%d",&o);
        int a,b,c;
        for(int i = 0; i < o; ++i)
        {
            scanf("%d %d %d",&a,&b,&c);
            Update(1,a,b,c);
        }
        printf("Case %d: The total value of the hook is %d.\n",time,segTree[1].sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41383801/article/details/81841536