计蒜客 ACM训练联盟周赛 G. 算个欧拉函数给大家助助兴-大数的因子个数 M. Big brother said the calculation-线段树+二分(HDU5649.DZY Loves Sorting)

ACM训练联盟周赛

这一场有几个数据结构的题,但是自己太菜,不会树套树,带插入的区间第K小-替罪羊套函数式线段树,先立个flag,BZOJ3065。

这一场写了两道(具体来说就一道)就开溜了,但是计蒜客上这个比赛貌似没有赛后补题,但是有差不多的题目,所以去补那些题就可以了。

G. 算个欧拉函数给大家助助兴

这个题和上一场的 F.Divisions,其实就是一样的题目,代码改都没改直接贴上就过了。题解前几天写过了,直接传送门吧——>我是傻子 骗点访问量。。。

 M. Big brother said the calculation

这个题和杭电的一道题几乎就是一样的题目。HDU5649.DZY Loves Sorting

题意就是一个n的排列,执行Q次操作,每次操作是对某个区间从小到大排序或者从大到小排序。最后只查询一次,输出第k个位置当前的数。

直接按HDU5649这个题写,可以交题。

因为只查询一次,而且这是n的全排列,所以直接二分答案,比mid小的赋值为0,大的赋值为1。区间查询判断的时候直接与0和1比较就可以了。

这个题写的简直要骂人,调一万年代码都没调对,调的简直要吐血,最后突然瞄一眼,发现,查询左右儿子的判断手抖写成一样的了,mdzz。。。

代码:

  1 //M-线段树+二分-HDU5649
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<iomanip>
  6 #include<stdio.h>
  7 #include<stdlib.h>
  8 #include<math.h>
  9 #include<cstdlib>
 10 #include<set>
 11 #include<map>
 12 #include<ctime>
 13 #include<stack>
 14 #include<queue>
 15 #include<vector>
 16 #include<set>
 17 using namespace std;
 18 typedef long long ll;
 19 const int inf=0x3f3f3f3f;
 20 const double eps=1e-5;
 21 const int maxn=1e5+10;
 22 #define lson l,m,rt<<1
 23 #define rson m+1,r,rt<<1|1
 24 
 25 struct node{
 26     int c,l,r;
 27 }q[maxn];
 28 int n,m,k;
 29 int a[maxn],tree[maxn<<2],add[maxn<<2];
 30 
 31 void pushup(int rt)
 32 {
 33     tree[rt]=tree[rt<<1]+tree[rt<<1|1];
 34 }
 35 void pushdown(int rt,int m)
 36 {
 37     if(add[rt]!=-1){
 38         add[rt<<1]=add[rt<<1|1]=add[rt];
 39         tree[rt<<1]=(m-(m>>1))*add[rt];
 40         tree[rt<<1|1]=(m>>1)*add[rt];
 41         add[rt]=-1;
 42     }
 43 }
 44 void build(int x,int l,int r,int rt)
 45 {
 46     add[rt]=-1;
 47     if(l==r){
 48         tree[rt]=a[l]<=x?0:1;
 49         return ;
 50     }
 51 
 52     int m=(l+r)>>1;
 53     build(x,lson);
 54     build(x,rson);
 55     pushup(rt);
 56 }
 57 void update(int L,int R,int c,int l,int r,int rt)
 58 {
 59     if(L>R) return ;
 60     if(L<=l&&r<=R){
 61         add[rt]=c;
 62         tree[rt]=(r-l+1)*c;
 63         return ;
 64     }
 65 
 66     pushdown(rt,r-l+1);
 67     int m=(l+r)>>1;
 68     if(L<=m) update(L,R,c,lson);
 69     if(R> m) update(L,R,c,rson);
 70     pushup(rt);
 71 }
 72 int query(int L,int R,int l,int r,int rt)
 73 {
 74     if(L<=l&&r<=R){
 75         return tree[rt];
 76     }
 77 
 78     pushdown(rt,r-l+1);
 79     int m=(l+r)>>1;int ret=0;
 80     if(L<=m) ret+=query(L,R,lson);
 81     if(R> m) ret+=query(L,R,rson);
 82     return ret;
 83 }
 84 bool check(int x)
 85 {
 86     build(x,1,n,1);
 87     for(int i=1;i<=m;i++){
 88         int c=q[i].c,l=q[i].l,r=q[i].r;
 89         int cnt=query(l,r,1,n,1);
 90         if(c){
 91             update(l,l+cnt-1,1,1,n,1);
 92             update(l+cnt,r,0,1,n,1);
 93         }
 94         else{
 95             update(r-cnt+1,r,1,1,n,1);
 96             update(l,r-cnt,0,1,n,1);
 97         }
 98     }
 99     return query(k,k,1,n,1);
100 }
101 int main()
102 {
103     int t;
104     scanf("%d",&t);
105     while(t--){
106         scanf("%d%d",&n,&m);
107         for(int i=1;i<=n;i++)
108             scanf("%d",&a[i]);
109         for(int i=1;i<=m;i++)
110             scanf("%d%d%d",&q[i].c,&q[i].l,&q[i].r);
111         scanf("%d",&k);
112         int l=1,r=n;
113         while(l<=r){
114             int mid=(l+r)>>1;
115             if(!check(mid)) r=mid-1;
116             else l=mid+1;
117         }
118         printf("%d\n",r+1);
119     }
120     return 0;
121 }

留校天气好热,基地空调一点用都没有,好闷(吐槽)

猜你喜欢

转载自www.cnblogs.com/ZERO-/p/9313676.html