Algo-Trian

This commit is contained in:
e2hang
2025-09-21 12:21:50 +08:00
parent 24522486f1
commit 3bde00039c
25 changed files with 842 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
#include <iostream>
#include <set>
#include <vector>
using namespace std;
set<vector<int>> result;
bool check(const vector<int>& path, int pos) {
int row = path.size(); // <20><>ǰҪ<C7B0>ŵ<EFBFBD><C5B5><EFBFBD>
for (int i = 0; i < row; i++) {
// ͬ<><CDAC> <20><> <20>ڶԽ<DAB6><D4BD><EFBFBD><EFBFBD><EFBFBD>
if (path[i] == pos || abs(path[i] - pos) == row - i) {
return false;
}
}
return true;
}
void dfs(vector<int>& path_, int n){
if(path_.size() == n){
result.emplace(path_);
return;
}
int js = 0;
for(int i = 0; i < n; i++)
{
if(check(path_, i)){
js++;
path_.push_back(i);
dfs(path_, n);
path_.pop_back();
}
}
}
int main(){
int n;
cin >> n;
vector<int> path;
dfs(path, n);
auto it = result.begin();
for(int i = 0; i < 3; i++){
for(auto x : *it){
cout << x + 1 << " ";
}
cout << endl;
it++;
}
cout << result.size() << endl;
return 0;
}

View File

@@ -0,0 +1,35 @@
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string add(string a, string b) {
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int ms = max(a.size(), b.size());
string tmp;
tmp.resize(ms + 1, '0');
int carry = 0;
for (int i = 0; i < ms; i++) {
int x = (i < a.size() ? a[i] - '0' : 0);
int y = (i < b.size() ? b[i] - '0' : 0);
int sum = x + y + carry;
tmp[i] = sum % 10 + '0';
carry = sum / 10;
}
if (carry) tmp[ms] = carry + '0';
else tmp.pop_back();
reverse(tmp.begin(), tmp.end());
return tmp;
}
int main() {
string a, b;
cin >> a >> b;
cout << add(a, b) << endl;
return 0;
}

View File

@@ -0,0 +1,28 @@
#include <iostream>
#include <vector>
using namespace std;
int checkbin(long long s){
int js = 0;
while(s){
js += (s & 1);
s = s>> 1;
}
return js;
}
int main(){
int n;
cin >> n;
vector<long long> s(n);
for(int i = 0; i < n; i++){
int a, b;
cin >> a >> b;
s[i] = ( 1 << (a - 1) ) + (b - 1);
}
for(int i = 0; i < n; i++){
if(checkbin(s[i]) % 2 == 0) cout << "BLACK" << endl;
else cout << "RED" << endl;
}
return 0;
}

View File

@@ -0,0 +1,35 @@
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
vector<vector<int>> result;
void dfs(vector<int>& path, vector<bool>& used, int n){
int js = 0;
for(int i = 0; i < n; i++){
if(used[i] == false){
js++;
used[i] = true;
path.push_back(i);
dfs(path, used, n);
path.pop_back();
used[i] = false;
}
}
if(js == 0){
result.push_back(path);
return;
}
}
int main(){
int n;
cin >> n;
vector<int> path;
vector<bool> used(n, false);
dfs(path, used, n);
for(auto x : result){
for(auto y: x){
cout << setw(5) << y + 1;
}
cout << endl;
}
}

View File

@@ -0,0 +1,22 @@
#include <iostream>
#include <utility>
#include <vector>
#include <climits>
#include <algorithm>
using namespace std;
const int INF = INT_MAX >> 1;
int main() {
int n;
cin >> n;
vector<int> a(n);
vector<int> dp(n + 1, -INF);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
dp[0] = 0;
for (int i = 1; i < n + 1; i++) {
dp[i] = max(dp[i - 1] + a[i - 1], a[i - 1]);
}
cout << *max_element(dp.begin() + 1, dp.end()) << endl;
return 0;
}

View File

