【HDU4391】【块状链表】Paint The Wall
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.
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.
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
0
4
1
#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的更多相关文章
- 【BZOJ-1507】Editor 块状链表
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 3397 Solved: 1360[Submit][Stat ...
- ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)
题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...
- POJ 2887 Big String(块状链表)
题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...
- 【BZOJ 1507】【NOI 2003】&【Tyvj P2388】Editor 块状链表模板题
2016-06-18 当时关于块状链表的想法是错误的,之前维护的是一个动态的$\sqrt{n}$,所以常数巨大,今天才知道原因TwT,请不要参照这个程序为模板!!! 模板题水啊水~~~ 第一次写块状链 ...
- BZOJ 1507 Editor(块状链表)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1507 题意:一个文本编辑器,模拟以下操作: 思路:块状链表的主要操作: (1)find( ...
- bzoj 3809 Gty的二逼妹子序列(莫队算法,块状链表)
[题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基 ...
- 【BZOJ1500】【块状链表】维修数列
Description Input 输入文件的第1行包含两个数N和M,N表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一条命令,格式参见问题描述 ...
- 【BZOJ2741】【块状链表+可持久化trie】FOTILE模拟赛L
Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max(Ai xor Ai+1 xor Ai+2 .. ...
- 【BZOJ3295】【块状链表+树状数组】动态逆序对
Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...
随机推荐
- 分布式文件系统FastDFS安装与配置(单机)
安装包如下:fastdfs-nginx-module_v1.16.tar.gzFastDFS_v5.05.tar.gzlibfastcommon-master.zipnginx-1.8.0.tar.g ...
- HDU-4972 A simple dynamic programming problem
http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming proble ...
- unity3d 雪与沙的渲染
很简单的一个shader,跟着上一篇地形顺便弄的 方法就是基于物理的diffuse,再加上noise权重的specular 公式参考 JOURNEY JOURNEY中认为OrenNayar比较浪费,所 ...
- 邮件发送小demo
//send email public static bool SendEmail() { //实例化发件人地址 MailAddress from = new MailAddress("aa ...
- C_FORCE_ROOT linux环境变量设置
nano /etc/profile gedit 也可以 结尾追加 export C_FORCE_ROOT="true"
- .\Obj\main.axf: Error: L6406E: No space in execution regions with .ANY selector matching sin_i.o(.co
这个问题原因是 芯片的 空间不足 解决方法是 在KEIL 的DEVICE中选择 更大的空间的芯片型号
- 385. Mini Parser
括号题一般都是stack.. 一开始想的是存入STACK的是SRING,然后POP出括号在构建新的NestedInteger放到另一个里面,但是操作起来费时费力. 后来猛然发现其实可以直接吧Neste ...
- JAVA Web学习篇--Servlet
Servlet由来 做过BS项目的人都知道,浏览器可以依据HTML静态标记语言来显示各式各样的网页.可是假设我们须要在网页上完毕一些业务逻辑:比方登陆验证.或者说网页显示的内容在server的数据库中 ...
- c++ 字符串流 sstream(常用于格式转换) 分类: C/C++ 2014-11-08 17:20 150人阅读 评论(0) 收藏
使用stringstream对象简化类型转换 C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中 ...
- linux的文件系统及节点表
linux的文件系统及节点表 一 linux的文件系统1 我们都知道当我们安装linux时会首先给系统分区,然后我们会把分区格式化成EXT3格式的文件系统.那么在linux系统中还有没有其他的文件系 ...