线段树的题。。。。。
诶。。。。MLE了。。。。。第一次碰到。。。。不会hash真可悲= =
区间更新的时候记得要pushdown。
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define maxn 1000010
struct node
{
int num;
int a[31];
}que[maxn*3];
int tol[31];
void creat(int l,int r,int rt)
{
que[rt].a[que[rt].num++]=2;
if(l==r) return;
int mid=(l+r)/2;
creat(l,mid,rt<<1);
creat(mid+1,r,rt<<1|1);
}
void pushdown(int l,int r,int rt,int x)
{
que[rt].num=1;
que[rt].a[0]=x;
if(l==r) return;
int mid=(l+r)/2;
pushdown(l,mid,rt<<1,x);
pushdown(mid+1,r,rt<<1|1,x);
}
void update(int ll,int rr,int l,int r,int rt,int x)
{
if(ll==l&&rr==r)
{
// printf("l=%d r=%d \n",l,r);
que[rt].num=1;
que[rt].a[0]=x;
pushdown(l,r,rt,x);
return;
// update(ll,rr,l,r,rt,x);
}
int mid=(l+r)/2;
if(rr<=mid) update(ll,rr,l,mid,rt<<1,x);
else if(ll>mid) update(ll,rr,mid+1,r,rt<<1|1,x);
else
{
update(ll,mid,l,mid,rt<<1,x);
update(mid+1,rr,mid+1,r,rt<<1|1,x);
}
int i,j,k;
que[rt].num=0;
int vis[31]={0};
//memset(vis,0,sizeof(vis));
for(i=0;i<que[rt<<1].num;i++) vis[que[rt<<1].a[i]]=1;
for(i=0;i<que[rt<<1|1].num;i++) vis[que[rt<<1|1].a[i]]=1;
for(i=1;i<=30;i++) if(vis[i]) {que[rt].a[que[rt].num++]=i;}//printf("%d ",i);}//printf("\n");
// printf("**l=%d r=%d \n",l,r);
}
void query(int ll,int rr,int l,int r,int rt)
{
if(ll==l&&rr==r)
{
// printf("--l=%d r=%d \n",l,r);
for(int i=0;i<que[rt].num;i++) tol[que[rt].a[i]]=1;
return;
}
int mid=(l+r)/2;
if(rr<=mid) query(ll,rr,l,mid,rt<<1);
else if(ll>mid) query(ll,rr,mid+1,r,rt<<1|1);
else
{
query(ll,mid,l,mid,rt<<1);
query(mid+1,rr,mid+1,r,rt<<1|1);
}
}
template<class T>
inline char read(T &n){
T x = 0, tmp = 1; char c = getchar();
while((c < '0' | c > '9') && c != '-' && c != EOF) c = getchar();
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return c;
}
template <class T>
inline void write(T n) {
if(n < 0) {
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n) {
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
int main()
{
int n,m;
char s[5];
int l,r,x;
while(scanf("%d%d",&n,&m)!=EOF&&n+m)
{
int i,j,k;
creat(1,n,1);
while(m--)
{
scanf("%s",s);
if(s[0]=='P')
{
// scanf("%d%d%d",&l,&r,&x);
read(l);read(r);read(x);
update(l,r,1,n,1,x);
}
if(s[0]=='Q')
{
// memset(tol,0,sizeof(tol));
for(i=1;i<=30;i++) tol[i]=0;
// scanf("%d%d",&l,&r);
read(l);read(r);
query(l,r,1,n,1);
int aa[31];
j=0;
for(i=1;i<=30;i++) if(tol[i]) aa[j++]=i;
for(i=0;i<j-1;i++) printf("%d ",aa[i]);
printf("%d\n",aa[j-1]);
// printf("\n");
}
}
}
return 0;
}
***********************************************
从别人那里撸来的离散化模板。。。。
while(scanf("%d%d", &n, &m) != EOF) {
if(n == 0 && m == 0) break;
hh = 0;
getchar();
for(i = 0; i < m; i ++) {
scanf("%c", &op[i]);
if(op[i] == 'P') {
scanf("%d%d%d", &a[i], &b[i], &c[i]);
Hash[hh++] = a[i];
Hash[hh++] = b[i];
} else {
scanf("%d%d", &a[i], &b[i]);
Hash[hh++] = a[i];
Hash[hh++] = b[i];
}
getchar();
}
sort(Hash, Hash + hh);
hh = unique(Hash, Hash + hh) - Hash;
build(1, hh, 1);
for(i = 0; i < m; i ++) {
a[i] = lower_bound(Hash, Hash + hh, a[i] ) - Hash + 1;
b[i] = lower_bound(Hash, Hash + hh, b[i] ) - Hash + 1;
}
然后离散了一下发现。。。。TLE了。。。。。吐血了。。。。