首页 > 其他 > 详细

Loj 6284. 数列分块入门 8

时间:2018-09-26 00:56:04      阅读:204      评论:0      收藏:0      [点我收藏+]

题目描述

给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间询问等于一个数 ccc 的元素,并将这个区间的所有元素改为 ccc。

输入格式

第一行输入一个数字 nnn。

第二行输入 nnn 个数字,第 i 个数字为 aia_iai?,以空格隔开。

接下来输入 nnn 行询问,每行输入三个数字 lll、rrr、ccc,以空格隔开。

表示先查询位于 [l,r][l,r][l,r] 的数字有多少个是 ccc,再把位于 [l,r][l,r][l,r] 的数字都改为 ccc。

输出格式

对于每次询问,输出一行一个数字表示答案。

样例

样例输入

4
1 2 2 4
1 3 1
1 4 4
1 2 2
1 4 2

样例输出

1
1
0
2

数据范围与提示

对于 100% 100\%100% 的数据,1≤n≤100000,?231≤others 1 \leq n \leq 100000, -2^{31} \leq \mathrm{others}1n100000,?231others、ans≤231?1 \mathrm{ans} \leq 2^{31}-1ans231?1。

技术分享图片
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define F(i,a,b) for(int i=a;i<=b;i++) 
 5 #define D(i,a,b) for(int i=a;i>=b;i--)
 6 #define ms(i,a)  memset(a,i,sizeof(a)) 
 7 #define LL       long long 
 8 #define st(x)    ((x-1)*B+1)
 9 #define ed(x)    min(x*B,n)
10 #define bl(x)    ((x-1)/B+1) 
11 
12 int inline read(){
13     int x=0,w=0; char c=getchar(); 
14     while (c<0 || c>9) w+=c==-,c=getchar(); 
15     while (c>=0 && c<=9) x=(x<<3)+(x<<1)+(c^48),c=getchar(); 
16     return w? -x: x;  
17 }
18 
19 int const maxn=100003;  
20 
21 map<int,int> mat; 
22 
23 int n,B=400,a[maxn],sum,t[251];  
24 
25 int query(int l,int r,int z){
26     int x=bl(l);
27     int y=bl(r);
28     int ans=0;  
29     if(x==y){
30         if(t[x]>-1){
31             if(t[x]==z)    ans=r-l+1; 
32         }else F(i,l,r) if(a[i]==z) ans++; 
33     }else {
34         if(t[x]>-1){
35             if(t[x]==z) ans+=ed(x)-l+1;  
36         }else F(i,l,ed(x)) if(a[i]==z) ans++;  
37         if(t[y]>-1){
38             if(t[y]==z) ans+=r-st(y)+1;
39         }else F(i,st(y),r) if(a[i]==z) ans++;  
40         F(i,x+1,y-1){
41             if(t[i]>-1) {
42                 if(t[i]==z) ans+=ed(i)-st(i)+1;  
43             }else F(j,st(i),ed(i)) if(a[j]==z) ans++;  
44         }
45     }
46     return ans;  
47 }
48 
49 void update(int l,int r,int z){
50     int x=bl(l); 
51     int y=bl(r); 
52     if(x==y){ 
53         if(t[x]>-1) F(i,st(x),ed(x)) a[i]=t[x] ;
54         t[x]=-1;  
55         F(i,l,r) a[i]=z;  
56     }else {
57         if(t[x]>-1) F(i,st(x),ed(x)) a[i]=t[x]; 
58         if(t[y]>-1) F(i,st(y),ed(y)) a[i]=t[y]; 
59         t[x]=t[y]=-1;  
60         F(i,l,ed(x)) a[i]=z;
61         F(i,st(y),r) a[i]=z; 
62         F(i,x+1,y-1) t[i]=z; 
63     }
64 }
65 
66 
67 int main(){
68     n=read();
69     ms(-1,t);  
70     F(i,1,n){
71         a[i]=read(); 
72         if(mat[a[i]]==0) mat[a[i]]=++sum; 
73         a[i]=mat[a[i]];  
74     }
75     F(i,1,n){
76         int l=read(); 
77         int r=read(); 
78         int x=read(); 
79         if(mat[x]==0) mat[x]=++sum;  
80         x=mat[x];  
81         printf("%d\n",query(l,r,x)); 
82         update(l,r,x);  
83     }
84     return 0; 
85 }
View Code

 

Loj 6284. 数列分块入门 8

原文:https://www.cnblogs.com/ZJXXCN/p/9704394.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!