Lottery

시간 제한5초메모리 제한2048 MB

요약
짝수 길이의 각 구간에 대해, 뽑은 빨간 공과 파란 공의 총수가 같아지는 최대 횟수를 구한다.
난이도

보통10점 중 7점

유형
누적 합, 그리디, 수학
정답자
아직 제출이 없습니다

문제

JOI-kun is planning a lottery event. In this lottery event, an even number of bags will be used. Each bag initially contains some red balls and blue balls (possibly zero). Participants will keep coming to the lottery event until at least one of the bags becomes empty. Each participant draws one ball from each bag. If the total number of red and blue balls they have drawn ends up being equal, they receive one prize. Balls drawn are not returned to the bags.

As preparation, JOI-kun has prepared NN bags, numbered from 00 to N−1N − 1. Bag ii (0≤i≤N−10 ≤ i ≤ N − 1) contains X_iX\_i red balls and Y_iY\_i blue balls.

In the lottery event, some of the NN bags will be selected for use. There are QQ plans for selecting bags. In the jj-th plan (1≤j≤Q1 ≤ j ≤ Q), the bags L_j,L_j+1,…,R_jL\_j , L\_{j + 1}, \dots , R\_j are used. Here, R_j−L_j+1R\_j − L\_j + 1 is even.

To prepare the prizes for the event, JOI-kun wants to know, for each plan, the maximum possible total number of prizes participants can obtain. Write a program that, given the bag contents and the plans, returns the maximum possible total number of prizes participants can obtain for each plan.

제한

  • 2≤N≤200,0002 ≤ N ≤ 200\\, 000.
  • 1≤Q≤500,0001 ≤ Q ≤ 500\\, 000.
  • 0≤X_i≤1090 ≤ X\_i ≤ 10^9 (0≤i≤N−10 ≤ i ≤ N − 1).
  • 0≤Y_i≤1090 ≤ Y\_i ≤ 10^9 (0≤i≤N−10 ≤ i ≤ N − 1).
  • 0≤L_j<R_j≤N−10 ≤ L\_j < R\_j ≤ N − 1 (1≤j≤Q1 ≤ j ≤ Q).
  • R_j−L_j+1R\_j − L\_j + 1 is even (1≤j≤Q1 ≤ j ≤ Q).
  • Given values are all integers.

예제2

  1. 예제 1

    입력
    5 3
    2 1 3 1 0
    1 1 0 2 0
    0 3
    1 4
    2 3
    
    예상 출력
    2
    0
    2
    
  2. 예제 2

    입력
    6 5
    1 3 3 2 1 0
    1 2 1 1 2 1
    0 1
    1 2
    1 4
    2 5
    4 5
    
    예상 출력
    2
    3
    3
    1
    1