3月流水账

 

日常是搬砖砖写写题划划水摸摸鱼看动画片画萌妹纸

以及好多好多好多好多的坑www

3.5

offer8

A.小Ho的强迫症

gcd,感觉对这种东西一点都不敏感阿之前那个青蛙跳石头的容斥也是

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxn = 1e5+5;
 8 
 9 int gcd(int a, int b)
10 {
11     return a % b ? gcd(b, a % b) : b;
12 }
13 
14 int main()
15 {
16     int T;
17     scanf("%d",&T);
18     while(T--)
19     {
20         int l,f,d;
21         scanf("%d %d %d",&l,&f,&d);
22         int g = gcd(l,d);
23         if(f <= g) puts("YES");
24         else puts("NO");
25     }
26     return 0;
27 }
View Code

B.拆字游戏

dfs输出读题要仔细

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 using namespace std;
  6 typedef long long LL;
  7 const int maxn = 1e5+5;
  8 
  9 char g[505][505];
 10 int n,m,vis[505][505];
 11 int xmn,xmx,ymx,ymn,ax,ay;
 12 int a,b,c,d;
 13 int dx[4] = {-1,1,0,0};
 14 int dy[4] = {0,0,1,-1};
 15 
 16 bool in(int x,int y) {return x >= 1 && x <= n && y >= 1 && y <= m;}
 17 
 18 struct node{
 19     int xmn,xmx,ymx,ymn;
 20     int ax,ay;
 21     int col;
 22 }p[505*505];
 23 
 24 int cmp(node n1,node n2)
 25 {
 26     if(n1.ay != n2.ay) return n1.ay < n2.ay;
 27     return n1.ax < n2.ax;
 28 }
 29 
 30 int tot;
 31 
 32 void dfs(int x,int y)
 33 {
 34     vis[x][y] = tot;
 35     if(y < ay) {ax = x;ay = y;}
 36     if(y == ay) 
 37     {
 38         if(x < ax) {ax= x;ay = y;}
 39     }
 40     xmn = min(xmn,x);xmx = max(xmx,x);
 41     ymn = min(ymn,y);ymx = max(ymx,y);
 42 //    printf("x = %d y = %d\n",x,y);
 43     for(int i = 0;i < 4;i++)
 44     {
 45         int xx = x + dx[i];
 46         int yy = y + dy[i];
 47         if(!in(xx,yy)) continue;
 48         if(g[xx][yy] == '0' || vis[xx][yy]) continue;
 49         dfs(xx,yy);
 50     }
 51 }
 52 
 53 void print(int xmn,int ymx,int xmx,int ymn,int col)
 54 {
 55     printf("%d %d\n",xmx-xmn+1,ymx-ymn+1);
 56     for(int i = xmn;i <= xmx;i++)
 57     {
 58         for(int j = ymn;j <= ymx;j++) 
 59         {
 60             if(vis[i][j] == col) printf("%c",g[i][j]);
 61             else printf("0");
 62         }
 63         printf("\n");
 64     }
 65 }
 66 
 67 int main()
 68 {
 69     scanf("%d %d",&n,&m);
 70     for(int i = 1;i <= n;i++) scanf("%s",g[i]+1);
 71     memset(vis,0,sizeof(vis));
 72     tot = 0;
 73     for(int i = 1;i <= n;i++)
 74     {
 75         for(int j = 1;j <= m;j++)
 76         {
 77             if(g[i][j] == '1' && vis[i][j] == 0)
 78             {
 79             //    printf("g[%d][%d] = %c\n",i,j,g[i][j]);
 80                 xmn = 1000;ymn = 1000;
 81                 xmx = 0;ymx = 0;
 82                 ax = 1000;ay = 1000;
 83                 tot++;
 84                 dfs(i,j);
 85                 p[tot].xmx = xmx;
 86                 p[tot].xmn = xmn;
 87                 p[tot].ymx = ymx;
 88                 p[tot].ymn = ymn;
 89                 p[tot].col = tot;
 90                 p[tot].ax = ax;
 91                 p[tot].ay = ay;
 92                 //print(xmn,ymx,xmx,ymn);
 93 
 94             }
 95         }
 96     }
 97     sort(p+1,p+tot+1,cmp);
 98     for(int i = 1;i <= tot;i++) print(p[i].xmn,p[i].ymx,p[i].xmx,p[i].ymn,p[i].col);
 99 //    printf("tot = %d\n",tot);
100 
101     return 0;
102 }
View Code

C.数组分拆

暴力的n2 dp就是见过很多次的dp[i]以i结尾的怎样怎样,枚举上一段结尾的为j来转移,这个的转化可以变成加上前缀和不等于当前前缀和的所有dp值

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <map>
 6 using namespace std;
 7 typedef long long LL;
 8 const int maxn = 5e6+5;
 9 const LL mod = 1e9+7;
10 
11 int n,a[maxn];
12 LL dp[maxn];
13 
14 int main()
15 {
16     scanf("%d",&n);
17     for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
18     map<int,int> h;
19     map<int,long long> f;
20     LL all = 0LL;
21     int sum = 0;
22     f[0] = 1LL;dp[0] = 1LL;
23     for(int i = 1;i <= n;i++)
24     {
25         all = (all + dp[i-1]) % mod;
26         sum = sum + a[i];
27         dp[i] = (all - f[sum] + mod) % mod;
28         f[sum] = (f[sum] + dp[i]) % mod;
29     }
30     //for(int i = 1;i <= n;i++) printf("dp[%d] = %lld\n",i,dp[i]);
31     printf("%lld\n",dp[n]);
32     return 0;
33 }
View Code

D.矩形计数

容斥,仍然是对这种东西不敏感T.T

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxn = 1e5+5;
 8 const int INF = 1e9+7;
 9 
10 int x[maxn],y[maxn],n,m,k;
11 
12 int main()
13 {
14     scanf("%d %d %d",&n,&m,&k);
15     for(int i = 0;i < k;i++) scanf("%d %d",&x[i],&y[i]);
16     LL ans =  (LL) (n*(n+1)/2 ) * (LL) (m*(m+1)/2);
17     //printf("ans = %lld\n",ans);
18     for(int i = 1;i < (1<<k);i++)
19     {
20         int xmin = INF,ymin = INF,xmax = 0,ymax = 0,cnt = 0;
21         for(int j = 0;j < k;j++)
22         {
23             if(i & (1<<j)) 
24             {
25                 cnt++;
26                 xmin = min(xmin,x[j]);ymin = min(ymin,y[j]);
27                 xmax = max(xmax,x[j]);ymax = max(ymax,y[j]);
28             }
29         }
30         LL tmp = (LL) (ymin  * (m - ymax + 1))  * (LL)(xmin * (m - xmax + 1));
31         //printf("i = %d xmin = %d ymin = %d xmax = %d ymax = %d tmp = %lld\n",i,xmin,ymin,xmax,ymax,tmp);
32         if(cnt&1) ans -= tmp;
33         else ans += tmp;
34     }
35     printf("%lld\n",ans);
36     return 0;
37 }
View Code

 

3.12

AtCoder Grand Contest 011

A.Airport Bus

排序

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxn = 1e5+5;
 8 
 9 int a[maxn],n,c,k;
10 
11 int main()
12 {
13     scanf("%d %d %d",&n,&c,&k);
14     for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
15     sort(a+1,a+n+1);
16     int ans = 1;
17     int ed = a[1] + k,cnt = 1;
18     for(int i = 2;i <= n;i++)
19     {
20         if(a[i] > ed || cnt == c) {ans++;ed = a[i]+k;cnt = 0;}
21         cnt++;
22         //printf("i = %d cnt = %d ans = %d\n",i,cnt,ans);
23     }
24     printf("%d\n",ans);
25     return 0;
26 }
View Code

B.Colorful Creatures

感觉贪心还是需要证明清楚才敢写..

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxn = 1e5+5;
 8 
 9 int n;
10 LL a[maxn],sum[maxn];
11 
12 int main()
13 {
14     scanf("%d",&n);
15     memset(sum,0,sizeof(sum));
16     for(int i = 1;i <= n;i++) scanf("%lld",&a[i]);
17     sort(a+1,a+n+1);
18     for(int i = 1;i <= n;i++) sum[i] = sum[i-1] + a[i];
19     int mx = 0;
20     for(int i = 1;i < n;i++)
21     {
22         if(2*sum[i] < a[i+1]) mx = max(mx,i);
23     }
24     printf("%d\n",n-mx);
25     return 0;
26 }
View Code

 

 

 

