首页 > 其他分享 >HDOJ 5296 Annoying problem

HDOJ 5296 Annoying problem

时间:2023-07-05 19:33:06浏览次数:41  
标签:include anc 5296 LL define int problem Annoying dis


根据每次的加点删点求对答案的贡献。。。


#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 100005
#define maxm 200005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL; 
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

struct Edge
{
    int v, w;
    Edge *next;
}E[maxm], *H[maxn], *edges;

const int M = 20;
set<pii> s;
set<pii>::iterator t1, t2;
int anc[maxn][20];
bool used[maxn];
int dep[maxn];
int in[maxn];
int dis[maxn];
int n, m, dfs_clock;

void init()
{
    dfs_clock = 0;
    edges = E;
    memset(H, 0, sizeof H);
    memset(used, 0, sizeof used);
}

void addedges(int u, int v, int w)
{
    edges->v = v;
    edges->w = w;
    edges->next = H[u];
    H[u] = edges++;
}

void dfs(int u, int fa)
{
    anc[u][0] = fa;
    in[u] = ++dfs_clock;
    for(Edge *e = H[u]; e; e = e->next) if(e->v != fa) {
        int v = e->v, w = e->w;
        dis[v] = dis[u] + w;
        dep[v] = dep[u] + 1;
        dfs(v, u);
    }
}

int to(int u, int d)
{
    for(int i = M - 1; i >= 0; i--) if(dep[anc[u][i]] >= d) u = anc[u][i];
    return u;
}

int lca(int u, int v)
{
    if(dep[u] < dep[v]) swap(u, v);
    u = to(u, dep[v]);
    for(int i = M - 1; i >= 0; i--) if(anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i];
    return u == v ? u : anc[u][0];
}

void work()
{
    int u, v, w;
    scanf("%d%d", &n, &m);
    for(int i = 1; i < n; i++) {
        scanf("%d%d%d", &u, &v, &w);
        addedges(u, v, w);
        addedges(v, u, w);
    }
    dfs(1, 1);
    for(int j = 1; j < M; j++)
        for(int i = 1; i <= n; i++)
            anc[i][j] = anc[anc[i][j-1]][j-1];
    
    int ans = 0, op;
    s.clear();
    for(int i = 1; i <= m; i++) {
        scanf("%d%d", &op, &u);
        if(op == 1) {
            if(used[u]) goto print;
            else used[u] = 1;
            if(s.empty()) {
                s.insert(mp(in[u], u));
                goto print;
            }
            t1 = s.lower_bound(mp(in[u], u));
            t2 = t1, t2--;
            int a, b;
            if(t1 == s.end() || t1 == s.begin()) t2 = s.begin(), t1 = --s.end();
            a = t2->second, b = t1->second;
            ans += dis[u] + dis[lca(a, b)] - dis[lca(u, b)] - dis[lca(u, a)];
            s.insert(mp(in[u], u));    
        }
        else {
            if(!used[u]) goto print;
            else used[u] = 0;
            s.erase(mp(in[u], u));
            
            t1 = s.lower_bound(mp(in[u], u));
            t2 = t1, t2--;
            int a, b;
            if(t1 == s.end() || t1 == s.begin()) t2 = s.begin(), t1 = --s.end();
            a = t2->second, b = t1->second;
            ans -= dis[u] + dis[lca(a, b)] - dis[lca(u, a)] - dis[lca(u, b)];
        }
print:
        printf("%d\n", ans);
    }
}

int main()
{
    int _;
    scanf("%d", &_);
    for(int i = 1; i <= _; i++) {
        printf("Case #%d:\n", i);
        init();
        work();
    }
    
    return 0;
}




标签:include,anc,5296,LL,define,int,problem,Annoying,dis
From: https://blog.51cto.com/u_8692734/6634290

相关文章

  • Could not fetch URL https://pypi.org/simple/keras-bert/: There was a problem co
    pip下载包的时候报错CouldnotfetchURLhttps://pypi.org/simple/keras-bert/:Therewasaproblemconfirmingthesslcertificate:HTTPSConnectionPool(host='pypi.org',port=443):Maxretriesexceededwithurl:/simple/keras-bert/(CausedbySSLError(SSLEO......
  • 【cs 50】lab4 & problemset4 -ing
    (1)lab4-Smileyhelpers.c#include"helpers.h"voidcolorize(intheight,intwidth,RGBTRIPLEimage[height][width]){//Changeallblackpixelstoacolorofyourchoosingfor(inti=0;i<height;++i){for(intj=0;j<width;++......
  • 【每日一题】Problem 414B. Mashmokh and ACM
    原题解决思路先计算\([1,n]\)中的约数集合\(dp[i][j](i\in[1,n],j\in[1,k])\)表示第\(j\)个数放置\(i\)所拥有的可能性以此类推,到达\(k\)时,计算\(\sum_{i=1}^{n}dp[i][k]\)即可#include<bits/stdc++.h>intmain(){intn,k;std::cin>>n>>k;......
  • 【哈佛cs50 2022】lab3 & problemSet3【ing】
    (1)lab3如何测试每个代码运行所需要的时间?time./sort1sorted5000.txt sort1sort2sort3sorted5000.txt0.037s0.020s0.045ssorted10000.txt0.058s0.050s0.151ssorted50000.txt1.244s2.238s5.637sreversed5000.txt0.088s0.026s0.045srever......
  • Constructive Problem
    ConstructiveProblemtimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputAsyouknow,anyproblemthatdoesnotrequiretheuseofcomplexdatastructuresisconsideredconstructive.Youare......
  • 【每日一题】Problem 489B. BerSU Ball
    原题解决思路排序+双指针#include<bits/stdc++.h>intmain(){intn;std::cin>>n;std::vector<int>a(n+1,0);for(inti=1;i<=n;++i)std::cin>>a[i];intm;std::cin>>m;std::vector<int>b(m+1,0);......
  • 【每日一题】Problem 443B. Kuriyama Mirai's Stones
    原题解决思路两个数组,一个未排序,一个排序;使用前缀和的方式减少时间复杂度#include<bits/stdc++.h>intmain(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);std::cout.tie(nullptr);intn;std::cin>>n;std::vector<int>v(n+1,0);f......
  • 【cs50 2022】lab1 && problem set1
    |lab1|#include<cs50.h>#include<stdio.h>intmain(void){//TODO:Promptforstartsizeintstart_size;do{start_size=get_int("Startsize:");}while(start_size<9);//TODO:Promptforend......
  • 【每日一题】Problem 359B. Permutation
    原题解决思路虽然题解思路里写着\(dp\),但是还是用最简单的\(math\)方法写了找规律题:如果结果为\(0\),只需要每个\(a_{2i-1}-a_{i}\)同向即可,即都为整数或负数如果结果为\(2k\),则只需要任意一个\(|a_{2i-1}-a_{i}|\)的值为\(k\),且与其他的\(a_{2i-1}-a_{i}\)方向......
  • 【每日一题】Problem 253B. Physics Practical
    原题解决思路定义\(dp[i][j]\)为对\(i\)元素做出选择后,要删除的最少元素个数对于\(i\),有两种情况,选或不选选:则找到\(y(y>2x)\)的个数,可以通过排序二分实现不选:则在\(i-1\)的最少删除个数的选择下\(+1\)#include<bits/stdc++.h>intbinarySearch(std::vect......