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. CImg 读取jpg, png ,tif 等格式失败解决方案

    CImg homepage :http://cimg.sourceforge.net CImg 给出的一个简单的示例:http://cimg.sourceforge.net/reference/gro ...

  2. JavaScript高级程序设计4.pdf

    虽然执行环境的类型总共只有两种——全局和局部(函数),但还有其他方法延长作用域链,有些语句可以在作用域链的前端临时增加一个变量对象,执行后会被移除try-catch语句的catch块和with语句 w ...

  3. SRM 410(1-250pt, 1-500pt)

    DIV1 250pt 题意:对于图G,有一些点和边,点中有一些点称为特殊点.问在所有特殊点最终不能处于同一个联通块的条件下,最多能给在图G中添加多少条边. 解法:首先,对于图G,处理出它有哪些联通块, ...

  4. openstack kilo版本控制节点异常流量分析

  5. Struct.xml Action配置

    <package name="default" namespace="/" extends="struts-default"> ...

  6. tomcat 配置https (单向认证)

    1.单向认证,就是传输的数据加密过了,但是不会校验客户端的来源 2.双向认证,如果客户端浏览器没有导入客户端证书,是访问不了web系统的,找不到地址 如果只是加密,单向就行 如果想要用系统的人没有证书 ...

  7. localstorage 初谈

    废话 : localStorage作为本地存储,比cookie大,可以看做一个小的服务器,几个api也可以看做增,删,查,找..... 设置 window.localStorage.setItem() ...

  8. Spring DI模式 小样例

           今儿跟同事讨论起来spring早期的,通过大篇幅xml的配置演变到今天annotation的过程,然后随手写了个小样例,感觉还不错,贴到这里留个纪念. 样例就是用JAVA API的方式, ...

  9. Java中泛型 问号的作用

    这是jdk1.5泛型的典型应用: 第一种写法,叫做使用泛型方法:    public <T extends Object> void thisIsT(List <T> list ...

  10. js数组操作的常用方法

    数组:arr=[1,2,3,4,5]; 1.数组转换成字符串,不会修改原数组内容: arr.join(); // "1,2,3,4,5" arr.join("" ...