tls 在群里说教教大家 组合数取模 先码在这里>.<

 

offer 9

整场暴力骗分QwQ,要补题的喔!恩恩!

A.闰秒

这题得80..不知道错哪儿..不想补了TwT

B.水陆距离

bfs把所有的0当起点

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <queue>
 6 using namespace std;
 7 typedef long long LL;
 8 const int maxn = 1e5+5;
 9 const int INF = 1e9+7;
10 int d[1005][1005],n,m;
11 char g[1005][1005];
12 int dx[4] = {1,-1,0,0};
13 int dy[4] = {0,0,1,-1};
14 
15 int in(int x,int y) {return x >= 1 && x <= n && y >= 1 && y <= m;}
16 
17 void bfs()
18 {
19     queue<pair<int,int> > q;
20     for(int i = 1;i <= n;i++)
21     {
22         for(int j = 1;j <= m;j++)
23         {
24             if(g[i][j] == '0') 
25             {
26                 d[i][j] = 0;
27                 q.push(make_pair(i,j));
28             }
29             else d[i][j] = INF;
30         }
31     }
32     while(!q.empty())
33     {
34         pair<int,int> u = q.front();q.pop();
35         int x = u.first,y = u.second;
36         for(int i = 0;i < 4;i++)
37         {
38             int xx = x + dx[i];
39             int yy = y + dy[i];
40             if(!in(xx,yy)) continue;
41             if(g[xx][yy] == '0') continue;
42             if(d[x][y]+1 < d[xx][yy])
43             {
44                 d[xx][yy] = d[x][y] + 1;
45                 q.push(make_pair(xx,yy));
46             }
47 
48         }
49     }
50 }
51 
52 int main()
53 {
54     scanf("%d %d",&n,&m);
55     for(int i = 1;i <= n;i++) scanf("%s",g[i]+1);
56     bfs();
57     for(int i = 1;i <= n;i++)
58     {
59         for(int j = 1;j < m;j++) printf("%d ",d[i][j]);
60         printf("%d\n",d[i][m]);
61     }
62     return 0;
63 }
View Code

C.三等分

过这么久才补..

内嵌2/3,分别1/3

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 using namespace std;
 7 typedef long long LL;
 8 const int maxn = 1e5+5;
 9 int n,m;
10 vector<int> g[maxn];
11 int a[maxn],s[maxn],ave[maxn],p[maxn],part,root;
12 LL ans;
13 
14 void dfs(int u,int fa)
15 {
16     s[u] = a[u];
17     ave[u] = 0;
18     for(int i = 0;i < g[u].size();i++)
19     {
20         int v = g[u][i];
21         if(v == fa) continue;
22         dfs(v,u);
23         s[u] += s[v];
24         ave[u] += ave[v];
25     }
26     if(u == root) return;
27     if(s[u] == 2 * part)
28     {
29         ans += ave[u];
30     }
31     if(s[u] == part)
32     {
33         ans -= ave[u];
34         ave[u]++;
35     }
36 }
37 
38 int main()
39 {
40     int T;
41     scanf("%d",&T);
42     while(T--)
43     {
44         scanf("%d",&n);
45         for(int i = 1;i <= n;i++) g[i].clear();
46         int sum = 0;
47         for(int i = 1;i <= n;i++)
48         {
49             scanf("%d %d",&a[i],&p[i]);
50             if(p[i] == 0) root = i;
51             else g[i].push_back(p[i]),g[p[i]].push_back(i);
52             sum += a[i];
53         }
54         if(sum%3) {puts("0");continue;}
55         ans = 0LL;part = sum/3;
56         dfs(root,-1);
57         ans += 1LL * ave[root] * (ave[root] - 1) / 2;
58         printf("%lld\n",ans);
59     }
60     return 0;
61 }
View Code

D.矩阵填数

写得很清楚了

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long LL;
 5 const int maxn = 1e4 + 10;
 6 const LL mod = 1e9 + 7;
 7 LL fac[maxn];
 8 
 9 LL qpow(LL a, LL b)
