SlideShare a Scribd company logo
2013 JOI春合宿 Day2
  Spy(スパイ) 解説
               2013/03/21
               山下 洋史
               @utatakiyoshi
おさらい

    ・JOI社とIOI社の社員の木構造が与えられる

    ・リーダーの配下にあるメンバー全員からな
    るプロジェクト達が与えられる

         j1                         i2    s4


               j2           i1 s2        i3
                    r4
              r2
                    j3


2             2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
おさらい

    ・2つの木の対応する頂点について,いくつ同
    じグループに入っているかを数える

    ・社員数:N<=2000,プロジェクト数:M<=500000


           1                      2

               2             1         3


                    3
                           3はどちらも青に入っている

3              2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
小課題 1 :
社員aはプロジェクトbにいる?



    ・「社員aがプロジェクトbに属
    する」がすべてのa,bについて分                    R1

    かればO(NM)でできる

    ・木を上に辿ってリーダーに辿
    り着くか調べる
                                    a
    ・1人1プロジェクトにつき
    O(N),合計でO(N^2M)

4           2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
小課題 2 (a):
社員xはどのプロジェクトにいる?



    ・1人の社員については,1
    回木を辿っておけば全てのプ
                                         R1
    ロジェクトについて属するか
                                    R2
    どうか分かる                                    R3



    ・1回木を辿る:O(N+M)
                                     x
    ・それがN回でO(N(N+M))
                                x は R1 と R2 に属する
    ・その後,O(NM)
5            2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
小課題 2 (b):
プロジェクトxにはどの社員がいる?




    ・リーダーから深さ優先探
    索すればメンバーが列挙で                         R1


    きる

    ・プロジェクトの個数回探
    索:O(NM)

    ・その後,O(NM)

6            2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
問題点



    ・プロジェクトが M=500000 もある

    ・1つあたりO(1)時間しかかけたくない!




7           2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
木の嬉しい順番

    ・木を深さ優先探索で走査                                                 A


    →ノードを訪れた順番に並べる                                                       B

                                                             C               E
    ・木が列のデータになる
                                                                         D
                                                     F


                                             G               H



     A   B   C   F   G   F   H   F   C   B       D   B   E   B       A


8                        2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
何が嬉しい?

                                                                    A

    ・部分木が根ノードを両端                                                            B
    にする区間に対応する                                                  C               E
     ・最小共通祖先(LCA)問題への応用(省略)
                                                                            D
     ・Euler Tour Technique                              F
      と呼ばれることがあります

                                                G               H



        A   B   C   F   G   F   H   F   C   B       D   B   E   B       A


9                           2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
やりたいこと
         1                                          1   2   3   2   1

                 2
                                                2   0   0   1   0   0
                         3                      1   0   1   2   1   0
                                                2   0   0   1   0   0
                     2
                                                3   0   0   1   0   0
             1               3                  2   0   0   1   0   0


     ・projectの組が長方形になり,同じ番号の社員の組がマス目になる

     →マス目の上に長方形がいくつ重なっているか を数える

10                               2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
長方形を足すのは大変

      1                                          1   2   3   2   1

              2
                                             2   1   1   2   1   1
                      3                      1   1   1   2   1   1
                                             2   1   1   2   1   1
                  2
                                             3   1   1   2   1   1
          1               3                  2   1   1   2   1   1


     ・表はO(N)×O(N)の大きさ 長方形も同じ

     ・愚直にマス目を塗るとO(N^2M)かかる
11                            2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
領域に値を足す:
      1 次元の場合

     ・配列サイズ: N ・区間の個数:Q ・各区間に 1 を足す

                0   0   0   0   0   0   0   0    0   0

         O(Q)       始点に 1 を足し,終点の後ろに -1 を足す

                0   0   1   0   1   0   0   -1   0   -1

         O(N)       累積和を取る

                0   0   1   1   2   2   2   1    1   0


12                  2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
領域に値を足す:
     2 次元の場合(1)


     こうなってほしい                             こうするために
         1   2   3   2   1                    1   2   3   2   1

     2   0   0   1   -1 0    横に累積和をとる     2   0   0   1   0   0
     1   0   1   1   -1 -1                1   0   1   2   1   0
     2   0   0   1   -1 0                 2   0   0   1   0   0
     3   0   0   1   -1 0                 3   0   0   1   0   0
     2   0   0   1   -1 0                 2   0   0   1   0   0



