Advertisement
Guest User

Untitled

a guest
Feb 7th, 2021
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC optimize ("unroll-loops")
  3. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  4. #pragma comment(linker, "/STACK:1024000000,1024000000")
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define BU 775
  8. int l[300005],stt[300005],ent[300005],ans[300005],cnt[300005],bl[BU],dp[20][300005],dep[300005];
  9. vector<int> v[300005],e;
  10. void dfs(int node,int p)
  11. {
  12.     stt[node]=e.size();
  13.     e.push_back(l[node]);
  14.     dp[0][node]=p;
  15.     for (int i=1;i<20;i++)
  16.     dp[i][node]=dp[i-1][dp[i-1][node]];
  17.     dep[node]=dep[p]+1;
  18.     for (int u:v[node])
  19.     {
  20.         if (u!=p)
  21.         dfs(u,node);
  22.     }
  23.     ent[node]=e.size();
  24.     e.push_back(l[node]);
  25. }
  26. int lca(int u,int v)
  27. {
  28.     if (dep[u]<dep[v])
  29.     swap(u,v);
  30.     int dist=dep[u]-dep[v];
  31.     for (int i=0;i<20;i++)
  32.     {
  33.         if (dist&(1<<i))
  34.         u=dp[i][u];
  35.     }
  36.     if (u==v)
  37.     return u;
  38.     for (int i=19;i>=0;i--)
  39.     {
  40.         if (dp[i][u]!=dp[i][v])
  41.         {
  42.             v=dp[i][v];
  43.             u=dp[i][u];
  44.         }
  45.     }
  46.     return dp[0][u];
  47. }
  48. struct query
  49. {
  50.     int u,v,l,r,lo,ro,i;
  51.     bool f;
  52.     query(int uu,int vv,int ii,int ll,int rr)
  53.     {
  54.         u=uu;
  55.         v=vv;
  56.         i=ii;
  57.         lo=ll;
  58.         ro=rr;
  59.         if (stt[u]>stt[v])
  60.         swap(u,v);
  61.         r=stt[v];
  62.         if (lca(u,v)==u)
  63.         {
  64.             l=stt[u];
  65.             f=0;
  66.         }
  67.         else
  68.         {
  69.             l=ent[u];
  70.             f=1;
  71.         }
  72.     }
  73.     bool operator<(const query &q) const
  74.     {
  75.         if (l/BU==q.l/BU)
  76.         return ((r<q.r)^((l/BU)%2));
  77.         return (l<q.l);
  78.     }
  79. };
  80. void upd(int i)
  81. {
  82.     i=e[i];
  83.     bl[i/BU]-=cnt[i];
  84.     cnt[i]^=1;
  85.     bl[i/BU]+=cnt[i];
  86. }
  87. int main()
  88. {
  89.     int n,q;
  90.     scanf("%d%d",&n,&q);
  91.     for (int i=1;i<=n;i++)
  92.     scanf("%d",&l[i]);
  93.     for (int i=1;i<n;i++)
  94.     {
  95.         int a,b;
  96.         scanf("%d%d",&a,&b);
  97.         v[a].push_back(b);
  98.         v[b].push_back(a);
  99.     }
  100.     dfs(1,0);
  101.     vector<query> qu;
  102.     for (int i=0;i<q;i++)
  103.     {
  104.         int a,b,l,r;
  105.         scanf("%d%d%d%d",&a,&b,&l,&r);
  106.         qu.push_back(query(a,b,i,l,r));
  107.         ans[i]=-1;
  108.     }
  109.     sort(qu.begin(),qu.end());
  110.     int cl=0,cr=-1;
  111.     for (auto c:qu)
  112.     {
  113.         while (cl>c.l)
  114.         upd(--cl);
  115.         while (cr<c.r)
  116.         upd(++cr);
  117.         while (cl<c.l)
  118.         upd(cl++);
  119.         while (cr>c.r)
  120.         upd(cr--);
  121.         if (c.f)
  122.         upd(stt[lca(c.u,c.v)]);
  123.         int cur=c.lo;
  124.         while (cur<=c.ro && cur%BU)
  125.         {
  126.             if (cnt[cur])
  127.             ans[c.i]=cur;
  128.             cur++;
  129.         }
  130.         while (cur+BU-1<=c.ro)
  131.         {
  132.             if (bl[cur/BU])
  133.             {
  134.                 while (!cnt[cur])
  135.                 cur++;
  136.                 ans[c.i]=cur;
  137.                 break;
  138.             }
  139.             cur+=BU;
  140.         }
  141.         if (ans[c.i]==-1)
  142.         {
  143.             while (cur<=c.ro)
  144.             {
  145.                 if (cnt[cur])
  146.                 ans[c.i]=cur;
  147.                 cur++;
  148.             }
  149.         }
  150.         if (c.f)
  151.         upd(stt[lca(c.u,c.v)]);
  152.     }
  153.     for (int i=0;i<q;i++)
  154.     printf("%d\n",ans[i]);
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement