皮卡丘的梦想2
1000 ms 65536 KiBDescription
一天,一只住在 501 实验室的皮卡丘决定发奋学习,成为像 LeiQ 一样的巨巨,于是他向镇上的贤者金桔请教如何才能进化成一只雷丘。
金桔告诉他需要进化石才能进化,并给了他一个地图,地图上有 n 个小镇,他需要从这些小镇中收集进化石。
接下来他会进行 q 次操作,可能是打听进化石的信息,也可能是向你询问第 l 个小镇到第 r 个小镇之间的进化石种类。
如果是打听信息,则皮卡丘会得到一个小镇的进化石变化信息,可能是引入了新的进化石,也可能是失去了全部的某种进化石。
如果是向你询问,你需要回答他第 l 个小镇到第 r 个小镇之间的进化石种类。
Input
首先输入一个整数 T (1 <= T <= 10),代表有 T 组数据。
每组数据的第一行输入一个整数 n (1 <= n <= 100000) 和一个整数 q (1 <= q <= 100000),分别代表有 n 个小镇,表皮卡丘有 q 次操作。
接下来输入 q 行,对于每次操作,先输入操作类型,然后根据操作类型读入:
- 1: 紧接着输入 2 个整数 a (1 <= a <= n), b (1 <= b <= 60),表示第 a 个小镇引入了第 b 种进化石
- 2: 紧接着输入 2 个整数 a (1 <= a <= n), b (1 <= b <= 60),表示第 a 个小镇失去了全部第 b 种进化石
- 3: 紧接着输入 2 个整数 l, r (1 <= l <= r <= n),表示他想询问从第 l 个到第 r 个小镇上可收集的进化石有哪几种
Output
对于每组输入,首先输出一行 "Case T:",表示当前是第几组数据。
对于每组数据中的每次 3 操作,在一行中按编号升序输出所有可收集的进化石。如果没有进化石可收集,则输出一个 MeiK 的百分号 "%"(不包括引号)。
Sample
Input
Copy1
10 10
3 1 10
1 1 50
3 1 5
1 2 20
3 1 1
3 1 2
2 1 50
2 2 20
3 1 2
3 1 10
Output
CopyCase 1:
%
50
50
20 50
%
%
题解:题目咋一看感觉是线段树的模板题,但是麻烦在需要你输出石头的种类,这就不是单纯的线段树了,还需要加入一点二进制的思想(好像还是第一次接触到这样的二进制思想题目),用二进制从右向左0代表没有1代表有,如01001代表这个村庄有1号和4号石头,如此便可快速的输出石头的种类,同时线段树的区间合并需要用或运算来维护,区间删除则且上需要删除种类的二进制取反(想一下不难理解)。
#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
const int maxn=100000;
ll a[maxn+5],sum[maxn<<2];
void pushup(int st){sum[st]=sum[st<<1] | sum[st<<1|1];return;}
void update(int p,int c,int l,int r,int st)
{
if(l==r){sum[st]|=1ll<<(c-1);return;}
int m=(l+r)>>1;
if(p<=m) update(p,c,l,m,st<<1);
else update(p,c,m+1,r,st<<1|1);
pushup(st);
}
void del(int p,int c,int l,int r,int st)
{
if(l==r){sum[st] &= ~(1ll<<(c-1));return;}
int m=(l+r)>>1;
if(p<=m) del(p,c,l,m,st<<1);
else del(p,c,m+1,r,st<<1|1);
pushup(st);
}
ll query(int L,int R,int l,int r,int st)
{
if(l>=L && r<=R) return sum[st];
int m=(l+r)>>1;
ll ans=0;
if(L<=m) ans|=query(L,R,l,m,st<<1);
if(R>m) ans|=query(L,R,m+1,r,st<<1|1);
return ans;
}
int main()
{
int t;
t=read();
for(int k=1;k<=t;k++)
{
memset(sum,0,sizeof(sum));
printf("Case %d:\n",k);
int n=read();
int q=read();
while(q--)
{
int node=read();
int pos=read();
int val=read();
if(node==1)
{
update(pos,val,1,n,1);
}
else if(node==2)
{
del(pos,val,1,n,1);
}
else
{
ll ans=query(pos,val,1,n,1);
ll cnt=0;
int flag=1,ot=1;
while(ans)
{
if(ans&1)
{
if(flag) flag=0;
else putchar(' ');
cout<<ot;
}
ot++;
ans>>=1;
}
if(flag)
{
printf("%%\n");
}
else
printf("\n");
}
}
}
return 0;
}