CSES - Datatähti Open 2019 - Results
Submission details
Task:Sum
Sender:2xJelly
Submission time:2019-01-18 13:33:07 +0200
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#10
Test results
testverdicttime
#10.03 sdetails
#20.02 sdetails
#30.02 sdetails
#40.04 sdetails
#50.03 sdetails
#60.02 sdetails
#70.02 sdetails
#80.01 sdetails
#90.02 sdetails
#100.02 sdetails
#110.04 sdetails
#120.01 sdetails
#130.04 sdetails
#140.02 sdetails
#150.03 sdetails
#160.04 sdetails
#170.02 sdetails
#180.03 sdetails
#190.02 sdetails
#200.03 sdetails
#210.02 sdetails
#220.02 sdetails
#230.02 sdetails
#240.02 sdetails
#250.02 sdetails
#260.03 sdetails
#270.02 sdetails
#280.02 sdetails

Code

#include <iostream>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <string>


using namespace std;

typedef long long LL;

const LL MAX = 200005;
const LL INF = (1000 * 1000 * 1000) + 50;

LL n, q;
LL h[MAX] = {}, nxt[MAX] = {};
vector<LL> target[MAX] = {};

LL b_Search(LL pos, LL x)
{
    LL answer = -1;
    LL lo = 0;
    LL hi = target[pos].size()-1;
    while(lo <= hi)
    {
        LL mid = lo + (hi-lo)/2;
        if(target[pos][mid] <= x)
        {
            answer = max(answer, mid);
            lo = mid+1;
        }else hi = mid-1;
    }
    return answer + 1;
}


int main(void)
{
  cin >> n >> q;
  for(LL i0 = 0; i0 < n; i0++)cin >> h[i0];

  stack<pair<LL, LL> > st;
  st.push(make_pair(0, h[0]));
  for(LL i0 = 1; i0 < n; i0++)
  {
      while(!st.empty() && st.top().second < h[i0])
      {
          nxt[st.top().first] = i0;
          st.pop();
      }
      st.push(make_pair(i0, h[i0]));
  }
  while(!st.empty() )
  {
      nxt[st.top().first] = INF;
      st.pop();
  }

  for(LL i0 = 0; i0 < n; i0++)
  {
      LL cur = i0;
      LL tempo = 0;
      while(tempo <= 250)
      {
          tempo++;
          LL t = nxt[cur];
          target[i0].push_back(t);
          cur = t;
          if(cur == INF)break;
      }
  }

  for(LL i0 = 0; i0 < q; i0++)
  {
      LL i, j, ans = 1;
      cin >> i >> j;
      i--;
      j--;
      while(i < j)
      {
          LL SIZE = target[i].size();
          LL last = target[i][SIZE-1];
          if(last != INF && last <= j)
          {
              ans += SIZE;
              i = last;
          }else
          {
              ans += b_Search(i, j);
              break;
          }
      }
      cout << ans << endl;
  }

  return 0;
}

Test details

Test 1

Verdict:

input
1

correct output
0

user output
(empty)

Test 2

Verdict:

input
2

correct output
0

user output
(empty)

Test 3

Verdict:

input
3

correct output
0

user output
(empty)

Test 4

Verdict:

input
4

correct output
0

user output
(empty)

Test 5

Verdict:

input
5

correct output
0

user output
(empty)

Test 6

Verdict:

input
6

correct output
1

user output
(empty)

Test 7

Verdict:

input
7

correct output
1

user output
(empty)

Test 8

Verdict:

input
8

correct output
2

user output
(empty)

Test 9

Verdict:

input
9

correct output
3

user output
(empty)

Test 10

Verdict:

input
10

correct output
4

user output
(empty)

Test 11

Verdict:

input
20

correct output
24

user output
(empty)

Test 12

Verdict:

input
30

correct output
61

user output
(empty)

Test 13

Verdict:

input
40

correct output
114

user output
(empty)

Test 14

Verdict:

input
50

correct output
184

user output
(empty)

Test 15

Verdict:

input
60

correct output
271

user output
(empty)

Test 16

Verdict:

input
70

correct output
374

user output
(empty)

Test 17

Verdict:

input
80

correct output
494

user output
(empty)

Test 18

Verdict:

input
90

correct output
631

user output
(empty)

Test 19

Verdict:

input
100

correct output
784

user output
(empty)

Test 20

Verdict:

input
200

correct output
3234

user output
(empty)

Test 21

Verdict:

input
300

correct output
7351

user output
(empty)

Test 22

Verdict:

input
400

correct output
13134

user output
(empty)

Test 23

Verdict:

input
500

correct output
20584

user output
(empty)

Test 24

Verdict:

input
600

correct output
29701

user output
(empty)

Test 25

Verdict:

input
700

correct output
40484

user output
(empty)

Test 26

Verdict:

input
800

correct output
52934

user output
(empty)

Test 27

Verdict:

input
900

correct output
67051

user output
(empty)

Test 28

Verdict:

input
1000

correct output
82834

user output
(empty)