【BestCoder】 HDOJ 5057 Argestes and Sequence

树状数组会MLE。。。。用分块可以nsqrt(n)内解决这个问题。。。

#include <iostream>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 100005
#define maxm 40005
#define eps 1e-10
#define mod 10000007
#define INF 1e9
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid  
#define rson o<<1 | 1, mid+1, R  
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
//head

struct node
{
	int cnt[10][10];
}block[400];
int num[maxn];
char ss[maxn];
int n, s, sn, m;

void init(void)
{
	memset(block, 0, sizeof block);
}
void read(void)
{
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++) scanf("%d", &num[i]);	
}

void build(void)
{
	s = sqrt((double)n);
	if(n % s == 0) sn = n / s;
	else sn = n / s + 1;
	for(int i = 0; i < n; i++) {
		int a = i / s, t = num[i];
		for(int j = 1; j <= 10; j++)
			block[a].cnt[j][t % 10]++, t /= 10;
	}
}

void work(void)
{
	int ans, ql, qr, d, p, a, b, t, x, v;
	while(m--) {
		scanf("%s", ss);
		if(ss[0] == 'S') {
			scanf("%d%d", &x, &v);
			x--;
			a = x / s, t = num[x];
			for(int i = 1; i <= 10; i++)
				block[a].cnt[i][t % 10]--, t /= 10;
			num[x] = t = v;
			for(int i = 1; i <= 10; i++)
				block[a].cnt[i][t % 10]++, t /= 10;
		}
		else {
			scanf("%d%d%d%d", &ql, &qr, &d, &p);
			ql--, qr--;
			a = ql / s, b = qr / s;
			t = qpow(10, d-1), ans = 0;
			if(a == b) {
				for(int i = ql; i <= qr; i++)
					if(num[i] / t % 10 == p) ans++;
			}
			else {
				for(int i = a+1; i <= b-1; i++) ans += block[i].cnt[d][p];
				for(int i = ql; i < (a+1) * s; i++)
					if(num[i] / t % 10 == p) ans++;
				for(int i = b * s; i <= qr; i++)
					if(num[i] / t % 10 == p) ans++;
			}
			printf("%d\n", ans);
		}
	}
}

int main(void)
{
	int _;
	while(scanf("%d", &_)!=EOF) {
		while(_--) {
			init();
			read();
			build();
			work();
		}
	}
	return 0;
}


你可能感兴趣的:(HDU)