10 {
11     LL ret = 1LL;
12     while(b)
13     {
14         if(b & 1) ret = ret * a % mod;
15         a = a * a % mod;
16         b >>= 1;
17     }
18     return ret;
19 }
20 
21 LL inv(LL x)
22 {
23     return qpow(x, mod - 2);
24 }
25 
26 
27 int main()
28 {
29     int n,m;
30     scanf("%d %d",&n,&m);
31     LL ans = 1LL;
32     for(int i = 1;i <= n*m;i++) ans = ans * i % mod;
33     for(int i = 1;i <= n;i++)
34     {
35         for(int j = 1;j <= m;j++)
36         {
37             ans = ans * inv(i+j-1) % mod;
38         }
39     }
40     printf("%lld\n",ans);
41     return 0;
42 }
View Code

 

 

3.16

cf 404 not find !

于是先看c二分wa整场,主要是check的时候没有再去转化一下,然后直接求和爆long long 还有边界二分完出来再判一下,还是不会二分边界TAT

C.Anton and Fairy Tale

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef long long LL;
 8 const int maxn = 1e6+5;
 9 
10 LL n,m;
11 LL pre;
12 
13 LL ok(LL x)
14 {
15     LL res = 0LL;
16     if(x%2 == 0) res = x/2LL * (x+1);
17     else res = (x+1)/2 * x;
18     return res;
19 }
20 
21 int main()
22 {
23     scanf("%I64d %I64d",&n,&m);
24     if(n <= m) {printf("%I64d\n",n);return 0;}
25     LL lb = 0,ub = 2e9,mid,ans = 0;
26     while(ub-lb > 1)
27     {
28         mid = lb + (ub - lb) / 2;
29         if(ok(mid) + m <= n) lb = mid,ans = max(ans,mid);
30         else ub = mid;
31         //printf("lb = %I64d ub = %I64d mid = %I64d ans = %I64d\n",lb,ub,mid,ans);
32     }
33     if(ok(lb) + m == n) ans = m + lb;
34     else ans = m + lb + 1;
35     printf("%I64d\n",ans);
36     return 0;
37 }
View Code

 

3.18

cf 405div2 

 

 
3.23
有向图博弈
 
cf 406div2
 
3.26
重回东方大坑...还是喜欢温柔的声音www
 
----------------- 这里是萌萌哒的昏割线 -----------------------
hiho offer 11
虽然还是做很挫...可是进步的是wa了顽强的寻找wa点!!!
大概是受上次面试影响太深马上就想尺取了,写了写发现不对,枚举左端点二分右端点,wa在于是刚好2个h,一个i,一个o
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <set>
 6 using namespace std;
 7 
 8 const int maxn = 1e5+5;
 9 char s[maxn];
10 int f[1005][1005][10];
11 int hh[maxn],hi[maxn],ho[maxn];
12 
13 int ok(int l,int r)
14 {
15     return hh[r]-hh[l-1] >= 2 && hi[r] - hi[l-1] >= 1 && ho[r] - ho[l-1] >= 1;
16 }
17 
18 int main()
19 {
20     scanf("%s",s+1);
21     int n = strlen(s+1);
22 
23     memset(hh,0,sizeof(hh));
24     memset(hi,0,sizeof(hi));
25     memset(ho,0,sizeof(ho));
26     for(int i = 1;i <= n;i++)
27     {
28         hh[i] = hh[i-1];
29         hi[i] = hi[i-1];
30         ho[i] = ho[i-1];
31         if(s[i] == 'h') hh[i]++;
32         if(s[i] == 'i') hi[i]++;
33         if(s[i] == 'o') ho[i]++;
34     }
35 
36    /* for(int i = 1;i <= n;i++)
37     {
38 
39         printf("hh[%d] = %d hi[%d] = %d ho[%d] = %d\n",i,hh[i],i,hi[i],i,ho[i]);
40     }*/
41 
42     if(hh[n] < 2 || hi[n] < 1 || ho[n] < 1) {puts("-1");return 0;}
43 
44     int ans = n+1;
45     for(int i = 1;i <= n-3;i++)
46     {
47         int lb = i,ub = n,mid,R = n+1;
48         if(hh[n]-hh[i-1] < 2 || hi[n]-hi[i-1] < 1 || ho[n]-ho[i-1] < 1) continue;
49         while(ub - lb > 1)
50         {
51             mid = lb + (ub - lb) / 2;
52             if(ok(i,mid))
53             {
54                     ub = mid - 1;
55                     if(hh[mid] - hh[i-1] == 2 && hi[mid]-hi[i-1] == 1 && ho[mid] - ho[i-1] == 1) R = min(R,mid);
56             }
57             else lb = mid;
58         }
59         if(hh[mid] - hh[i-1] == 2 && hi[mid]-hi[i-1] == 1 && ho[mid] - ho[i-1] == 1) R = min(R,mid);
60         if(hh[ub] - hh[i-1] == 2 && hi[ub]-hi[i-1] == 1 && ho[ub] - ho[i-1] == 1) R = min(R,ub);
61 
62         //printf("i = %d R = %d\n",i,R);
63         ans = min(ans,R-i+1);
64     }
65     printf("%d\n",ans);
66 
67     return 0;
68 }
View Code

