题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4467

题意:给定n个点m条边的无向图,点被染色(黑0/白1),边带边权。然后q个询问。询问分为两种: Change u:把点u的颜色反转(黑变白,白变黑),Asksum a b(a,b的值为0/1):统计所以边的权值和,边的两个点满足一个点的颜色为a,一个点的颜色为b。

思路:考虑暴力的做法。修改可以做法O(1),但是查询就得O(m).所以总复杂度为O(m*q)会TLE。然后考虑图分块。参考HDU 4858的做法,将点分为重点和轻点。重点(度数>=sqrt(m)),轻点(度数sqrt(m))。 由于此题查询比较大,所以需要预处理一下优化之后的运算。我们定义每个顶点维护3个属性:顶点的颜色color,与该点相连的边并且另外一个点是白点的边权和W, 同理B是黑色点的边权和。 然后维护3个变量ansWW:边上两个点都是白色的边权和。同理ansWB,ansBB。

修改:

轻点更新自己的color,W,B。同时更新所有的邻点的W,B值

重点更新自己的color,W,B。同时只更新相邻重点的W,B值(所以重点不需要连边到轻点)

对于更新。若未更新时该点的颜色为白色,那么更新后该点的颜色为黑色。那么对于相邻的点的W就要减去对应边的权值,相邻点的B就要加上对应边的权值。

对于更新。若未更新时该点的颜色为白色,那么更新后该点的颜色为黑色。那么边为WW的答案就要减去与该点相连的W的总和,边WB的答案就要减去与该点相邻的B的总和。  并且边为BB的答案就要加上与该点相邻的B的总和,边为WB的答案就要加上与该点相邻的W的总和。

查询:

直接输出对应的值

性质:

与重点相邻的重点不超过sqrt(m)个。

与轻点相邻的所有点不超过sqrt(m)个。

注意:

该题会有重边,如果不合并重边的话会TLE。所以用个map来合并下重边的值即可。

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<vector>
#include<iostream>
#include<map>
using namespace std;
typedef long long int LL;
const int MAXN = + ;
struct Edges{
int u, v; LL w;
Edges(int u = , int v = ,LL w=) :u(u), v(v),w(w){};
};vector<Edges>G[MAXN];
map<pair<int, int>, LL>edge;
struct Node{
int val;
LL W, B;
Node(int val = , LL W = , LL B = ) :val(val), W(W), B(B){};
}node[MAXN];
LL ansWB, ansWW, ansBB;
int du[MAXN], block;
void init(int n,int m){
edge.clear(); ansWB = ansWW = ansBB = ;
block = (int)sqrt(m + 0.5);
for (int i = ; i <= n; i++){
G[i].clear(); du[i] = ; node[i].W = node[i].B = ;
}
}
void makeGraph(int n, int m){
for (map<pair<int,int>,LL>::iterator it=edge.begin(); it!=edge.end(); it++){
int u = it->first.first, v = it->first.second; LL w = it->second;
if (du[u] >= block&&du[v] >= block){
G[u].push_back(Edges(u, v, w)); G[v].push_back(Edges(v, u, w));
}
if (du[u] < block){
G[u].push_back(Edges(u, v, w));
}
if (du[v] < block){
G[v].push_back(Edges(v, u, w));
}
if (node[u].val&&node[v].val){
ansWW += w; node[u].W += w; node[v].W += w;
}
else if (!node[u].val&&!node[v].val){
ansBB += w; node[u].B += w; node[v].B += w;
}
else{
ansWB += w;
if (node[u].val){
node[u].B += w; node[v].W += w;
}
else{
node[u].W += w; node[v].B += w;
}
}
}
}
void modify(int pos){
if (node[pos].val){
ansWW -= node[pos].W; ansWB -= node[pos].B;
ansBB += node[pos].B; ansWB += node[pos].W;
for (int i = ; i < G[pos].size(); i++){
node[G[pos][i].v].W -= G[pos][i].w;
node[G[pos][i].v].B += G[pos][i].w;
}
}
else{
ansBB -= node[pos].B; ansWB -= node[pos].W;
ansWW += node[pos].W; ansWB += node[pos].B;
for (int i = ; i < G[pos].size(); i++){
node[G[pos][i].v].W += G[pos][i].w;
node[G[pos][i].v].B -= G[pos][i].w;
}
}
node[pos].val = !node[pos].val;
}
LL query(int u, int v){
if (u&&v){ return ansWW; }
if (!u&&!v){ return ansBB; }
return ansWB;
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
int n, m, q,Case=;
while (~scanf("%d%d", &n, &m)){
init(n, m);
for (int i = ; i <= n; i++)
scanf("%d", &node[i].val);
for (int i = ; i <= m; i++){
int u, v; LL w; scanf("%d%d%I64d", &u, &v, &w);
if (u > v){ swap(u, v); }
if (edge.find(make_pair(u, v)) == edge.end()){
du[u]++; du[v]++;
edge.insert(make_pair(make_pair(u, v), w));
}
else{
edge[make_pair(u, v)] += w;
}
}
makeGraph(n, m);
printf("Case %d:\n", Case++);
scanf("%d", &q);
while (q--){
int u,v; char type[];
scanf("%s", type);
if (type[]=='A'){
scanf("%d%d", &u, &v);
printf("%I64d\n", query(u, v));
}
else{
scanf("%d", &u); modify(u);
}
//Debug:printf("BB=%I64d WB=%I64d WW=%I64d\n", ansBB, ansWB, ansWW);
}
}
return ;
}

