Processing math: 100%

2019牛客国庆集训派对day3

E. Grid

大意: 给定nm个点的图, 初始无边, q个操作, (1,a,b)表示第a列到第b列全连起来, (2,a,b)表示把第a行到第b行全连起来, 每次操作后输出连通块个数.

 

直接用set暴力模拟即可.

还有一种线段树做法, 设一共a行连通, b列连通, 可以发现答案是nma(m1)b(n1)+max(0,(a1)(b1)), 可以用线段树维护ab即可

复制代码
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



int n,m,q;
struct _ {
    int l,r;
    bool operator < (const _ &rhs) const {
        return l<rhs.l;
    }
};
set<_> A,B;
//A为行
//B为列

int main() {
    while (~scanf("%d%d%d", &n, &m, &q)) {
        A.clear(),B.clear();
        ll ans = (ll)n*m, suma = 0, sumb = 0;
        while (q--) {
            int op,l,r;
            scanf("%d%d%d",&op,&l,&r);
            if (op==1) {
                auto p = A.lower_bound({l,0});
                if (p!=A.begin()&&(--p)->r>=l) l = p->l;
                while (1) {
                    p = A.lower_bound({l,0});
                    if (p==A.end()||p->l>r) break;
                    r = max(r, p->r);
                    ans += (p->r-p->l+1ll)*(m-(B.empty()?1:sumb));
                    suma -= p->r-p->l+1;
                    A.erase(p);
                    if (B.size()&&A.empty()) ans += sumb-1;
                }
                suma += r-l+1;
                ans -= (r-l+1ll)*(m-(B.empty()?1:sumb));
                if (B.size()&&A.empty()) ans -= sumb-1;
                A.insert({l,r});
            }
            else {
                auto p = B.lower_bound({l,0});
                if (p!=B.begin()&&(--p)->r>=l) l = p->l;
                while (1) {
                    p = B.lower_bound({l,0});
                    if (p==B.end()||p->l>r) break;
                    r = max(r, p->r);
                    ans += (p->r-p->l+1ll)*(n-(A.empty()?1:suma));
                    sumb -= p->r-p->l+1;
                    B.erase(p);
                    if (A.size()&&B.empty()) ans += suma-1;
                }
                sumb += r-l+1;
                ans -= (r-l+1ll)*(n-(A.empty()?1:suma));
                if (A.size()&&B.empty()) ans -= suma-1;
                B.insert({l,r});
            }
            printf("%lld\n",ans);
        }
    }
}
View Code
复制代码

 

 

G. 排列

大意: 给定m个二元组(ai,bi), 给定序列p, 求将p重排, 使得mi=1|paipbi|最小, 输出最小值.

 

从小到大添入每一个p, 这样就能去掉绝对值号, 跑一个状压dp就行了.

复制代码
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 1e6+50;
int n,m,p[N],g[N],cnt[1<<20];
ll dp[1<<20];
void chkmin(ll &a, ll b) {a>b?a=b:0;}
int main() {
    REP(i,1,(1<<20)-1) cnt[i]=cnt[i>>1]+(i&1);
    while (~scanf("%d%d", &n, &m)) {
        REP(i,0,n-1) scanf("%d",p+i),g[i]=0;
        REP(i,1,m) {
            int a,b;
            scanf("%d%d",&a,&b);
            --a,--b;
            g[a] ^= 1<<b;
            g[b] ^= 1<<a;
        }
        sort(p,p+n);
        dp[0] = 0;
        int mx = (1<<n)-1;
        REP(i,1,mx) dp[i] = 1e18;
        REP(i,0,mx-1) {
            REP(j,0,n-1) if (i>>j&1^1) {
                int x = cnt[i&g[j]], y = cnt[~i&mx&g[j]];
                chkmin(dp[i^1<<j],dp[i]+(ll)(x-y)*p[cnt[i]]);
            }
        }
        printf("%lld\n", dp[mx]);
    }
}
View Code
复制代码

 

posted @   uid001  阅读(194)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
阅读排行:
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 2025成都.NET开发者Connect圆满结束
点击右上角即可分享
微信分享提示