从零单排线段树

本文深入探讨了线段树的应用,并提供了几个实际问题的解决方案,涉及数据结构优化、算法实现及复杂度分析。

饿..回家休息几天...

的确是很舒服..

22号回去集训...

感觉拖小明的后腿真是不好意思...


好好体会了线段树

贴几道题


A. 敌兵布阵

1000ms
1000ms
32768KB
64-bit integer IO format:  %I64d      Java class name:  Main
Font Size:   
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询问,输出一个整数并回车,表示询问的段中的总人数,这个数最多不超过1000000。

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<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
struct node
{
	int left;
	int right;
	int value;
}tree[150005];
int a[50005];
int ans;
void build(int v,int left,int right)
{
	tree[v].left=left;
	tree[v].right=right;
	if(left==right)
	{
		tree[v].value=a[right];
		return ;
	}
	int mid=(left+right)/2;
	build(v*2,left,mid);
	build(v*2+1,mid+1,right);
	tree[v].value=tree[v*2].value+tree[v*2+1].value;
}
void update(int v,int left,int right,int m)
{
	if(tree[v].left==tree[v].right)
	{
		tree[v].value+=m;
		return ;
	}
	int mid=(tree[v].left+tree[v].right)/2;
	if(right<=mid)
	{
		update(v*2,left,right,m);
	}
	else
	{
		update(v*2+1,left,right,m);
	}
	tree[v].value=tree[v*2].value+tree[v*2+1].value;
}
void query(int v,int left,int right)
{
	if(tree[v].left==left&&tree[v].right==right)
	{
		ans+=tree[v].value;
		return ;
	}
	int mid=(tree[v].left+tree[v].right)/2;
	if(right<=mid)
	{
		query(v*2,left,right);
	}
	else if(left>mid)
	{
		query(v*2+1,left,right);
	}
	else
	{
		query(v*2,left,mid);
		query(v*2+1,mid+1,right);
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	for(int tt=1;tt<=T;tt++)
	{
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		build(1,1,n);
		printf("Case %d:\n",tt);
		char str[10];
		while(scanf("%s",str)&&strcmp(str,"End")!=0)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			if(strcmp(str,"Add")==0)
			{
				update(1,1,x,y);
			}
			else if(strcmp(str,"Sub")==0)
			{
				update(1,1,x,-y);
			}
			else
			{
				ans=0;
				query(1,x,y);
				printf("%d\n",ans);
			}
		}
	}
	return 0;
}

B. I Hate It

3000ms
3000ms
32768KB
64-bit integer IO format:  %I64d      Java class name:  Main
Font Size:   
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

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


  
Hint
Huge input,the C function scanf() will work better than cin
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
struct node
{
	int left;
	int right;
	int value;
	int max;
}tree[600005];
int a[200005];
int ans;
void build(int v,int left,int right)
{
	tree[v].left=left;
	tree[v].right=right;
	if(left==right)
	{
		tree[v].value=a[right];
		tree[v].max=a[right];
		return ;
	}
	int mid=(left+right)/2;
	build(v*2,left,mid);
	build(v*2+1,mid+1,right);
	tree[v].max=max(tree[v*2].max,tree[v*2+1].max);
}
void update(int v,int left,int right,int m)
{
	if(tree[v].left==tree[v].right)
	{
		tree[v].value=m;
		tree[v].max=max(tree[v].max,m);
		return ;
	}
	int mid=(tree[v].left+tree[v].right)/2;
	if(right<=mid)
	{
		update(v*2,left,right,m);
	}
	else
	{
		update(v*2+1,left,right,m);
	}
	tree[v].max=max(tree[v*2].max,tree[v*2+1].max);
}
void query(int v,int left,int right)
{
	if(tree[v].left==left&&tree[v].right==right)
	{
		ans=max(ans,tree[v].max);
		return ;
	}
	int mid=(tree[v].left+tree[v].right)/2;
	if(right<=mid)
	{
		query(v*2,left,right);
	}
	else if(left>mid)
	{
		query(v*2+1,left,right);
	}
	else
	{
		query(v*2,left,mid);
		query(v*2+1,mid+1,right);
	}
}

int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		build(1,1,n);
		for(int i=0;i<m;i++)
		{
			char str[5];
			int x,y;
			scanf("%s%d%d",str,&x,&y);
			if(str[0]=='U')
			{
				update(1,1,x,y);
			}
			else
			{
				if(x>y)
				{
					swap(x,y);
				}
				ans=0;
				query(1,x,y);
				printf("%d\n",ans);
			}
		}
	}
	return 0;
}

G. Just a Hook

2000ms
2000ms
32768KB
64-bit integer IO format:  %I64d      Java class name:  Main
Font Size:   
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<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
struct node
{
	int left;
	int right;
	int add;
}tree[400005];
int ans=0;
void build(int v,int left,int right)
{
	tree[v].left=left;
	tree[v].right=right;
	tree[v].add=1;
	if(left==right)
	{
		return ;
	}
	int mid=(left+right)/2;
	build(v*2,left,mid);
	build(v*2+1,mid+1,right);
}
void update(int v,int left,int right,int m)
{
	if(left==tree[v].left&&tree[v].right==right)
	{
		tree[v].add=m;
		return ;
	}
	if(tree[v].add==m)
	{
		return ;
	}
	if(tree[v].left==tree[v].right)
	{
		return ;
	}
	if(tree[v].add)
	{
		tree[v*2].add=tree[v].add;
		tree[v*2+1].add=tree[v].add;
		tree[v].add=0;
	}
	int mid=(tree[v].left+tree[v].right)/2;
	if(right<=mid)
	{
		update(v*2,left,right,m);
	}
	else if(left>mid)
	{
		update(v*2+1,left,right,m);
	}
	else
	{
		update(v*2,left,mid,m);
		update(v*2+1,mid+1,right,m);
	}
}
void query(int v,int left,int right)
{
	if(tree[v].add)
	{
		ans+=tree[v].add*(right-left+1);
		return ;
	}
	if(tree[v].left==tree[v].right)
	{
		return ;
	}
	int mid=(left+right)/2;
	query(v*2,left,mid);
	query(v*2+1,mid+1,right);
}
int main()
{
	int T;
	scanf("%d",&T);
	for(int tt=1;tt<=T;tt++)
	{
		int n;
		scanf("%d",&n);
		build(1,1,n);
		int q;
		scanf("%d",&q);
		int x,y,z;
		while(q--)
		{
			scanf("%d%d%d",&x,&y,&z);
			update(1,x,y,z);
		}
		ans=0;
		query(1,1,n);
		printf("Case %d: The total value of the hook is %d.\n",tt,ans);
	}
	return 0;
}