HDU 4467 分块的更多相关文章

  1. HDU 5213 分块 容斥

    给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...

  2. HDU 5145 分块 莫队

    给定n个数,q个询问[l,r]区间,每次询问该区间的全排列多少种. 数值都是30000规模 首先考虑计算全排列,由于有同种元素存在,相当于每次在len=r-l+1长度的空格随意放入某种元素即$\bin ...

  3. HDU 2920 分块底数优化 暴力

    其实和昨天写的那道水题是一样的,注意爆LL $1<=n,k<=1e9$,$\sum\limits_{i=1}^{n}(k \mod i) = nk - \sum\limits_{i=1}^ ...

  4. HDU 4858 分块

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858 题意:中文题面 思路:来自此博客 对每个点定义两个值:val,sum,val记录自己的特征值,s ...

  5. Successor HDU - 4366 分块

    代码+注释: 1 /* 2 题意: 3 一共有n个人,其中0号是总裁(金字塔顶尖).后面输入其他n-1个人的信息啊a.b.c,分别代表第i个人的上级是a,他的 4 忠诚度为b,他的能力为c.后面有m次 ...

  6. hdu 4467 Graph

    P. T. Tigris is a student currently studying graph theory. One day, when he was studying hard, GS ap ...

  7. HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)

    Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...

  8. Graph HDU - 4467

    https://vjudge.net/problem/HDU-4467 大概就是,设一个块大小T 对于度数<=T的点,设为1类点,在改变颜色的时候暴力查询与其相邻点,更新答案 对于度数>T ...

  9. HDU 2608 底数优化分块 暴力

    T(n) as the sum of all numbers which are positive integers can divied n. and S(n) = T(1) + T(2) + T( ...

随机推荐

  1. TeamViewer 12.0.71503 Patch By.Sound

    TeamViewer - the All-In-One Software for Remote Support and Online Meetings - Remote control any com ...

  2. 改变bootstrap-wysiwyg样式(如hide()show()等),上传图片失效

    最近在试验bootstrap-wysiwyg鱼easyui的整合,两者的兼容性,可以说是基本不兼容... 但是由于需求摆在那里,再大的困难也得克服. 比如像是将bootstrap-wysiwyg放入e ...

  3. Android handler的使用简单示例

    Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMes ...

  4. a标签产生间隙,<a> 包裹 <img> 产生 4px 间隙

    图片文字等inline元素默认是和父级元素的baseline对齐的,而baseline又和父级底边有一定距离(这个距离和 font-size,font-family 相关),所以设置 vertical ...

  5. html5 canvas-绘制贝塞尔曲线

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. linux 环境变量

    电脑中必不可少的就是操作系统.而Linux的发展非常迅速,有赶超微软的趋势.这里介绍Linux的知识,让你学好应用Linux系统.比如要把/etc/apache/bin目录添加到PATH中,方法有三: ...

  7. Python模块之"prettytable"

    Python模块之"prettytable" 摘要: Python通过prettytable模块可以将输出内容如表格方式整齐的输出.(对于用Python操作数据库会经常用到) 1. ...

  8. java代理类及AOP

    1.代理架构图 2.AOP 3.动态代理概念 4.动态代理工作原理图

  9. 第三天--html列表

    <!Doctype html><html>    <head>        <meta charset="utf-8">      ...

  10. Qt5.5.1编译出来的程序出现libgcc_s_dw2-1.dll的解决方案

    问题如图: 输入"myudp2016.exe 1  " 后出现 这是因为没有在系统环境变量path里加上相关路径,我们添加如下路径: 比如说WIN7系统-开始-计算机-右键-属性- ...