CF #368 div2

题目链接:http://codeforces.com/contest/707/problem/A

A. Brain's Photos
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was upset and decided to postpone his plans of taking over the world, but to become a photographer instead.

As you may know, the coolest photos are on the film (because you can specify the hashtag #film for such).

Brain took a lot of colourful pictures on colored and black-and-white film. Then he developed and translated it into a digital form. But now, color and black-and-white photos are in one folder, and to sort them, one needs to spend more than one hour!

As soon as Brain is a photographer not programmer now, he asks you to help him determine for a single photo whether it is colored or black-and-white.

Photo can be represented as a matrix sized n × m, and each element of the matrix stores a symbol indicating corresponding pixel color. There are only 6 colors:

  • 'C' (cyan)
  • 'M' (magenta)
  • 'Y' (yellow)
  • 'W' (white)
  • 'G' (grey)
  • 'B' (black)

The photo is considered black-and-white if it has only white, black and grey pixels in it. If there are any of cyan, magenta or yellow pixels in the photo then it is considered colored.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100) — the number of photo pixel matrix rows and columns respectively.

Then n lines describing matrix rows follow. Each of them contains m space-separated characters describing colors of pixels in a row. Each character in the line is one of the 'C', 'M', 'Y', 'W', 'G' or 'B'.

Output

Print the "#Black&White" (without quotes), if the photo is black-and-white and "#Color" (without quotes), if it is colored, in the only line.

Examples
input
2 2
C M
Y Y
output
#Color
input
3 2
W W
W W
B B
output
#Black&White
input
1 1
W
output
#Black&White




水题。。。不多说了。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 int n,m;
 6 bool bo;
 7 char ch;
 8 int main(){
 9     scanf("%d%d",&n,&m);bo=1;
10     for(int i=1;i<=n;i++)
11     for(int j=1;j<=m;j++){
12         for(ch=getchar();ch!='C'&&ch!='M'&&ch!='Y'&&ch!='W'&&ch!='G'&&ch!='B';ch=getchar());
13         if(ch!='W'&&ch!='G'&&ch!='B')bo=0;
14     }
15     if(bo)printf("#Black&White
");
16     else printf("#Color
");
17     return 0;
18 }
View Code

题目链接:http://codeforces.com/contest/707/problem/B

B. Bakery
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.

To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.

Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid — for every kilometer of path between storage and bakery Masha should pay 1 ruble.

Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai ≠ b for every 1 ≤ i ≤ k) and choose a storage in some city s (s = aj for some 1 ≤ j ≤ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).

Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.

Input

The first line of the input contains three integers nm and k (1 ≤ n, m ≤ 105, 0 ≤ k ≤ n) — the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.

Then m lines follow. Each of them contains three integers uv and l (1 ≤ u, v ≤ n1 ≤ l ≤ 109, u ≠ v) meaning that there is a road between cities u and v of length of l kilometers .

If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n) — the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.

Output

Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.

If the bakery can not be opened (while satisfying conditions) in any of the n cities, print  - 1 in the only line.

Examples
input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
output
3
input
3 1 1
1 2 3
3
output
-1
Note

Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.

 最优点一定是与k个特殊点直接相连的点,枚举就好了。。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #define maxn 100005
 5 using namespace std;
 6 int n,m,k,tot,h[maxn],way[maxn*2],fuck[maxn*2],val[maxn*2],a[maxn],f[maxn],ans;
 7 bool b[maxn];
 8 const int inf=2e9;
 9 void insert(int x,int y,int z){way[++tot]=y;fuck[tot]=h[x];h[x]=tot;val[tot]=z;}
