poj3368–線段樹統計區間的最大頻率

來源:互聯網
上載者:User

今天又做了點圖論和線段樹的題目,比較有感觸的就是這道,一開始感覺有點像hotel那道題,感覺記錄區間的左右值和左右的最大頻率以及總的最大頻率即可,最後比較取最大,

後來在初始化的時候RE了一次,然後就AC了,感覺線段樹終於入門了


import java.io.*;public class Main {static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));static  PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));final static  int nextInt() throws IOException {in.nextToken();return (int) in.nval;}class Node{int left,right;int lenum,rignum;//資訊域,儲存該區間的左右端的元素是什麼int num,lnum,rnum;//儲存左端的長度和右端長度以及最長的Node(int left,int right){this.left=left;this.right=right;}int mid(){return (right+left)>>1;}int lenght(){return (right-left+1);}}static int number;static int MAX=100010;static Node tree[]=new Node[MAX<<2];static int data[]=new int[MAX]; void build(int left,int right,int idx) throws IOException{// System.out.println(idx+"&^&^&");tree[idx]=new Node(left,right);tree[idx].left=left;tree[idx].right=right;if(tree[idx].left==tree[idx].right){tree[idx].lenum=tree[idx].rignum=data[left];//System.out.println(data[left]);tree[idx].lnum=tree[idx].rnum=tree[idx].num=1;return;}int mid=tree[idx].mid();build(left,mid,idx<<1);build(mid+1,right,idx<<1|1);tree[idx].num=tree[idx<<1].num;if(tree[idx<<1|1].num>tree[idx].num)tree[idx].num=tree[idx<<1|1].num;if(tree[idx<<1].rnum+tree[idx<<1|1].lnum>tree[idx].num&&tree[idx<<1].rignum==tree[idx<<1|1].lenum){tree[idx].num=tree[idx<<1].rnum+tree[idx<<1|1].lnum;}tree[idx].lenum=tree[idx<<1].lenum;tree[idx].rignum=tree[idx<<1|1].rignum;tree[idx].lnum=tree[idx<<1].lnum;tree[idx].rnum=tree[idx<<1|1].rnum;if(tree[idx<<1].lnum==tree[idx<<1].lenght()&&tree[idx<<1].rignum==tree[idx<<1|1].lenum)tree[idx].lnum=tree[idx<<1].lnum+tree[idx<<1|1].lnum;if(tree[idx<<1|1].rnum==tree[idx<<1|1].lenght()&&tree[idx<<1|1].lenum==tree[idx<<1].rignum)tree[idx].rnum=tree[idx<<1|1].rnum+tree[idx<<1].rnum;}int query(int a,int b,int idx){//System.out.println("idx"+idx);if(tree[idx].left==a&&tree[idx].right==b){return tree[idx].num;}int mid=tree[idx].mid();//System.out.println("MID"+mid);int ans1=-1,ans2=-1,ans3=-1,ans=-1;if(mid>=b){return query(a,b,idx<<1);}else if(mid<a){return query(a,b,idx<<1|1);}else{int temp1=query(a,mid,idx<<1);int temp2=query(mid+1,b,idx<<1|1);ans3=Math.max(temp1,temp2);int temp=-1;if(tree[idx<<1].rignum==tree[idx<<1|1].lenum){if(mid-tree[idx<<1].rnum+1>=a){a=mid-tree[idx<<1].rnum+1;}if(mid+tree[idx<<1|1].lnum<=b)b=mid+tree[idx<<1|1].lnum;temp=b-a+1;}ans3=Math.max(ans3, temp);return ans3;}} void run() throws IOException{ while((number=nextInt())!=0){int qnum=nextInt();for(int i=1;i<=number;i++)data[i]=nextInt();build(1,number,1);for(int i=1;i<=qnum;i++){int a=nextInt();int b=nextInt();int ans=query(a,b,1);System.out.println(ans);}}} public static void main(String[] args) throws IOException { new Main().run(); } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.