@@ -0,0 +1,31 @@
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
int n = s.size();
vector<int> dp(n, 1);
//dp[i]: <20><>i<EFBFBD><69>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><D3B4><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
//Ԥ<><D4A4><EFBFBD><EFBFBD>
for(int i = 1; i < n; i++){
if(s[i] == s[i - 1] + 1 || s[i] == s[i - 1]){
dp[i] = dp[i - 1] + 1;
}
}
//<2F><><EFBFBD><EFBFBD>Ҫ<EFBFBD>ݳ⣬<DDB3><E2A3AC>ô<EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD>
int sum = 0;
for(int i = 0; i < n; i++){
//<2F>Լ<EFBFBD><D4BC><EFBFBD>
sum += (dp[i] - 1) * dp[i] / 2;
//ǰ<><C7B0>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
sum += dp[i] * (i - dp[i]);
}
cout << sum << endl;
return 0;
}

View File

@@ -0,0 +1,36 @@
#include <iostream>
#include <tuple>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
//tuple(t, x, y)
int dist(const tuple<int, int, int>& i, const tuple<int, int, int>& j){
return ( abs(get<1>(i) - get<1>(j)) + abs(get<2>(i) - get<2>(j)) );
}
int time(const tuple<int, int, int>& i, const tuple<int, int, int>& j){
return abs( get<0>(i) - get<0>(j) );
}
int main(){
int n, m;
cin >> n >> m;
//n * n, m <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
vector<tuple<int, int, int>> ham(m);
vector<int> dp(m, 1);
for(int i = 0; i < m; i++){
int t, x, y;
cin >> t >> x >> y;
ham[i] = make_tuple(t, x, y);
}
//dp[m] = max (<28><><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD>(t2 - t1 > dist)dp[x] + 1)
for(int i = 1; i < m; i++){
for(int j = 0; j < i; j++){
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5>͸<EFBFBD><CDB8><EFBFBD>
if(dist(ham[i], ham[j]) <= time(ham[i], ham[j])){
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
cout << *max_element(dp.begin(), dp.end()) << endl;
return 0;
}

View File

@@ -0,0 +1,42 @@
#include <iostream>
#include <vector>
using namespace std;
const int NEG_INF = -1e9;
int main() {
int v, e;
cin >> v >> e;
vector<vector<int>> dp(v, vector<int>(v, NEG_INF));
for (int i = 0; i < v; i++) dp[i][i] = 0; // <20>Ի<EFBFBD>=0
for (int i = 0; i < e; i++) {
int a, b, w;
cin >> a >> b >> w;
dp[a - 1][b - 1] = max(dp[a - 1][b - 1], w); // <20><><EFBFBD>ܶ<EFBFBD><DCB6><EFBFBD><EFBFBD>ߣ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
}
for (int k = 0; k < v; k++) {
for (int i = 0; i < v; i++) {
if (dp[i][k] <= NEG_INF / 2) continue;
for (int j = 0; j < v; j++) {
if (dp[k][j] <= NEG_INF / 2) continue;
dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j]);
}
}
}
int mx = -1;
for (int i = 0; i < v; i++) {
for (int j = 0; j < v; j++) {
if (dp[i][j] > NEG_INF / 2) {
mx = max(mx, dp[i][j]);
}
}
}
mx == -1 ? cout << mx << endl;
return 0;
}

View File

@@ -0,0 +1,61 @@
#include <iostream>
#include <vector>
using namespace std;
int main(){
//dpֻ<70><D6BB>O(n3)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bfs<66><73><EFBFBD><EFBFBD>dfs
int v, e;
cin >> v >> e;
//vector<vector<int>> adj(v, vector<int>());
vector<vector<bool>> dp(v, vector<bool>(v, false));
for(int i = 0; i < e; i++){
int a, b;
cin >> a >> b;
//adj[a - 1].push_back(b - 1);
//adj[b - 1].push_back(a - 1);
dp[a - 1][b - 1] = true;
}
//<2F><><EFBFBD><EFBFBD>dp[i][j] : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>k<EFBFBD>ķ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
for (int i = 0; i < v; i++) dp[i][i] = true; // <20>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>ͨ
for(int k = 0; k < v; k++){
for(int i = 0; i < v; i++){
for(int j = 0; j < v; j++){
if(dp[i][j]) continue;
dp[i][j] = dp[i][j] || (dp[i][k] & dp[k][j]);
}
}
}
//bfs
for(int i = 0; i < v; i++) {
vector<bool> visited(v, false);
queue<int> q;
q.push(i);
visited[i] = true;
int mx = i;
while (!q.empty()) {
int curr = q.front();
q.pop();
for (int next : adj[curr]) {
if (!visited[next]) {
visited[next] = true;
q.push(next);
mx = max(mx, next);
}
}
}
cout << mx + 1 << " ";
}
for(int i = 0; i < v; i++){
int mx = -1;
for(int j = 0; j < v; j++){
if(dp[i][j] && mx < j) mx = j;
}
cout << mx + 1 << " ";
}
cout << endl;
return 0;
}

View File

@@ -0,0 +1 @@
#include <iostream>

View File

@@ -0,0 +1,48 @@
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <climits>
using namespace std;
const int INF = INT_MAX >> 1;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
bool check(int bx, int by, int x, int y) {
return (x >= 0 && x < bx && y >= 0 && y < by);
}
int main() {
int n, m, x, y;
cin >> n >> m >> x >> y;
vector<vector<int>> mp(n, vector<int>(m, INF));
x--; y--; // ת<><D7AA><EFBFBD><EFBFBD> 0-based
queue<tuple<int, int, int>> q;
q.push({x, y, 0});
mp[x][y] = 0;
while (!q.empty()) {
auto [qx, qy, path] = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
int nx = qx + dx[i];
int ny = qy + dy[i];
if (check(n, m, nx, ny) && mp[nx][ny] == INF) {
mp[nx][ny] = path + 1;
q.push({nx, ny, path + 1});
}
}
}
for (auto &row : mp) {
for (auto v : row) {
cout << (v == INF ? -1 : v) << " ";
}
cout << "\n";
}
return 0;
}