10 int main(){
11     scanf("%d%d%d",&n,&m,&k);
12     for(int i=1;i<=n;i++)f[i]=inf;
13     for(int i=1,x,y,z;i<=m;i++){
14         scanf("%d%d%d",&x,&y,&z);insert(x,y,z);insert(y,x,z);
15     }
16     for(int i=1;i<=k;i++){
17         scanf("%d",&a[i]);b[a[i]]=1;
18     }
19     for(int i=1;i<=k;i++){
20         for(int j=h[a[i]];j;j=fuck[j]){
21             if(b[way[j]]==1)continue;
22             f[way[j]]=min(f[way[j]],val[j]);
23         }
24     }
25     ans=inf;
26     for(int i=1;i<=n;i++)ans=min(ans,f[i]);
27     if(ans==inf)printf("-1
");
28     else printf("%d
",ans);
29     return 0;
30 }
View Code

题目链接:http://codeforces.com/contest/707/problem/C

C. Pythagorean Triples
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are calledPythagorean triples.

For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples.

Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse.

Katya had no problems with completing this task. Will you do the same?

Input

The only line of the input contains single integer n (1 ≤ n ≤ 109) — the length of some side of a right triangle.

Output

Print two integers m and k (1 ≤ m, k ≤ 1018), such that nm and k form a Pythagorean triple, in the only line.

In case if there is no any Pythagorean triple containing integer n, print  - 1 in the only line. If there are many answers, print any of them.

Examples
input
3
output
4 5
input
6
output
8 10
input
1
output
-1
input
17
output
144 145
input
67
output
2244 2245
Note

Illustration for the first sample.

套公式。。如果a=2n+1,b=2n^2+2n, c=2n^2+2n+1。a=2n,b=n^2-1, c=n^2+1。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 long long a,b,c;
 5 int main(){
 6     scanf("%I64d",&a);
 7     if(a==1||a==2){printf("-1
");return 0;}
 8     if(a==4){printf("3 5
");return 0;}
 9     if(a&1){a=(a-1)/2;b=2*a*a+2*a;c=b+1;}
10     else{a/=2;b=a*a-1;c=a*a+1;}
11     printf("%I64d %I64d
",b,c);
12     return 0;
13 } 
View Code

题目链接:http://codeforces.com/contest/707/problem/D

D. Persistent Bookcase
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Recently in school Alina has learned what are the persistent data structures: they are data structures that always preserves the previous version of itself and access to it when it is modified.

After reaching home Alina decided to invent her own persistent data structure. Inventing didn't take long: there is a bookcase right behind her bed. Alina thinks that the bookcase is a good choice for a persistent data structure. Initially the bookcase is empty, thus there is no book at any position at any shelf.

The bookcase consists of n shelves, and each shelf has exactly m positions for books at it. Alina enumerates shelves by integers from 1to n and positions at shelves — from 1 to m. Initially the bookcase is empty, thus there is no book at any position at any shelf in it.

Alina wrote down q operations, which will be consecutively applied to the bookcase. Each of the operations has one of four types:

  • i j — Place a book at position j at shelf i if there is no book at it.
  • i j — Remove the book from position j at shelf i if there is a book at it.
  • i — Invert book placing at shelf i. This means that from every position at shelf i which has a book at it, the book should be removed, and at every position at shelf i which has not book at it, a book should be placed.
  • k — Return the books in the bookcase in a state they were after applying k-th operation. In particular, k = 0 means that the bookcase should be in initial state, thus every book in the bookcase should be removed from its position.

After applying each of operation Alina is interested in the number of books in the bookcase. Alina got 'A' in the school and had no problem finding this values. Will you do so?

Input

The first line of the input contains three integers nm and q (1 ≤ n, m ≤ 103, 1 ≤ q ≤ 105) — the bookcase dimensions and the number of operations respectively.

The next q lines describes operations in chronological order — i-th of them describes i-th operation in one of the four formats described in the statement.

It is guaranteed that shelf indices and position indices are correct, and in each of fourth-type operation the number k corresponds to some operation before it or equals to 0.

Output

For each operation, print the number of books in the bookcase after applying it in a separate line. The answers should be printed in chronological order.

Examples
input
2 3 3
1 1 1
3 2
4 0
output
1
4
0
input
4 2 6
3 2
2 2 2
3 3
3 2
2 2 2
3 2
output
2
1
3
3
2
4
input
2 2 2
3 2
2 2 1
output
2
1
Note

This image illustrates the second sample case.

如果是操作1,2,3,i-1向i连边,如果是4,k向i连边。然后一遍暴力dfs就行了。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<vector>
 5 #define maxn 1005
 6 #define maxq 100005
 7 using namespace std;
 8 int n,m,q,a[maxq][4],ans[maxq];
 9 bool b[maxn][maxn];
10 vector <int> son[maxq];
11 void dfs(int x,int y){
12     bool bo=0;
13     if(a[x][0]==1){
14         if(b[a[x][1]][a[x][2]]==0)++y,bo=1;
15         b[a[x][1]][a[x][2]]=1;
16     }else if(a[x][0]==2){
17         if(b[a[x][1]][a[x][2]]==1)--y,bo=1;
18         b[a[x][1]][a[x][2]]=0;
19     }else if(a[x][0]==3){
20         for(int i=1;i<=m;i++){
21             if(b[a[x][1]][i]==0)y++;else y--;
22             b[a[x][1]][i]=!b[a[x][1]][i];
23         }
24     }
25     ans[x]=y;
26     int sz=son[x].size();
27     for(int i=0;i<sz;i++)dfs(son[x][i],y);
28     if(a[x][0]==1){
29         if(bo)b[a[x][1]][a[x][2]]=0;
30     }else if(a[x][0]==2){
31         if(bo)b[a[x][1]][a[x][2]]=1;
32     }else if(a[x][0]==3){
33         for(int i=1;i<=m;i++)b[a[x][1]][i]=!b[a[x][1]][i];
34     }
35 }
36 int main(){
37     scanf("%d%d%d",&n,&m,&q);
38     for(int i=1;i<=q;i++){
39         scanf("%d",&a[i][0]);
40         if(a[i][0]==1||a[i][0]==2)scanf("%d%d",&a[i][1],&a[i][2]),son[i-1].push_back(i);
41         else{
42             scanf("%d",&a[i][1]);
43             if(a[i][0]==3)son[i-1].push_back(i);
44             else son[a[i][1]].push_back(i);
45         }
46     }
47     dfs(0,0);
48     for(int i=1;i<=q;i++)printf("%d
",ans[i]);
49     return 0;
50 }
View Code

题目链接:http://codeforces.com/contest/707/problem/E

E. Garlands
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Like all children, Alesha loves New Year celebration. During the celebration he and his whole family dress up the fir-tree. Like all children, Alesha likes to play with garlands — chains consisting of a lightbulbs.

Alesha uses a grid field sized n × m for playing. The rows of the field are numbered from 1 to n from the top to the bottom and columns are numbered from 1 to m from the left to the right.

Alesha has k garlands which he places at the field. He does so in the way such that each lightbulb of each garland lies in the center of some cell in the field, and each cell contains at most one lightbulb. Of course lightbulbs, which are neighbours in some garland, appears in cells neighbouring by a side.

The example of garland placing.

Each garland is turned off or turned on at any moment. If some garland is turned on then each of its lightbulbs is turned on, the same applies for garland turned off. Each lightbulb in the whole garland set is unique, and thus, being turned on, brings Alesha some pleasure, described by an integer value. Turned off lightbulbs don't bring Alesha any pleasure.

Alesha can turn garlands on and off and wants to know the sum of pleasure value which the lightbulbs, placed in the centers of the cells in some rectangular part of the field, bring him. Initially all the garlands are turned on.

Alesha is still very little and can't add big numbers. He extremely asks you to help him.

Input

The first line of the input contains three integers nm and k (1 ≤ n, m, k ≤ 2000) — the number of field rows, the number of field columns and the number of garlands placed at the field respectively.

Next lines contains garlands set description in the following format:

The first line of a single garland description contains a single integer len (1 ≤ len ≤ 2000) — the number of lightbulbs in the garland.

Each of the next len lines contains three integers ij and w (1 ≤ i ≤ n1 ≤ j ≤ m1 ≤ w ≤ 109) — the coordinates of the cell containing a lightbullb and pleasure value Alesha gets from it if it is turned on. The lightbulbs are given in the order they are forming a chain in the garland. It is guaranteed that neighbouring lightbulbs are placed in the cells neighbouring by a side.

The next line contains single integer q (1 ≤ q ≤ 106) — the number of events in Alesha's game. The next q lines describes events in chronological order. The i-th of them describes the i-th event in the one of the following formats:

  • SWITCH i — Alesha turns off i-th garland if it is turned on, or turns it on if it is turned off. It is guaranteed that 1 ≤ i ≤ k.
  • ASK xyxy2 — Alesha wants to know the sum of pleasure values the lightbulbs, placed in a rectangular part of the field. Top-left cell of a part has coordinates (x1, y1) and right-bottom cell has coordinates (x2, y2). It is guaranteed that 1 ≤ x1 ≤ x2 ≤ n and1 ≤ y1 ≤ y2 ≤ m. There is no more than 2000 events of this type in the input.

All the numbers in the input are integers.

Please note that the input is quite large, so be careful while using some input ways. In particular, it's not recommended to use cin in codes on C++ and class Scanner in codes on Java.

Output

For each ASK operation print the sum Alesha wants to know in a separate line. Print the answers in chronological order.

Examples
input
4 4 3
5
1 1 2
1 2 3
2 2 1
2 1 4
3 1 7
4
1 3 1
2 3 3
2 4 3
1 4 1
7
4 1 1
4 2 9
3 2 8
3 3 3
4 3 4
4 4 1
3 4 1
2
ASK 2 2 3 3
ASK 1 1 4 4
output
15
52
input
4 4 1
8
4 1 1
3 1 2
2 1 1
1 1 7
1 2 5
2 2 4
2 3 1
1 3 1
3
ASK 1 1 3 2
SWITCH 1
ASK 1 1 3 2
output
19
0
Note

This image illustrates the first sample case.

预处理出每条链对每个询问的贡献(o(n^2log^2n)),然后就可以o(k)地询问了。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<vector>
 5 #define maxn 2005
 6 #define maxq 1000005
 7 using namespace std;
 8 int n,m,k,tot,q,a[maxq][7],b[maxn];
 9 long long val[maxn][maxn],t[maxn][maxn];
10 struct dian{int x,y,v;};
11 vector <dian> lian[maxn];
12 char s[10];
13 bool pp[maxn];
14 void insert(int x,int y,int v){
15     for(int i=x;i<=n;i+=i&(-i))
16     for(int j=y;j<=m;j+=j&(-j))t[i][j]+=v;
17 }
18 long long query(int x,int y){
19     long long tt=0;
20     for(int i=x;i;i-=i&(-i))
21     for(int j=y;j;j-=j&(-j))tt+=t[i][j];
22     return tt;
23 }
24 long long calc(int x1,int y1,int x2,int y2){return query(x2,y2)-query(x1-1,y2)-query(x2,y1-1)+query(x1-1,y1-1);}
25 int main(){
26     scanf("%d%d%d",&n,&m,&k);
27     for(int i=1,x,y,z,w;i<=k;i++){
28         scanf("%d",&x);
29         while(x--){
30             scanf("%d%d%d",&y,&z,&w);lian[i].push_back((dian){y,z,w});
31         }
32     }
33     scanf("%d",&q);
34     for(int i=1;i<=q;i++){
35         scanf("%s",s);
36         if(s[0]=='A'){b[++tot]=i;a[i][5]=tot;a[i][0]=1;scanf("%d%d%d%d",&a[i][1],&a[i][2],&a[i][3],&a[i][4]);}
37         else{a[i][0]=0;scanf("%d",&a[i][1]);}
38     }
39     for(int i=1;i<=k;i++){
40         int sz=lian[i].size();
41         for(int j=0;j<sz;j++)insert(lian[i][j].x,lian[i][j].y,lian[i][j].v);
42         for(int j=1;j<=tot;j++)val[i][j]=calc(a[b[j]][1],a[b[j]][2],a[b[j]][3],a[b[j]][4]);
43         for(int j=0;j<sz;j++)insert(lian[i][j].x,lian[i][j].y,-lian[i][j].v);
44     }
45     for(int i=1;i<=q;i++){
46         if(a[i][0]==0)pp[a[i][1]]=!pp[a[i][1]];
47         else{
48             long long ans=0;
49             for(int j=1;j<=k;j++){
50                 if(pp[j]==0)ans+=val[j][a[i][5]];
51             }
52             printf("%I64d
",ans);
53         }
54     }
55     return 0;
56 }
View Code
原文地址:https://www.cnblogs.com/longshengblog/p/5792363.html