Submission #1117288

# Submission time Handle Problem Language Result Execution time Memory
1117288 2024-11-23T08:30:56 Z ntdaccode Tourism (JOI23_tourism) C++17
35 / 100
5000 ms 31164 KB
#include<bits/stdc++.h>
#define fori(i,a,b) for(int i=a;i<=b;i++)
#define int long long
#define pb push_back

using namespace std;
typedef pair<int,int> ii;
typedef tuple<int,int,int> tp;
const int M=1e5+10;
const int N=1e3+10;
const int mod=1e9+7;

int n,m,q,c[M];
vector<int> ke[M];

namespace sub {
   int num[M],tail[M],cnt = 0, up[M][20], d[M];
   void dfs(int u,int p)
   {
      num[u] = ++cnt;
      for(int i =1;i <= 18; i++) up[u][i] = up[up[u][i-1]][i-1];
      for(int v : ke[u]) {
         if(v == p) continue;
         up[v][0] = u;
         d[v] = d[u] + 1;
         dfs(v,u);
      }
      tail[u] = cnt;
   }

   bool anc(int u,int v)
   {
      return num[u] <= num[v] && tail[u] >= tail[v];
   }

   int lca(int u,int v)
   {
      if(anc(u,v)) return u;
      if(anc(v,u)) return v;
      for(int i = 18;i >=0 ; i--) if(!anc(up[u][i],v)) u = up[u][i];
      return up[u][0];
   }

   int dist(int u,int v)
   {
      int x = lca(u,v);
      return d[u] + d[v] - 2 * d[x];
   }

   bool cmp(const int &a,const int &b)
   {
      return num[a] < num[b];
   }
   void solve()
   {
      up[1][0] = 1;
     dfs(1,0);
     while(q--) {
        int l,r;
         cin >> l >> r;
        // cout << l << " " << r << "\n";

         vector<int> Q;
         for(int i = l;i <= r; i++) Q.pb(c[i]);

         sort(Q.begin(),Q.end(),cmp);
         int sz = Q.size();
         for(int i = 0;i <= sz - 2; i++) {
            Q.pb(lca(Q[i],Q[i + 1]));
         }
         //for(int v : Q) cout << v << " ";
         sort(Q.begin(),Q.end(),cmp);
         Q.resize(unique(Q.begin(),Q.end()) - Q.begin());
         stack<int> sk;
         int kq = 0;
         for(int v : Q) {
            while(!sk.empty() && !anc(sk.top(),v)) sk.pop();
            if(!sk.empty()) kq += dist(sk.top(),v);
            sk.push(v);
         }
         cout << kq  + 1 << "\n";

     }
   }
}

namespace sub3
{
   vector<tp> Q;
   vector<int> pos[M];
   int ma[M],mi[M];

   void dnc(int l,int r,vector<tp> &cur) {
      if(l > r) return ;
      int mid = (l + r)/2;
      vector<tp> L;
      vector<tp> R;
      int u,v,idx;

      fori(i,0,cur.size() - 1) {
            tie(u,v,idx) = cur[i];
            if(v < mid) {
               L.pb({u,v,idx});
               continue;
            }
            if(u > mid) {
               R.pb({u,v,idx});
               continue;
            }
            pos[u].pb(idx);
            pos[v].pb(idx);
      }
      cur.clear();
      int mx = 0,mn = 1e9;
      for(int i = mid;i >= l; i--) {
         mx = max(mx,c[i]);
         mn = min(mn,c[i]);
         for(int idx : pos[i]) {
            ma[idx] = max(ma[idx],mx);
            mi[idx] = min(mi[idx],mn);
         }
      }
      mx = 0,mn = 1e9;
      for(int i = mid;i <= r; i++) {
         mx = max(mx,c[i]);
         mn = min(mn,c[i]);
         for(int idx : pos[i]) {
            ma[idx] = max(ma[idx],mx);
            mi[idx] = min(mi[idx],mn);
         }
      }
      for(int i = l;i <= r; i++) pos[i].clear();

      if(L.size()) dnc(l, mid - 1, L);
      if(R.size()) dnc(mid + 1 , r, R);
   }

