【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个元素,你的任务是在每次删除一个元素之前统计 ...
随机推荐
- Light OJ 1037 - Agent 47(预处理状态压缩DP)
题目大意: 有个特工要执行任务,他会遭遇到最多15个目标,特工必须把他们全部杀死.当他杀死一个目标后他可以使用目标的武器来杀死其他人.因此他必须有一个杀人的顺序,使得他开枪的次数最小. 现在给你一个表 ...
- 2015第43周三memcached
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...
- C/C++基础(二)
(1)运算符优先级 #include <cstdio> using namespace std; int main() { unsigned char a = 0xA5; un ...
- ecshop判断搜索引擎是否为蜘蛛
<?php /** * 判断是否为搜索引擎蜘蛛 * * @access public * @return string */ function is_spider($record = true) ...
- ng-if与ng-show、ng-hide指令的区别和注意事项
http://blog.csdn.net/aitangyong/article/details/44701769
- WCF服务部署到IIS上,然后通过web服务引用方式出现错误的解决办法
本文转载:http://www.cnblogs.com/shenba/archive/2012/01/06/2313932.html 昨天在用IIS部署一个WCF服务时,碰到了如下错误: 理解了文档内 ...
- Android常用网址[转]
转自:http://my.oschina.net/u/593225/blog/404423 1.AndroidDevTools URL: http://www.androiddevtools.cn/ ...
- [RxJS] Transformation operator: scan
All of the combination operators take two or more observables as input. These operators may also be ...
- [RxJS] Stream Processing With RxJS vs Array Higher-Order Functions
Higher order Array functions such as filter, map and reduce are great for functional programming, bu ...
- 瑕疵(bug)严重性定义
======================== 严重性定义缺陷: o 最先进的–造成执行中断(应用程序崩溃),该功能未预期实现,测试等工作无法进行. o 急-事件是非常重要的,须要立即给予关注. o ...