Minimum
时间限制:1000ms
单点时限:1000ms
内存限制:256MB
描述
You are given a list of integers a0, a1, …, a2^k-1.
You need to support two types of queries:
Output Minx,y∈[l,r] {ax∙ay}.
Let ax=y.
输入
The first line is an integer T, indicating the number of test cases. (1≤T≤10).
For each test case:
The first line contains an integer k (0 ≤ k ≤ 17).
The following line contains 2k integers, a0, a1, …, a2^k-1 (-2k ≤ ai < 2k).
The next line contains a integer (1 ≤ Q < 2k), indicating the number of queries. Then next Q lines, each line is one of:
1 l r: Output Minx,y∈[l,r]{ax∙ay}. (0 ≤ l ≤ r < 2k)
2 x y: Let ax=y. (0 ≤ x < 2k, -2k ≤ y < 2k)
输出
For each query 1, output a line contains an integer, indicating the answer.
样例输入
1
3
1 1 2 2 1 1 2 2
5
1 0 7
1 1 2
2 1 2
2 2 2
1 1 2
样例输出
1
1
4
单点更新+区间查询, 附了个输入挂的版子
结构体中定义四个变量,最大,最小,绝对值最大, 绝对值最小233
然后按照需求更新就好,注意结果要转成long long型, 而且数组是1e5级别的, 一些细节的地方再注意一下
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 140000;
const int INF = 0x3f3f3f3f;
namespace fastIO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
#define ll long long
//fread->read
bool IOerror=0;
inline char nc(){
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if (p1==pend){
p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);
if (pend==p1){IOerror=1;return -1;}
//{printf("IO error!\n");system("pause");for (;;);exit(0);}
}
return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline void read(int &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (sign)x=-x;
}
#undef ll
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace fastIO;
struct Tree{
int max, min, ab_m, ab_s; //初始化为INF
int l, r;
} tree[5*maxn];
void up(int node){
tree[node].max = max(tree[node*2].max, tree[node*2+1].max);
tree[node].min = min(tree[node*2].min, tree[node*2+1].min);
tree[node].ab_m = min(tree[node*2].ab_m, tree[node*2+1].ab_m);
tree[node].ab_s = min(max(tree[node*2].ab_m, tree[node*2+1].ab_m), min(tree[node*2].ab_s, tree[node*2+1].ab_s));
}
void update(int node, int tar, int c){
if(tree[node].l == tree[node].r){
tree[node].max = tree[node].min = c;
tree[node].ab_m = tree[node].ab_s= abs(c);
return;
}
int mid = (tree[node].l + tree[node].r)/2;
if(tar <= mid) update(node*2, tar, c);
else update(node*2+1, tar, c);
up(node);
}
void build(int node){
if(tree[node].l == tree[node].r){
int c; read(c);
tree[node].max = tree[node].min = c;
tree[node].ab_m = tree[node].ab_s= abs(c);
return;
}
int mid = (tree[node].l + tree[node].r)/2;
tree[node*2].l = tree[node].l, tree[node*2].r = mid;
tree[node*2+1].l = mid+1, tree[node*2+1].r = tree[node].r;
build(node*2);
build(node*2+1);
up(node);
}
void query(int node, int l , int r, int& a, int& b, int& ab1, int& ab2){ // ab1 means ab_m, ab2 means ab_s
if(tree[node].l >= l && tree[node].r <= r){
a = max(a, tree[node].max);
b = min(b, tree[node].min);
if(ab1 > tree[node].ab_m){
ab2 = min(ab1, tree[node].ab_s);
ab1 = tree[node].ab_m;
}
else ab2 = min(tree[node].ab_m, ab2);
return;
}
int mid = (tree[node].l + tree[node].r)/2;
if(l <= mid) query(node*2, l, r, a, b, ab1, ab2);
if(r > mid) query(node*2+1, l, r, a, b, ab1, ab2);
return;
}
int main()
{
int t;
read(t);
while(t--){
int k, n;
read(k);
tree[1].l = 1, tree[1].r = pow(2, k);
build(1);
int q;
read(q);
for(int i = 0; i < q; i++){
int op, x, y;
read(op);
read(x);
read(y);
if(op == 2)
update(1, x+1, y);
else{
int _max, _min, ab_s, ab_m;
ab_s = ab_m = INF;
_max = -INF, _min = INF;
query(1, x+1, y+1, _max, _min, ab_m, ab_s);
ll ans = (ll)_max * (ll)_min;
ll rec = (ll)ab_m * (ll)ab_s;
if(ans < 0) printf("%lld\n", ans);
else printf("%lld\n", rec);
}
}
}
return 0;
}
Territorial Dispute
时间限制:1000ms
单点时限:1000ms
内存限制:256MB
描述
In 2333, the C++ Empire and the Java Republic become the most powerful country in the world. They compete with each other in the colonizing the Mars.
There are n colonies on the Mars, numbered from 1 to n. The i-th colony’s location is given by a pair of integers (xi, yi). Notice that latest technology in 2333 finds out that the surface of Mars is a two-dimensional plane, and each colony can be regarded as a point on this plane. Each colony will be allocated to one of the two countries during the Mars Development Summit which will be held in the next month.
After all colonies are allocated, two countries must decide a border line. The Mars Development Convention of 2048 had declared that: A valid border line of two countries should be a straight line, which makes colonies of different countries be situated on different sides of the line.
The evil Python programmer, David, notices that there may exist a plan of allocating colonies, which makes the valid border line do not exist. According to human history, this will cause a territorial dispute, and eventually lead to war.
David wants to change the colony allocation plan secretly during the Mars Development Summit. Now he needs you to give him a specific plan of allocation which will cause a territorial dispute. He promises that he will give you 1000000007 bitcoins for the plan.
输入
The first line of the input is an integer T, the number of the test cases (T ≤ 50).
For each test case, the first line contains one integer n (1 ≤ n ≤ 100), the number of colonies.
Then n lines follow. Each line contains two integers xi, yi (0 ≤ xi, yi ≤ 1000), meaning the location of the i-th colony. There are no two colonies share the same location.
There are no more than 10 test cases with n > 10.
输出
For each test case, if there exists a plan of allocation meet David’s demand, print “YES” (without quotation) in the first line, and in the next line, print a string consisting of English letters “A” and “B”. The i-th character is “A” indicates that the i-th colony was allocated to C++ Empire, and “B” indicates the Java Republic.
If there are several possible solutions, you could print just one of them.
If there is no solution, print “NO”.
注意
This problem is special judged.
样例输入
2
2
0 0
0 1
4
0 0
0 1
1 0
1 1
样例输出
NO
YES
ABBA
比较巧妙,判断一下是否存在一个凸包, 边数大于等于4,或者为一个三角形里面包着一些点,这些情况都可以
Astronaut
The Bandulu Space Agency (BSA) has plans for the following three space missions:
• Mission A: Landing on Ganymede, the largest moon of Jupiter.
• Mission B: Landing on Callisto, the second largest moon of Jupiter.
• Mission C: Landing on Titan, the largest moon of Saturn.
Your task is to assign a crew for each mission. BSA has trained a number of excellent astronauts;
everyone of them can be assigned to any mission. However, if two astronauts hate each other, then it
is not wise to put them on the same mission. Furthermore, Mission A is clearly more prestigious than
Mission B; who would like to go to the second largest moon if there is also a mission to the largest one?
Therefore, the assignments have to be done in such a way that only young, inexperienced astronauts go
to Mission B, and only senior astronauts are assigned to Mission A. An astronaut is considered young
if their age is less than the average age of the astronauts and an astronaut is senior if their age is at
least the averageage. Every astronaut can be assigned to Mission C, regardless of their age (but you
must not assign two astronauts to the same mission if they hate each other).
Input
The input contains several blocks of test cases. Each case begins with a line containing two integers
1 ≤ n ≤ 100000 and 1 ≤ m ≤ 100000. The number n is the number of astronauts. The next n lines
specify the age of the n astronauts; each line contains a single integer number between 0 and 200. The
next m lines contains two integers each, separated by a space. A line containing i and j (1 ≤ i, j ≤ n)
means that the i-th astronaut and the j-th astronaut hate each other.
The input is terminated by a block with n = m = 0.
Output
For each test case, you have to output n lines, each containing a single letter. This letter is either ‘A’,
‘B’, or ‘C’. The i-th line describes which mission the i-th astronaut is assigned to. Astronauts that hate
each other should not be assigned to the same mission, only young astronauts should be assigned to
Mission B and only senior astronauts should be assigned to Mission A. If there is no such assignment,
then output the single line ‘No solution.’ (without quotes).
Sample Input
16 20
21
22
23
24
25
26
27
28
101
102
103
104
105
106
107
108
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
1 10
2 9
3 12
4 11
5 14
6 13
7 16
8 15
1 12
1 13
3 16
6 15
0 0
Sample Output
B
C
C
B
C
B
C
B
A
C
C
A
C
A
C
A
思考一下就知道是怎样的2-SAT了, 待查错
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
typedef long long ll;
ll tot, avl;
int n, m;
int age[maxn];
vector<int> g[maxn*2];
bool mark[maxn*2];
vector<int> path;
void init(){
memset(mark, 0, sizeof(mark));
for(int i = 0; i < n*2; i++) g[i].clear();
}
void add_edge(int x, int xval, int y, int yval){
x = x*2+xval;
y = y*2+yval;
g[x^1].push_back(y);
g[y^1].push_back(x);
}
bool dfs(int x){
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x] = true;
path.push_back(x);
for(int i = 0; i < g[x].size(); i++){
if(!dfs(g[x][i])) return false;
}
return true;
}
bool solve(){
for(int i = 2; i <= 2*n; i+=2){
if(!mark[i] && !mark[i+1]){
path.clear();
if(!dfs(i)){
for(int j = 0; j < path.size(); j++)
mark[path[j]] = false;
path.clear();
if(!dfs(i+1)) return false;
}
}
}
return true;
}
int main()
{
while(~scanf("%d%d", &n, &m) && n){
tot = 0;
for(int i = 1; i <= n; i++){
scanf("%d", &age[i]);
tot += age[i];
}
double tmp = (double)tot / (double)n;
if(tmp != floor(tmp)) avl = tmp+1; //向上取整
else avl = tmp;
init(); // 0 means c
for(int i = 0; i < m; i++){
int a, b;
scanf("%d%d", &a, &b);
if(a > b) swap(a, b);
add_edge(a, 1, b, 1);
if(!(age[a]<avl && avl<=age[b])) add_edge(a, 0, b, 0);
}
bool ans = solve();
if(ans == false) printf("No solution.\n"); else
for(int i = 2; i <= 2*n; i+=2)
if(mark[i]) printf("C\n"); else
age[i/2]<avl ? printf("B\n"):printf("A\n");
}
return 0;
}
关于“两端”的思考
去年在杭州女生赛上,碰到一道题就是分别
从头尾开始
记录一下各个连续位置的gcd的值, 然后遍历一遍就可以求得解
今天看到大白书图论中的一道题, 也是分别
从起点和终点开始
分别记录到各个点的最小路径,然后遍历一下可以缩短的点的边, 求得解
觉得这两者有点共性在里面嗯, 有点像对称的思考,很神奇呢