H. A Simple Problem with Integers

5000ms
5000ms
131072KB
64-bit integer IO format:  %lld      Java class name:  Main
Font Size:   

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.The second line contains N numbers, the initial values ofA1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.Each of the next Q lines represents an operation."C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000."Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
struct node
{
	int left;
	int right;
	long long value;
	long long add;
}tree[400005];
int a[100005];
long long ans;
void build(int v,int left,int right)
{
	tree[v].left=left;
	tree[v].right=right;
	tree[v].add=0;
	if(left==right)
	{
		tree[v].value=a[right];
		return ;
	}
	int mid=(left+right)/2;
	build(v*2,left,mid);
	build(v*2+1,mid+1,right);
	tree[v].value=tree[v*2].value+tree[v*2+1].value;
}
void update(int v,int left,int right,int m)
{
	if(tree[v].left==left&&tree[v].right==right)
	{
		tree[v].add+=m;
		return ;
	}
	tree[v].value+=(long long)m*(right-left+1);
	int mid=(tree[v].left+tree[v].right)/2;
	if(right<=mid)
	{
		update(v*2,left,right,m);
	}
	else if(left>mid)
	{
		update(v*2+1,left,right,m);
	}
	else
	{
		update(v*2,left,mid,m);
		update(v*2+1,mid+1,right,m);
	}
}
void query(int v,int left,int right)
{
	if(tree[v].left==left&&tree[v].right==right)
	{
		ans+=(long long)tree[v].value+tree[v].add*(long long)(right-left+1);
		return ;
	}
	if(tree[v].add)
	{
		tree[v*2].add+=tree[v].add;
		tree[v*2+1].add+=tree[v].add;
		tree[v].value+=(long long)(tree[v].right-tree[v].left+1)*tree[v].add;
		tree[v].add=0;
	}
	int mid=(tree[v].left+tree[v].right)/2;
	if(right<=mid)
	{
		query(v*2,left,right);
	}
	else if(left>mid)
	{
		query(v*2+1,left,right);
	}
	else
	{
		query(v*2,left,mid);
		query(v*2+1,mid+1,right);
	}
}
int main()
{
	int n,q;
	while(scanf("%d%d",&n,&q)!=EOF)
	{ 
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		build(1,1,n);
		while(q--)
		{
			char str[10];
			scanf("%s",str);
			if(str[0]=='C')
			{
				int x,y,c;
				scanf("%d%d%d",&x,&y,&c);
				update(1,x,y,c);
			}
			else
			{
				int x,y;
				scanf("%d%d",&x,&y);
				ans=0;
				query(1,x,y);
				printf("%lld\n",ans);
			}
		}
	}
	return 0;
}


昨天只敲了这三题..

同一专题其他几题就先不敲了

学习了基本的操作和记录增量的方法

圆满了~

回家了么不能要求太多..


### C语言学习路径 为了成为百强级别的C语言程序员,需遵循一条系统的成长路线。这条路线不仅涵盖了基础知识的学习,还包括实践项目的积累和技术深度的理解。 #### 基础阶段:掌握核心概念 在这个阶段,重点应放在理解C语言的基础语法和特性上。这包括变量声明、数据类型、运算符、控制流语句(if/else, switch)、循环结构(for, while),以及函数定义与调用[^1]。 ```c #include <stdio.h> int main() { int a = 5; printf("Value of a is %d\n", a); return 0; } ``` #### 进阶阶段:深入探索高级主题 随着对基本概念的熟悉度增加,可以转向更复杂的话题,比如指针操作、内存管理、文件I/O处理、动态分配数组等。这些知识点对于编写高效可靠的程序至关重要[^2]。 ```c // 动态分配内存的例子 #include <stdlib.h> void* safe_malloc(size_t size){ void *ptr = malloc(size); if(ptr == NULL){ fprintf(stderr,"Out of memory"); exit(1); } return ptr; } ``` #### 实践应用:构建实际项目经验 理论知识固然重要,但真正的提升来自于动手做项目。可以选择参与开源项目贡献代码,或是自己发起一些小型的应用开发工作来锻炼解决问题的能力。通过不断尝试新的挑战,逐步建立起解决各种问题所需的技能集[^3]。 #### 技术深化:关注性能优化与安全编码 当具备了一定的经验之后,应该更加注重于如何写出高性能且安全性高的代码。了解编译器的工作原理可以帮助更好地利用底层资源;而学习常见的漏洞防范措施则能有效减少潜在风险[^4]。 ### 编程技巧 - **阅读优秀源码**:经常浏览高质量的开源库或工具包中的实现细节,从中汲取灵感。 - **练习算法题目**:定期参加在线竞赛平台上的刷题活动,强化逻辑思维能力。 - **保持好奇心**:积极跟踪行业最新趋势和发展方向,持续更新自己的知识体系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值