View File

@@ -0,0 +1,48 @@
#include <iostream>
#include <tuple>
#include <vector>
#include <climits>
using namespace std;
const int INF = INT_MAX >> 1;
int main(){
int n, edge_num, start;
cin >> n >> edge_num >> start;
//<2F><><EFBFBD><EFBFBD>Ҫadj, <20><>Ҫdp <20><> edges
vector<tuple<int, int , int>> edges(edge_num);// p1, p2, w
vector<int> dp(n, INF);
dp[start] = 0;
//<2F><><EFBFBD><EFBFBD>
for(int i = 0; i < edge_num; i++){
int a, b, w;
cin >> a >> b >> w;
edges[i] = {a, b, w};
}
//<2F>Ƚ<EFBFBD><C8BD><EFBFBD>n - 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(int i = 0; i < n - 1; i++){
for(auto [p1, p2, w] : edges){
if(dp[p2] > dp[p1] + w){
dp[p2] = dp[p1] + w;
}
}
}
//<2F>Ƿ<EFBFBD><C7B7>и<EFBFBD><D0B8><EFBFBD>
bool negcc = false;
for(auto [p1, p2, w] : edges){
if(dp[p2] > dp[p1] + w){
negcc = true;
break;
}
}
if(negcc) cout << "Negative Circle Detected" << endl;
else{
for(auto x : dp){
cout << x << " ";
}
}
return 0;
}

View File

@@ -0,0 +1,39 @@
#include <iostream>
#include <vector>
using namespace std;
int js = 0;
vector<int> arr;
bool checkprime(int x) {
if (x < 2) return false;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) return false;
}
return true;
}
void dfs(int idx, int k, int sum) {
if (k == 0) { // <20>Ѿ<EFBFBD>ѡ<EFBFBD><D1A1>k<EFBFBD><6B>
if (checkprime(sum)) js++;
return;
}
if (idx >= arr.size()) return;
// ѡ<><D1A1><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>
dfs(idx + 1, k - 1, sum + arr[idx]);
// <20><>ѡ<EFBFBD><D1A1>ǰ<EFBFBD><C7B0>
dfs(idx + 1, k, sum);
}
int main() {
int n, k;
cin >> n >> k;
arr.resize(n);
for (int i = 0; i < n; i++) cin >> arr[i];
dfs(0, k, 0);
cout << js << endl;
return 0;
}

View File