13                       2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
領域に値を足す:
     2 次元の場合(2)


     こうするために                             こうなってほしい
         1   2   3   2   1                    1   2   3    2    1

     2   0   0   1   -1 0    縦に累積和をとる     2   0   0   1    -1   0
     1   0   1   1   -1 -1                1   0   1   0    0    -1
     2   0   0   1   -1 0                 2   0   -1 0     0    1
     3   0   0   1   -1 0                 3   0   0   0    0    0
     2   0   0   1   -1 0                 2   0   0   0    0    0
                                                      -1   1


14                       2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
領域に値を足す:
     2 次元の場合(3)


       長方形について
          1     -1                   1   2   3    2    1

                                 2   0   0   1    -1   0
                                 1   0   1   0    0    -1
          -1     1
                                 2   0   -1 0     0    1
       を足せばよい.                   3   0   0   0    0    0
 (長方形1つあたり定数時間)                  2   0   0   0    0    0
      (こうした手法はJOI界隈では                        -1   1
       imos法と呼ばれています)

15              2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
解法のまとめ

     ・Euler Tour Techniqueを用いてJOI木,IOI木をそれぞれ1列化

     ・各社員の現れる位置を1つ覚えておく

     ・各頂点を根とした部分木の区間も覚えておく

     ・2N×2Nの配列をとり,各プロジェクトの組について

      [JOI木での区間] [IOI木での区間]の長方形を 塗る

      (1,-1を適切なところに足す)

     ・縦横に累積和をとる

     ・各aについて,配列の[jaが現れる位置][iaが現れる位置]を出力する

16                2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
計算量?

     ・2つの木のDFS O(N)

     ・長方形の個数 定数時間 O(M)
                                      (実は行き掛け順でよいの
     ・2回累積和をとる O((2N)^2)               でもっと少なく出来る)


     ・M <= 500000, N <= 2000

     ・間に合う

17               2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
得点分布
     10


      8


      6


      4


      2


      0
          0   10   20   30   40   50   60   70   80   90   100


18                      2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
DPによる解法

     ・詳しくは説明しません

     ・キーとなる式がこちら
     dp[j][i] =
       project[j       ][i       ]
     +      dp[j.parent][i       ]
     +      dp[j       ][i.parent]
     -      dp[j.parent][i.parent]
     ・スライドをアップロードするのでこれを見て考えて
     みてください
19              2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
関連するJOIの問題



     ・少し幸せ になる木の順番
     ・2010-day4 高速道路(Highway) ・2011-day3 報告(Report)

     ・領域をO(1)で塗る
     ・2012 本選4 釘(Nails) ・2010-day4 プラグ(Plugs)




20                  2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

