9.2 solution to a problem

T1

Exam called memory of the violent search, wireless proximity positive solution, but did not think to put it another $ dp $ fashion store, to optimize their own optimization of the results, in fact, a little bit on it to change

Set $ ​​dp [i] [j] $ represent the first $ i $ the maximum number of points with the $ j $ attractions can pass the time, they put a $ dp $ transferred $ dfs $ on it, this kind of deposit $ dp $ value method can also be understood as a search of memory, the memory of a good way to $ dfs $ time complexity of guarantee

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #define maxv 2010
 5 #define maxm 510
 6 #define maxn 510
 7 #define maxe 5010
 8 #define inf 4557430888798830399
 9 #define int long long
10 using namespace std;
11 int v,m,n,e,l,r,c,ww;
12 int js,ans;
13 int ru[maxv],a[maxm],chu[maxv],b[maxn];
14 int head[maxv],to[maxe],xia[maxe],w[maxe];
15 int visit[maxv];
16 int dp[maxv][maxv];
17 void add(int x,int y,int z)
18 {
19     to[++js]=y;  xia[js]=head[x];  w[js]=z;  head[x]=js;
20 }
21 void dfs(int x)
22 {
23     visit[x]=1;
24     for(int i=head[x];i;i=xia[i])
25     {
26         int ls=to[i];
27         if(!visit[ls])  dfs(to[i]);
28         for(int j=1;j<=v;++j)
29         {
30             if(dp[ls][j]<inf)  dp[x][j+1]=min(dp[x][j+1],dp[ls][j]+w[i]);
31             if(ru[x]&&dp[x][j+1]+a[ru[x]]<=l)  ans=max(ans,j+1);
32         }
33     }
34     if(chu[x])  dp[x][1]=min(dp[x][1],b[chu[x]]);
35 }
36 main()
37 {
38     scanf("%lld%lld%lld%lld%lld",&v,&m,&n,&e,&l);  memset(dp,0x3f,sizeof(dp));
39     for(int i=1;i<=m;++i)  {scanf("%lld",&r);  scanf("%lld",&a[i]);  ru[r]=i;}
40     for(int i=1;i<=n;++i)  {scanf("%lld",&c);  scanf("%lld",&b[i]);  chu[c]=i;}
41     for(int i=1;i<=e;++i)  {scanf("%lld%lld%lld",&r,&c,&ww);  add(r,c,ww);}
42         for(int i=1;i<=v;++i)
43         {
44             if(chu[i]&&ru[i])  ans=max(ans,1ll*1);
45             if(!visit[i])  dfs(i);
46         }
47     printf("%lld\n",ans);
48     return 0;
49 }
View Code

T2

Played on the test a $ O (n ^ 2) $ violent, actually quite miss, my idea is to find the nearest one to meet not boring point corresponding to each point, then for the current point, all can not at this point found the previous point, all you can, so we can use $ (n ^ 2) O after this point of $ pretreatment, and $ O (n) $ answer to water the fat portion is divided, of course, that the $ (n ^ 2) $ pretreatment may become $ O (n) $ by optimizing monotone pointer from O, but the inquiry $ O (n) $ can not be processed, plus $ q $ is $ (n ^ 2) O $ time complexity, so even if you are a lot quicker, but still can not escape the fate of $ T80 $

Next is a positive solution, we first wave of frenzied push formula

Set $ ​​to [i] $ is what I just said that to the $ i $ the left end point of a minimum interval of not boring the right point

TOT set $ [i] and is $, arithmetic formulas 1 to i, would be finished dry

Then get to the point