@@ -0,0 +1,46 @@
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = INT_MAX >> 1;
int main(){
int n, edge_num, start;
cin >> n >> edge_num >> start;
vector<vector<pair<int, int>>> adj(n);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> dp;
vector<bool> used(n, false);
vector<int> dist(n, INF);
for(int i = 0; i < edge_num; i++){
int a, b, w;
cin >> a >> b >> w;
adj[a].push_back({b, w});
adj[b].push_back({a, w});
}
dp.push({0, start});
dist[start] = 0;
while(!dp.empty()){
//ȷ<><C8B7>ѡ
auto[dists, v] = dp.top();
dp.pop();
if(used[v]) continue;
used[v] = true;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD>
for(auto [u, d] : adj[v]){
if(!used[u] && dist[v] + d < dist[u]){//<2F><>С
dist[u] = dist[v] + d;
dp.push({dist[u], u});
}
}
}
for(auto x : dist){
if(x != INF) cout << x << " ";
else cout << "INF" << " ";
}
cout << endl;
return 0;
}

View File

@@ -0,0 +1,37 @@
#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
const int INF = 100001;//<2F><><EFBFBD><EFBFBD>
int main(){
//dp<64>ķֽ׶<D6BD><D7B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD>ӽṹ<D3BD><E1B9B9><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>)
int n, edge_num, start, end;
cin >> n >> edge_num >> start >> end;
//<2F><><EFBFBD>ش洢
//vector<tuple<int, int, int>> edge(edge_num);
vector<vector<int>> dp(n, vector<int>(n, INF));
for(int i = 0; i < edge_num; i++){
int a, b, w;
cin >> a >> b >> w;
//edge[i] = {a, b, w};
dp[a][b] = w;
}
//dp, һ<><D2BB><EFBFBD><EFBFBD><6B><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(int k = 0; k < n; k++){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
}
}
for(int k = 0; k < n; k++){
if(dp[k][k] < 0){
cout << "Negative Cycle Detected!" << endl;
return 0;
}
}
cout << dp[start][end] << endl;
return 0;
}

View File

@@ -0,0 +1,72 @@
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> parent;
struct edge{
int from;
int to;
int weight;
edge() : from(-1), to(-1), weight(-1){}
edge(int from_, int to_, int weight_): from(from_), to(to_), weight(weight_){}
};
struct cmp{
bool operator()(const edge& a, const edge& b){
return a.weight < b.weight;
}
};
int find(int x){
if(parent[x] != x) parent[x] = find(parent[x]);
return parent[x];
}
void merge(int x, int y){
int rootX = find(x);
int rootY = find(y);
if(rootX != rootY) parent[rootY] = rootX;
return;
}
int main(){
//<2F><>adj<64>ķ<EFBFBD>ʽ
int n;
cin >> n;
//vector<vector<pair<int,int>>> adj(n, vector<pair<int,int>>());//<2F><><EFBFBD><EFBFBD>û<EFBFBD><C3BB>
parent.resize(n);
for(int i = 0; i < n; i++){
parent[i] = i;
}
vector<pair<int, int>> result(n, {-1, -1});
int path = 0;
int edge_num;
cin >> edge_num;
vector<edge> arr(edge_num);
for(int i = 0; i < edge_num; i++){
int a, b, w;
cin >> a >> b >> w;
arr[i] = edge(a, b, w);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
if(edge_num < n - 1) {
cout << "Not Valid Full Graph" << endl;
return ;
}
sort(arr.begin(), arr.end(), cmp());
for(auto x : arr){
if(find(x.from) == find(x.to)){
continue;
}
merge(x.from, x.to);
result.push_back({x.from, x.to});
path += x.weight;
}
cout << path << endl;
for(auto x : result){
if(x.first != -1 && x.second != -1) cout << "{" << x.first << ", " << x.second << "}" << " ";
}
cout << endl;
return 0;
}

View File

@@ -0,0 +1,9 @@
#include <iostream>
#include <vector>
using namespace std;
int main(){
int v, e, start;
cin >> v >> e >> start;
vector<int> map(v, 0);
return 0;
}

View File

@@ -0,0 +1,51 @@
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
vector<vector<int>> adj;
vector<bool> used;
//
void dfs(int idx){
if(used[idx]) return;
used[idx] = true; // <20><><EFBFBD><EFBFBD><EFBFBD>ѷ<EFBFBD><D1B7><EFBFBD>
cout << idx + 1 << " ";
for(auto x : adj[idx]){
dfs(x);
}
}
int main(){
int v, e;
cin >> v >> e;
adj.resize(v);
used.resize(v, false);
for(int i = 0; i < e; i++){
int a, b;
cin >> a >> b;
adj[a - 1].push_back(b - 1);
}
for(int i = 0; i < v; i++){
sort(adj[i].begin(), adj[i].end());
}
dfs(0);
fill(used.begin(), used.end(), false);
cout << endl;
//bfs
deque<int> q;
q.push_back(0);
used[0] = true;
while(!q.empty()){
int top = q.front(); q.pop_front(); // ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> front()
cout << top + 1 << " ";
for(auto x : adj[top]){
if(!used[x]){
used[x] = true; // <20><><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>
q.push_back(x);
}
}
}
return 0;
}

View File

@@ -0,0 +1,26 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> a(n);
int sum = 0;
for(int i = 0; i < n; i++){
cin >> a[i];
sum += a[i];
}
int avg = sum / n;
int js = 0;
for(int i = 0; i < n - 1; i++){
if(a[i] != avg){
int tmp = a[i] - avg;
a[i] = avg;
a[i + 1] += tmp;
js++;
}
}
cout << js << endl;
return 0;
}

