Submission #798346

# Submission time Handle Problem Language Result Execution time Memory
798346 2023-07-30T15:46:48 Z vjudge1 Two Currencies (JOI23_currencies) C++14
100 / 100
1720 ms 57544 KB
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)

struct FenwickTree{
   int n; vector<int> t;

   void init(int _n){
      n=_n;
      t.assign(n+1, 0);
   }

   void update(int pos, int val){
      for (int i=pos; i<=n; i+=i&(-i)) t[i]+=val;
   }

   void update(int l, int r, int val){
      update(l, val);
      update(r+1, -val);
   }

   int get(int pos){
      int ans=0;
      for (int i=pos; i; i-=i&(-i)) ans+=t[i];
      return ans;
   }
} bit, bit2;

struct nigga{
   int s, t, x, y;
   nigga(int a=0, int b=0, int c=0, int d=0): s(a), t(b), x(c), y(d){};
};

const int N=1e5+1, LG=17;
int n, m, q, tdfs, tin[N], tout[N], up[N][LG], dep[N], l[N], r[N], ans[N];
vector<int> g[N], vv[N];
nigga a[N];

void dfs(int u){
   tin[u]=++tdfs;
   dep[u]=dep[up[u][0]]+1;
   for (int v:g[u]) if (v!=up[u][0]){
      up[v][0]=u;
      for (int k=1; k<LG; ++k) up[v][k]=up[up[v][k-1]][k-1];
      dfs(v);
   }
   tout[u]=tdfs;
}

int lca(int u, int v){
   if (dep[u]!=dep[v]){
      if (dep[u]<dep[v]) swap(u, v);
      int d=dep[u]-dep[v];
      for (int k=0; k<LG; ++k) if (d>>k&1) u=up[u][k];
   }
   if (u==v) return u;
   for (int k=LG-1; k>=0; --k) if (up[u][k]!=up[v][k]) u=up[u][k], v=up[v][k];
   return up[u][0];
}

void solve(int tc){
   // cout << "Case #" << tc << ": ";
   cin >> n >> m >> q;
   vector<pair<int, int>> edges;
   for (int i=1; i<n; ++i){
      int x, y; cin >> x >> y;
      g[x].push_back(y);
      g[y].push_back(x);
      edges.emplace_back(x, y);
   }
   dfs(1);
   vector<pair<int, int>> pts{{0, 0}};
   bit2.init(n);
   for (int i=1; i<=m; ++i){
      int p, c; cin >> p >> c; --p;
      int x=edges[p].first, y=edges[p].second;
      if (tin[x]>tin[y]) swap(x, y);
      pts.emplace_back(c, y);
      bit2.update(tin[y], tout[y], 1);
   }
   sort(all(pts));
   for (int i=1; i<=q; ++i){
      cin >> a[i].s >> a[i].t >> a[i].x >> a[i].y;
      l[i]=0, r[i]=sz(pts)-1;
   }
   for (int rep=0; rep<LG; ++rep){
      for (int i=1; i<=q; ++i) if (l[i]<=r[i]) vv[(l[i]+r[i])>>1].push_back(i);
      bit.init(n);
      for (int i=0; i<sz(pts); ++i){
         if (i) bit.update(tin[pts[i].second], tout[pts[i].second], pts[i].first);
         for (int j:vv[i]){
            int u=a[j].s, v=a[j].t, w=lca(u, v);
            int sum_silver=bit.get(tin[u])+bit.get(tin[v])-(bit.get(tin[w])<<1);
            if (sum_silver<=a[j].y) l[j]=i+1;
            else r[j]=i-1;
         }
         vv[i].clear();
      }
   }
   for (int i=1; i<=q; ++i) vv[r[i]].push_back(i);
   bit.init(n);
   for (int i=0; i<sz(pts); ++i){
      if (i) bit.update(tin[pts[i].second], tout[pts[i].second], 1);
      for (int j:vv[i]){
         int u=a[j].s, v=a[j].t, w=lca(u, v);
         int cnt_silver=bit.get(tin[u])+bit.get(tin[v])-(bit.get(tin[w])<<1);
         int cnt_pts=bit2.get(tin[u])+bit2.get(tin[v])-(bit2.get(tin[w])<<1);
         int cnt_gold=cnt_pts-cnt_silver;
         ans[j]=a[j].x-min(cnt_gold, a[j].x+1);
      }
      vv[i].clear();
   }
   for (int i=1; i<=q; ++i) cout << ans[i] << '\n';
}

int32_t main(){
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   int ntests=1;
   // cin >> ntests;
   for (int i=1; i<=ntests; ++i) solve(i);
   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 8 ms 8804 KB Output is correct
6 Correct 9 ms 9044 KB Output is correct
7 Correct 9 ms 8808 KB Output is correct
8 Correct 10 ms 9064 KB Output is correct
9 Correct 10 ms 9084 KB Output is correct
10 Correct 10 ms 9036 KB Output is correct
11 Correct 10 ms 9064 KB Output is correct
12 Correct 10 ms 9044 KB Output is correct
13 Correct 9 ms 9064 KB Output is correct
14 Correct 10 ms 9044 KB Output is correct
15 Correct 10 ms 9064 KB Output is correct
16 Correct 10 ms 9072 KB Output is correct
17 Correct 11 ms 9044 KB Output is correct
18 Correct 10 ms 9044 KB Output is correct
19 Correct 9 ms 9044 KB Output is correct
20 Correct 12 ms 9012 KB Output is correct
21 Correct 9 ms 9044 KB Output is correct
22 Correct 9 ms 9044 KB Output is correct
23 Correct 9 ms 9044 KB Output is correct
24 Correct 9 ms 9044 KB Output is correct
25 Correct 9 ms 9044 KB Output is correct
26 Correct 9 ms 8948 KB Output is correct
27 Correct 9 ms 9068 KB Output is correct
28 Correct 8 ms 9076 KB Output is correct
29 Correct 9 ms 9044 KB Output is correct
30 Correct 10 ms 9044 KB Output is correct
31 Correct 10 ms 9044 KB Output is correct
32 Correct 10 ms 9044 KB Output is correct
33 Correct 9 ms 9168 KB Output is correct
34 Correct 9 ms 9064 KB Output is correct
35 Correct 9 ms 9104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8160 KB Output is correct
2 Correct 10 ms 9080 KB Output is correct
3 Correct 11 ms 9044 KB Output is correct
4 Correct 10 ms 9068 KB Output is correct
5 Correct 580 ms 44992 KB Output is correct
6 Correct 682 ms 44336 KB Output is correct
7 Correct 679 ms 45792 KB Output is correct
8 Correct 555 ms 41940 KB Output is correct
9 Correct 532 ms 41428 KB Output is correct
10 Correct 971 ms 53048 KB Output is correct
11 Correct 945 ms 52868 KB Output is correct
12 Correct 930 ms 52764 KB Output is correct
13 Correct 890 ms 52960 KB Output is correct
14 Correct 912 ms 52908 KB Output is correct
15 Correct 1481 ms 56936 KB Output is correct
16 Correct 1324 ms 57284 KB Output is correct
17 Correct 1523 ms 56796 KB Output is correct
18 Correct 1472 ms 52724 KB Output is correct
19 Correct 1451 ms 53084 KB Output is correct
20 Correct 1419 ms 52740 KB Output is correct
21 Correct 719 ms 53980 KB Output is correct
22 Correct 707 ms 53820 KB Output is correct
23 Correct 715 ms 54012 KB Output is correct
24 Correct 742 ms 53760 KB Output is correct
25 Correct 797 ms 52432 KB Output is correct
26 Correct 753 ms 52152 KB Output is correct
27 Correct 721 ms 52500 KB Output is correct
28 Correct 351 ms 52308 KB Output is correct
29 Correct 333 ms 50632 KB Output is correct
30 Correct 450 ms 51012 KB Output is correct
31 Correct 423 ms 50368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 9 ms 9096 KB Output is correct
3 Correct 9 ms 9116 KB Output is correct
4 Correct 9 ms 9128 KB Output is correct
5 Correct 684 ms 47956 KB Output is correct
6 Correct 636 ms 47172 KB Output is correct
7 Correct 592 ms 41504 KB Output is correct
8 Correct 1276 ms 57268 KB Output is correct
9 Correct 1162 ms 57544 KB Output is correct
10 Correct 1323 ms 57484 KB Output is correct
11 Correct 741 ms 57356 KB Output is correct
12 Correct 671 ms 57268 KB Output is correct
13 Correct 678 ms 57300 KB Output is correct
14 Correct 268 ms 57216 KB Output is correct
15 Correct 267 ms 57236 KB Output is correct
16 Correct 455 ms 57396 KB Output is correct
17 Correct 483 ms 57180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 8 ms 8804 KB Output is correct
6 Correct 9 ms 9044 KB Output is correct
7 Correct 9 ms 8808 KB Output is correct
8 Correct 10 ms 9064 KB Output is correct
9 Correct 10 ms 9084 KB Output is correct
10 Correct 10 ms 9036 KB Output is correct
11 Correct 10 ms 9064 KB Output is correct
12 Correct 10 ms 9044 KB Output is correct
13 Correct 9 ms 9064 KB Output is correct
14 Correct 10 ms 9044 KB Output is correct
15 Correct 10 ms 9064 KB Output is correct
16 Correct 10 ms 9072 KB Output is correct
17 Correct 11 ms 9044 KB Output is correct
18 Correct 10 ms 9044 KB Output is correct
19 Correct 9 ms 9044 KB Output is correct
20 Correct 12 ms 9012 KB Output is correct
21 Correct 9 ms 9044 KB Output is correct
22 Correct 9 ms 9044 KB Output is correct
23 Correct 9 ms 9044 KB Output is correct
24 Correct 9 ms 9044 KB Output is correct
25 Correct 9 ms 9044 KB Output is correct
26 Correct 9 ms 8948 KB Output is correct
27 Correct 9 ms 9068 KB Output is correct
28 Correct 8 ms 9076 KB Output is correct
29 Correct 9 ms 9044 KB Output is correct
30 Correct 10 ms 9044 KB Output is correct
31 Correct 10 ms 9044 KB Output is correct
32 Correct 10 ms 9044 KB Output is correct
33 Correct 9 ms 9168 KB Output is correct
34 Correct 9 ms 9064 KB Output is correct
35 Correct 9 ms 9104 KB Output is correct
36 Correct 4 ms 8160 KB Output is correct
37 Correct 10 ms 9080 KB Output is correct
38 Correct 11 ms 9044 KB Output is correct
39 Correct 10 ms 9068 KB Output is correct
40 Correct 580 ms 44992 KB Output is correct
41 Correct 682 ms 44336 KB Output is correct
42 Correct 679 ms 45792 KB Output is correct
43 Correct 555 ms 41940 KB Output is correct
44 Correct 532 ms 41428 KB Output is correct
45 Correct 971 ms 53048 KB Output is correct
46 Correct 945 ms 52868 KB Output is correct
47 Correct 930 ms 52764 KB Output is correct
48 Correct 890 ms 52960 KB Output is correct
49 Correct 912 ms 52908 KB Output is correct
50 Correct 1481 ms 56936 KB Output is correct
51 Correct 1324 ms 57284 KB Output is correct
52 Correct 1523 ms 56796 KB Output is correct
53 Correct 1472 ms 52724 KB Output is correct
54 Correct 1451 ms 53084 KB Output is correct
55 Correct 1419 ms 52740 KB Output is correct
56 Correct 719 ms 53980 KB Output is correct
57 Correct 707 ms 53820 KB Output is correct
58 Correct 715 ms 54012 KB Output is correct
59 Correct 742 ms 53760 KB Output is correct
60 Correct 797 ms 52432 KB Output is correct
61 Correct 753 ms 52152 KB Output is correct
62 Correct 721 ms 52500 KB Output is correct
63 Correct 351 ms 52308 KB Output is correct
64 Correct 333 ms 50632 KB Output is correct
65 Correct 450 ms 51012 KB Output is correct
66 Correct 423 ms 50368 KB Output is correct
67 Correct 4 ms 8148 KB Output is correct
68 Correct 9 ms 9096 KB Output is correct
69 Correct 9 ms 9116 KB Output is correct
70 Correct 9 ms 9128 KB Output is correct
71 Correct 684 ms 47956 KB Output is correct
72 Correct 636 ms 47172 KB Output is correct
73 Correct 592 ms 41504 KB Output is correct
74 Correct 1276 ms 57268 KB Output is correct
75 Correct 1162 ms 57544 KB Output is correct
76 Correct 1323 ms 57484 KB Output is correct
77 Correct 741 ms 57356 KB Output is correct
78 Correct 671 ms 57268 KB Output is correct
79 Correct 678 ms 57300 KB Output is correct
80 Correct 268 ms 57216 KB Output is correct
81 Correct 267 ms 57236 KB Output is correct
82 Correct 455 ms 57396 KB Output is correct
83 Correct 483 ms 57180 KB Output is correct
84 Correct 583 ms 41716 KB Output is correct
85 Correct 544 ms 36964 KB Output is correct
86 Correct 468 ms 36084 KB Output is correct
87 Correct 1034 ms 52944 KB Output is correct
88 Correct 1014 ms 53024 KB Output is correct
89 Correct 977 ms 53012 KB Output is correct
90 Correct 972 ms 53068 KB Output is correct
91 Correct 983 ms 53232 KB Output is correct
92 Correct 1720 ms 55532 KB Output is correct
93 Correct 1611 ms 56624 KB Output is correct
94 Correct 1499 ms 52708 KB Output is correct
95 Correct 1542 ms 52768 KB Output is correct
96 Correct 1499 ms 52808 KB Output is correct
97 Correct 1528 ms 52716 KB Output is correct
98 Correct 772 ms 53568 KB Output is correct
99 Correct 773 ms 53300 KB Output is correct
100 Correct 750 ms 53724 KB Output is correct
101 Correct 770 ms 53548 KB Output is correct
102 Correct 831 ms 53104 KB Output is correct
103 Correct 804 ms 53292 KB Output is correct
104 Correct 821 ms 53472 KB Output is correct
105 Correct 380 ms 50368 KB Output is correct
106 Correct 364 ms 52032 KB Output is correct
107 Correct 433 ms 50132 KB Output is correct
108 Correct 435 ms 50188 KB Output is correct