Problem Description
As a amateur artist, Xenocide loves painting the wall. The wall can be considered as a line consisting of n nodes. Each node has its own color.

Xenocide spends all day in front of the wall. Sometimes, he paints some consecutive nodes so that these nodes have the same color. When he feels tired, he focuses on a particular color and counts the number of nodes that have this color within a given interval.

Now Xenocide is tired of counting, so he turns to you for help.

 
Input
The input consists of several test cases.
The first line of each test case contains two integer n, m(1<=n, m<=100000) indicating the length of the wall and the number of queries.
The following line contains N integers which describe the original color of every position.
Then m lines follow. Each line contains 4 non-negative integers a, l, r, z(1<=a<=2, 0<=l<=r<n ,0<=z<231).
a = 1 indicates that Xenocide paints nodes between l and r and the resulting color is z.
a = 2 indicates that Xenocide wants to know how many nodes between l and r have the color z.
 
Output
Print the corresponding answer for each queries.
 
Sample Input
5 5
1 2 3 4 0
2 1 3 3
1 1 3 1
2 1 3 3
2 0 3 1
2 3 4 1
 
Sample Output
1
0
4
1
 
Source
【分析】
块状链表的裸题,同样可以用线段树做。
记得在map中把不要用的元素要erase掉,不然很占空间,会超。
晕了,搞了一个半小时,T了无数遍,后面借鉴了别人的代码....,同样是块状链表,差别怎么这么大(╯‵□′)╯︵┻━┻。
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map> const int N = + ;
using namespace std;
int n, c, data[N];
int SIZE;
map<int, int>color[]; void change2(int l,int r){
int x = l / SIZE;
if(color[x].size() == ){//整个块只有一个颜色
map<int, int>::iterator it = color[x].begin();
int last = it->first, cnt = it->second;
if(c == last) return ;//显然等于原来的颜色就没必要修改了
if(r - l + == cnt){//整个块都修改
color[x][c] = r - l + ;
color[x].erase(it);
return;
}
for(int i = SIZE * x; i < n && i < SIZE * ( x + ); i++){
if(i >= l && i <= r) data[i] = c;
else data[i] = last;
}
color[x][c] = r - l + ;
color[x][last] = cnt - (r - l + );
}else
for(int i = l; i <= r; i++) {//进行直接修改
if(data[i] == c) continue;
map<int, int>::iterator it = color[x].find(data[i]);
if(it->second == ) color[x].erase(it);//记住一定要删除不然会超空间
else it->second--;
color[x][c]++;
data[i] = c;
}
}
//注意这种带2的操作是在同一个块内操作的
int query2(int l,int r){
int x = l / SIZE;
if(color[x].count(c)){
if(color[x].size() == ) return r - l + ;//整块的颜色唯一
else{
int cnt = ;
for(int i = l; i <= r; i++) if(data[i] == c) cnt++;
return cnt;
}
} else return ;
} void change(int l,int r){
int x1 = l / SIZE, x2 = r / SIZE;
if(x1 == x2){change2(l, r);return;}
change2(l, SIZE * (x1 + ) - );
change2(SIZE * x2, r);
//真正无奈
for(int i = x1 + ; i < x2; i++){
color[i].clear();
color[i][c] = SIZE;
}
} int query(int l,int r){
int x1 = l / SIZE, x2 = r / SIZE;
if(x1 == x2) return query2(l,r);
int Ans = query2(l, SIZE * (x1 + ) - ) + query2(SIZE * x2 ,r);
for(int i = x1 + ;i < x2;i++)
if(color[i].count(c)) Ans += color[i][c];
return Ans;
} void init(){
SIZE = (int)sqrt(n * 1.0);
for(int i = ; i < n; i++) scanf("%d", &data[i]);
for(int i = ; i <= n / SIZE; i++) color[i].clear();
for(int i = ; i < n; i++) {
int tmp = i / SIZE;
color[tmp][ data[i] ]++;
}
} int main(){
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int m;
while(scanf("%d%d",&n,&m) != EOF){
init();
for (int i = ; i <= m; i++){
int t, l, r;
scanf("%d%d%d%d", &t, &l, &r, &c);
if(t == ) change(l,r);
else printf("%d\n",query(l,r));
}
}
return ;
}

