浙江大学 acm程序设计竞赛 培训 线段树共66页文档

合集下载

浙江大学 acm程序设计竞赛 培训 线段树共68页文档

浙江大学 acm程序设计竞赛 培训 线段树共68页文档
浙江大学 acm程序设计竞赛 培训 线段 树
61、辍学如磨刀之石,不见其损,日 有所亏 。 62、奇文共欣赞,疑义相与析。
63、暧暧远人村,依依墟里烟,狗吠 深巷中 ,鸡鸣 桑树颠 。 64、一生复能几,倏如流电惊。 65、少无适俗韵,性本爱丘山。
61、奢侈是舒适的,否则就不是奢侈 。——CocoCha nel 62、少而好学,如日出之阳;壮而好学 ,如日 中之光 ;志而 好学, 如炳烛 之光。 ——刘 向 63、三军可夺帅也,匹夫不可夺志也。 ——孔 丘 64、人生就是学校。在那里,与其说好 的教师 是幸福 ,不如 说好的 教师是 不幸。 ——海 贝尔 65、接受挑战,就可以享受胜利的喜悦 。——杰纳勒 尔·乔治·S·巴顿
谢谢!
ห้องสมุดไป่ตู้

杭电acm初学者课件

杭电acm初学者课件



2006年5月,浙江省第二届“舜宇”杯大学生程序设计大 赛
2006年11~12月,第31届ACM首尔、北京、上海和西安赛 区比赛 今年…
9 2019/5/16


预期赛事(今后每年)

3~4月,举行校内大赛(暨选拔赛) 5月,参加浙江省大学生程序设计大赛 11月,参加ACM/ICPC亚洲区比赛(至 少参加4~5个赛区的比赛) 另外,每学期至少有三次月赛以及适当 的练习赛

26 2019/5/16
Hdoj_1089源代码:
#include <stdio.h> int main() {
int a,b;
while(scanf("%d %d",&a, &b) != EOF) printf("%d\n",a+b);
}
27 2019/5/16
本类输入解决方案:

C语法: while(scanf("%d %d",&a, &b) != EOF) { .... } C++语法: while( cin >> a >> b ) { .... }
4 2019/5/16
我们说的“ACM” 是什么?
5 2019/5/16
ACM/ICPC:
ACM主办的国际大学生程序设计竞赛 (International Collegiate Programming Contest),简称 ACM / ICPC,自从1977年开始至今已经连续举 办31届。其宗旨是提供一个让大学生向IT界展 示自己分析问题和解决问题的能力的绝好机会, 让下一代IT天才可以接触到其今后工作中将要 用到的各种软件。 现在,ACM / ICPC已成为世界各国大学生中最 具影响力的国际计算机赛事。(非官方)

acm程序设计 线段树和间断树

acm程序设计 线段树和间断树

