• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ACM s1124yy
守りたいものが 強くさせること
博客园    首页    新随笔    联系   管理     

UVA 12345 Dynamic len(带修莫队)

Dynamic len

【题目链接】Dynamic len

【题目类型】带修莫队

&题解:

莫队可以单点更改,只要再多加一维,代表查询次数,排序的时候3个关键字.
之后循环离线的时候,先暴力时间指针(oi大佬说的),之后l,和r就随便了.还有要会用vis数组.

【时间复杂度】\(O(n^{\frac{5}{3}})\)

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
const int maxn = 5e4 + 7;
struct nt {
	int l, r, id, ts;
} ask[maxn];
struct nd {
	int x, pre, v;
} p[maxn];
int n, m, an[maxn], c[maxn], pos[maxn], num[maxn * 20], last[maxn], ans;
bool vis[maxn];
char s[9];
bool cmp(nt a, nt b) {
	return pos[a.l] < pos[b.l] ||
	       pos[a.l] == pos[b.l] && pos[a.r] < pos[b.r] ||
	       pos[a.l] == pos[b.l] && pos[a.r] == pos[b.r] && a.ts < b.ts;
}
void update(int x) {
	if(vis[x]) {
		if(!--num[c[x]]) --ans;
	}
	else if(++num[c[x]] == 1) ans++;
	vis[x] ^= 1;
}
void change(int x, int v) {
	if(vis[x]) {
		update(x); c[x] = v; update(x);
	}
	else c[x] = v;
}
int main() {
	//("E:1.in", "r", stdin);
	scanf("%d%d", &n, &m);
	int x, y, tot = 0, top = 0;
	int bk = sqrt(n);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
		last[i] = c[i];
		pos[i] = i / bk;
	}
	for(int i = 1; i <= m; i++) {
		scanf("%s%d%d", s, &x, &y);
		x++;
		if(s[0] == 'Q') {
			//[++tot].id必须要赋为tot. 而且这句话必须在第一位写
			ask[++tot].id = tot;
			ask[tot].l = x, ask[tot].r = y, ask[tot].ts = top;
		}
		else {
			p[++top].x = x, p[top].v = y, p[top].pre = last[x];
			last[x] = y;
		}
	}
	sort(ask + 1, ask + 1 + tot, cmp);
	int now = 0, pl = 1, pr = 0;
	for(int i = 1; i <= tot; i++) {
		if(now < ask[i].ts) {
			for(int j = now + 1; j <= ask[i].ts; j++)
				change(p[j].x, p[j].v);
		}
		else {
			for(int j = now; j >= ask[i].ts + 1; j--)
				change(p[j].x, p[j].pre);
		}
		if(pr < ask[i].r) {
			for(int j = pr + 1; j <= ask[i].r; j++)
				update(j);
		}
		else {
			for(int j = pr; j >= ask[i].r + 1; j--)
				update(j);
		}
		if(pl < ask[i].l) {
			for(int j = pl; j <= ask[i].l - 1; j++)
				update(j);
		}
		else {
			for(int j = pl - 1; j >= ask[i].l; j--)
				update(j);
		}
		pl = ask[i].l, pr = ask[i].r, now = ask[i].ts;
		an[ask[i].id] = ans;
	}
	for(int i = 1; i <= tot; i++) {
		printf("%d\n", an[i]);
	}
	return 0;
}
posted @ 2017-05-06 17:31  s1124yy  阅读(491)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3