7-12 关于堆的判断 (堆)

    科技2024-10-13  18

    #include<bits/stdc++.h> using namespace std; const int N=1010; const int M=10010; int h[N]; int size; int wei[M*2+100]; void down(int u){ int t=u; if(u*2<=size&&h[u*2]<h[t]) t=u*2; if(u*2+1<=size&&h[u*2+1]<h[t]) t=u*2+1; if(t!=u){ swap(h[u],h[t]); swap(wei[h[u]+M],wei[h[t]+M]); down(t); } } void up(int u){ if(u/2>=1&&h[u]<h[u/2]){ swap(h[u],h[u/2]); swap(wei[h[u]+M],wei[h[u/2]+M]); up(u/2); } } int main(){ int n,q;scanf("%d%d",&n,&q); for(int i=1;i<=n;i++){ scanf("%d",&h[i]); wei[h[i]+M]=i; up(i); } //for(int i=n/2;i;i--) down(i); //for(int i=1;i<=n;i++) cout<<h[i]<<endl; /*for(int i=1;i<=n;i++){ scanf("%d",&h[i]); int k=i; wei[h[i]+M]=i; while(k>1&&h[k]<h[k/2]){ swap(h[k],h[k/2]); swap(wei[h[k]+M],wei[h[k/2]+M]); k/=2; } }*/ getchar(); while(q--){ //cout<<q<<endl; string s; getline(cin,s); //cout<<s<<endl; bool pa=false,ch=false; for(int i=0;i<s.length();i++){ if(s[i]=='p') pa=true; if(s[i]=='c') ch=true; } if(s[s.length()-1]=='t'){ bool f=false; int num=0; int i=0; if(s[0]=='-') f=true; if(f) i=1; while(s[i]>='0'&&s[i]<='9'){ num=num*10+s[i]-'0'; i++; } if(f) num=-num; if(h[1]==num) puts("T"); else puts("F"); } else if(s[s.length()-1]=='s'){ int i=0; bool f=false; if(s[0]=='-') f=true; if(f) i=1; int num1=0,num2=0; while(s[i]>='0'&&s[i]<='9'){ num1=num1*10+s[i]-'0'; i++; } if(f) num1=-num1; i+=5; bool g=false; if(s[i]=='-') g=true; if(g) i+=1; while(s[i]>='0'&&s[i]<='9'){ num2=num2*10+s[i]-'0'; i++; } if(g) num2=-num2; //cout<<num1<<endl; //cout<<num2<<endl; if(wei[num1+M]/2==wei[num2+M]/2) puts("T"); else puts("F"); } else if(pa){ int i=0; bool f=false; if(s[0]=='-') f=true; if(f) i=1; int num1=0,num2=0; while(s[i]>='0'&&s[i]<='9'){ num1=num1*10+s[i]-'0'; i++; } if(f) num1=-num1; i=s.length()-1; while(s[i]>='0'&&s[i]<='9'){ i--; } bool g=false; if(s[i]=='-') g=true; i+=1; while(s[i]>='0'&&s[i]<='9'){ num2=num2*10+s[i]-'0'; i++; } if(g) num2=-num2; if(wei[num1+M]==wei[num2+M]/2) puts("T"); else puts("F"); } else if(ch){ int i=0; bool f=false; if(s[0]=='-') f=true; if(f) i=1; int num1=0,num2=0; while(s[i]>='0'&&s[i]<='9'){ num1=num1*10+s[i]-'0'; i++; } if(f) num1=-num1; i=s.length()-1; while(s[i]>='0'&&s[i]<='9'){ i--; } bool g=false; if(s[i]=='-') g=true; i+=1; while(s[i]>='0'&&s[i]<='9'){ num2=num2*10+s[i]-'0'; i++; } if(g) num2=-num2; if(wei[num1+M]/2==wei[num2+M]) puts("T"); else puts("F"); } } return 0; }

     

    Processed: 0.018, SQL: 8