Segment trees and interval treesLecture5Antoine Vigneronantoine.vigneron@jouy.inra.frINRAOutline referencetextbook chapter10D.Mount Lectures13and24segment trees⇒stabbing queries⇒rectangle intersection interval trees⇒improvementhigher dimensionStabbing queries orthogonal range searching:data points,queryrectanglestabbing problem:data rectangles,query pointin one dimensioninput:a set of n intervals,a query point qoutput:the k intervals that contain qin I R da boxb is isothetic iff it can be writtenb=[x1,x 1]×[x2,x 2]×...×[x d,x d]in other words it is axis–parallelinput:a set of n isothetic boxes,a query point qoutput:the k boxes that contain qMotivationin graphics and databases,objects are often stored intheir bounding boxquery:which objects does point x belong to?firstfind objects whose bounding boxes intersect xSegment treesSegmenttreea data structure to store intervals,or segments in I R2allows to answer stabbingqueriesin I R 2:report the segments that intersect a query vertical line lreportedreportedreportedlquery time:O (log n +k)space usage:O (n log n)preprocessing time:O (n log n )Notationslet S=(s1,s2,...s n)be a set of segments in I R2let E be the set of the x–coordinates of the endpoints ofthe segments of Swe assume general position,that is:|E|=2nfirst sort E in increasing orderE={e1<e2<...e2n}AtomicintervalsE splits I R into2n+1atomicintervals:[−∞,e1][e i,e i+1]for i∈{1,2,...2n−1}[e2n,∞]these are the leaves of the segmenttreeInternalnodesthe segment tree T is a balanced binarytree each internal node u with children v and v is associatedwith an interval I u =I v ∪Iv an elementary interval is an interval associated with a node of T (it can be an atomicinterval)I v I vExamplePartitioning asegmentlet s∈S be a segment whose endpoints havex–coordinates e i and ej[e i,e j]is split into several elementaryintervalsthey are chosen as close as possible to theroots is stored in each node associated with these elementaryintervalsStandard listseach node u is associated with a standard list L ulet e i<e j be the x–coordinates of the endpoints of s∈Sthen s is stored in L u iff I u⊂[e i,e j]andI parent(u)⊂[e i,e j](see previous slide and next slide)ExampleAlgorithm ReportStabbing(u,x l) Input:root u of T,x–coordinate of l Output:segments in S that cross l1.if u==NULL2.then return3.output L u4.if x l∈I u.left5.then ReportStabbing(u.left,x l)6.if x l∈I u.right7.then ReportStabbing(u.right,x l)it clearly takes O(k+log n)timeInserting asegmentInsertion in a segment tree Algorithm Insert(u,s)Input:root u of T,segment s.Endpoints of s have x–coordinates x−<x+1.if I u⊂[x−,x+]2.then insert s into L u3.else4.if[x−,x+]∩I u.left=∅5.then Insert(u.left,s)6.if[x−,x+]∩I u.right=∅7.then Insert(u.right,s)Propertys is stored at most twice at each level ofTproof:bycontradictionif s stored at more than2nodes at leveli let u be the leftmost such node,u be therightmost let v be another node at level i containingsu v uv.parentthen I v.parent⊂[x−,x+]so s cannot be stored at vAnalysisproperty of previous slide impliesspace usage:O(n log n)insertion in O(log n)time(similar proof:four nodes atmost are visited at each level)actually space usage isΘ(n log n)(example?)query time:O(k+log n)preprocessingsort endpoints:Θ(n log n)timebuild empty segment tree over these endpoints:O(n)timeinsert n segments into T:O(n log n)timeoverall:Θ(n log n)preprocessing timeRectangle intersectionProblem statementinput:a set B of n isothetic boxes in I R2output:all the intersecting pairs in B2using segment trees,we give an O(n log n+k)timealgorithm when k is the number of intersecting pairs note:this is optimalnote:faster than our line segment intersection algorithmspace usage:Θ(n log n)due to segment treesspace usage is not optimal(O(n)is possible withoptimal query time and preprocessing time)Exampleb 2b 5output:(b 1,b 3),(b 2,b 3),(b 2,b 4),(b 3,b 4)Two kinds ofintersectionsoverlapintersecting edges⇒reduces to intersectionreporting for isotheticsegmentsinclusion we can find them using stabbing queriesReporting overlapsequivalent to reporting intersecting edgesplane sweep approachsweep line status:BBST containing the horizontal linesegments that intersect the sweep line,by increasingy–coordinateseach time a vertical line segment is encountered,reportintersection by range searching in the BBST preprocessing time:O(n log n)for sorting endpointsrunning time:O(k+n log n)Reporting inclusionsstill using plane sweepsweep line status:the boxes that intersect the sweepline l,in a segment tree with respect to y–coordinates the endpoints are the y–coordinates of the horizontaledges of the boxesat a given time,only rectangles that intersect l are inthe segment treewe can perform insertion and deletions in a segmenttree in O(log n)timeeach time a vertex of a box is encountered,perform astabbing query in the segment treeRemarksat each step a box intersection can be reported severaltimesin addition there can be overlap and vertex stabbing abox at the same timeto obtain each intersecting pair only once,make somesimple checks(how?)Interval treesIntroductioninterval trees allow to perform stabbing queries in onedimensionquery time:O(k+log n)preprocessing time:O(n log n)space:O(n)reference:D.Mount notes,page100(vertical linestabbing queries)to page103(not including vertical segment stabbing queries)Preliminarylet x med be the median ofES l:segments of S that are completely to the left of xmedS med:segments of S that contain xmedS r:segments of S that are completely to the right of x medx medS medS rS lData structurerecursive data structureleft child of the root:interval tree storing S lright child of the root:interval tree storing S rat the root of the interval tree,we store S med in two listsM L is sorted according to the coordinate of the leftendpoint(in increasing order)M R is sorted according to the coordinate of the rightendpoint(in decreasing order)Examples 1s 2s 3s 5s 7s 4s 6Interval tree ons 3and s 5Interval tree on M l =(s 4,s 6,s 1)M r =(s 1,s 4,s 6)s 2and s 7Stabbing queriesquery:x q,find the intervals that contain x qif x q<x med thenScan M l in increasing order,and report segmentsthat are stabbed.When x q becomes smaller than the x–coordinate of the current left endpoint,stop.recurse on S lif x q>x medanalogous,but on the right sideAnalysisquery timesize of the subtree divided by at least two at eachlevelscanning through M l or M r:proportional to thenumber of reported intervalsconclusion:O(k+log n)timespace usage:O(n)(each segment is stored in two lists,and the tree is balanced)preprocessing time:easy to do it in O(n log n)timeStabbing queries in higherdimensionApproachin I R d,a set B of n boxesfor a query point qfind all the boxes that contain itwe use a multi–level segment treeinductive definition,induction on dfirst,we store B in a segment tree T with respect tox1–coordinatefor all node u of T,associate a(d−1)–dimensionalmulti–level segment tree over L u,with respect to (x2,x3...x d)Performing queriessearch for q in Tfor all nodes in the search path,query recursively the(d−1)–dimensional multi–level segment treethere are log n such queriesby induction on d,we can prove thatquery time:O(k+log d n)space usage:O(n log d n)preprocessing time:O(n log d n)Improvementsfractional cascading at the deepest level of the tree:gains a factor log n on the query time boundinterval trees at the deepest level:gains log n on the space bound。

