http://acm.cqu.edu.cn:8888/oj/problem_show.php?pid=24916
区间赋值,问你区间里值为c的有多少个
c很大,显然记录不下,这题时限给的很大,分块可以一试,用map存,要用lazy标记,然后每次操作之前先pushdown,我写的太搓了。
代码:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define MAX 200005
#define MAXN 6005
#define maxnode 15
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
//const double inf = 1e18;
const double eps = 1e-8;
const LL mod = 1e9+7;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
const int tmp=350;
int a[MAX];
int lazy[tmp];
map<int,int> ma[tmp];
int n;
void pushdown(int l,int r){
if(l/tmp==r/tmp){
if(lazy[l/tmp]!=-1){
for(int i=(l/tmp)*tmp;i<min((l/tmp+1)*tmp,n);i++){
a[i]=lazy[l/tmp];
}
lazy[l/tmp]=-1;
}
}
else{
if(lazy[l/tmp]!=-1){
for(int i=(l/tmp)*tmp;i<min((l/tmp+1)*tmp,n);i++){
a[i]=lazy[l/tmp];
}
lazy[l/tmp]=-1;
}
if(lazy[r/tmp]!=-1){
for(int i=(r/tmp)*tmp;i<min((r/tmp+1)*tmp,n);i++){
a[i]=lazy[r/tmp];
}
lazy[r/tmp]=-1;
}
}
}
int query(int l,int r,int c){
int ans=0;
pushdown(l,r);
if(l/tmp==r/tmp){
for(int i=l;i<=r;i++){
if(a[i]==c) ans++;
}
}
else{
for(int i=l/tmp+1;i<r/tmp;i++){
ans+=ma[i][c];
}
for(int i=l;i<min((l/tmp+1)*tmp,n);i++){
if(a[i]==c) ans++;
}
for(int i=(r/tmp)*tmp;i<=r;i++){
if(a[i]==c) ans++;
}
}
return ans;
}
void update(int l,int r,int c){
pushdown(l,r);
if(l/tmp==r/tmp){
for(int i=l;i<=r;i++){
ma[i/tmp][a[i]]--;
a[i]=c;
}
ma[l/tmp][c]+=(r-l+1);
}
else{
for(int i=l/tmp+1;i<r/tmp;i++){
lazy[i]=c;
ma[i].clear();
ma[i][c]=tmp;
}
int i;
for(i=l;i<min((l/tmp+1)*tmp,n);i++){
ma[i/tmp][a[i]]--;
a[i]=c;
}
ma[l/tmp][c]+=(i-l);
for(i=(r/tmp)*tmp;i<=r;i++){
ma[i/tmp][a[i]]--;
a[i]=c;
}
ma[r/tmp][c]+=(i-r/tmp*tmp);
}
}
int main(){
int t;
cin>>t;
while(t--){
int m;
cin>>n>>m;
mem(lazy,-1);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
if(i%tmp==0) ma[i/tmp].clear();
ma[i/tmp][a[i]]++;
}
while(m--){
int op,a,b,c;
scanf("%d%d%d%d",&op,&a,&b,&c);
if(op==1) update(a,b,c);
else printf("%d\n",query(a,b,c));
}
}
return 0;
}