【HDU4391】【块状链表】Paint The Wall的更多相关文章

  1. 【BZOJ-1507】Editor 块状链表

    1507: [NOI2003]Editor Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 3397  Solved: 1360[Submit][Stat ...

  2. ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)

    题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...

  3. POJ 2887 Big String(块状链表)

    题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...

  4. 【BZOJ 1507】【NOI 2003】&【Tyvj P2388】Editor 块状链表模板题

    2016-06-18 当时关于块状链表的想法是错误的,之前维护的是一个动态的$\sqrt{n}$,所以常数巨大,今天才知道原因TwT,请不要参照这个程序为模板!!! 模板题水啊水~~~ 第一次写块状链 ...

  5. BZOJ 1507 Editor(块状链表)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1507 题意:一个文本编辑器,模拟以下操作: 思路:块状链表的主要操作: (1)find( ...

  6. bzoj 3809 Gty的二逼妹子序列(莫队算法,块状链表)

    [题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基 ...

  7. 【BZOJ1500】【块状链表】维修数列

    Description Input 输入文件的第1行包含两个数N和M,N表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一条命令,格式参见问题描述 ...

  8. 【BZOJ2741】【块状链表+可持久化trie】FOTILE模拟赛L

    Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max(Ai xor Ai+1 xor Ai+2 .. ...

  9. 【BZOJ3295】【块状链表+树状数组】动态逆序对

    Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...

随机推荐

  1. NHibernate 存储过程使用

    NHibernate也是能够操作存储过程的,不过第一次配置可能会碰到很多错误. 一.删除 首先,我们新建一个存储过程如下: CREATE PROC DeletePerson @Id int AS DE ...

  2. 【转】VSync Count 垂直同步

    原文:http://blog.csdn.net/yesy10/article/details/7794556 Unity3D中新建一个场景空的时候,帧速率(FPS总是很低),大概在60~70之间.一直 ...

  3. Java---网络蜘蛛-网页邮箱抓取器~源码

    刚刚学完Socket,迫不及待的做了这个网页邮箱抓取~~~ 现在有越来越多的人热衷于做网络爬虫(网络蜘蛛),也有越来越多的地方需要网络爬虫,比如搜索引擎.资讯采集.舆情监测等等,诸如此类.网络爬虫涉及 ...

  4. [转载]jquery的extend和fn.extend

    jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend(object); 为扩展j ...

  5. Matlab编程-基本命令行语句

    (1) mathlab命令行中“,”与“:”的区别: 结尾不加任何东西也会输出结果 以“,”结尾不显示变量数值,但是再次输入变量名之后可以输出变量值 以“:”结尾显示变量值 (2)    输出格式控制 ...

  6. [转]C语言单引号和双引号的区别

    单引号和双引号在C中的意义完全不同,包围在单引号中的一个字符只是编写整数的另一种方法.这个整数是给定的字符在实现的对照序列中的一个对应的值,即ASCII码值.因此在一个ASCII实现中,‘a’和014 ...

  7. GCC的编译和安装 很好的资料

    http://blog.csdn.net/yrj/article/details/492404 1.GCC的编译和安装2.预处理    #define 可以支持不定数量的参数.    例子如下:    ...

  8. An Easy Problem?! - POJ 2826(求面积)

    题目大意:有两块木板交叉起来接雨水,问最多能接多少.   分析:题目描述很简单,不过有些细节还是需要注意到,如下图几种情况:   #include<stdio.h> #include< ...

  9. 在Windows上安装私有GitHub的开源替代-GitLab

    在我之前的一篇博客中介绍过GitLab: 开源免费的git管理工具,今天说一下怎么在windows安装GitLab. BitNami可以很容易的帮助你安装开源应用,和Helicon Zoo类似,我之前 ...

  10. MySQL基础 (DML)

    DML语句             DML操作是指对数据库中表记录的操作,主要包括表记录的插入(insert).更新(update).删除(delete)和查询(select) 1.插入记录 插入一条 ...