View File

@@ -0,0 +1,26 @@
#include <iostream>
#include <queue>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
priority_queue<int, vector<int>, greater<int>> q;
for(int i = 0; i < n; i++){
int a;
cin >> a;
q.emplace(a);
}
int value = 0;
int min1 = 0, min2 = 0;
while(q.size() > 1){
min1 = q.top(); q.pop();
min2 = q.top(); q.pop();
int tmp = min1 + min2;
value += tmp;
q.emplace(tmp);
}
cout << value<< endl;
return 0;
}

View File

@@ -0,0 +1,39 @@
#include <iostream>
#include <string>
#include <deque>
using namespace std;
int main() {
string s;
int k;
cin >> s >> k;
deque<char> q;
for(char curr : s) {
while(!q.empty() && k > 0 && q.back() > curr){
q.pop_back();
k--;
}
q.push_back(curr);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3> k <20><><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD>ɾ<EFBFBD><C9BE>ջβ
while(k > 0 && !q.empty()) {
q.pop_back();
k--;
}
// ƴ<>ӽ<EFBFBD><D3BD><EFBFBD>
string res;
for(char c : q) res += c;
// ȥ<><C8A5>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
int i = 0;
while(i < res.size() && res[i] == '0') i++;
res = res.substr(i);
if(res.empty()) res = "0";
cout << res << endl;
return 0;
}

View File

@@ -0,0 +1,25 @@
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> sum(n + 1);
sum[0] = 0;
for (int i = 1; i < n + 1; i++) {
int tmp;
cin >> tmp;
sum[i] = sum[i - 1] + tmp;
}
int cnum;
cin >> cnum;
vector<pair<int, int>> check(cnum);
for (int i = 0; i < cnum; i++) {
cin >> check[i].first >> check[i].second;
}
for (int i = 0; i < cnum; i++) {
cout << sum[check[i].second] - sum[check[i].first - 1] << endl;
}
return 0;
}

View File

@@ -0,0 +1,34 @@
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
struct cmp{
bool operator()(const pair<int, int>& a, const pair<int, int>& b){
return a.first < b.first;
}
};
int main(){
int n;
cin >> n;
vector<pair<int, int>> dp(n);
//<2F><><EFBFBD><EFBFBD>
for(int i = 0; i < n; i++){
cin >> dp[i].first >> dp[i].second;
}
//<2F><><EFBFBD><EFBFBD>dp.first<73><74><EFBFBD><EFBFBD>
sort(dp.begin(), dp.end(), cmp());
vector<pair<int, int>> merge;
for(auto [l, r] : dp){
if (merged.empty() || merged.back().second < l)
merged.push_back({l,r});
else
merged.back().second = max(merged.back().second, r);
}
int sum = 0;
for(auto [l, r] : merge){
sum += r - l;
}
cout << sum << endl;
return 0;
}