物品价值

m这么小怎么没想到状压一下呢,然后就是01背包

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 1e3+5;
 8 int dp[1<<15],cp[1<<15];
 9 int v[maxn],s[maxn][maxn],w[1<<15];
10 int n,m;
11 
12 int main()
13 {
14     int T;
15     scanf("%d",&T);
16     while(T--)
17     {
18         scanf("%d %d",&n,&m);
19         memset(w,0,sizeof(w));
20         for(int i = 1;i <= n;i++)
21         {
22             scanf("%d %d",&v[i],&s[i][0]);
23             for(int j = 1;j <= s[i][0];j++)
24             {
25                 scanf("%d",&s[i][j]);
26                 s[i][j]--;
27                 w[i] = w[i]^(1 << s[i][j]);
28             }
29         }
30 
31         memset(dp,-1,sizeof(dp));
32         memset(cp,-1,sizeof(cp));
33         cp[0] = 0;
34         for(int i = 1;i <= n;i++)
35         {
36 
37             for(int j = 0;j < (1<<m);j++)
38             {
39                if(cp[j^w[i]] >= 0) dp[j] = max(cp[j^w[i]] + v[i],dp[j]);
40                if(cp[j] >= 0) dp[j] = max(cp[j],dp[j]);
41             }
42 
43             for(int j = 0;j < (1<<m);j++) cp[j] = dp[j];
44 
45             //for(int j = 0;j < (1<<m);j++) printf("i = %d dp[%d] = %d\n",i,j,dp[j]);
46 
47         }
48 
49         //for(int j = 0;j < (1<<m);j++) printf("dp[%d] = %d\n",j,dp[j]);
50         printf("%d\n",dp[(1<<m)-1]);
51 
52     }
53     return 0;
54 }
View Code

岛屿3