浙大ACM简单讲解 1

浙大ACM简单讲解 1
总结比赛经验团队合作角色定位任务分工队长codingdebug一题读两遍赛前准备赛前准备熟悉题目编译器差异vc60编译通过但是在zoj编译出错devc中longlong的输入格式为i64d而zoj中为lld打印功能比赛进行时比赛策略全面开花vs孤注一掷输入输出输出格式如时间02d
Lecture I 比赛经验与编程技巧
– 得到右数第k位值 – 把右数第k位置1 – 把右数第k位取反 – 把最右边的1置0 – 把最右边的0置1 – 判断奇偶(最低位) (x >> k) & 1 x | (1 << k) x ^ (1 << k) x & (x – 1) x | (x + 1) x&1
枚举所有选取方式
• 枚举n个数中选一些的方式
熟悉比赛环境
• 熟悉交题、查看返回信息的方法 • 熟悉Runs中的Search功能 • 打印功能
比赛进行时
比赛策略
• 寻找简单题 • 看题、交流题意 • 观察气球、ranklist,跟风 • 打印、纸上调试 • 学会放弃 • 全面开花 vs 孤注一掷
输入输出
• 几种输入输出要求 • 负数和-1的区别 • 行内拆分(使用stringstream) • 输出格式,如时间”%02d:%02d:%02d”
• 申请新节点,并加到p节点后
– next[p] = ind++;
Standard C++ Library
C++风格头文件
• #include • #include • #include • #include • #include • #include • #include <iostream> <cstdio> <string> <cstring> <cmath> <vector> <algorithm>

浙大ACM简单讲解

浙大ACM简单讲解

• dp[2] = dp[1] + 1 + 1 = 4
• dp[3] = min{ dp[1] + 1 * 2 + 1, dp[2] + 1 + 1}

=5
G Islands
• 给定最多100个点的有向图,问有多少种加 边的方式,可以让最后整个图只由一些环 构成,没有多余的边。一个环至少要两个 点。
Lecture III 赛题解析
杭航@ZJU mailto:Hang.Hang.ZJU@
QQ:54621399
Part I The 10th ZJU Programming Contest
概述
• A Clock
472/614
• B CAPTCHA
87/399
• C Runaway t Bulb
• 三分法 • 求单峰函数在(x1,x2)上的极值 • 令a=(x2-x1)/3 + x1 • 令b=(x2-x1)/3 *2 + x1 • 如果f(a)>f(b),则用b更新x2 • 如果f(a)<f(b),则用a更新x1 • 每次把区间缩小成原来的2/3,最后收敛到
B Light Bulb
• 方法一:数学解法,列出函数式子,求导 解决
• 方法二:猜测L是关于某个参数的单峰函数, 可以采用三分法。
B Light Bulb
• 二分法 • 求单调函数在(x1,x2)上的零点 • 令x=(x1+x1)/2 • 如果f(x)和f(x1)同号,则用x更新x1 • 如果f(x)和f(x2)同号,则用x更新x2 • 每次把区间缩小成原来的1/2,最后收敛到
H Break Out
• 预处理后,变成了一个背包问题,背包的 容量最多为200,有最多200个物品,每个 物品不同容量的时候有不同的价值,求使 得总价值最高的方案。

ACM培训资料

ACM培训资料

ACM培训资料目录第一篇入门篇 (3)第1章新手入门 (5)1ACM国际大学生程序设计竞赛简介 (5)2ACM竞赛需要的知识 (8)3团队配合 (14)4练习、练习、再练习 (15)5对新手的一些建议 (16)第2章C++语言介绍 (22)1C++简介 (22)2变量 (23)3C++数据类型 (25)4C++操作符 (30)5数组 (35)6字符数组 (38)7字串操作函数 (41)8过程控制 (45)9C++中的函数 (54)10函数规则 (59)第3章STL简介 (61)1泛型程序设计 (61)2STL 的组成 (67)第二篇算法篇 (102)第1章基本算法 (103)1算法初步 (103)2分治算法 (115)3搜索算法 (124)4贪婪算法 (135)第2章进阶算法 (165)1数论基础 (165)2图论算法 (180)3计算几何基础 (222)第三篇实践篇 (246)第1章《多边形》 (247)第2章《灌溉问题》 (255)第3章《L GAME》 (263)第4章《NUMBER》解题报告 (271)第5章《J OBS》解题报告 (275)第6章《包裹运送》 (283)第7章《桶的摆放》 (290)第一篇入门篇练就坚实的基础,总有一天……我们可以草木皆兵!第1章新手入门1ACM国际大学生程序设计竞赛简介1.1背景与历史1970年在美国TexasA&M大学举办了首次区域竞赛,从而拉开了国际大学生程序设计竞赛的序幕。

1977年,该项竞赛被分为两个级别,即区域赛和总决赛,这便是现代ACM竞赛的开始。

在亚洲、美国、欧洲、太平洋地区均设有区域赛点。

1995至1996年,来自世界各地的一千多支高校的代表队参加了ACM区域竞赛。

ACM 大学生程序设计竞赛由美国计算机协会(ACM)举办,旨在向全世界的大学生提供一个展示和锻炼其解决问题和运用计算机能力的机会,现已成为全世界范围内历史最悠久、规模最大的大学生程序设计竞赛。

ACM 程序设计竞赛入门:第4讲 简单数据结构

ACM 程序设计竞赛入门:第4讲 简单数据结构

2020/12/10
7
1. 并查集
初始状态 : {1} {2} {3} {4} {5} {6} {7} {8} {9}
输入关系 分离集合
(2,4)
{2,4}
•最后我们得到3 个集合{1,2,3,4},
(5,7) (1,3) (8,9) (1,2) (5,6)
{2,4} {5,7} {1,3} {2,4} {5,7} {1,3} {2,4} {5,7} {8,9} {1,2,3,4} {5,7} {8,9} {1,2,3,4} {5,6,7} {8,9}
两个强盗是同一团伙的条件是当且仅当他们是朋 友。现在给你一些关于强盗们的信息,问你最多 有多少个强盗团伙。
2020/12/10
3
1. 并查集
1.1 引例
输入格式 (Input Format):
输入的第一行是一个整数N(2<=N<=1000),表 示强盗的个数(从1编号到N)。 第二行 M(1<=M<=5000),表示关于强盗的信息条数。 以下M行,每行可能是F p q或是E p q(1<=p q<=N),F表示p和q是朋友,E表示p和q是敌人 。输入数据保证不会产生信息的矛盾。
第四讲 简单数据结构
2020/12/10
1
主要内容
并查集 树状数组 线段树
2020/12/10
2
1. 并查集
1.1 引例
题目描述:
1920年的芝加哥,出现了一群强盗。如果两个强 盗遇上了,那么他们要么是朋友,要么是敌人。 而且有一点是肯定的,就是:
我朋友的朋友是我的朋友。
我敌人的敌人也是我的朋友。
效果:任意顺序的合并操作以后,包 含k个节点的树的最大高度不超过lgk

ACM 竞赛培训

ACM 竞赛培训





河南赛区赛题3

【 样例 】
河南赛区赛题4-枚举





在灾区,多数人已经受伤,缺水,少食物,精神处在崩溃的边缘。很多人的生存条件仅能维持几天。灾民 需要帐篷、衣物、食品和医疗器材、药品等物资。14日上午,中央军委委员、空军司令员许其亮组织召开 空军首长办公会,将空军下一步救灾重点确定为抢救伤员、空投、空运。空军各部队都派出多架运输机, 准备向灾区空运急需物品。 现在已知四种打包过的急需物品重量分别为C1, C2, C3,C4 ,数量分别为M1,M2,M3,M4包。一架运输 机的载重量为W, 现在各部队关心将一架运输机装满共有多少种运载方案,以便调度进行空运。 比如C={ 100, 200, 500, 1000},M={ 3, 2, 3, 1 }, W=1000, 一共有4种运载方案: 1000=100+100+100+200+500 1000=100+200+200+500 1000=500+500 1000=1000 【标准输入】 C1 C2 C3 C4 N 其中 N为空运的部队数 Mi1 Mi2 Mi3 Mi4 Wi 表示各运载部队需空运的4种物品数量Mi 和各自运输机的载重量Wi i=1,2,….. , N
河南赛区赛题2

【样 例】
河南赛区赛题3-排序查找算法

【试题三】 密码破译 某组织欲破获一个外星人的密码,密码由一定长度的字串组成。此组织拥有一些破译此密码 的长度不同的钥匙,若两个钥匙的长度之和恰好为此密码的长度,则此密码被成功破译。现 在就请你编程找出能破译此密码的两个钥匙。 【标准输入】 第一行: N N为钥匙的个数(1<=N<=1000) 第二行: L L为密码的长度 以下有N行: Ai 每一行是一把钥匙的长度 i=1,2,……,N 【标准输出】 若无法找到破译此密码的钥匙,则输出0 若找到两把破译的钥匙,则输出文件有两行,分别为两把钥匙的编号,按从小到大输出。若 有多种破译方案,则只输出包含起始编号最小的一组即可。 [【约束条件】 (1)1<= N,L,Ai <=1000(i=1, 2, ….., N ) (2)时间限制: 1000MS
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档