$ans=\sum\limits_{i=l}^{r}[to[i]{\leq}r]\sum\limits_{j=to[i]}^{r}(j-i)$

   $=\sum\limits_{i=l}^{r}[to[i]{\leq}r](-i{\times}(r-to[i]+1)+\sum\limits_{j=to[i]}^{r}j$

   $=\sum\limits_{i=l}^{r}[to[i]{\leq}r](to[i]{\times}i-i{\times}(r+1)+tot[r]-tot[to[i]-1])$

   $=\sum\limits_{i=l}^{r}[to[i]{\leq}r]to[i]{\times}i-(r+1){\times}\sum\limits_{i=l}^{r}[to[i]{\leq}r]i+tot[r]{\times}\sum\limits_{i=l}^{r}[to[i]{\leq}r]-\sum\limits_{i=l}^{r}[to[i]{\leq}r]tot[to[i]-1]$

So if we can maintain Fenwick tree, then how to deal with $ \ sum \ limits_ {i = l} ^ {r} [to [i] {\ leq} r] $ this condition, a great method, in a tree array with $ to [i] $ insert for the next standard, open four Fenwick tree, the query has been good, so we only meet the current $ to [i] {\ leq} r $ , how to meet the $ i {\ geq} l $ for this condition? In the tree clearance is not constant array meet the conditions $ i $ on it

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #define int long long
 6 #define maxn 100100
 7 using namespace std;
 8 struct node{
 9     int l,r,bh;
10 }Q[maxn];
11 int n,m,q,tot,head=1,head1=1;
12 int a[maxn],to[maxn],kind[maxn];
13 int c1[maxn],c2[maxn],c3[maxn],c4[maxn],ans[maxn],pd[maxn];
14 bool cmp(const node &a,const node &b)
15 {
16     return a.l<b.l;
17 }
18 int lowbit(int x)
19 {
20     return x&(-x);
21 }
22 void add(int pos,int w,int a[])
23 {
24     for(;pos<=n;pos+=lowbit(pos))  a[pos]+=w;
25 }
26 int query(int pos,int a[])
27 {
28     int ans=0;
29     for(;pos>0;pos-=lowbit(pos))  ans+=a[pos];
30     return ans;
31 }
32 main()
33 {
34     scanf("%lld%lld%lld",&n,&m,&q);
35     for(int i=1;i<=n;++i)  scanf("%lld",&a[i]);
36     for(int i=1;i<=q;++i)  {scanf("%lld%lld",&Q[i].l,&Q[i].r);  Q[i].bh=i;}
37     for(int i=1;i<=n;++i)
38     {
39         if(tot<m)
40         {
41             if(!kind[a[i]])  tot++;
42             kind[a[i]]++;
43         }
44         while(tot==m)
45         {
46             to[head]=i;  kind[a[head]]--;
47             if(!kind[a[head]])  tot--;
48             head++;
49         }
50     }
51     sort(Q+1,Q+q+1,cmp);  head=1;
52     for(int i=1;i<=q;++i)
53     {
54         while(to[head]<=Q[i].r&&head<=n)
55         {
56             if(to[head]==0)  {head++;  continue;}
57             if(head<Q[i].l)  {head++;  continue;}
58             add(to[head],to[head]*head,c1);  add(to[head],head,c2);
59             add(to[head],1,c3);  add(to[head],to[head]*(to[head]-1)/2,c4);
60             pd[head]=1;  head++;
61         }
62         while(head1<Q[i].l&&head1<=n)
63         {
64             if(!pd[head1])  {head1++;  continue;}
65             add(to[head1],-to[head1]*head1,c1);  add(to[head1],-head1,c2);
66             add(to[head1],-1,c3);  add(to[head1],-to[head1]*(to[head1]-1)/2,c4);
67             head1++;
68         }
69         int ls1=query(Q[i].r,c1)-query(max(Q[i].l,to[Q[i].l])-1,c1);
70         int ls2=query(Q[i].r,c2)-query(max(Q[i].l,to[Q[i].l])-1,c2);
71         int ls3=query(Q[i].r,c3)-query(max(Q[i].l,to[Q[i].l])-1,c3);
72         int ls4=query(Q[i].r,c4)-query(max(Q[i].l,to[Q[i].l])-1,c4);
73         ls2=ls2*(Q[i].r+1);  ls3=ls3*Q[i].r*(Q[i].r+1)/2;  ans[Q[i].bh]=ls1-ls2+ls3-ls4;
74     }
75     for(int i=1;i<=q;++i)  printf("%lld\n",ans[i]);
76     return 0;
77 }
Pass parameters, I actually have not tried for a long time

T3

Is desirable, successful Gugu Gu

Guess you like

Origin www.cnblogs.com/hzjuruo/p/11518788.html