并茶几,写wa了在于,给坐标编号的时候,想着x,y 分别乘以不同权值就好,x*1000 + y*2000 (纸张TAT..

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <set>
 6 using namespace std;
 7 
 8 const int maxn = 5e6+5;
 9 int fa[maxn];
10 
11 //dsu
12 int Find(int x)
13 {
14     return x == fa[x] ? x : fa[x] = Find(fa[x]);
15 }
16 
17 void Union(int x,int y)
18 {
19     int u = Find(x);
20     int v = Find(y);
21     if(u != v) fa[u] = v;
22 }
23 
24 int n;
25 int dx[4] = {0,0,-1,1};
26 int dy[4] = {1,-1,0,0};
27 int g[1005][1005];
28 
29 int main()
30 {
31     scanf("%d",&n);
32     int cnt = 0,cc = 0,cs = 0;
33     int x,y;
34     memset(g,0,sizeof(g));
35 
36     for(int i = 0;i < maxn;i++) fa[i] = i;
37 
38     for(int i = 1;i <= n;i++)
39     {
40         scanf("%d %d",&x,&y);g[x][y] = 1;
41         int u = x * 1000 + y ;
42         int ok = 0,add = 0;
43         set<int> root;
44         for(int j = 0;j < 4;j++)
45         {
46             int xx = x + dx[j];
47             int yy = y + dy[j];
48             if(xx < 0 || xx >= 1000 || yy < 0 || yy >= 1000)
49             {
50                 add++;continue;
51             }
52             if(g[xx][yy])
53             {
54                 ok++;
55                 int v = xx * 1000 + yy;
56                 root.insert(Find(v));
57                // Union(u,v);
58             }
59             else add++;
60         }
61 
62          for(int j = 0;j < 4;j++)
63          {
64             int xx = x + dx[j];
65             int yy = y + dy[j];
66             if(xx < 0 || xx >= 1000 || yy < 0 || yy >= 1000)
67             {
68                 continue;
69             }
70             if(g[xx][yy])
71             {
72                 int v = xx * 1000 + yy ;
73                 Union(u,v);
74             }
75          }
76 
77 
78         if(ok == 0) cnt++;
79         else cnt = cnt - root.size() + 1;
80         cc = cc + add - ok;
81         printf("%d %d %d\n",cnt,i,cc);
82        // printf("root.size() = %d\n",root.size());
83     }
84     return 0;
85 }
View Code

 

排队接水

莫队,树状数组,赛后偷瞄代码补的....比赛的时候想法极其sb(蠢哭

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef long long LL;
 8 const int maxn = 2e4+5;
 9 int a[maxn];
10 
11 //BIT
12 LL c[2][maxn];
13 int lowbit(int x) {return x & (-x);}
14 
15 void update(int op,int i,int x)
16 {
17     while(i < maxn) c[op][i] += x,i += lowbit(i);
18 }
19 
20 LL query(int op,int i)
21 {
22     LL ret = 0;
23     while(i) ret += c[op][i],i -= lowbit(i);
24     return ret;
25 }
26 
27 //Mo
28 int block;
29 LL ans[maxn];
30 struct node
31 {
32     int id,l,r;
33 }q[maxn];
34 
35 bool cmp(node n1,node n2)
36 {
37     if(n1.l / block != n2.l / block) return n1.l / block < n2.l / block;
38     return n1.r < n2.r;
39 }
40 
41 int n,m;
42 
43 int main()
44 {
45     int T;
46     scanf("%d",&T);
47     while(T--)
48     {
49         memset(c,0,sizeof(c));
50         scanf("%d %d",&n,&m);
51         for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
52         for(int i = 0;i < m;i++)
53             scanf("%d %d",&q[i].l,&q[i].r),q[i].id = i;
54         block = sqrt(n),sort(q,q+m,cmp);
55         int l = 1,r = 0;
56         LL tmp = 0;
57         for(int i = 0;i < m;i++)
58         {
59             while(r < q[i].r)
60             {
61                 r++;
62                 update(0,a[r],1);
63                 update(1,a[r],a[r]);
64                 tmp += (query(0,maxn) - query(0,a[r]-1)) * a[r];
65                 tmp += query(1,a[r]-1);
66             }
67             while(r > q[i].r)
68             {
69                 tmp -= (query(0,maxn) - query(0,a[r]-1)) * a[r];
70                 tmp -= query(1,a[r]-1);
71                 update(0,a[r],-1);
72                 update(1,a[r],-a[r]);
73                 r--;
74             }
75             while(l > q[i].l)
76             {
77                 l--;
78                 update(0,a[l],1);
79                 update(1,a[l],a[l]);
80                 tmp += (query(0,maxn) - query(0,a[l]-1)) * a[l];
81                 tmp += query(1,a[l]-1);
82             }
83             while(l < q[i].l)
84             {
85                 tmp -= (query(0,maxn) - query(0,a[l]-1)) * a[l];
86                 tmp -= query(1,a[l]-1);
87                 update(0,a[l],-1);
88                 update(1,a[l],-a[l]);
89                 l++;
90             }
91             //printf("l = %d r = %d \n",l,r);
92             ans[q[i].id] = tmp;
93         }
94         for(int i = 0;i < m;i++) printf("%lld\n",ans[i]);
95     }
96     return 0;
97 }
View Code

 

 

 
 
 
 

 

 

 

转载于:https://www.cnblogs.com/wuyuewoniu/p/6481751.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值