日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【2019浙江省赛 - A】Vertices in the Pocket(权值线段树下二分,图,思维)

發布時間:2023/12/10 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【2019浙江省赛 - A】Vertices in the Pocket(权值线段树下二分,图,思维) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

DreamGrid has just found an undirected simple graph with??vertices and no edges (that's to say, it's a graph with??isolated vertices) in his right pocket, where the vertices are numbered from 1 to?. Now he would like to perform??operations of the following two types on the graph:

  • 1?a?b?-- Connect the?-th vertex and the?-th vertex by an edge. It's guaranteed that before this operation, there does not exist an edge which connects vertex??and??directly.
  • 2?k?-- Find the answer for the query: What's the minimum and maximum possible number of connected components after adding??new edges to the graph. Note that after adding the??edges, the graph must still be a simple graph, and the query does NOT modify the graph.

?

Please help DreamGrid find the answer for each operation of the second type. Recall that a simple graph is a graph with no self loops or multiple edges.

Input

There are multiple test cases. The first line of the input is an integer?, indicating the number of test cases. For each test case:

The first line contains two integers??and??(,?), indicating the number of vertices and the number of operations.

For the following??lines, the?-th line first contains an integer??(), indicating the type of the?-th operation.

  • If?, two integers??and??follow (,?), indicating an operation of the first type. It's guaranteed that before this operation, there does not exist an edge which connects vertex??and??directly.
  • If?, one integer??follows (), indicating an operation of the second type. It's guaranteed that after adding??edges to the graph, the graph is still possible to be a simple graph.

?

It's guaranteed that the sum of??in all test cases will not exceed?, and the sum of??in all test cases will not exceed?.

Output

For each operation of the second type output one line containing two integers separated by a space, indicating the minimum and maximum possible number of connected components in this query.

Sample Input

1 5 5 1 1 2 2 1 1 1 3 2 1 2 3

Sample Output

3 3 2 3 1 2

解題報告:

以連通塊中元素個數為權值,建立線段樹,維護三個變量,分別是cnt(這樣的連通塊的數量),sum(連通塊的總的點數),pfh(各連通塊之間點數的平方和)(注意不是和的平方)

cnt代表連通塊數量,tot_ret代表當前狀態下的圖,在不增長連通塊個數的前提下,可以加的邊數。

所以對于最小值很顯然。

對于最大值,首先減去塊內連的邊,然后去線段樹查詢剩下的邊怎么加,首先填連通塊元素大小大的,這樣也就是類似查詢第k大,查到葉子結點,再判斷 塊中元素為tree[cur].l的 塊 我需要多少個 返回回去,就行了。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int n,q,cnt,f[MAX]; ll tot_ret,ret[MAX],num[MAX]; int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]); } struct TREE {int l,r;ll cnt,sum;ll pfh; } tree[MAX<<2]; void pushup(int cur) {tree[cur].cnt = tree[cur*2].cnt + tree[cur*2+1].cnt;tree[cur].sum = tree[cur*2].sum + tree[cur*2+1].sum;tree[cur].pfh = tree[cur*2].pfh + tree[cur*2+1].pfh; } void build(int l,int r,int cur) {tree[cur].l=l;tree[cur].r=r;if(l == r) {tree[cur].pfh = tree[cur].sum = tree[cur].cnt = (l==1?n:0);return;}int mid = (l+r)>>1;build(l,mid,cur*2);build(mid+1,r,cur*2+1);pushup(cur); } void update(int cur,ll tar,ll val) {if(tree[cur].l == tree[cur].r) {tree[cur].cnt += val;// 連通塊元素數量為tar的連通塊的數量。 tree[cur].sum += val*tar;tree[cur].pfh += val*tar*tar;return;}int mid = (tree[cur].l+tree[cur].r)>>1;if(tar<=mid) update(cur*2,tar,val);else update(cur*2+1,tar,val); pushup(cur); } ll query(int cur,ll k,int sum) {if(tree[cur].l == tree[cur].r) {ll l=1,r=tree[cur].cnt,mid;mid=(l+r)>>1;while(l<r) {mid=(l+r)>>1;if(mid*(mid-1)/2*tree[cur].l*tree[cur].l + mid*tree[cur].l*sum >= k) r=mid;else l = mid+1;}return l;}int mid = (tree[cur].l+tree[cur].r)>>1;ll tmp = (tree[cur*2+1].sum * tree[cur*2+1].sum - tree[cur*2+1].pfh)/2 + tree[cur*2+1].sum * sum ;if(tmp < k)return tree[cur*2+1].cnt + query(cur*2,k-tmp,sum+tree[cur*2+1].sum);else return query(cur*2+1,k,sum); } ll cal(ll k) {if(k<=tot_ret) return cnt;k-=tot_ret;ll tmp = query(1,k,0);return cnt-tmp+1; } int main() {int t;cin>>t;while(t--) {scanf("%d%d",&n,&q);for(int i = 1; i<=n; i++) f[i] = i,num[i] = 1,ret[i] = 0;tot_ret=0,cnt=n;//cnt記錄連通塊數 build(1,n,1);while(q--) {int op;scanf("%d",&op);if(op == 1) {int a,b;scanf("%d%d",&a,&b);a=getf(a),b=getf(b);if(a==b) {ret[a]--;tot_ret--;continue;}if(num[a] > num[b]) swap(a,b);//讓b是邊多的update(1,num[a],-1);update(1,num[b],-1);f[a]=b;//按秩合并到大集合tot_ret += num[a]*num[b]-1;cnt--;ret[b]=ret[a]+ret[b] + num[a]*num[b]-1;num[b]+=num[a];ret[a]=num[a]=0;update(1,num[b],1); }else {ll k;scanf("%lld",&k);ll minn = max(1LL,cnt-k);ll maxx = cal(k);printf("%lld %lld\n",minn,maxx);} }}return 0 ; }

?

總結

以上是生活随笔為你收集整理的【2019浙江省赛 - A】Vertices in the Pocket(权值线段树下二分,图,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。