T2正解

/*
带修莫队+值域分块 
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;
const int N = 2000010 , M = 4000010;

int h[N] , to[M] , nxt[M] , tot;
int n , m , LEN , tim;
int bel[N] , bb[N] , sum[N] , gg[N];
int a[N] , num[N] , len;
int in[N] , out[N] , idx , cnt1;
int ans[N];

struct query
{
	int l , r , t  , id;
} b[N];

struct nod2
{
	int x , y; 
} q[N];

inline void add(int a , int b)
{
	to[++tot] = b ; nxt[tot] = h[a] ; h[a] = tot;
}

inline int find(int x)
{
	int l = 1 , r = len , mid , best;
	while(l <= r)
	{
		mid = (l + r) >> 1;
		if(num[mid] >= x) best = mid , r = mid - 1;
		else l = mid + 1;
	}
	return best;
}

void dfs(int x , int lst)
{
	in[x] = ++idx;
	for(int i = h[x] , v ; v = to[i] , i ; i = nxt[i])
	{
		if(v == lst) continue;
		dfs(v , x);
	}
	out[x] = idx;
}

bool cmp(query a , query b)
{
	return (bel[a.l] == bel[b.l]) ? a.r < b.r : bel[a.l] < bel[b.l];
}
void Del(int x)
{
	gg[a[x]]-- , sum[bb[a[x]]]--;
}

void Add(int x)
{
	gg[a[x]]++ , sum[bb[a[x]]]++;
}

void work(int tt , int l , int r)
{
	int x = q[tt].x;
	if(l <= x && x <= r) Del(x);
	swap(a[x] , q[tt].y);
	if(l <= x && x <= r) Add(x);
}

int get(int x)
{
	int ret = 0;
	for(int i = (bb[x] - 1) * LEN + 1 ; i <= x ; i++) ret += gg[i];
	for(int i = 1 ; i < bb[x] ; i++) ret += sum[i];
	return ret;
}

inline int read()
{
	int ret = 0 ; char c = getchar();
	while(c < '0' || c > '9') c = getchar();
	while(c >= '0' && c <= '9')
	{
		ret = ret * 10 + c - '0';
		c = getchar();
	}
	return ret;
}

void solve()
{
	int l = 1 , r = 0 , tt = 0;
	for(int i = 1 ; i <= cnt1 ; i++)
	{
		int ql = b[i].l , qr = b[i].r , t2 = b[i].t;
		while(tt < t2) work(++tt , l , r);
		while(tt > t2) work(tt-- , l , r);
		while(l < ql) Del(l++);
		while(l > ql) Add(--l);
		while(r > qr) Del(r--);
		while(r < qr) Add(++r);
		
		ans[b[i].id] = get(a[ql]);
	}
	
	for(int i = 1 ; i <= cnt1 ; i++) printf("%d\n" , ans[i]);
}

int main()
{
	freopen("suprme_chicken20.in" , "r" , stdin);
	freopen("suprme_chicken20.ans" , "w" , stdout);
	n = read() ; m = read() ; len = n; LEN = sqrt(n);
	for(int i = 1 ; i <= n ; i++) bel[i] = (i - 1) / LEN + 1;
	for(int i = 1 ; i <= n ; i++) a[i] = read() , num[i] = a[i];
	for(int i = 1 , a , b ; i < n ; i++)
	{
		a = read() ; b = read();
		add(a , b) ; add(b , a); 
	}
	dfs(1 , 1);
	
	for(int i = 1 ; i <= m ; i++)
	{
		int opt , x , y;
		opt = read();
		if(opt == 1)
		{
			x = read() ; y = read();
			num[++len] = y;
			q[++tim].x = x;
			q[tim].y = y;
		}
		else
		{
			x = read();
			b[++cnt1].l = in[x];
			b[cnt1].r = out[x];
			b[cnt1].t = tim;
			b[cnt1].id = cnt1;
		}
	}
	
	sort(num + 1 , num + len + 1) ; len = unique(num + 1 , num + len + 1) - num - 1;
	for(int i = 1 ; i <= tim ; i++) q[i].y = find(q[i].y);
	for(int i = 1 ; i <= n ; i++) a[i] = find(a[i]);
	sort(b + 1 , b + cnt1 + 1 , cmp);
	
	LEN = sqrt(len); for(int i = 1 ; i <= len ; i++) bb[i] = (i - 1) / LEN + 1;
	
	solve();
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值