BZOJ3757 苹果树

(不加传送门啦……)现在\(vjudge\)也不支持评测……

这题是不带修改的树上莫队。做完糖果公园再做这道题就会觉得非常简单。然后我想吐槽一下这道题……为什么色盲看颜色还能把一种颜色认为是与之相同的颜色啊……

所以要注意判断一下两种给定颜色相同的情况就可以了。还有这题我不知道\(vjudge\)不能提交,结果硬是一直\(TLE\),不过代码是没问题的。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('\n')
#define fr friend inline
#define y1 poj
#define mp make_pair
#define fi first
#define sc second
#define pb push_back
#define I puts("Oops")

using namespace std;
typedef long long ll;
const int M = 100005;
const int N = 1000005;
const int INF = 1000000009;
const double eps = 1e-7;

int read()
{
    int ans = 0,op = 1;char ch = getchar();
    while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
    while(ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
    return ans * op;
}

int n,m,st[22][M<<2],x,y,col[M],blo[M],head[M],ecnt;
int dep[M],dfn[M<<1],B,sta[M],top,pos[M];
int idx,lg[M<<1],cnt,pl,pr,fa[M],cur,walk[M];
ll ans[M],res;
bool vis[M];

struct node
{
   int l,r,tim,id,oc,nc;
   bool operator < (const node &g) const
   {
      if(blo[l] != blo[g.l]) return blo[l] < blo[g.l];
      if(blo[r] != blo[g.r]) return blo[r] < blo[g.r];
      return tim < g.tim;
   }
}q[M];

struct edge
{
   int next,to;
}e[M<<1];

void add(int x,int y)
{
   e[++ecnt].to = y;
   e[ecnt].next = head[x];
   head[x] = ecnt;
}

void dfs(int x,int f)
{
   int cur = top;
   dep[x] = dep[f] + 1,dfn[++idx] = x,pos[x] = idx,fa[x] = f;
   for(int i = head[x];i;i = e[i].next)
   {
      if(e[i].to == f) continue;
      dfs(e[i].to,x),dfn[++idx] = x;
      if(top - cur > B)
      {
	 cnt++;
	 while(top > cur) blo[sta[top--]] = cnt;
      }
   }
   sta[++top] = x;
}

int Min(int p,int q) {return (dep[p] < dep[q]) ? p : q;}

void build()
{
   lg[0] = -1;
   rep(i,1,idx) st[0][i] = dfn[i],lg[i] = lg[i>>1] + 1;
   rep(j,1,lg[idx])
   rep(i,1,idx)
   {
      if(i + (1 << j) - 1 > idx) break;
      st[j][i] = Min(st[j-1][i],st[j-1][i + (1 << (j-1))]);
   }
}

int lca(int p,int q)
{
   int kl = pos[p],kr = pos[q];
   if(kl > kr) swap(kl,kr);
   int k = lg[kr - kl + 1];
   return Min(st[k][kl],st[k][kr - (1<<k) + 1]);
}

void rev(int x)
{
   if(vis[x]) {walk[col[x]]--; if(!walk[col[x]]) res--;}
   else {if(!walk[col[x]]) res++; walk[col[x]]++;}
   vis[x] ^= 1;
}

void check(int i)
{
   if(walk[q[i].oc] && walk[q[i].nc] && q[i].nc != q[i].oc) ans[q[i].id] = res - 1;
   else ans[q[i].id] = res;
}

void twalk(int a,int b)
{
   int g = lca(a,b);
   while(a != g) rev(a),a = fa[a];
   while(b != g) rev(b),b = fa[b];
}

int main()
{
   n = read(),m = read(),B = 1357;
   rep(i,1,n) col[i] = read();
   rep(i,1,n) x = read(),y = read(),add(x,y),add(y,x);
   rep(i,1,m) q[i].l = read(),q[i].r = read(),q[i].oc = read(),q[i].nc = read(),q[i].id = i;
   dfs(1,0),build();
   while(top) blo[sta[top--]] = cnt;
   sort(q+1,q+1+m),pl = pr = 1;
   rep(i,1,m)
   {
      twalk(pl,q[i].l),pl = q[i].l,twalk(pr,q[i].r),pr = q[i].r;
      rev(lca(pl,pr)),check(i),rev(lca(pl,pr));
   }
   rep(i,1,m) printf("%lld\n",ans[i]);
   return 0;
}
posted @ 2018-12-12 07:41  CaptainLi  阅读(100)  评论(0编辑  收藏  举报