2013 JOI春合宿 Day2 Spy(スパイ) 解説

  • 1. 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21 山下 洋史 @utatakiyoshi
  • 2. おさらい ・JOI社とIOI社の社員の木構造が与えられる ・リーダーの配下にあるメンバー全員からな るプロジェクト達が与えられる j1 i2 s4 j2 i1 s2 i3 r4 r2 j3 2 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 3. おさらい ・2つの木の対応する頂点について,いくつ同 じグループに入っているかを数える ・社員数:N<=2000,プロジェクト数:M<=500000 1 2 2 1 3 3 3はどちらも青に入っている 3 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 4. 小課題 1 : 社員aはプロジェクトbにいる? ・「社員aがプロジェクトbに属 する」がすべてのa,bについて分 R1 かればO(NM)でできる ・木を上に辿ってリーダーに辿 り着くか調べる a ・1人1プロジェクトにつき O(N),合計でO(N^2M) 4 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 5. 小課題 2 (a): 社員xはどのプロジェクトにいる? ・1人の社員については,1 回木を辿っておけば全てのプ R1 ロジェクトについて属するか R2 どうか分かる R3 ・1回木を辿る:O(N+M) x ・それがN回でO(N(N+M)) x は R1 と R2 に属する ・その後,O(NM) 5 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 6. 小課題 2 (b): プロジェクトxにはどの社員がいる? ・リーダーから深さ優先探 索すればメンバーが列挙で R1 きる ・プロジェクトの個数回探 索:O(NM) ・その後,O(NM) 6 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 7. 問題点 ・プロジェクトが M=500000 もある ・1つあたりO(1)時間しかかけたくない! 7 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 8. 木の嬉しい順番 ・木を深さ優先探索で走査 A →ノードを訪れた順番に並べる B C E ・木が列のデータになる D F G H A B C F G F H F C B D B E B A 8 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 9. 何が嬉しい? A ・部分木が根ノードを両端 B にする区間に対応する C E ・最小共通祖先(LCA)問題への応用(省略) D ・Euler Tour Technique F  と呼ばれることがあります G H A B C F G F H F C B D B E B A 9 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 10. やりたいこと 1 1 2 3 2 1 2 2 0 0 1 0 0 3 1 0 1 2 1 0 2 0 0 1 0 0 2 3 0 0 1 0 0 1 3 2 0 0 1 0 0 ・projectの組が長方形になり,同じ番号の社員の組がマス目になる →マス目の上に長方形がいくつ重なっているか を数える 10 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 11. 長方形を足すのは大変 1 1 2 3 2 1 2 2 1 1 2 1 1 3 1 1 1 2 1 1 2 1 1 2 1 1 2 3 1 1 2 1 1 1 3 2 1 1 2 1 1 ・表はO(N)×O(N)の大きさ 長方形も同じ ・愚直にマス目を塗るとO(N^2M)かかる 11 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 12. 領域に値を足す: 1 次元の場合 ・配列サイズ: N ・区間の個数:Q ・各区間に 1 を足す 0 0 0 0 0 0 0 0 0 0 O(Q) 始点に 1 を足し,終点の後ろに -1 を足す 0 0 1 0 1 0 0 -1 0 -1 O(N) 累積和を取る 0 0 1 1 2 2 2 1 1 0 12 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 13. 領域に値を足す: 2 次元の場合(1) こうなってほしい こうするために 1 2 3 2 1 1 2 3 2 1 2 0 0 1 -1 0 横に累積和をとる 2 0 0 1 0 0 1 0 1 1 -1 -1 1 0 1 2 1 0 2 0 0 1 -1 0 2 0 0 1 0 0 3 0 0 1 -1 0 3 0 0 1 0 0 2 0 0 1 -1 0 2 0 0 1 0 0 13 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 14. 領域に値を足す: 2 次元の場合(2) こうするために こうなってほしい 1 2 3 2 1 1 2 3 2 1 2 0 0 1 -1 0 縦に累積和をとる 2 0 0 1 -1 0 1 0 1 1 -1 -1 1 0 1 0 0 -1 2 0 0 1 -1 0 2 0 -1 0 0 1 3 0 0 1 -1 0 3 0 0 0 0 0 2 0 0 1 -1 0 2 0 0 0 0 0 -1 1 14 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 15. 領域に値を足す: 2 次元の場合(3) 長方形について 1 -1 1 2 3 2 1 2 0 0 1 -1 0 1 0 1 0 0 -1 -1 1 2 0 -1 0 0 1 を足せばよい. 3 0 0 0 0 0 (長方形1つあたり定数時間) 2 0 0 0 0 0 (こうした手法はJOI界隈では -1 1 imos法と呼ばれています) 15 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 16. 解法のまとめ ・Euler Tour Techniqueを用いてJOI木,IOI木をそれぞれ1列化 ・各社員の現れる位置を1つ覚えておく ・各頂点を根とした部分木の区間も覚えておく ・2N×2Nの配列をとり,各プロジェクトの組について  [JOI木での区間] [IOI木での区間]の長方形を 塗る  (1,-1を適切なところに足す) ・縦横に累積和をとる ・各aについて,配列の[jaが現れる位置][iaが現れる位置]を出力する 16 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 17. 計算量? ・2つの木のDFS O(N) ・長方形の個数 定数時間 O(M) (実は行き掛け順でよいの ・2回累積和をとる O((2N)^2) でもっと少なく出来る) ・M <= 500000, N <= 2000 ・間に合う 17 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 18. 得点分布 10 8 6 4 2 0 0 10 20 30 40 50 60 70 80 90 100 18 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 19. DPによる解法 ・詳しくは説明しません ・キーとなる式がこちら dp[j][i] = project[j ][i ] + dp[j.parent][i ] + dp[j ][i.parent] - dp[j.parent][i.parent] ・スライドをアップロードするのでこれを見て考えて みてください 19 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21
  • 20. 関連するJOIの問題 ・少し幸せ になる木の順番 ・2010-day4 高速道路(Highway) ・2011-day3 報告(Report) ・領域をO(1)で塗る ・2012 本選4 釘(Nails) ・2010-day4 プラグ(Plugs) 20 2013 JOI春合宿 Day2 Spy(スパイ) 解説 2013/03/21