标签:fine unsigned unique deque syn 作用 strcat sign max
先建好整棵树。
遇到+val操作用dfn对区间+val
遇到加新点清空一下该点的点值(这里保证了之后查询的点肯定是清空过的)
差分+树状数组or线段树
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr strcat 13 #include <string> 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 #include <cassert> 21 #include <iomanip> 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 25 //****************** 26 clock_t __START,__END; 27 double __TOTALTIME; 28 void _MS(){__START=clock();} 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;} 30 //*********************** 31 #define rint register int 32 #define fo(a,b,c) for(rint a=b;a<=c;++a) 33 #define fr(a,b,c) for(rint a=b;a>=c;--a) 34 #define mem(a,b) memset(a,b,sizeof(a)) 35 #define pr printf 36 #define sc scanf 37 #define ls rt<<1 38 #define rs rt<<1|1 39 typedef pair<int,int> PII; 40 typedef vector<int> VI; 41 typedef long long ll; 42 const double E=2.718281828; 43 const double PI=acos(-1.0); 44 const ll INF=(1LL<<60); 45 const int inf=(1<<30); 46 const double ESP=1e-9; 47 const int mod=(int)1e9+7; 48 const int N=(int)1e6+10; 49 50 int c[N],maxn; 51 int lowbit(int t) 52 { 53 return t&(-t); 54 } 55 void add(int i,int t)//i处加t 56 { 57 if(i==0) 58 { 59 c[0]+=t; 60 return; 61 } 62 while(i<=maxn) 63 { 64 c[i]+=t; 65 i+=lowbit(i); 66 } 67 } 68 int sum(int i)//求i之前的前缀和 69 { 70 int sum=c[0]; 71 while(i) 72 { 73 sum+=c[i]; 74 i-=lowbit(i); 75 } 76 return sum; 77 } 78 79 vector<vector<int> >G(N); 80 struct 81 { 82 int a,b,c; 83 }op[N]; 84 int dfn[N],SZ[N],tot=-1; 85 int dfs(int u) 86 { 87 int sz=G[u].size(); 88 dfn[u]=++tot; 89 SZ[u]=1; 90 for(int i=0;i<sz;++i) 91 { 92 int to=G[u][i]; 93 SZ[u]+=dfs(to); 94 } 95 return SZ[u]; 96 } 97 98 int main() 99 { 100 int n,sz=0; 101 sc("%d",&n); 102 for(int i=1;i<=n;++i) 103 { 104 int x; 105 sc("%d",&x); 106 if(x==1) 107 { 108 sc("%d",&op[i].b),op[i].a=1,op[i].c=++sz; 109 G[op[i].b].push_back(sz); 110 // G[sz].push_back(op[i].b); 111 } 112 else if(x==2) 113 sc("%d%d",&op[i].b,&op[i].c),op[i].a=2; 114 else 115 sc("%d",&op[i].b),op[i].a=3; 116 } 117 maxn=sz+10; 118 SZ[0]=dfs(0); 119 for(int i=1;i<=n;++i) 120 { 121 if(op[i].a==1) 122 { 123 int val=sum(dfn[op[i].c]); 124 add(dfn[op[i].c],-val); 125 add(dfn[op[i].c]+SZ[op[i].c],val); 126 } 127 else if(op[i].a==2) 128 { 129 add(dfn[op[i].b],op[i].c); 130 add(dfn[op[i].b]+SZ[op[i].b],-op[i].c); 131 } 132 else 133 { 134 pr("%d\n",sum(dfn[op[i].b])); 135 } 136 // for(int j=0;j<=3;++j) 137 // pr("%d ",sum(dfn[j])); 138 // cout<<endl; 139 } 140 return 0; 141 } 142 143 /**************************************************************************************/
标签:fine unsigned unique deque syn 作用 strcat sign max
原文地址:https://www.cnblogs.com/--HPY-7m/p/11953454.html