Guest User

Ratinho

a guest
Apr 1st, 2024
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #pragma GCC optimize (03)
  4. // #define int long long
  5. #define ld long double
  6. #define tii tuple<int,int,int>
  7. #define tii4 tuple<int,int,int,int>
  8. #define pii pair<int,int>
  9. #define ff first
  10. #define ss second
  11. #define all(x) x.begin(),x.end()
  12. #define pb push_back
  13. #define ppb pop_back
  14. #define ppf pop_front
  15. #define mp make_pair
  16. const int maxn=1e5+5, maxval=1e6+60, INF=0x3f3f3f3f, MOD=1e9+7;
  17.  
  18. int depth[maxn], pai[25][maxn], bit[maxval], l[maxn], r[maxn];
  19. int n;
  20. vector<int> queries[maxn];
  21.  
  22. bool comp_l(int a, int b){
  23.     return l[a] < l[b];
  24. }
  25. bool comp_depth(int a, int b){
  26.     return depth[a] < depth[b];
  27. }
  28.  
  29.  
  30. int find_depth(int u){
  31.     if(u==0) return depth[u]=0;
  32.     if(depth[u]!=0) return depth[u];
  33.     return depth[u] = 1+find_depth(pai[0][u]);
  34. }
  35.  
  36. void upd(int id, int val){
  37.     if(id==0) return;
  38.     for(; id<maxval; id+=id&-id) bit[id]+=val;
  39. }
  40.  
  41. int fquery(int id){
  42.     int rt=0;
  43.     for(; id>0; id-=id&-id) rt+=bit[id];
  44.     return rt;
  45. }
  46. int query(int v){
  47.     return !!(fquery(r[v])-fquery(l[v]-1));
  48. }
  49.  
  50. int lca(int a, int b){
  51.     if(depth[a]<depth[b]) swap(a,b);
  52.     int d = depth[a]-depth[b];
  53.     for(int log=0; log<20; log++){
  54.         if(d&(1<<log)) a=pai[log][a];
  55.     }
  56.     if(a==b) return a;
  57.  
  58.     for(int log=19; log>=0; log--){
  59.         int pa = pai[log][a];
  60.         int pb = pai[log][b];
  61.         if(pa!=pb) a=pa, b=pb;
  62.     }
  63.     return pai[0][a];
  64. }
  65.  
  66. void solve(){
  67.     cin >> n;
  68.     vector<tii4> pontos;
  69.     pontos.pb({0,0,0,0}), pontos.pb({1e6+5,0,1,0});
  70.     for(int i=1; i<=n; i++){
  71.         int a,b,x; cin >> a >> x >> b >> x;
  72.         a++,b++;
  73.         l[i]=a, r[i]=b;
  74.         pontos.pb({a,i,0,0}), pontos.pb({b,i,1,0});
  75.     }
  76.  
  77.     int q; cin >> q;
  78.     for(int i=0; i<q; i++){
  79.         int k; cin >> k;
  80.         for(int j=0; j<k; j++){
  81.             int x; cin >> x, x++;
  82.             pontos.pb({x,i,0,1});
  83.         }
  84.     }
  85.  
  86.     sort(all(pontos));
  87.     vector<int> abertos;
  88.     for(auto[pos,id,func, qry] : pontos){
  89.         if(qry){
  90.             assert(abertos.size());
  91.             // if(abertos.back())
  92.             queries[id].pb(abertos.back());
  93.         }
  94.         else if(func==0){
  95.             if(abertos.size()) pai[0][id] = abertos.back();
  96.             abertos.pb(id);
  97.         }
  98.         else abertos.ppb();
  99.     }
  100.  
  101.     for(int i=1; i<=n; i++){
  102.         if(depth[i]==0) find_depth(i);
  103.     }
  104.  
  105.     for(int log=1; log<20; log++){
  106.         for(int i=1; i<=n; i++){
  107.             pai[log][i] = pai[log-1][pai[log-1][i]];
  108.         }
  109.     }
  110.  
  111.  
  112.     for(int i=0; i<q; i++){
  113.         vector<int> vertex=queries[i];
  114.         sort(all(vertex));
  115.         vertex.erase(unique(all(vertex)), vertex.end());
  116.         // if(vertex.size()==1){
  117.         //  cout << 0 << '\n';
  118.         //  continue;
  119.         // }
  120.  
  121.         sort(all(vertex), comp_l);
  122.         int highest_lca=depth[vertex.front()];
  123.         for(int j=0; j<vertex.size()-1; j++){
  124.             highest_lca = min(highest_lca, depth[lca(vertex[j], vertex[j+1])]);
  125.         }
  126.  
  127.         int ans = -highest_lca;
  128.         // cout << "a" << ans << endl;
  129.  
  130.         sort(all(vertex), comp_depth);
  131.         reverse(all(vertex));
  132.         for(int x : vertex){
  133.             if(x==0) continue;
  134.             ans += (query(x)==0);
  135.             int cur=x;
  136.             for(int log=19; log>=0; log--){
  137.                 int p = pai[log][cur];
  138.                 if(p!=0 && !query(p))
  139.                     ans += (1<<log), cur = pai[log][cur];
  140.             }
  141.             upd(l[x],1);
  142.         }
  143.  
  144.         cout << ans << '\n';
  145.  
  146.         for(int x : vertex) upd(l[x],-1);
  147.     }
  148. }
  149.  
  150. int32_t main(){
  151.     ios::sync_with_stdio(false); cin.tie(0);
  152.     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  153.     int t=1;
  154.     // cin >> t;
  155.     while(t--){
  156.         solve();
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment