Codeforces Round 909 (Div. 3)(A~G)(启发式合并 , DSU ON TREE)

1899A - Game with Integers 

        题意:给定一个数 x , 两个人玩游戏,每人能够执行 +1 / -1操作,若操作完x是3的倍数则获胜,问先手的人能否获胜(若无限循环则先手的人输)。

        思路:假如一个数模3余1或者2,那么第一轮操作先手就能获胜,若余0则后手获胜。

        

// Problem: A. Game with Integers
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=1e05+10;
const LL mod=1e09+7;
typedef pairpl;
priority_queue, greater >t;
priority_queue q;
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
int a[N];
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
void solve() 
{
	cin >> n;
	if(n % 3 == 0){
		cout << "Second\n";
	}	
	else{
		cout << "First\n";
	}	
}            
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

1899B - 250 Thousand Tons of TNT 

        题意:给定一个整数n , 表示有 n 个集装箱,接下来给定一个数组a , 代表了第i个集装箱有a_{i}吨重。现要将n个集装箱恰好分成连续的k组。要求这当中所有k的取值下集装箱重量的最大值减去最小值的最大值。

        思路:直接暴力做 , 假设每一组有1、2、3、4、...n个,看满足题意的情况下最大值减最小值的值。时间复杂度O(NlogN).

        

// Problem: B. 250 Thousand Tons of TNT
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=2e05+10;
const LL mod=1e09+7;
typedef pairpl;
priority_queue, greater >t;
priority_queue q;
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
LL a[N] , sum[N];
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
void solve() 
{
	cin >> n;
	for(int i = 1 ; i <= n ; i ++){
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];	
	}
	LL ans = 0;
	for(int i = 1 ; i <= n ; i ++){
		LL maxx = 0 , minn = 1e18;
		if(n % i == 0){
			for(int j = 0 ; j < n ; j += i){
				maxx = max(sum[j + i] - sum[j] , maxx);
				minn = min(minn , sum[j + i] - sum[j]);
			}
			ans = max(ans , maxx - minn);
		}
	}
	cout << ans<>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

1899C - Yarik and Array 

        题意:给定一组序列,求连续奇数偶数非空子序列的和的最大值(相邻的奇偶性不能相同)。

        思路:同最大连续子序列差不多的做法,只不过要求前一项和当前的奇偶性不同,且至少要选一项。时间O(N)。

        

// Problem: C. Yarik and Array
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=2e05+10;
const LL mod=1e09+7;
typedef pairpl;
priority_queue, greater >t;
priority_queue q;
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
LL a[N];
int dp[N][2];
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
void solve() 
{
	int n;
	cin >> n;
	for(int i = 1 ; i <= n ; i ++)
		cin >> a[i];
	LL ans = -1e18;
	LL now = 0;
	for(int i = 1 ; i <= n ; i ++){
		if(now == 0){
			now += a[i];
		}
		else{
			if(abs(a[i]) % 2 == abs(a[i - 1]) % 2 ){
				now = a[i];
			}
			else{
				if(now > 0)
					now += a[i];
				else
					now = a[i];
			}
		}
		ans = max(ans , now);
	//	cout << now << endl;
		if(now < 0)
		 	now = 0;
	}
	cout << ans << endl;
}            
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

1899D - Yarik and Musical Notes 

        题意:给定一组数,要求数对(i , j)满足(2^{a_i})^{2^{a_{j}}} = (2^{a_j})^{2^{a_{i}}}(i < j)的数量。

        思路:构造辅助哈希nex(范围过大无法构造数组) , nex[i]标记了当前位置之后有多少个数字i。观察后发现 , 当 i = 1 ,j = 2/i = 2 , j = 1时能够满足,其余均需要i =j才能满足。因此对于1或者2而言,数对的数量为nex[1] +nex[2] ,其余的 i 构成的数对数量都是nex[i]。首先遍历一遍算出nex,然后再遍历一遍求答案,同时不断更新nex即可(unordered_map会被卡(哈希冲突),直接用map即可。

        

// Problem: D. Yarik and Musical Notes
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=3e05+10;
const LL mod=1e09+7;
typedef pairpl;
priority_queue, greater >t;
priority_queue q;
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
int a[N];
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
void solve() 
{
	cin >> n;
	mapmp;
	LL ans = 0;
	for(int i = 1 ; i <= n ; i ++){
		cin >> a[i];
		mp[a[i]]++;
	}
	for(int i = 1 ; i <= n ; i ++){
		mp[a[i]]--;
		if(a[i] == 1 || a[i] == 2){
			ans += mp[1] + mp[2];
		}
		else{
			ans += mp[a[i]];
		}
	}
	cout << ans << endl;
}            
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

1899E - Queue Sort 

        题意:给定一个数组,每次能够进行如下操作:

1、选择第一个数放入数组最后一个。

2、将最后一个数往前交换,直到到达第一位或者比前一个数大为止。

        问将数组变为递增的最少操作数,若无法则输出-1.

        思路:若最小的数为数组第一个,那么一轮操作之后他还是在第一位,这样便会无限循环。因此只需要满足最小的数之后的数全是递增的即可。

        

// Problem: E. Queue Sort
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/E
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=3e05+10;
const LL mod=1e09+7;
typedef pairpl;
priority_queue, greater >t;
priority_queue q;
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
LL a[N];
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
void solve() 
{
	cin >> n;
	LL minn = 1e18;
	for(int i = 1 ;i <= n ; i ++){
		cin >> a[i];
		minn = min(a[i] , minn);
	}
/*	for(int i = 1 ; i < n - 1; i++){
		if(a[i] < a[i + 1]){
			break;
		}
		if(i == n - 2){
			cout << 0 << endl;
			return;
		}
	}*/
	for(int i = 1 ; i <= n ; i ++){
		if(a[i] == minn){
			for(int j = i + 1; j <= n ; j ++){
				if(a[j] < a[j - 1]){
					cout << -1 << endl;
					return;
				}
			}
			cout << i - 1 << endl;
			return;
		}
	}
}            
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

1899F - Alex's whims 

        题意:现有n个顶点,起初你可以任意构造将其形成一棵树。接下来有 d 个数,表示共有d轮。每一轮你可以选择一个已有的边将其删除,然后再连一条边形成一颗新的数。要求每一轮能够满足至少有两个叶子结点的距离恰好为d_{i}

        思路:首先可以将n个顶点连城一条链。然后每一轮当中,我们固定第一个点和最后一个点的距离为我们要的d_{i}。每次我们可以将与点1相连的边删去,然后新增一条边,使得刚好1到n的距离为d_{i}。由于结点2 ~ n都是一条链,所以2 ~ n - 1 任意一个距离都是可以构造出来的。如此便一定能够满足题意。所以我们只需要记录当前1结点和哪个结点相连,然后需要连到哪个结点即可。

        

// Problem: F. Alex's whims
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/F
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=1e05+10;
const LL mod=1e09+7;
typedef pairpl;
priority_queue, greater >t;
priority_queue q;
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
int a[N];
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
void solve() 
{
	cin >> n >> m;
	for(int i = 2 ; i <= n ; i ++){
		cout << i - 1 << " " << i << endl;
	}
	int pre = 2;
	for(int i = 0 ; i < m ; i ++){
		int x;
		cin >> x;
		int len = (n - pre) + 1;
		if(len == x){
			cout <<"-1 -1 -1\n";
			continue;
		}
		else{
			int to = (n - x + 1);
			cout << 1 << " " << pre << " " << to << endl;
			pre = to; 
		}
	}
}            
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

1899G - Unusual 进入tainment 

        思路:给定一棵树和一个排列p。现有q组询问,每组询问包含了三个数l , r , x。问点x的子树的结点是否在[p_{l} , p_{r}]中出现。

        思路:子树问题,首先想到了用dfs序来解决。对于一颗子树而言,我们可以用set来维护其所有结点在排列p中的位置。然后对于一个询问而言,只需要找到set中大于等于 l 的第一个位置即可,然后判断该位置是否小于等于r。若小于等于r则代表了其子树的结点包含在了[p_{l} , p_{r}]中。共有n个结点,所以我们需要创立n个set,来记录他们的结点在p中的位置。在子树向上合并的过程中,我们可以用启发式合并来实现优化:每一轮虽然是将子树的set合并到父节点的set上,但是可以用swap来交换两个set,确保每次都将小集合合并到大集合上面(swap是O(1)的)。如此总的时间复杂度是O(nlogn + qlogn)的。

        

// Problem: G. Unusual Entertainment
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/G
// Memory Limit: 256 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=2e05+10;
const LL mod=1e09+7;
typedef pairpl;
priority_queue, greater >t;
priority_queue q;
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}
LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
int a[N] , pos[N];
vectortr[N + 5];
vector >que[N];
vector> num(N);
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0 , tr[i].clear() , que[i].clear();
		num[i].clear();
	}
}
LL ans[N];
void merge(set &a , set&b){
	if(a.size() < b.size()){
		swap(a , b);
	}
	for(auto it : b){
		a.insert(it);
	}
	b.clear();
}
void dfs(int cur , int f){
	num[cur].insert(pos[cur]);
	for(auto it : tr[cur]){
		if(it == f)
			continue;
		dfs(it , cur);
		merge(num[cur] , num[it]);
	}
	for(auto it : que[cur]){
		auto p = num[cur].lower_bound(it[0]);
		ans[it[2]] = p != num[cur].end() && *p <= it[1];
	}
};
void solve() 
{

	cin >> n >> m;
	for(int i = 1 ; i < n ; i++){
		int x , y;
		cin >> x >> y;
		tr[x].pb(y);
		tr[y].pb(x);
	}
	for(int i = 1 ; i <= n ; i ++){
		cin >> a[i];
		pos[a[i]] = i;
	}
	for(int i = 0 ; i < m ; i ++){
		int l , r , x;
		cin >> l >> r >> x;
		que[x].pb({l , r , i});
	}
	dfs(1 , 0);
	for(int i = 0 ; i < m ; i ++){
		if(ans[i]){
			cout <<"YES\n";
		}
		else{
			cout <<"NO\n";
		}
	}
	init(n);
	cout << endl;
	num.clear();
}            
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

