CodeVS第二次月赛

传送门

http://codevs.cn/problem/4109/
http://codevs.cn/problem/4110/
http://codevs.cn/problem/4111/

T1 TTY loves numbers

题目大意

给定一个模数m,构造一个最小01串,使其mod m=0
长度为len的01串满足,从第1位到第i位都为1,第i+1到第len位都为0 (1<=i<=len)

题解

11111...11110len+10len1+...101

10len19

根据欧拉定理, 10ϕ(m)1(mod  m)
要求10与m互质,那么我们假设m为1e7以内的最大素数10999997
所以长度不超过1e7,一定有解不存在输出’Impossible’的情况
所以,我们从小到大构造全是1的串,如果能整除就直接输出,
如果不能找到之前与其取模之后相等的数做差即为答案

var
 x:array[0..11000005]of longint;
 i,j,k:longint;
 m,ans,len:longint;
begin
 readln(m); ans:=1; len:=1;
 for i:=1 to 100000000 do
  begin
   if ans mod m=0
   then begin
     for j:=1 to len do write(1);
     writeln; halt;
   end
   else
    if x[ans mod m]<>0
    then begin
     for j:=1 to len-x[ans mod m] do
      write(1);
     for j:=1 to x[ans mod m] do
      write(0);
     writeln;
     halt;
    end
    else x[ans mod m]:=len;
   inc(len); ans:=(ans*10+1) mod m;
  end;
end.

T2 TTY Plants Sunflowers

题目大意

给你一棵n个点n-1条边的无根树和每个点的权值,询问任意两点间路线的LIS值

题解

回顾 O(nlogn) 的LIS,每次修改的是ans以及维护的长度为i的LIS结尾的最小值,dfs动态维护数组和ans就好了,离线处理处任意两点的答案
O(n2logn+m)

var
 w:array[0..8000,1..2]of longint;
 ans:array[0..2005,0..2005]of longint;
 d,x,y:array[0..2005]of longint;
 i,j,k:longint;
 n,m,len,a,b,anss:longint;
procedure init(a,b:longint);
begin
 w[len,1]:=b;
 if w[a,2]=0
 then w[a,2]:=len else w[w[a,1],2]:=len;
 w[a,1]:=len; inc(len);
end;

function find(a:longint):longint;
var l,r,mid:longint;
begin
 l:=1; r:=anss;
 while l<r do
  begin
   mid:=(l+r)>>1;
   if a<d[mid]
   then r:=mid
   else l:=mid+1;
  end;
 exit(l);
end;

procedure dfs(a,b:longint);
var tt,temp,e,f,g:longint;
begin
 y[a]:=1;
 if x[a]>d[anss]
 then begin inc(anss); d[anss]:=x[a]; e:=anss; f:=0; g:=-1; end
 else begin temp:=find(x[a]); e:=temp; f:=d[e]; g:=0; d[e]:=x[a]; end;
 ans[b,a]:=anss;
 tt:=w[a,2];
 while tt<>0 do
  begin
   if y[w[tt,1]]=0 then begin
    dfs(w[tt,1],b);
   end;
   tt:=w[tt,2];
  end;
 d[e]:=f; inc(anss,g);
end;

begin
 readln(n); len:=n+1;
 for i:=1 to n-1 do
  begin readln(a,b); init(a,b); init(b,a); end;
 for i:=1 to n do
  read(x[i]);
 for i:=1 to n do begin
  fillchar(y,sizeof(y),0);
  fillchar(d,sizeof(d),0);
  anss:=0;
  dfs(i,i);
 end;
 readln(m);
 for i:=1 to m do
  begin
   readln(a,b);
   writeln(ans[a,b]);
  end;
end.

T3 TTY Saves Da’shgua

题目大意

给出一个序列,进行如下3个操作
1. Ia[i]x
2. [L,R]a[i],ln(a[i])
3. [L,R]

题解

[BZOJ3211] 花神游历各国/[BZOJ3038] 上帝造题的七分钟2
ln(a[i])40 ,ln(0)=0,没有意义,所以我们对于操作2当某区间和为0时,不继续向下修改

var
 x:array[0..100000]of longint;
 w:array[0..600000,1..3]of int64;
 i,j,k:longint;
 n,m:longint;
 a,b,c:longint;
procedure build(a,l,r:longint);
var mid:longint;
begin
 w[a,1]:=l; w[a,2]:=r; 
 if l=r 
 then begin w[a,3]:=x[l]; exit; end;
 mid:=(l+r)>>1;
 build(a*2,l,mid); build(a*2+1,mid+1,r);
 w[a,3]:=w[a*2,3]+w[a*2+1,3];
end;

procedure update1(a,b,c:longint);
var mid:longint;
begin
 if (w[a,1]=w[a,2])//and(w[a,1]=b)
 then begin w[a,3]:=c; exit; end;
 mid:=(w[a,1]+w[a,2])>>1;
 if b<=mid
 then update1(a*2,b,c)
 else update1(a*2+1,b,c);
 w[a,3]:=w[a*2,3]+w[a*2+1,3];
end;

procedure update2(a,l,r:longint);
var mid:longint;
begin
 if w[a,3]=0 then exit;
 if (w[a,1]=w[a,2])
 then begin w[a,3]:=trunc(ln(w[a,3])); exit; end;
 mid:=(w[a,1]+w[a,2])>>1;
 if r<=mid then update2(a*2,l,r) else
 if l>mid then update2(a*2+1,l,r) 
 else begin update2(a*2,l,mid); update2(a*2+1,mid+1,r); end;
 w[a,3]:=w[a*2,3]+w[a*2+1,3];
end;

function query(a,l,r:longint):int64;
var mid:longint;
begin
 if (w[a,1]=l)and(w[a,2]=r)
 then exit(w[a,3]);
 mid:=(w[a,1]+w[a,2])>>1;
 if r<=mid then exit(query(a*2,l,r)) else
 if l>mid then exit(query(a*2+1,l,r)) 
 else exit(query(a*2,l,mid)+query(a*2+1,mid+1,r));
end;

begin
 readln(n,m);
 for i:=1 to n do 
  read(x[i]);
 build(1,1,n);
 for i:=1 to m do
  begin
   readln(a,b,c);
   case a of
   1:update1(1,b,c);
   2:update2(1,b,c);
   3:writeln(query(1,b,c));
   end;
  end;
end.

你可能感兴趣的:(CodeVS第二次月赛)