   void solve()
   {
      for(int i = 1; i <= q; i++) {
         int l,r;
         cin >> l >> r;
         Q.pb({l,r,i});
      }
      for(int i =1; i <= q; i++) mi[i] = 1e9;
      dnc(1,m,Q);
      for(int i = 1;i <= q; i++) {
            cout << ma[i]  - mi[i] + 1 << "\n";

      }
   }
}

int32_t main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if(fopen("1.inp","r"))
  {
    freopen("1.inp","r",stdin);
    freopen("1.out","w",stdout);
  }
  #define task "tourism"
  if(fopen(task".inp","r"))
  {
    freopen(task".inp","r",stdin);
    freopen(task".out","w",stdout);
  }

  cin >> n >> m >> q;
  int ok = 0;
  for(int i = 1;i <= n - 1; i++) {
      int u,v;
      cin >> u >> v;
      ke[u].pb(v);
      ke[v].pb(u);
      if(u != i || v != i + 1) ok = 1;
  }
  for(int i = 1;i <= m; i++) cin >> c[i];
  if(ok == 1) sub :: solve();
  else sub3 :: solve();

}

Compilation message

tourism.cpp: In function 'void sub3::dnc(long long int, long long int, std::vector<std::tuple<long long int, long long int, long long int> >&)':
tourism.cpp:2:34: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::tuple<long long int, long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    2 | #define fori(i,a,b) for(int i=a;i<=b;i++)
......
  100 |       fori(i,0,cur.size() - 1) {
      |            ~~~~~~~~~~~~~~~~~~     
tourism.cpp:100:7: note: in expansion of macro 'fori'
  100 |       fori(i,0,cur.size() - 1) {
      |       ^~~~
tourism.cpp: In function 'int32_t main()':
tourism.cpp:161:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |     freopen("1.inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~
tourism.cpp:162:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |     freopen("1.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~
tourism.cpp:167:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  167 |     freopen(task".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:168:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  168 |     freopen(task".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6736 KB Output is correct
2 Correct 3 ms 6168 KB Output is correct
3 Correct 3 ms 6224 KB Output is correct
4 Correct 7 ms 6224 KB Output is correct
5 Correct 6 ms 6224 KB Output is correct
6 Correct 6 ms 6224 KB Output is correct
7 Correct 7 ms 6464 KB Output is correct
8 Correct 6 ms 6480 KB Output is correct
9 Correct 9 ms 6480 KB Output is correct
10 Correct 9 ms 6480 KB Output is correct
11 Correct 9 ms 6480 KB Output is correct
12 Correct 19 ms 6480 KB Output is correct
13 Correct 19 ms 6652 KB Output is correct
14 Correct 18 ms 6512 KB Output is correct
15 Correct 8 ms 6480 KB Output is correct
16 Correct 8 ms 6648 KB Output is correct
17 Correct 8 ms 6480 KB Output is correct
18 Correct 7 ms 6992 KB Output is correct
19 Correct 7 ms 7160 KB Output is correct
20 Correct 7 ms 6992 KB Output is correct
21 Correct 8 ms 6480 KB Output is correct
22 Correct 8 ms 6480 KB Output is correct
23 Correct 8 ms 6480 KB Output is correct
24 Correct 9 ms 6480 KB Output is correct
25 Correct 8 ms 6480 KB Output is correct
26 Correct 9 ms 6648 KB Output is correct
27 Correct 4 ms 6400 KB Output is correct
28 Correct 3 ms 6992 KB Output is correct
29 Correct 4 ms 6480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6736 KB Output is correct
2 Correct 3 ms 6168 KB Output is correct
3 Correct 3 ms 6224 KB Output is correct
4 Correct 7 ms 6224 KB Output is correct
5 Correct 6 ms 6224 KB Output is correct
6 Correct 6 ms 6224 KB Output is correct
7 Correct 7 ms 6464 KB Output is correct
8 Correct 6 ms 6480 KB Output is correct
9 Correct 9 ms 6480 KB Output is correct
10 Correct 9 ms 6480 KB Output is correct
11 Correct 9 ms 6480 KB Output is correct
12 Correct 19 ms 6480 KB Output is correct
13 Correct 19 ms 6652 KB Output is correct
14 Correct 18 ms 6512 KB Output is correct
15 Correct 8 ms 6480 KB Output is correct
16 Correct 8 ms 6648 KB Output is correct
17 Correct 8 ms 6480 KB Output is correct
18 Correct 7 ms 6992 KB Output is correct
19 Correct 7 ms 7160 KB Output is correct
20 Correct 7 ms 6992 KB Output is correct
21 Correct 8 ms 6480 KB Output is correct
22 Correct 8 ms 6480 KB Output is correct
23 Correct 8 ms 6480 KB Output is correct
24 Correct 9 ms 6480 KB Output is correct
25 Correct 8 ms 6480 KB Output is correct
26 Correct 9 ms 6648 KB Output is correct
27 Correct 4 ms 6400 KB Output is correct
28 Correct 3 ms 6992 KB Output is correct
29 Correct 4 ms 6480 KB Output is correct
30 Correct 155 ms 6992 KB Output is correct
31 Correct 228 ms 6736 KB Output is correct
32 Correct 350 ms 6736 KB Output is correct
33 Correct 331 ms 6736 KB Output is correct
34 Correct 335 ms 6736 KB Output is correct
35 Correct 1069 ms 6736 KB Output is correct
36 Correct 985 ms 6944 KB Output is correct
37 Correct 1024 ms 7456 KB Output is correct
38 Correct 283 ms 6992 KB Output is correct
39 Correct 292 ms 6992 KB Output is correct
40 Correct 277 ms 7504 KB Output is correct
41 Correct 988 ms 7064 KB Output is correct
42 Correct 950 ms 7072 KB Output is correct
43 Correct 960 ms 7008 KB Output is correct
44 Correct 284 ms 6992 KB Output is correct
45 Correct 289 ms 7248 KB Output is correct
46 Correct 298 ms 6984 KB Output is correct
47 Correct 947 ms 6992 KB Output is correct
48 Correct 918 ms 6736 KB Output is correct
49 Correct 943 ms 7484 KB Output is correct
50 Correct 267 ms 6736 KB Output is correct
51 Correct 277 ms 7248 KB Output is correct
52 Correct 283 ms 6736 KB Output is correct
53 Correct 258 ms 7464 KB Output is correct
54 Correct 276 ms 6952 KB Output is correct
55 Correct 272 ms 6984 KB Output is correct
56 Correct 4 ms 6492 KB Output is correct
57 Correct 5 ms 6736 KB Output is correct
58 Correct 5 ms 6736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6224 KB Output is correct
2 Correct 3 ms 6224 KB Output is correct
3 Correct 4 ms 6444 KB Output is correct
4 Correct 45 ms 18120 KB Output is correct
5 Correct 42 ms 17084 KB Output is correct
6 Correct 42 ms 17120 KB Output is correct
7 Correct 59 ms 21948 KB Output is correct
8 Correct 59 ms 22056 KB Output is correct
9 Correct 58 ms 22204 KB Output is correct
10 Correct 61 ms 22216 KB Output is correct
11 Correct 60 ms 22060 KB Output is correct
12 Correct 51 ms 20208 KB Output is correct
13 Correct 45 ms 19900 KB Output is correct
14 Correct 47 ms 19956 KB Output is correct
15 Correct 37 ms 18620 KB Output is correct
16 Correct 75 ms 25272 KB Output is correct
17 Correct 39 ms 17132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6736 KB Output is correct
2 Correct 47 ms 19004 KB Output is correct
3 Correct 56 ms 20136 KB Output is correct
4 Correct 57 ms 21836 KB Output is correct
5 Correct 108 ms 31164 KB Output is correct
6 Correct 105 ms 30764 KB Output is correct
7 Correct 112 ms 29612 KB Output is correct
8 Correct 105 ms 29256 KB Output is correct
9 Correct 101 ms 29000 KB Output is correct
10 Correct 91 ms 28488 KB Output is correct
11 Correct 90 ms 28496 KB Output is correct
12 Correct 77 ms 29348 KB Output is correct
13 Correct 82 ms 28608 KB Output is correct
14 Correct 95 ms 29064 KB Output is correct
15 Correct 85 ms 29548 KB Output is correct
16 Correct 85 ms 29892 KB Output is correct
17 Correct 88 ms 30656 KB Output is correct
18 Correct 83 ms 30656 KB Output is correct
19 Correct 108 ms 30396 KB Output is correct
20 Correct 96 ms 29884 KB Output is correct
21 Correct 102 ms 29724 KB Output is correct
22 Correct 95 ms 27636 KB Output is correct
23 Correct 100 ms 27412 KB Output is correct
24 Correct 118 ms 29020 KB Output is correct
25 Correct 92 ms 28796 KB Output is correct
26 Correct 100 ms 28680 KB Output is correct
27 Correct 100 ms 28524 KB Output is correct
28 Correct 113 ms 27212 KB Output is correct
29 Correct 91 ms 28744 KB Output is correct
30 Correct 109 ms 27208 KB Output is correct
31 Correct 102 ms 28976 KB Output is correct
32 Correct 84 ms 28488 KB Output is correct
33 Correct 97 ms 28868 KB Output is correct
34 Correct 84 ms 30280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6224 KB Output is correct
2 Correct 4 ms 6224 KB Output is correct
3 Correct 4 ms 6528 KB Output is correct
4 Execution timed out 5061 ms 23056 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6736 KB Output is correct
2 Correct 3 ms 6168 KB Output is correct
3 Correct 3 ms 6224 KB Output is correct
4 Correct 7 ms 6224 KB Output is correct
5 Correct 6 ms 6224 KB Output is correct
6 Correct 6 ms 6224 KB Output is correct
7 Correct 7 ms 6464 KB Output is correct
8 Correct 6 ms 6480 KB Output is correct
9 Correct 9 ms 6480 KB Output is correct
10 Correct 9 ms 6480 KB Output is correct
11 Correct 9 ms 6480 KB Output is correct
12 Correct 19 ms 6480 KB Output is correct
13 Correct 19 ms 6652 KB Output is correct
14 Correct 18 ms 6512 KB Output is correct
15 Correct 8 ms 6480 KB Output is correct
16 Correct 8 ms 6648 KB Output is correct
17 Correct 8 ms 6480 KB Output is correct
18 Correct 7 ms 6992 KB Output is correct
19 Correct 7 ms 7160 KB Output is correct
20 Correct 7 ms 6992 KB Output is correct
21 Correct 8 ms 6480 KB Output is correct
22 Correct 8 ms 6480 KB Output is correct
23 Correct 8 ms 6480 KB Output is correct
24 Correct 9 ms 6480 KB Output is correct
25 Correct 8 ms 6480 KB Output is correct
26 Correct 9 ms 6648 KB Output is correct
27 Correct 4 ms 6400 KB Output is correct
28 Correct 3 ms 6992 KB Output is correct
29 Correct 4 ms 6480 KB Output is correct
30 Correct 155 ms 6992 KB Output is correct
31 Correct 228 ms 6736 KB Output is correct
32 Correct 350 ms 6736 KB Output is correct
33 Correct 331 ms 6736 KB Output is correct
34 Correct 335 ms 6736 KB Output is correct
35 Correct 1069 ms 6736 KB Output is correct
36 Correct 985 ms 6944 KB Output is correct
37 Correct 1024 ms 7456 KB Output is correct
38 Correct 283 ms 6992 KB Output is correct
39 Correct 292 ms 6992 KB Output is correct
40 Correct 277 ms 7504 KB Output is correct
41 Correct 988 ms 7064 KB Output is correct
42 Correct 950 ms 7072 KB Output is correct
43 Correct 960 ms 7008 KB Output is correct
44 Correct 284 ms 6992 KB Output is correct
45 Correct 289 ms 7248 KB Output is correct
46 Correct 298 ms 6984 KB Output is correct
47 Correct 947 ms 6992 KB Output is correct
48 Correct 918 ms 6736 KB Output is correct
49 Correct 943 ms 7484 KB Output is correct
50 Correct 267 ms 6736 KB Output is correct
51 Correct 277 ms 7248 KB Output is correct
52 Correct 283 ms 6736 KB Output is correct
53 Correct 258 ms 7464 KB Output is correct
54 Correct 276 ms 6952 KB Output is correct
55 Correct 272 ms 6984 KB Output is correct
56 Correct 4 ms 6492 KB Output is correct
57 Correct 5 ms 6736 KB Output is correct
58 Correct 5 ms 6736 KB Output is correct
59 Correct 5 ms 6224 KB Output is correct
60 Correct 3 ms 6224 KB Output is correct
61 Correct 4 ms 6444 KB Output is correct
62 Correct 45 ms 18120 KB Output is correct
63 Correct 42 ms 17084 KB Output is correct
64 Correct 42 ms 17120 KB Output is correct
65 Correct 59 ms 21948 KB Output is correct
66 Correct 59 ms 22056 KB Output is correct
67 Correct 58 ms 22204 KB Output is correct
68 Correct 61 ms 22216 KB Output is correct
69 Correct 60 ms 22060 KB Output is correct
70 Correct 51 ms 20208 KB Output is correct
71 Correct 45 ms 19900 KB Output is correct
72 Correct 47 ms 19956 KB Output is correct
73 Correct 37 ms 18620 KB Output is correct
74 Correct 75 ms 25272 KB Output is correct
75 Correct 39 ms 17132 KB Output is correct
76 Correct 3 ms 6736 KB Output is correct
77 Correct 47 ms 19004 KB Output is correct
78 Correct 56 ms 20136 KB Output is correct
79 Correct 57 ms 21836 KB Output is correct
80 Correct 108 ms 31164 KB Output is correct
81 Correct 105 ms 30764 KB Output is correct
82 Correct 112 ms 29612 KB Output is correct
83 Correct 105 ms 29256 KB Output is correct
84 Correct 101 ms 29000 KB Output is correct
85 Correct 91 ms 28488 KB Output is correct
86 Correct 90 ms 28496 KB Output is correct
87 Correct 77 ms 29348 KB Output is correct
88 Correct 82 ms 28608 KB Output is correct
89 Correct 95 ms 29064 KB Output is correct
90 Correct 85 ms 29548 KB Output is correct
91 Correct 85 ms 29892 KB Output is correct
92 Correct 88 ms 30656 KB Output is correct
93 Correct 83 ms 30656 KB Output is correct
94 Correct 108 ms 30396 KB Output is correct
95 Correct 96 ms 29884 KB Output is correct
96 Correct 102 ms 29724 KB Output is correct
97 Correct 95 ms 27636 KB Output is correct
98 Correct 100 ms 27412 KB Output is correct
99 Correct 118 ms 29020 KB Output is correct
100 Correct 92 ms 28796 KB Output is correct
101 Correct 100 ms 28680 KB Output is correct
102 Correct 100 ms 28524 KB Output is correct
103 Correct 113 ms 27212 KB Output is correct
104 Correct 91 ms 28744 KB Output is correct
105 Correct 109 ms 27208 KB Output is correct
106 Correct 102 ms 28976 KB Output is correct
107 Correct 84 ms 28488 KB Output is correct
108 Correct 97 ms 28868 KB Output is correct
109 Correct 84 ms 30280 KB Output is correct
110 Correct 4 ms 6224 KB Output is correct
111 Correct 4 ms 6224 KB Output is correct
112 Correct 4 ms 6528 KB Output is correct
113 Execution timed out 5061 ms 23056 KB Time limit exceeded
114 Halted 0 ms 0 KB -