1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <iomanip> #include <set> #include <queue> #include <stack> #include <vector> #include <map> #include <string> #include <sstream> #include <bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<n;++i) #define per(i,a,n) for(int i=n-1;i>=a;--i); #define ms(x,a) memset((x),a,sizeof((x))) #define all(x) x.begin(),x.end() #define ins(x) inserter(x,x.begin()) #define INF 0x3f3f3f3f #define eps 1e-8 #define fi first #define e 2.718281828459045235360287471352662497757247093699959574966967627724076630353 #define se second #define mp make_pair #define pb push_back #define np next_permutation #define gapline cout<<"##======================##"<<endl using namespace std; const int maxn=1e5+5; const int maxl=26; const int maxm=1e5+5; typedef long long ll; typedef unsigned long long ull; typedef set<int> si; map <si,int> IDcache; vector <si> sicache; int t,n;
int ID(si x){ if(IDcache.count(x)) return IDcache[x]; sicache.pb(x);
return IDcache[x]=sicache.size()-1; } stack <int> s;
int main(){ ios::sync_with_stdio(false);
cin>>t; while(t--){ while(!s.empty()) s.pop(); sicache.clear(); IDcache.clear(); cin>>n; string op; rep(i,0,n){ cin>>op; if(op[0]=='P') s.push(ID(si())); else if(op[0]=='D') s.push(s.top()); else{ si x1=sicache[s.top()];s.pop(); si x2=sicache[s.top()];s.pop(); si x;
if(op[0]=='U') set_union(all(x1),all(x2),ins(x)); if(op[0]=='I') set_intersection(all(x1),all(x2),ins(x)); if(op[0]=='A') { x=x2; x.insert(ID(x1)); } s.push(ID(x)); } cout<<sicache[s.top()].size()<<endl; } cout<<"***"<<endl; } }
|