【HDU4391】【块状链表】Paint The Wall

时间:2024-01-12 16:20:20
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 ;
}