更新下新做法:用一个辅助数组vis,其中vis[i] = 1则表示p[i]这个结点存在在子树当中,那么对于一个询问而言,只需要看([vis[l] , vis[r])当中是否有结点存在,即sum(vis[l] , vis[r]) > 0即可。 那么在子树合并的过程之中,可以将轻儿子合并到重儿子上面,然后再将轻儿子的信息删除(DSU on tree)。整个过程的时间复杂度是O(NlogN)的。而要维护区间和,可以用树状数组来实现,每次的复杂度是O(logN)的。而对于每个询问而言,区间和的复杂度也是O(logN)的,因此总体复杂度为O(NlogNlogN + qlogN)。

        

// Problem: G. Unusual Entertainment
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/G
// Memory Limit: 256 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=1e05+10;
const LL mod=1e09+7;
typedef pairpl;
priority_queue, greater >t;
priority_queue q;
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}
LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
int a[N] , pos[N];
vectortr[N + 5];
vector >que[N];
struct BIT{//Binary indexed Tree(树状数组)
	int n;
	vector tr;
	BIT(int n) : n(n) , tr(n + 1 , 0){
	}
	int lowbit(int x){
		return x & -x;
	}
	void modify(int x , int modify_number){
		for(int i = x ; i <= n ; i += lowbit(i)){
			tr[i] += modify_number;
		}
	}
	void modify(int l , int r , int modify_number){
		modify(l , modify_number);
		modify(r + 1 , -modify_number);
	}
	int query(int x){
		int res = 0;
		for(int i = x ; i ;  i -= lowbit(i))
			res += tr[i];
		return res;
	}
	int query(int x , int y){
		if(x > y)
		swap(x , y);
		return query(y) - query(x);
	}
};
BIT bit(N);
int res[N];
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0 , tr[i].clear() , que[i].clear() , bit.tr[i] = 0 , res[i] = 0;
	}
}
LL l[N] , r[N] , id[N] , sz[N] , hs[N] , tot = 0 , ans[N];
setnum;
void dfs(int cur , int f){
	l[cur] = ++ tot;
	id[tot] = cur;
	sz[cur] = 1;
	hs[cur] = -1;
	for(auto it : tr[cur]){
		if(it != f){
			dfs(it , cur);
			sz[cur] += sz[it];
			if(hs[cur] == -1 || sz[it] > sz[hs[cur]]){
				hs[cur] = it;
			}
		}		
	} 
	r[cur] = tot;
}
void dfs2(int cur , int f , int keep){
	vectorres(que[cur].size());
	int i = 0;
/*	for(auto it : que[cur]){
		res[i++] = bit.query(it[1] , it[0] - 1);
	}*/
	for(auto it : tr[cur]){
		if(it != f && it != hs[cur]){
			dfs2(it , cur , 0);
		}
	}
	if(hs[cur] != -1){
		dfs2(hs[cur] , cur , 1);
	}
	auto add = [&](int x){
		bit.modify(pos[x] , 1);
	};
	auto del = [&](int x){
		bit.modify(pos[x] , -1);
	};
	for(auto it : tr[cur]){
		if(it != f && it != hs[cur]){
			for(int x = l[it] ; x <= r[it] ; x ++){
					add(id[x]);
			}
		}
	}
	add(cur);
	i = 0;
	for(auto it : que[cur]){
		ans[it[2]] = bit.query(it[1] , it[0] - 1) > 0 ? 1 : 0;
	}
	if(!keep){
		for(int x = l[cur] ; x <= r[cur] ; x ++){
			del(id[x]);
		}
	}
}
void solve() 
{
	cin >> n >> m;
	for(int i = 1 ; i < n ; i++){
		int x , y;
		cin >> x >> y;
		tr[x].pb(y);
		tr[y].pb(x);
	}
	for(int i = 1 ; i <= n ; i ++){
		cin >> a[i];
		pos[a[i]] = i;
	}
	for(int i = 0 ; i < m ; i ++){
		int l , r , x;
		cin >> l >> r >> x;
		que[x].pb({l , r , i});
	}
	dfs(1 , 0);
	dfs2(1 , 0 , 0);
	for(int i = 0 ; i < m ; i ++){
		if(ans[i]){
			cout <<"YES\n";
		}
		else{
			cout <<"NO\n";
		}
	}
	init(n);
	cout << endl;
}            
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

        

你可能感兴趣的:(算法,数据结构,c++)